SELinux Archive mirror
 help / color / mirror / Atom feed
From: "Christian Göttsche" <cgoettsche@seltendoof.de>
To: selinux@vger.kernel.org
Cc: "Christian Göttsche" <cgzones@googlemail.com>
Subject: [PATCH 2/3] libselinux: avoid pointer dereference before check
Date: Mon, 29 Apr 2024 18:39:00 +0200	[thread overview]
Message-ID: <20240429163901.65239-2-cgoettsche@seltendoof.de> (raw)
In-Reply-To: <20240429163901.65239-1-cgoettsche@seltendoof.de>

From: Christian Göttsche <cgzones@googlemail.com>

Since commit 5876aca0 ("libselinux: free data on selabel open failure")
the close handler of label backends must support partial initialized
state, e.g. ->data being NULL.  Thus checks for NULL were added, but in
two cases the pointers in question were already dereferenced before.

Reorder the dereference after the NULL-checks.

Fixes: 5876aca0 ("libselinux: free data on selabel open failure")
Reported-by: Cppcheck
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/src/label_media.c | 4 +++-
 libselinux/src/label_x.c     | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/libselinux/src/label_media.c b/libselinux/src/label_media.c
index 94a58062..852aeada 100644
--- a/libselinux/src/label_media.c
+++ b/libselinux/src/label_media.c
@@ -164,12 +164,14 @@ finish:
 static void close(struct selabel_handle *rec)
 {
 	struct saved_data *data = (struct saved_data *)rec->data;
-	struct spec *spec, *spec_arr = data->spec_arr;
+	struct spec *spec, *spec_arr;
 	unsigned int i;
 
 	if (!data)
 		return;
 
+	spec_arr = data->spec_arr;
+
 	for (i = 0; i < data->nspec; i++) {
 		spec = &spec_arr[i];
 		free(spec->key);
diff --git a/libselinux/src/label_x.c b/libselinux/src/label_x.c
index f994eefa..a8decc7a 100644
--- a/libselinux/src/label_x.c
+++ b/libselinux/src/label_x.c
@@ -191,12 +191,14 @@ finish:
 static void close(struct selabel_handle *rec)
 {
 	struct saved_data *data = (struct saved_data *)rec->data;
-	struct spec *spec, *spec_arr = data->spec_arr;
+	struct spec *spec, *spec_arr;
 	unsigned int i;
 
 	if (!data)
 		return;
 
+	spec_arr = data->spec_arr;
+
 	for (i = 0; i < data->nspec; i++) {
 		spec = &spec_arr[i];
 		free(spec->key);
-- 
2.43.0


  reply	other threads:[~2024-04-29 16:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-29 16:38 [PATCH 1/3] libselinux: free empty scandir(3) result Christian Göttsche
2024-04-29 16:39 ` Christian Göttsche [this message]
2024-04-29 20:36   ` [PATCH 2/3] libselinux: avoid pointer dereference before check William Roberts
2024-04-29 16:39 ` [PATCH 3/3] mcstrans: free constraint in error branch Christian Göttsche
2024-04-29 20:37   ` William Roberts
2024-04-29 20:34 ` [PATCH 1/3] libselinux: free empty scandir(3) result William Roberts
2024-04-30 14:34   ` Christian Göttsche
2024-04-30 14:52     ` William Roberts

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=20240429163901.65239-2-cgoettsche@seltendoof.de \
    --to=cgoettsche@seltendoof.de \
    --cc=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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).