All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nicolas Iooss <nicolas.iooss@m4x.org>
To: selinux@tycho.nsa.gov
Subject: [PATCH 5/6] policycoreutils: newrole: do not free pw strings twice
Date: Tue, 11 Apr 2017 23:46:02 +0200	[thread overview]
Message-ID: <20170411214603.28040-5-nicolas.iooss@m4x.org> (raw)
In-Reply-To: <20170411214603.28040-1-nicolas.iooss@m4x.org>

In main(), if "extract_pw_data(&pw)" returns a failed value, it has
already freed pw.pw_name, pw.pw_dir and pw.pw_shell. These fields are
freed a second time in main's err_free label, which is incorrect. Work
around this by setting them to NULL after they are freed.

This issue has been found using clang's static analyzer.

While at it, make extract_pw_data() static.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
---
 policycoreutils/newrole/newrole.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/policycoreutils/newrole/newrole.c b/policycoreutils/newrole/newrole.c
index faf937b94f6d..bed92e4e7494 100644
--- a/policycoreutils/newrole/newrole.c
+++ b/policycoreutils/newrole/newrole.c
@@ -412,7 +412,7 @@ static int verify_shell(const char *shell_name)
  * This function assigns malloc'd memory into the pw_copy struct.
  * Returns zero on success, non-zero otherwise
  */
-int extract_pw_data(struct passwd *pw_copy)
+static int extract_pw_data(struct passwd *pw_copy)
 {
 	uid_t uid;
 	struct passwd *pw;
@@ -456,6 +456,9 @@ int extract_pw_data(struct passwd *pw_copy)
 	free(pw->pw_name);
 	free(pw->pw_dir);
 	free(pw->pw_shell);
+	pw->pw_name = NULL;
+	pw->pw_dir = NULL;
+	pw->pw_shell = NULL;
 	return -1;
 }
 
-- 
2.12.0

  parent reply	other threads:[~2017-04-11 21:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-11 21:45 [PATCH 1/6] libsemanage: make semanage_..._destroy return void Nicolas Iooss
2017-04-11 21:45 ` [PATCH 2/6] libsepol: cil: check cil_fill_list return value Nicolas Iooss
2017-04-11 21:46 ` [PATCH 3/6] libselinux: avoid calling strcmp() on a NULL pointer Nicolas Iooss
2017-04-11 21:46 ` [PATCH 4/6] libselinux: getsebool: always free names Nicolas Iooss
2017-04-11 21:46 ` Nicolas Iooss [this message]
2017-04-11 21:46 ` [PATCH 6/6] policycoreutils: newrole: always initialize pw fields Nicolas Iooss
2017-04-12 18:50   ` Stephen Smalley
2017-04-12 18:25 ` [PATCH 1/6] libsemanage: make semanage_..._destroy return void Stephen Smalley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170411214603.28040-5-nicolas.iooss@m4x.org \
    --to=nicolas.iooss@m4x.org \
    --cc=selinux@tycho.nsa.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.