Skip to contentSkip to conversation

libselinux: Prevent cached context giving wrong results

PATCH In review Johannes Segitz 2 participants 2 messages Jan 27, 2022 – May 29, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: Prevent cached context giving wrong results

The procattr cache doesn't work properly in all cases. This fixes the issue at the cost of not using the cache as soon as a pid is specified.

In most use cases this will never occur, but it's still a small security issue, since this incorrect information might lead to incorrect access decisions.

---
 libselinux/src/procattr.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
libselinux/src/procattr.c +8 -6
index 142fbf3a..4ca8337a 100644
@@ -148,7 +148,7 @@ static int getprocattrcon_raw(char ** context,
 			return -1;
 	}
 
-	if (prev_context && prev_context != UNSET) {
+	if (pid == 0 && prev_context && prev_context != UNSET) {
 		*context = strdup(prev_context);
 		if (!(*context)) {
 			return -1;
@@ -242,9 +242,9 @@ static int setprocattrcon_raw(const char * context,
 			return -1;
 	}
 
-	if (!context && !*prev_context)
+	if (pid == 0 && !context && !*prev_context)
 		return 0;
-	if (context && *prev_context && *prev_context != UNSET
+	if (pid == 0 && context && *prev_context && *prev_context != UNSET
 	    && !strcmp(context, *prev_context))
 		return 0;
 
@@ -272,9 +272,11 @@ out:
 		free(context2);
 		return -1;
 	} else {
-		if (*prev_context != UNSET)
-			free(*prev_context);
-		*prev_context = context2;
+		if (pid == 0) {
+			if (*prev_context != UNSET)
+				free(*prev_context);
+			*prev_context = context2;
+		}
 		return 0;
 	}
 }
Signature · 1 line
2.31.1
#1 lore raw

Hello,

Sorry for not answering sooner. I was busy in the past few months, and it seems the other libselinux maintainers too.

I had some difficulty understanding what your patch was about, as I missed its context (from
https://lore.kernel.org/selinux/[email protected]/ ) and the commit message did not explain what the issue actually was. While trying to reverse-engineer it, I found (again) the bug which is fixed. Instead of adding "pid == 0" to some functions, I decided to rework/simplify libselinux/src/procattr.c to fix this bug. The result of this work was sent on the mailing list:
https://patchwork.kernel.org/project/selinux/patch/[email protected]/
. I personally prefer this simplification, but if anyone thinks this makes it more difficult to maintain libselinux (for example), I am open to discussion.

Best regards,
Nicolas

Quoted 55 lines · Johannes Segitz

The procattr cache doesn't work properly in all cases. This fixes the issue at the cost of not using the cache as soon as a pid is specified.

In most use cases this will never occur, but it's still a small security issue, since this incorrect information might lead to incorrect access decisions.

---
 libselinux/src/procattr.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
libselinux/src/procattr.c +8 -7
index 142fbf3a..4ca8337a 100644
@@ -148,7 +148,7 @@ static int getprocattrcon_raw(char ** context,
                        return -1;
        }
 
-       if (prev_context && prev_context != UNSET) {
+       if (pid == 0 && prev_context && prev_context != UNSET) {
                *context = strdup(prev_context);
                if (!(*context)) {
                        return -1;
@@ -242,9 +242,9 @@ static int setprocattrcon_raw(const char * context,
                        return -1;
        }
 
-       if (!context && !*prev_context)
+       if (pid == 0 && !context && !*prev_context)
                return 0;
-       if (context && *prev_context && *prev_context != UNSET
+       if (pid == 0 && context && *prev_context && *prev_context != UNSET
            && !strcmp(context, *prev_context))
                return 0;
 
@@ -272,9 +272,11 @@ out:
                free(context2);
                return -1;
        } else {
-               if (*prev_context != UNSET)
-                       free(*prev_context);
-               *prev_context = context2;
+               if (pid == 0) {
+                       if (*prev_context != UNSET)
+                               free(*prev_context);
+                       *prev_context = context2;
+               }
                return 0;
        }
 }
--

2.31.1