Skip to contentSkip to conversation

libselinux: simplify policy path logic to avoid uninitialized read

RFC PATCH In review Christian Göttsche 2 participants 10 messages May 10, 2022 – Jun 8, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/4 libselinux: simplify policy path logic to avoid uninitialized read

In case the function __policy_init() gets called with a NULL pointer, the stack variable path remains uninitialized (except at its last index). If parsing the binary policy fails in sepol_policydb_read() the error branch would access those uninitialized memory.

---
 libselinux/src/audit2why.c | 34 +++++++++++++---------------------
 1 file changed, 13 insertions(+), 21 deletions(-)
libselinux/src/audit2why.c +13 -21
index ca38e13c..44a9a341 100644
@@ -192,25 +192,16 @@ static PyObject *finish(PyObject *self __attribute__((unused)), PyObject *args)
 static int __policy_init(const char *init_path)
 {
 	FILE *fp;
-	char path[PATH_MAX];
+	const char *curpolicy;
 	char errormsg[PATH_MAX+1024+20];
 	struct sepol_policy_file *pf = NULL;
 	int rc;
 	unsigned int cnt;
 
-	path[PATH_MAX-1] = '\0';
 	if (init_path) {
-		strncpy(path, init_path, PATH_MAX-1);
-		fp = fopen(path, "re");
-		if (!fp) {
-			snprintf(errormsg, sizeof(errormsg), 
-				 "unable to open %s:  %m\n",
-				 path);
-			PyErr_SetString( PyExc_ValueError, errormsg);
-			return 1;
-		}
+		curpolicy = init_path;
 	} else {
-		const char *curpolicy = selinux_current_policy_path();
+		curpolicy = selinux_current_policy_path();
 		if (!curpolicy) {
 			/* SELinux disabled, must use -p option. */
 			snprintf(errormsg, sizeof(errormsg),
@@ -218,14 +209,15 @@ static int __policy_init(const char *init_path)
 			PyErr_SetString( PyExc_ValueError, errormsg);
 			return 1;
 		}
-		fp = fopen(curpolicy, "re");
-		if (!fp) {
-			snprintf(errormsg, sizeof(errormsg), 
-				 "unable to open %s:  %m\n",
-				 curpolicy);
-			PyErr_SetString( PyExc_ValueError, errormsg);
-			return 1;
-		}
+	}
+
+	fp = fopen(curpolicy, "re");
+	if (!fp) {
+		snprintf(errormsg, sizeof(errormsg),
+			 "unable to open %s:  %m\n",
+			 curpolicy);
+		PyErr_SetString( PyExc_ValueError, errormsg);
+		return 1;
 	}
 
 	avc = calloc(sizeof(struct avc_t), 1);
@@ -249,7 +241,7 @@ static int __policy_init(const char *init_path)
 	sepol_policy_file_set_fp(pf, fp);	
 	if (sepol_policydb_read(avc->policydb, pf)) {
 		snprintf(errormsg, sizeof(errormsg), 
-			 "invalid binary policy %s\n", path);
+			 "invalid binary policy %s\n", curpolicy);
 		PyErr_SetString( PyExc_ValueError, errormsg);
 		fclose(fp);
 		return 1;
Signature · 1 line
2.36.1
#1 lore raw

RFC PATCH 2/4 libselinux: add header guard for internal header

---
 libselinux/src/selinux_internal.h | 5 +++++
 1 file changed, 5 insertions(+)
libselinux/src/selinux_internal.h +5 -0
index 297dcf26..9f4c9073 100644
@@ -1,3 +1,6 @@
+#ifndef SELINUX_INTERNAL_H_
+#define SELINUX_INTERNAL_H_
+
 #include <selinux/selinux.h>
 #include <pthread.h>
 
@@ -90,3 +93,5 @@ extern int selinux_page_size ;
 #define SELINUXCONFIG SELINUXDIR "config"
 
 extern int has_selinux_config ;
+
+#endif /* SELINUX_INTERNAL_H_ */
Signature · 1 line
2.36.1
#2 lore raw

RFC PATCH 3/4 libselinux: introduce strlcpy

In reply to Christian Göttsche

To copy string safely, by always NULL-terminating them, and provide an easy way to check for truncation introduce the nonstandard function strlcpy(3). Use the system implementation if available.

---
 libselinux/src/Makefile           |  6 ++++++
 libselinux/src/selinux_internal.c | 18 ++++++++++++++++++
 libselinux/src/selinux_internal.h |  4 ++++
 3 files changed, 28 insertions(+)
 create mode 100644 libselinux/src/selinux_internal.c
libselinux/src/Makefile +6 -0
index 04bf4f24..88aa32f8 100644
@@ -103,6 +103,12 @@ FTS_LDLIBS ?=
 
 override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
 
+# check for strlcpy(3) availability
+H := \#
+ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlcpy(d, s, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+override CFLAGS += -DHAVE_STRLCPY
+endif
+
 SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
 		-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \
 		-Wno-deprecated-declarations
libselinux/src/selinux_internal.c new file +18 -0
new file mode 100644
index 00000000..c2be7c0a
@@ -0,0 +1,18 @@
+#include "selinux_internal.h"
+
+#include <string.h>
+
+
+#ifndef HAVE_STRLCPY
+size_t strlcpy(char *dest, const char *src, size_t size)
+{
+	size_t ret = strlen(src);
+
+	if (size) {
+		size_t len = (ret >= size) ? size - 1 : ret;
+		memcpy(dest, src, len);
+		dest[len] = '\0';
+	}
+	return ret;
+}
+#endif /* HAVE_STRLCPY */
libselinux/src/selinux_internal.h +4 -0
index 9f4c9073..06f2c038 100644
@@ -94,4 +94,8 @@ extern int selinux_page_size ;
 
 extern int has_selinux_config ;
 
+#ifndef HAVE_STRLCPY
+size_t strlcpy(char *dest, const char *src, size_t size);
+#endif
+
 #endif /* SELINUX_INTERNAL_H_ */
Signature · 1 line
2.36.1
#3 lore raw

RFC PATCH 4/4 libselinux: check for truncations

In reply to Christian Göttsche

Check for truncations when building or copying strings involving user input.

---
 libselinux/src/canonicalize_context.c |  6 +++++-
 libselinux/src/compute_av.c           |  7 ++++++-
 libselinux/src/compute_create.c       |  6 ++++++
 libselinux/src/compute_member.c       |  7 ++++++-
 libselinux/src/compute_relabel.c      |  7 ++++++-
 libselinux/src/compute_user.c         |  7 ++++++-
 libselinux/src/selinux_restorecon.c   | 11 ++++++++++-
 libselinux/src/setrans_client.c       |  8 +++++++-
 8 files changed, 52 insertions(+), 7 deletions(-)
libselinux/src/canonicalize_context.c +5 -1
index faab7305..8a22a4cd 100644
@@ -33,7 +33,11 @@ int security_canonicalize_context_raw(const char * con,
 		ret = -1;
 		goto out;
 	}
-	strncpy(buf, con, size);
+	if (strlcpy(buf, con, size) >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out;
+	}
 
 	ret = write(fd, buf, strlen(buf) + 1);
 	if (ret < 0)
libselinux/src/compute_av.c +6 -1
index 9d17339d..e513be6a 100644
@@ -40,8 +40,13 @@ int security_compute_av_flags_raw(const char * scon,
 	}
 
 	kclass = unmap_class(tclass);
-	snprintf(buf, len, "%s %s %hu %x", scon, tcon,
+
+	ret = snprintf(buf, len, "%s %s %hu %x", scon, tcon,
 		 kclass, unmap_perm(tclass, requested));
+	if (ret < 0 || ret >= len) {
+		errno = EOVERFLOW;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_create.c +6 -0
index 1d75714d..4cba2d2f 100644
@@ -75,8 +75,14 @@ int security_compute_create_name_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
+
 	len = snprintf(buf, size, "%s %s %hu",
 		       scon, tcon, unmap_class(tclass));
+	if (len < 0 || len >= size) {
+		errno = EOVERFLOW;
+		goto out2;
+	}
+
 	if (objname &&
 	    object_name_encode(objname, buf + len, size - len) < 0) {
 		errno = ENAMETOOLONG;
libselinux/src/compute_member.c +6 -1
index 16234b79..82d76080 100644
@@ -36,7 +36,12 @@ int security_compute_member_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+	ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+	if (ret < 0 || ret >= size) {
+		errno = EOVERFLOW;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_relabel.c +6 -1
index dd20d652..96259bac 100644
@@ -36,7 +36,12 @@ int security_compute_relabel_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+	ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+	if (ret < 0 || ret >= size) {
+		errno = EOVERFLOW;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_user.c +6 -1
index ae5e7b4a..23a551e4 100644
@@ -38,7 +38,12 @@ int security_compute_user_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s", scon, user);
+
+	ret = snprintf(buf, size, "%s %s", scon, user);
+	if (ret < 0 || ret >= size) {
+		errno = EOVERFLOW;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/selinux_restorecon.c +10 -1
index e6192912..7436dab5 100644
@@ -940,7 +940,16 @@ loop_body:
 			}
 			/* fall through */
 		default:
-			strcpy(ent_path, ftsent->fts_path);
+			if (strlcpy(ent_path, ftsent->fts_path, sizeof(ent_path)) >= sizeof(ent_path)) {
+				selinux_log(SELINUX_ERROR,
+					    "Path name too long on %s.\n",
+					    ftsent->fts_path);
+				errno = ENAMETOOLONG;
+				state->error = -1;
+				state->abort = true;
+				goto finish;
+			}
+
 			ent_st = *ftsent->fts_statp;
 			if (state->parallel)
 				pthread_mutex_unlock(&state->mutex);
libselinux/src/setrans_client.c +7 -1
index faa12681..920f9032 100644
@@ -66,7 +66,13 @@ static int setransd_open(void)
 
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_UNIX;
-	strncpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path));
+
+	if (strlcpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) {
+		close(fd);
+		errno = EOVERFLOW;
+		return -1;
+	}
+
 	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
 		close(fd);
 		return -1;
Signature · 1 line
2.36.1
#4 lore raw

Re: [RFC PATCH 4/4] libselinux: check for truncations

Quoted 38 lines · Christian Göttsche

Check for truncations when building or copying strings involving user input.

---
 libselinux/src/canonicalize_context.c |  6 +++++-
 libselinux/src/compute_av.c           |  7 ++++++-
 libselinux/src/compute_create.c       |  6 ++++++
 libselinux/src/compute_member.c       |  7 ++++++-
 libselinux/src/compute_relabel.c      |  7 ++++++-
 libselinux/src/compute_user.c         |  7 ++++++-
 libselinux/src/selinux_restorecon.c   | 11 ++++++++++-
 libselinux/src/setrans_client.c       |  8 +++++++-
 8 files changed, 52 insertions(+), 7 deletions(-)
libselinux/src/canonicalize_context.c +5 -1
index faab7305..8a22a4cd 100644
@@ -33,7 +33,11 @@ int security_canonicalize_context_raw(const char * con,
                ret = -1;
                goto out;
        }
-       strncpy(buf, con, size);
+       if (strlcpy(buf, con, size) >= size) {
+               errno = EOVERFLOW;
+               ret = -1;
+               goto out;
+       }
 
        ret = write(fd, buf, strlen(buf) + 1);
        if (ret < 0)
libselinux/src/compute_av.c +0 -0
index 9d17339d..e513be6a 100644

libselinux/src/compute_av.c int security_compute_av_flags_raw(const char * scon, · line 46

Quoted 8 lines
libselinux/src/compute_av.c +3 -1
@@ -40,8 +40,13 @@ int security_compute_av_flags_raw(const char * scon,
        }
 
        kclass = unmap_class(tclass);
-       snprintf(buf, len, "%s %s %hu %x", scon, tcon,
+
+       ret = snprintf(buf, len, "%s %s %hu %x", scon, tcon,
                 kclass, unmap_perm(tclass, requested));
+       if (ret < 0 || ret >= len) {

error: comparison of integer expressions of different signedness:
‘int’ and ‘size_t’

Quoted 11 lines
libselinux/src/compute_av.c +3 -0
+               errno = EOVERFLOW;
+               goto out2;
+       }
 
        ret = write(fd, buf, strlen(buf));
        if (ret < 0)
libselinux/src/compute_create.c +0 -0
index 1d75714d..4cba2d2f 100644

libselinux/src/compute_create.c int security_compute_create_name_raw(const char * scon, · line 81

Quoted 7 lines
libselinux/src/compute_create.c +2 -0
@@ -75,8 +75,14 @@ int security_compute_create_name_raw(const char * scon,
                ret = -1;
                goto out;
        }
+
        len = snprintf(buf, size, "%s %s %hu",
                       scon, tcon, unmap_class(tclass));
+       if (len < 0 || len >= size) {

error: comparison of integer expressions of different signedness:
‘int’ and ‘size_t’

libselinux/src/compute_create.c int security_compute_create_name_raw(const char * scon, · line 82

Quoted 1 line
libselinux/src/compute_create.c +1 -0
+               errno = EOVERFLOW;

You need ret = -1 here.

Quoted 11 lines
libselinux/src/compute_create.c +3 -0
+               goto out2;
+       }
+
        if (objname &&
            object_name_encode(objname, buf + len, size - len) < 0) {
                errno = ENAMETOOLONG;
libselinux/src/compute_member.c +0 -0
index 16234b79..82d76080 100644

libselinux/src/compute_member.c int security_compute_member_raw(const char * scon, · line 41

Quoted 7 lines
libselinux/src/compute_member.c +3 -1
@@ -36,7 +36,12 @@ int security_compute_member_raw(const char * scon,
                ret = -1;
                goto out;
        }
-       snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+       ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+       if (ret < 0 || ret >= size) {

error: comparison of integer expressions of different signedness:
‘int’ and ‘size_t’

Quoted 11 lines
libselinux/src/compute_member.c +3 -0
+               errno = EOVERFLOW;
+               goto out2;
+       }
 
        ret = write(fd, buf, strlen(buf));
        if (ret < 0)
libselinux/src/compute_relabel.c +0 -0
index dd20d652..96259bac 100644

libselinux/src/compute_relabel.c int security_compute_relabel_raw(const char * scon, · line 41

Quoted 7 lines
libselinux/src/compute_relabel.c +3 -1
@@ -36,7 +36,12 @@ int security_compute_relabel_raw(const char * scon,
                ret = -1;
                goto out;
        }
-       snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+       ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+       if (ret < 0 || ret >= size) {

error: comparison of integer expressions of different signedness:
‘int’ and ‘size_t’

Quoted 11 lines
libselinux/src/compute_relabel.c +3 -0
+               errno = EOVERFLOW;
+               goto out2;
+       }
 
        ret = write(fd, buf, strlen(buf));
        if (ret < 0)
libselinux/src/compute_user.c +0 -0
index ae5e7b4a..23a551e4 100644

libselinux/src/compute_user.c int security_compute_user_raw(const char * scon, · line 43

Quoted 7 lines
libselinux/src/compute_user.c +3 -1
@@ -38,7 +38,12 @@ int security_compute_user_raw(const char * scon,
                ret = -1;
                goto out;
        }
-       snprintf(buf, size, "%s %s", scon, user);
+
+       ret = snprintf(buf, size, "%s %s", scon, user);
+       if (ret < 0 || ret >= size) {

error: comparison of integer expressions of different signedness:
‘int’ and ‘size_t’

Everything else looks fine.

Thanks,
Jim

Quoted 50 lines
libselinux/src/compute_user.c +3 -0
+               errno = EOVERFLOW;
+               goto out2;
+       }
 
        ret = write(fd, buf, strlen(buf));
        if (ret < 0)
libselinux/src/selinux_restorecon.c +10 -1
index e6192912..7436dab5 100644
@@ -940,7 +940,16 @@ loop_body:
                        }
                        /* fall through */
                default:
-                       strcpy(ent_path, ftsent->fts_path);
+                       if (strlcpy(ent_path, ftsent->fts_path, sizeof(ent_path)) >= sizeof(ent_path)) {
+                               selinux_log(SELINUX_ERROR,
+                                           "Path name too long on %s.\n",
+                                           ftsent->fts_path);
+                               errno = ENAMETOOLONG;
+                               state->error = -1;
+                               state->abort = true;
+                               goto finish;
+                       }
+
                        ent_st = *ftsent->fts_statp;
                        if (state->parallel)
                                pthread_mutex_unlock(&state->mutex);
libselinux/src/setrans_client.c +7 -2
index faa12681..920f9032 100644
@@ -66,7 +66,13 @@ static int setransd_open(void)
 
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
-       strncpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path));
+
+       if (strlcpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) {
+               close(fd);
+               errno = EOVERFLOW;
+               return -1;
+       }
+
        if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
                close(fd);
                return -1;
--

2.36.1

#5 lore raw

RFC PATCH v2 4/4 libselinux: check for truncations

In reply to Christian Göttsche

Check for truncations when building or copying strings involving user input.

---
v2:
- add explicit casts to avoid int <-> size_t comparisons
- ensure containing functions return -1

---
 libselinux/src/canonicalize_context.c |  6 +++++-
 libselinux/src/compute_av.c           |  8 +++++++-
 libselinux/src/compute_create.c       |  7 +++++++
 libselinux/src/compute_member.c       |  8 +++++++-
 libselinux/src/compute_relabel.c      |  8 +++++++-
 libselinux/src/compute_user.c         |  8 +++++++-
 libselinux/src/selinux_restorecon.c   | 10 +++++++++-
 libselinux/src/setrans_client.c       |  8 +++++++-
 8 files changed, 56 insertions(+), 7 deletions(-)
libselinux/src/canonicalize_context.c +5 -1
index faab7305..8a22a4cd 100644
@@ -33,7 +33,11 @@ int security_canonicalize_context_raw(const char * con,
 		ret = -1;
 		goto out;
 	}
-	strncpy(buf, con, size);
+	if (strlcpy(buf, con, size) >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out;
+	}
 
 	ret = write(fd, buf, strlen(buf) + 1);
 	if (ret < 0)
libselinux/src/compute_av.c +7 -1
index 9d17339d..354a19e1 100644
@@ -40,8 +40,14 @@ int security_compute_av_flags_raw(const char * scon,
 	}
 
 	kclass = unmap_class(tclass);
-	snprintf(buf, len, "%s %s %hu %x", scon, tcon,
+
+	ret = snprintf(buf, len, "%s %s %hu %x", scon, tcon,
 		 kclass, unmap_perm(tclass, requested));
+	if (ret < 0 || (size_t)ret >= len) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_create.c +7 -0
index 1d75714d..e9f3c96a 100644
@@ -75,8 +75,15 @@ int security_compute_create_name_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
+
 	len = snprintf(buf, size, "%s %s %hu",
 		       scon, tcon, unmap_class(tclass));
+	if (len < 0 || (size_t)len >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
+
 	if (objname &&
 	    object_name_encode(objname, buf + len, size - len) < 0) {
 		errno = ENAMETOOLONG;
libselinux/src/compute_member.c +7 -1
index 16234b79..53d2f559 100644
@@ -36,7 +36,13 @@ int security_compute_member_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+	ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_relabel.c +7 -1
index dd20d652..9c0a2304 100644
@@ -36,7 +36,13 @@ int security_compute_relabel_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+	ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_user.c +7 -1
index ae5e7b4a..f55f945a 100644
@@ -38,7 +38,13 @@ int security_compute_user_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s", scon, user);
+
+	ret = snprintf(buf, size, "%s %s", scon, user);
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/selinux_restorecon.c +9 -1
index 9dd6be81..ab79f543 100644
@@ -961,7 +961,15 @@ loop_body:
 			}
 			/* fall through */
 		default:
-			strcpy(ent_path, ftsent->fts_path);
+			if (strlcpy(ent_path, ftsent->fts_path, sizeof(ent_path)) >= sizeof(ent_path)) {
+				selinux_log(SELINUX_ERROR,
+					    "Path name too long on %s.\n",
+					    ftsent->fts_path);
+				errno = ENAMETOOLONG;
+				state->error = -1;
+				state->abort = true;
+				goto finish;
+			}
 
 			if (state->parallel)
 				pthread_mutex_unlock(&state->mutex);
libselinux/src/setrans_client.c +7 -1
index faa12681..920f9032 100644
@@ -66,7 +66,13 @@ static int setransd_open(void)
 
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_UNIX;
-	strncpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path));
+
+	if (strlcpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) {
+		close(fd);
+		errno = EOVERFLOW;
+		return -1;
+	}
+
 	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
 		close(fd);
 		return -1;
Signature · 1 line
2.36.1
#6 lore raw

PATCH v3 4/4 libselinux: check for truncations

Check for truncations when building or copying strings involving user input.

---
v3:
- free buf in error branch in security_canonicalize_context_raw()
v2:
- add explicit casts to avoid int <-> size_t comparisons
- ensure containing functions return -1

---
 libselinux/src/canonicalize_context.c |  6 +++++-
 libselinux/src/compute_av.c           |  8 +++++++-
 libselinux/src/compute_create.c       |  7 +++++++
 libselinux/src/compute_member.c       |  8 +++++++-
 libselinux/src/compute_relabel.c      |  8 +++++++-
 libselinux/src/compute_user.c         |  8 +++++++-
 libselinux/src/selinux_restorecon.c   | 10 +++++++++-
 libselinux/src/setrans_client.c       |  8 +++++++-
 8 files changed, 56 insertions(+), 7 deletions(-)
libselinux/src/canonicalize_context.c +5 -1
index faab7305..6af8491d 100644
@@ -33,7 +33,11 @@ int security_canonicalize_context_raw(const char * con,
 		ret = -1;
 		goto out;
 	}
-	strncpy(buf, con, size);
+	if (strlcpy(buf, con, size) >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf) + 1);
 	if (ret < 0)
libselinux/src/compute_av.c +7 -1
index 9d17339d..354a19e1 100644
@@ -40,8 +40,14 @@ int security_compute_av_flags_raw(const char * scon,
 	}
 
 	kclass = unmap_class(tclass);
-	snprintf(buf, len, "%s %s %hu %x", scon, tcon,
+
+	ret = snprintf(buf, len, "%s %s %hu %x", scon, tcon,
 		 kclass, unmap_perm(tclass, requested));
+	if (ret < 0 || (size_t)ret >= len) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_create.c +7 -0
index 1d75714d..e9f3c96a 100644
@@ -75,8 +75,15 @@ int security_compute_create_name_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
+
 	len = snprintf(buf, size, "%s %s %hu",
 		       scon, tcon, unmap_class(tclass));
+	if (len < 0 || (size_t)len >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
+
 	if (objname &&
 	    object_name_encode(objname, buf + len, size - len) < 0) {
 		errno = ENAMETOOLONG;
libselinux/src/compute_member.c +7 -1
index 16234b79..53d2f559 100644
@@ -36,7 +36,13 @@ int security_compute_member_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+	ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_relabel.c +7 -1
index dd20d652..9c0a2304 100644
@@ -36,7 +36,13 @@ int security_compute_relabel_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+	ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_user.c +7 -1
index ae5e7b4a..f55f945a 100644
@@ -38,7 +38,13 @@ int security_compute_user_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s", scon, user);
+
+	ret = snprintf(buf, size, "%s %s", scon, user);
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/selinux_restorecon.c +9 -1
index 9dd6be81..ab79f543 100644
@@ -961,7 +961,15 @@ loop_body:
 			}
 			/* fall through */
 		default:
-			strcpy(ent_path, ftsent->fts_path);
+			if (strlcpy(ent_path, ftsent->fts_path, sizeof(ent_path)) >= sizeof(ent_path)) {
+				selinux_log(SELINUX_ERROR,
+					    "Path name too long on %s.\n",
+					    ftsent->fts_path);
+				errno = ENAMETOOLONG;
+				state->error = -1;
+				state->abort = true;
+				goto finish;
+			}
 
 			if (state->parallel)
 				pthread_mutex_unlock(&state->mutex);
libselinux/src/setrans_client.c +7 -1
index faa12681..920f9032 100644
@@ -66,7 +66,13 @@ static int setransd_open(void)
 
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_UNIX;
-	strncpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path));
+
+	if (strlcpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) {
+		close(fd);
+		errno = EOVERFLOW;
+		return -1;
+	}
+
 	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
 		close(fd);
 		return -1;
Signature · 1 line
2.36.1
#7 lore raw

PATCH v4 4/4 libselinux: check for truncations

In reply to Christian Göttsche

Check for truncations when building or copying strings involving user input.

---
v4:
- rebase on top of de285252 ("Revert "libselinux: restorecon: pin file to avoid TOCTOU issues"")
v3:
- free buf in error branch in security_canonicalize_context_raw()
v2:
- add explicit casts to avoid int <-> size_t comparisons
- ensure containing functions return -1

---
 libselinux/src/canonicalize_context.c |  6 +++++-
 libselinux/src/compute_av.c           |  8 +++++++-
 libselinux/src/compute_create.c       |  7 +++++++
 libselinux/src/compute_member.c       |  8 +++++++-
 libselinux/src/compute_relabel.c      |  8 +++++++-
 libselinux/src/compute_user.c         |  8 +++++++-
 libselinux/src/selinux_restorecon.c   | 11 ++++++++++-
 libselinux/src/setrans_client.c       |  8 +++++++-
 8 files changed, 57 insertions(+), 7 deletions(-)
libselinux/src/canonicalize_context.c +5 -1
index faab7305..6af8491d 100644
@@ -33,7 +33,11 @@ int security_canonicalize_context_raw(const char * con,
 		ret = -1;
 		goto out;
 	}
-	strncpy(buf, con, size);
+	if (strlcpy(buf, con, size) >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf) + 1);
 	if (ret < 0)
libselinux/src/compute_av.c +7 -1
index 9d17339d..354a19e1 100644
@@ -40,8 +40,14 @@ int security_compute_av_flags_raw(const char * scon,
 	}
 
 	kclass = unmap_class(tclass);
-	snprintf(buf, len, "%s %s %hu %x", scon, tcon,
+
+	ret = snprintf(buf, len, "%s %s %hu %x", scon, tcon,
 		 kclass, unmap_perm(tclass, requested));
+	if (ret < 0 || (size_t)ret >= len) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_create.c +7 -0
index 1d75714d..e9f3c96a 100644
@@ -75,8 +75,15 @@ int security_compute_create_name_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
+
 	len = snprintf(buf, size, "%s %s %hu",
 		       scon, tcon, unmap_class(tclass));
+	if (len < 0 || (size_t)len >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
+
 	if (objname &&
 	    object_name_encode(objname, buf + len, size - len) < 0) {
 		errno = ENAMETOOLONG;
libselinux/src/compute_member.c +7 -1
index 16234b79..53d2f559 100644
@@ -36,7 +36,13 @@ int security_compute_member_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+	ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_relabel.c +7 -1
index dd20d652..9c0a2304 100644
@@ -36,7 +36,13 @@ int security_compute_relabel_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+
+	ret = snprintf(buf, size, "%s %s %hu", scon, tcon, unmap_class(tclass));
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/compute_user.c +7 -1
index ae5e7b4a..f55f945a 100644
@@ -38,7 +38,13 @@ int security_compute_user_raw(const char * scon,
 		ret = -1;
 		goto out;
 	}
-	snprintf(buf, size, "%s %s", scon, user);
+
+	ret = snprintf(buf, size, "%s %s", scon, user);
+	if (ret < 0 || (size_t)ret >= size) {
+		errno = EOVERFLOW;
+		ret = -1;
+		goto out2;
+	}
 
 	ret = write(fd, buf, strlen(buf));
 	if (ret < 0)
libselinux/src/selinux_restorecon.c +10 -1
index 9f5b326c..66e6a4a2 100644
@@ -954,7 +954,16 @@ loop_body:
 			}
 			/* fall through */
 		default:
-			strcpy(ent_path, ftsent->fts_path);
+			if (strlcpy(ent_path, ftsent->fts_path, sizeof(ent_path)) >= sizeof(ent_path)) {
+				selinux_log(SELINUX_ERROR,
+					    "Path name too long on %s.\n",
+					    ftsent->fts_path);
+				errno = ENAMETOOLONG;
+				state->error = -1;
+				state->abort = true;
+				goto finish;
+			}
+
 			ent_st = *ftsent->fts_statp;
 			if (state->parallel)
 				pthread_mutex_unlock(&state->mutex);
libselinux/src/setrans_client.c +7 -1
index faa12681..920f9032 100644
@@ -66,7 +66,13 @@ static int setransd_open(void)
 
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_UNIX;
-	strncpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path));
+
+	if (strlcpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) {
+		close(fd);
+		errno = EOVERFLOW;
+		return -1;
+	}
+
 	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
 		close(fd);
 		return -1;
Signature · 1 line
2.36.1
Acked-by on v1 1/4
#8 lore raw
In reply to Christian Göttsche
Quoted 7 lines · Christian Göttsche

In case the function __policy_init() gets called with a NULL pointer, the stack variable path remains uninitialized (except at its last index). If parsing the binary policy fails in sepol_policydb_read() the error branch would access those uninitialized memory.

For the series with v2 of patch 4:

Quoted 73 lines
---
 libselinux/src/audit2why.c | 34 +++++++++++++---------------------
 1 file changed, 13 insertions(+), 21 deletions(-)
libselinux/src/audit2why.c +13 -22
index ca38e13c..44a9a341 100644
@@ -192,25 +192,16 @@ static PyObject *finish(PyObject *self __attribute__((unused)), PyObject *args)
 static int __policy_init(const char *init_path)
 {
        FILE *fp;
-       char path[PATH_MAX];
+       const char *curpolicy;
        char errormsg[PATH_MAX+1024+20];
        struct sepol_policy_file *pf = NULL;
        int rc;
        unsigned int cnt;
 
-       path[PATH_MAX-1] = '\0';
        if (init_path) {
-               strncpy(path, init_path, PATH_MAX-1);
-               fp = fopen(path, "re");
-               if (!fp) {
-                       snprintf(errormsg, sizeof(errormsg),
-                                "unable to open %s:  %m\n",
-                                path);
-                       PyErr_SetString( PyExc_ValueError, errormsg);
-                       return 1;
-               }
+               curpolicy = init_path;
        } else {
-               const char *curpolicy = selinux_current_policy_path();
+               curpolicy = selinux_current_policy_path();
                if (!curpolicy) {
                        /* SELinux disabled, must use -p option. */
                        snprintf(errormsg, sizeof(errormsg),
@@ -218,14 +209,15 @@ static int __policy_init(const char *init_path)
                        PyErr_SetString( PyExc_ValueError, errormsg);
                        return 1;
                }
-               fp = fopen(curpolicy, "re");
-               if (!fp) {
-                       snprintf(errormsg, sizeof(errormsg),
-                                "unable to open %s:  %m\n",
-                                curpolicy);
-                       PyErr_SetString( PyExc_ValueError, errormsg);
-                       return 1;
-               }
+       }
+
+       fp = fopen(curpolicy, "re");
+       if (!fp) {
+               snprintf(errormsg, sizeof(errormsg),
+                        "unable to open %s:  %m\n",
+                        curpolicy);
+               PyErr_SetString( PyExc_ValueError, errormsg);
+               return 1;
        }
 
        avc = calloc(sizeof(struct avc_t), 1);
@@ -249,7 +241,7 @@ static int __policy_init(const char *init_path)
        sepol_policy_file_set_fp(pf, fp);
        if (sepol_policydb_read(avc->policydb, pf)) {
                snprintf(errormsg, sizeof(errormsg),
-                        "invalid binary policy %s\n", path);
+                        "invalid binary policy %s\n", curpolicy);
                PyErr_SetString( PyExc_ValueError, errormsg);
                fclose(fp);
                return 1;
--

2.36.1

#9 lore raw
Quoted 9 lines · James Carter
Quoted 6 lines · Christian Göttsche

In case the function __policy_init() gets called with a NULL pointer, the stack variable path remains uninitialized (except at its last index). If parsing the binary policy fails in sepol_policydb_read() the error branch would access those uninitialized memory.

Quoted 5 lines
Quoted 1 line

For the series with v2 of patch 4:

Merged with v4 of patch 4.
Thanks,
Jim

Quoted 73 lines
Quoted 73 lines
---
 libselinux/src/audit2why.c | 34 +++++++++++++---------------------
 1 file changed, 13 insertions(+), 21 deletions(-)
libselinux/src/audit2why.c +13 -22
index ca38e13c..44a9a341 100644
@@ -192,25 +192,16 @@ static PyObject *finish(PyObject *self __attribute__((unused)), PyObject *args)
 static int __policy_init(const char *init_path)
 {
        FILE *fp;
-       char path[PATH_MAX];
+       const char *curpolicy;
        char errormsg[PATH_MAX+1024+20];
        struct sepol_policy_file *pf = NULL;
        int rc;
        unsigned int cnt;
 
-       path[PATH_MAX-1] = '\0';
        if (init_path) {
-               strncpy(path, init_path, PATH_MAX-1);
-               fp = fopen(path, "re");
-               if (!fp) {
-                       snprintf(errormsg, sizeof(errormsg),
-                                "unable to open %s:  %m\n",
-                                path);
-                       PyErr_SetString( PyExc_ValueError, errormsg);
-                       return 1;
-               }
+               curpolicy = init_path;
        } else {
-               const char *curpolicy = selinux_current_policy_path();
+               curpolicy = selinux_current_policy_path();
                if (!curpolicy) {
                        /* SELinux disabled, must use -p option. */
                        snprintf(errormsg, sizeof(errormsg),
@@ -218,14 +209,15 @@ static int __policy_init(const char *init_path)
                        PyErr_SetString( PyExc_ValueError, errormsg);
                        return 1;
                }
-               fp = fopen(curpolicy, "re");
-               if (!fp) {
-                       snprintf(errormsg, sizeof(errormsg),
-                                "unable to open %s:  %m\n",
-                                curpolicy);
-                       PyErr_SetString( PyExc_ValueError, errormsg);
-                       return 1;
-               }
+       }
+
+       fp = fopen(curpolicy, "re");
+       if (!fp) {
+               snprintf(errormsg, sizeof(errormsg),
+                        "unable to open %s:  %m\n",
+                        curpolicy);
+               PyErr_SetString( PyExc_ValueError, errormsg);
+               return 1;
        }
 
        avc = calloc(sizeof(struct avc_t), 1);
@@ -249,7 +241,7 @@ static int __policy_init(const char *init_path)
        sepol_policy_file_set_fp(pf, fp);
        if (sepol_policydb_read(avc->policydb, pf)) {
                snprintf(errormsg, sizeof(errormsg),
-                        "invalid binary policy %s\n", path);
+                        "invalid binary policy %s\n", curpolicy);
                PyErr_SetString( PyExc_ValueError, errormsg);
                fclose(fp);
                return 1;
--

2.36.1