All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Christian Göttsche" <cgzones@googlemail.com>
To: selinux@vger.kernel.org
Subject: [PATCH 03/11] libselinux/utils: free allocated resources
Date: Tue, 19 Dec 2023 17:09:25 +0100	[thread overview]
Message-ID: <20231219160943.334370-3-cgzones@googlemail.com> (raw)
In-Reply-To: <20231219160943.334370-1-cgzones@googlemail.com>

Remove noise while running with sanitizers or under valgrind.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/utils/getconlist.c     | 10 +++++++---
 libselinux/utils/getdefaultcon.c  | 20 +++++++++++++++++---
 libselinux/utils/selinuxexeccon.c |  1 +
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/libselinux/utils/getconlist.c b/libselinux/utils/getconlist.c
index 92f6a793..1ff05209 100644
--- a/libselinux/utils/getconlist.c
+++ b/libselinux/utils/getconlist.c
@@ -19,8 +19,9 @@ static __attribute__ ((__noreturn__)) void usage(const char *name, const char *d
 
 int main(int argc, char **argv)
 {
-	char **list, *cur_context = NULL;
-	char *user = NULL, *level = NULL;
+	char **list;
+	const char *cur_context, *user;
+	char *cur_con = NULL, *level = NULL;
 	int ret, i, opt;
 
 	while ((opt = getopt(argc, argv, "l:")) > 0) {
@@ -54,11 +55,12 @@ int main(int argc, char **argv)
 
 	/* If a context wasn't passed, use the current context. */
 	if (((argc - optind) < 2)) {
-		if (getcon(&cur_context) < 0) {
+		if (getcon(&cur_con) < 0) {
 			fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
 			free(level);
 			return 2;
 		}
+		cur_context = cur_con;
 	} else {
 		cur_context = argv[optind + 1];
 		if (security_check_context(cur_context) != 0) {
@@ -82,10 +84,12 @@ int main(int argc, char **argv)
 	} else {
 		fprintf(stderr, "get_ordered_context_list%s failure: %d(%s)\n",
 			level ? "_with_level" : "", errno, strerror(errno));
+		free(cur_con);
 		free(level);
 		return 4;
 	}
 
+	free(cur_con);
 	free(level);
 
 	return 0;
diff --git a/libselinux/utils/getdefaultcon.c b/libselinux/utils/getdefaultcon.c
index 50f1ea91..67c84f94 100644
--- a/libselinux/utils/getdefaultcon.c
+++ b/libselinux/utils/getdefaultcon.c
@@ -19,8 +19,9 @@ static __attribute__ ((__noreturn__)) void usage(const char *name, const char *d
 
 int main(int argc, char **argv)
 {
-	char * usercon = NULL, *cur_context = NULL;
-	char *user = NULL, *level = NULL, *role=NULL, *seuser=NULL, *dlevel=NULL;
+	const char *cur_context, *user;
+	char *usercon = NULL, *cur_con = NULL;
+	char *level = NULL, *role=NULL, *seuser=NULL, *dlevel=NULL;
 	char *service = NULL;
 	int ret, opt;
 	int verbose = 0;
@@ -54,6 +55,9 @@ int main(int argc, char **argv)
 	if (!is_selinux_enabled()) {
 		fprintf(stderr,
 			"%s may be used only on a SELinux kernel.\n", argv[0]);
+		free(level);
+		free(role);
+		free(service);
 		return 1;
 	}
 
@@ -61,15 +65,23 @@ int main(int argc, char **argv)
 
 	/* If a context wasn't passed, use the current context. */
 	if ((argc - optind) < 2) {
-		if (getcon(&cur_context) < 0) {
+		if (getcon(&cur_con) < 0) {
 			fprintf(stderr, "%s:  couldn't get current context:  %s\n", argv[0], strerror(errno));
+			free(level);
+			free(role);
+			free(service);
 			return 2;
 		}
+		cur_context = cur_con;
 	} else
 		cur_context = argv[optind + 1];
 
 	if (security_check_context(cur_context)) {
 		fprintf(stderr, "%s:  invalid from context '%s'\n", argv[0], cur_context);
+		free(cur_con);
+		free(level);
+		free(role);
+		free(service);
 		return 3;
 	}
 
@@ -101,6 +113,8 @@ out:
 	if (level != dlevel) free(level);
 	free(dlevel);
 	free(usercon);
+	free(cur_con);
+	free(service);
 
 	return ret >= 0;
 }
diff --git a/libselinux/utils/selinuxexeccon.c b/libselinux/utils/selinuxexeccon.c
index 66754b6a..463bf5aa 100644
--- a/libselinux/utils/selinuxexeccon.c
+++ b/libselinux/utils/selinuxexeccon.c
@@ -45,6 +45,7 @@ int main(int argc, char **argv)
 		con = strdup(argv[2]);
 		if (security_check_context(con)) {
 			fprintf(stderr, "%s:  invalid from context '%s'\n", argv[0], con);
+			free(con);
 			return -1;
 		}
 	}
-- 
2.43.0


  parent reply	other threads:[~2023-12-19 16:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-19 16:09 [PATCH 01/11] libselinux/man: mention errno for regex compilation failure Christian Göttsche
2023-12-19 16:09 ` [PATCH 02/11] libselinux/man: sync selinux_check_securetty_context(3) Christian Göttsche
2023-12-19 16:09 ` Christian Göttsche [this message]
2023-12-19 16:09 ` [PATCH 04/11] libselinux/utils: improve compute_av output Christian Göttsche
2023-12-19 16:09 ` [PATCH 05/11] libselinux: align SELABEL_OPT_DIGEST usage with man page Christian Göttsche
2023-12-19 16:09 ` [PATCH 06/11] libselinux: fail selabel_open(3) on invalid option Christian Göttsche
2023-12-19 16:09 ` [PATCH 07/11] libselinux: use logging wrapper in getseuser(3) and get_default_context(3) family Christian Göttsche
2023-12-19 16:09 ` [PATCH 08/11] libselinux: support huge passwd/group entries Christian Göttsche
2023-12-19 16:09 ` [PATCH 09/11] libsemanage: support huge passwd entries Christian Göttsche
2023-12-19 16:09 ` [PATCH 10/11] libselinux: enable usage with pedantic UB sanitizers Christian Göttsche
2023-12-19 16:09 ` [PATCH 11/11] setfiles: avoid unsigned integer underflow Christian Göttsche
2024-01-05 19:15 ` [PATCH 01/11] libselinux/man: mention errno for regex compilation failure James Carter
2024-01-25 19:55   ` James Carter

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=20231219160943.334370-3-cgzones@googlemail.com \
    --to=cgzones@googlemail.com \
    --cc=selinux@vger.kernel.org \
    /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.