Skip to contentSkip to conversation

libselinux: restorecon: add fallback for pre 3.6 Linux

PATCH In review Christian Göttsche 3 participants 9 messages May 17, 2022 – Jun 30, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libselinux: restorecon: add fallback for pre 3.6 Linux

fstat(2) on file descriptors obtained via O_PATH is supported since Linux 3.6. Fallback on older systems to lstat(2).

Fixes 7e979b56 ("libselinux: restorecon: pin file to avoid TOCTOU issues")
---
 libselinux/src/selinux_restorecon.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
libselinux/src/selinux_restorecon.c +25 -4
index 9dd6be81..1a185ced 100644
@@ -89,10 +89,22 @@ struct rest_flags {
 	bool count_errors;
 };
 
+/* Linux version for availability tests. */
+static struct utsname uts;
+
 static void restorecon_init(void)
 {
 	struct selabel_handle *sehandle = NULL;
 
+	if (uname(&uts) < 0) {
+		/*
+		 * utsname(2) should never fail, but assume oldest supported
+		 * LTS release as backup
+		 */
+		strncpy(uts.release, "4.9", sizeof(uts.release));
+		uts.release[sizeof(uts.release) - 1] = '\0';
+	}
+
 	if (!fc_sehandle) {
 		sehandle = selinux_restorecon_default_handle();
 		selinux_restorecon_set_sehandle(sehandle);
@@ -238,7 +250,6 @@ static uint64_t file_system_count(const char *name)
  */
 static uint64_t exclude_non_seclabel_mounts(void)
 {
-	struct utsname uts;
 	FILE *fp;
 	size_t len;
 	int index = 0, found = 0;
@@ -247,7 +258,7 @@ static uint64_t exclude_non_seclabel_mounts(void)
 	char *buf = NULL, *item;
 
 	/* Check to see if the kernel supports seclabel */
-	if (uname(&uts) == 0 && strverscmp(uts.release, "2.6.30") < 0)
+	if (strverscmp(uts.release, "2.6.30") < 0)
 		return 0;
 	if (is_selinux_enabled() <= 0)
 		return 0;
@@ -648,9 +659,19 @@ static int restorecon_sb(const char *pathname, struct rest_flags *flags, bool fi
 	if (fd < 0)
 		goto err;
 
+	/*
+	 * fstat(2) on file descriptors obtained via O_PATH are supported
+	 * since Linux 3.6, see man:open(2).
+	 * Test fstat(2) first, support might have been backported.
+	 */
 	rc = fstat(fd, &stat_buf);
-	if (rc < 0)
-		goto err;
+	if (rc < 0) {
+		if (errno == EBADF && strverscmp(uts.release, "3.6") < 0)
+			rc = lstat(pathname, &stat_buf);
+
+		if (rc < 0)
+			goto err;
+	}
 
 	if (rootpath != NULL && lookup_path[0] == '\0')
 		/* this is actually the root dir of the alt root. */
Signature · 1 line
2.36.1
#1 lore raw

PATCH 2/2 libselinux: restorecon: avoid printing NULL pointer

The variable curcon is NULL in case the file has no current security context. Most C standard libraries handle it fine, avoid it nonetheless for standard conformance.

---
"(null)" might not be the best token to display, it was only taken to not change current behavior

---
 libselinux/src/selinux_restorecon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libselinux/src/selinux_restorecon.c +3 -1
index 1a185ced..1b21a605 100644
@@ -771,7 +771,9 @@ static int restorecon_sb(const char *pathname, struct rest_flags *flags, bool fi
 			selinux_log(SELINUX_INFO,
 				    "%s %s from %s to %s\n",
 				    updated ? "Relabeled" : "Would relabel",
-				    pathname, curcon, newcon);
+				    pathname,
+				    curcon ? curcon : "(null)",
+				    newcon);
 
 		if (flags->syslog_changes && !flags->nochange) {
 			if (curcon)
Signature · 1 line
2.36.1
#2 lore raw

Re: [PATCH 2/2] libselinux: restorecon: avoid printing NULL pointer

Quoted 18 lines · Christian Göttsche

The variable curcon is NULL in case the file has no current security context. Most C standard libraries handle it fine, avoid it nonetheless for standard conformance.

---
"(null)" might not be the best token to display, it was only taken to not change current behavior

---
 libselinux/src/selinux_restorecon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libselinux/src/selinux_restorecon.c +0 -0
index 1a185ced..1b21a605 100644

libselinux/src/selinux_restorecon.c static int restorecon_sb(const char *pathname, struct rest_flags *flags, bool fi · line 775

Quoted 6 lines
libselinux/src/selinux_restorecon.c +2 -1
@@ -771,7 +771,9 @@ static int restorecon_sb(const char *pathname, struct rest_flags *flags, bool fi
                        selinux_log(SELINUX_INFO,
                                    "%s %s from %s to %s\n",
                                    updated ? "Relabeled" : "Would relabel",
-                                   pathname, curcon, newcon);
+                                   pathname,
+                                   curcon ? curcon : "(null)",

Use "<<none>>", this is already used in file context files to indicate a file should not have a label.

Thanks,
Jim

Quoted 7 lines
libselinux/src/selinux_restorecon.c +1 -1
+                                   newcon);
 
                if (flags->syslog_changes && !flags->nochange) {
                        if (curcon)
--

2.36.1

#3 lore raw

Re: [PATCH 2/2] libselinux: restorecon: avoid printing NULL pointer

Quoted 16 lines · James Carter
Quoted 13 lines · Christian Göttsche

The variable curcon is NULL in case the file has no current security context. Most C standard libraries handle it fine, avoid it nonetheless for standard conformance.

---
"(null)" might not be the best token to display, it was only taken to not change current behavior

---
 libselinux/src/selinux_restorecon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 14 lines

libselinux/src/selinux_restorecon.c static int restorecon_sb(const char *pathname, struct rest_flags *flags, bool fi · line 775

Quoted 11 lines
libselinux/src/selinux_restorecon.c +2 -1
index 1a185ced..1b21a605 100644
@@ -771,7 +771,9 @@ static int restorecon_sb(const char *pathname, struct rest_flags *flags, bool fi
                        selinux_log(SELINUX_INFO,
                                    "%s %s from %s to %s\n",
                                    updated ? "Relabeled" : "Would relabel",
-                                   pathname, curcon, newcon);
+                                   pathname,
+                                   curcon ? curcon : "(null)",

Use "<<none>>", this is already used in file context files to indicate a file should not have a label.

"<<none>>" used in file contexts definitions does not mean no security context but never relabel the existing context.
Maybe something like "<no context>" or "<empty context>"?

Quoted 11 lines

Thanks,
Jim

Quoted 7 lines
libselinux/src/selinux_restorecon.c +1 -1
+                                   newcon);
 
                if (flags->syslog_changes && !flags->nochange) {
                        if (curcon)
--

2.36.1

#4 lore raw

PATCH v2 1/1 libselinux: restorecon: avoid printing NULL pointer

In reply to Christian Göttsche

The variable curcon is NULL in case the file has no current security context. Most C standard libraries handle it fine, avoid it nonetheless for standard conformance.

---
v2:
print "<no context>" instead of "(null)"

---
 libselinux/src/selinux_restorecon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libselinux/src/selinux_restorecon.c +3 -1
index 9f5b326c..3c441119 100644
@@ -744,7 +744,9 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
 			selinux_log(SELINUX_INFO,
 				    "%s %s from %s to %s\n",
 				    updated ? "Relabeled" : "Would relabel",
-				    pathname, curcon, newcon);
+				    pathname,
+				    curcon ? curcon : "<no context>",
+				    newcon);
 
 		if (flags->syslog_changes && !flags->nochange) {
 			if (curcon)
Signature · 1 line
2.36.1
Acked-by on v2 1/1
#5 lore raw

Re: [PATCH v2] libselinux: restorecon: avoid printing NULL pointer

Quoted 6 lines · Christian Göttsche

The variable curcon is NULL in case the file has no current security context. Most C standard libraries handle it fine, avoid it nonetheless for standard conformance.

Hello,
What is the status of this patch? As it looks good to me, I can merge it if nobody has any more comments.

Thanks,
Nicolas

Quoted 25 lines

---
v2:
print "<no context>" instead of "(null)"

---
 libselinux/src/selinux_restorecon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libselinux/src/selinux_restorecon.c +3 -2
index 9f5b326c..3c441119 100644
@@ -744,7 +744,9 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
                        selinux_log(SELINUX_INFO,
                                    "%s %s from %s to %s\n",
                                    updated ? "Relabeled" : "Would relabel",
-                                   pathname, curcon, newcon);
+                                   pathname,
+                                   curcon ? curcon : "<no context>",
+                                   newcon);
 
                if (flags->syslog_changes && !flags->nochange) {
                        if (curcon)
--

2.36.1

#6 lore raw

Re: [PATCH v2] libselinux: restorecon: avoid printing NULL pointer

Quoted 8 lines · Nicolas Iooss
Quoted 5 lines · Christian Göttsche

The variable curcon is NULL in case the file has no current security context. Most C standard libraries handle it fine, avoid it nonetheless for standard conformance.

Quoted 6 lines
Quoted 1 line

Hello,
What is the status of this patch? As it looks good to me, I can merge it if nobody has any more comments.

This patch is fine. Patch 1 fixes a commit that has been reverted, so it is not needed.
Thanks,
Jim

Quoted 31 lines

Thanks,
Nicolas

Quoted 25 lines

---
v2:
print "<no context>" instead of "(null)"

---
 libselinux/src/selinux_restorecon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libselinux/src/selinux_restorecon.c +3 -2
index 9f5b326c..3c441119 100644
@@ -744,7 +744,9 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
                        selinux_log(SELINUX_INFO,
                                    "%s %s from %s to %s\n",
                                    updated ? "Relabeled" : "Would relabel",
-                                   pathname, curcon, newcon);
+                                   pathname,
+                                   curcon ? curcon : "<no context>",
+                                   newcon);
 
                if (flags->syslog_changes && !flags->nochange) {
                        if (curcon)
--

2.36.1

#7 lore raw

Re: [PATCH v2] libselinux: restorecon: avoid printing NULL pointer

Quoted 17 lines · James Carter
Quoted 8 lines · Nicolas Iooss
Quoted 5 lines · Christian Göttsche

The variable curcon is NULL in case the file has no current security context. Most C standard libraries handle it fine, avoid it nonetheless for standard conformance.

Quoted 6 lines
Quoted 1 line

Hello,
What is the status of this patch? As it looks good to me, I can merge it if nobody has any more comments.

Quoted 4 lines

This patch is fine. Patch 1 fixes a commit that has been reverted, so it is not needed.
Thanks,
Jim

This patch is now applied.

Thanks,
Nicolas

Quoted 31 lines
Quoted 31 lines

Thanks,
Nicolas

Quoted 25 lines

---
v2:
print "<no context>" instead of "(null)"

---
 libselinux/src/selinux_restorecon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libselinux/src/selinux_restorecon.c +3 -2
index 9f5b326c..3c441119 100644
@@ -744,7 +744,9 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
                        selinux_log(SELINUX_INFO,
                                    "%s %s from %s to %s\n",
                                    updated ? "Relabeled" : "Would relabel",
-                                   pathname, curcon, newcon);
+                                   pathname,
+                                   curcon ? curcon : "<no context>",
+                                   newcon);
 
                if (flags->syslog_changes && !flags->nochange) {
                        if (curcon)
--

2.36.1

#8 lore raw
In reply to Christian Göttsche
Quoted 19 lines · Christian Göttsche

fstat(2) on file descriptors obtained via O_PATH is supported since Linux 3.6. Fallback on older systems to lstat(2).

Fixes 7e979b56 ("libselinux: restorecon: pin file to avoid TOCTOU issues")
---
 libselinux/src/selinux_restorecon.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
libselinux/src/selinux_restorecon.c +0 -0
index 9dd6be81..1a185ced 100644
@@ -89,10 +89,22 @@ struct rest_flags {
        bool count_errors;
 };
 

libselinux/src/selinux_restorecon.c struct rest_flags { · line 101

Quoted 10 lines
libselinux/src/selinux_restorecon.c +6 -0
+/* Linux version for availability tests. */
+static struct utsname uts;
+
 static void restorecon_init(void)
 {
        struct selabel_handle *sehandle = NULL;
 
+       if (uname(&uts) < 0) {
+               /*
+                * utsname(2) should never fail, but assume oldest supported

Should be "uname(2) should ..."

libselinux/src/selinux_restorecon.c struct rest_flags { · line 104

Quoted 3 lines
libselinux/src/selinux_restorecon.c +3 -0
+                * LTS release as backup
+                */
+               strncpy(uts.release, "4.9", sizeof(uts.release));

I don't know. Using 4.9 seems arbitrary. I think that I would prefer just using "3.6" and note that only behavior is only changed for kernels prior to 3.6 or something like that.

Thanks,
Jim

Quoted 48 lines
libselinux/src/selinux_restorecon.c +16 -5
+               uts.release[sizeof(uts.release) - 1] = '\0';
+       }
+
        if (!fc_sehandle) {
                sehandle = selinux_restorecon_default_handle();
                selinux_restorecon_set_sehandle(sehandle);
@@ -238,7 +250,6 @@ static uint64_t file_system_count(const char *name)
  */
 static uint64_t exclude_non_seclabel_mounts(void)
 {
-       struct utsname uts;
        FILE *fp;
        size_t len;
        int index = 0, found = 0;
@@ -247,7 +258,7 @@ static uint64_t exclude_non_seclabel_mounts(void)
        char *buf = NULL, *item;
 
        /* Check to see if the kernel supports seclabel */
-       if (uname(&uts) == 0 && strverscmp(uts.release, "2.6.30") < 0)
+       if (strverscmp(uts.release, "2.6.30") < 0)
                return 0;
        if (is_selinux_enabled() <= 0)
                return 0;
@@ -648,9 +659,19 @@ static int restorecon_sb(const char *pathname, struct rest_flags *flags, bool fi
        if (fd < 0)
                goto err;
 
+       /*
+        * fstat(2) on file descriptors obtained via O_PATH are supported
+        * since Linux 3.6, see man:open(2).
+        * Test fstat(2) first, support might have been backported.
+        */
        rc = fstat(fd, &stat_buf);
-       if (rc < 0)
-               goto err;
+       if (rc < 0) {
+               if (errno == EBADF && strverscmp(uts.release, "3.6") < 0)
+                       rc = lstat(pathname, &stat_buf);
+
+               if (rc < 0)
+                       goto err;
+       }
 
        if (rootpath != NULL && lookup_path[0] == '\0')
                /* this is actually the root dir of the alt root. */
--

2.36.1