Skip to contentSkip to conversation

libselinux/man: mention errno for regex compilation failure

PATCH In review Christian Göttsche 2 participants 13 messages Dec 19, 2023 – Jan 25, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/11 libselinux/man: mention errno for regex compilation failure

Selabel lookups might fail with errno set to EINVAL in the unlikely case a regular expression from the file context definition failed to compile.

---
 libselinux/man/man3/selabel_lookup.3            | 3 ++-
 libselinux/man/man3/selabel_lookup_best_match.3 | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
libselinux/man/man3/selabel_lookup.3 +2 -1
index 4e47c3ec..e20345e6 100644
@@ -64,7 +64,8 @@ The
 .I key
 and/or
 .I type
-inputs are invalid, or the context being returned failed validation.
+inputs are invalid, or the context being returned failed validation, or a
+regular expression in the database failed to compile.
 .TP
 .B ENOMEM
 An attempt to allocate memory failed.
libselinux/man/man3/selabel_lookup_best_match.3 +2 -1
index ef2efb4a..985a8600 100644
@@ -78,7 +78,8 @@ The
 .I key
 and/or
 .I type
-inputs are invalid, or the context being returned failed validation.
+inputs are invalid, or the context being returned failed validation, or a
+regular expression in the database failed to compile.
 .TP
 .B ENOMEM
 An attempt to allocate memory failed.
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/11 libselinux/man: sync selinux_check_securetty_context(3)

Add the missing const qualifier.

---
 libselinux/man/man3/selinux_check_securetty_context.3 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libselinux/man/man3/selinux_check_securetty_context.3 +2 -2
index 429023bb..7b64cc5e 100644
@@ -5,12 +5,12 @@ selinux_check_securetty_context \- check whether a SELinux tty security context
 .SH "SYNOPSIS"
 .B #include <selinux/selinux.h>
 .sp
-.BI "int selinux_check_securetty_context(char *" tty_context );
+.BI "int selinux_check_securetty_context(const char *" tty_context );
 .
 .SH "DESCRIPTION"
 .BR selinux_check_securetty_context ()
 returns 0 if tty_context is a securetty context,
-returns < 0 otherwise. 
+returns < 0 otherwise.
 .
 .SH "SEE ALSO"
 .BR selinux "(8)"
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/11 libselinux/utils: free allocated resources

In reply to Christian Göttsche

Remove noise while running with sanitizers or under valgrind.

---
 libselinux/utils/getconlist.c     | 10 +++++++---
 libselinux/utils/getdefaultcon.c  | 20 +++++++++++++++++---
 libselinux/utils/selinuxexeccon.c |  1 +
 3 files changed, 25 insertions(+), 6 deletions(-)
libselinux/utils/getconlist.c +7 -3
index 92f6a793..1ff05209 100644
@@ -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;
libselinux/utils/getdefaultcon.c +17 -3
index 50f1ea91..67c84f94 100644
@@ -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;
 }
libselinux/utils/selinuxexeccon.c +1 -0
index 66754b6a..463bf5aa 100644
@@ -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;
 		}
 	}
Signature · 1 line
2.43.0
#3 lore raw

PATCH 4/11 libselinux/utils: improve compute_av output

In reply to Christian Göttsche

Show the more interesting inverse of the auditdeny vector as dontaudit.

Show the inverse of the decided vector, although since Linux v2.6.30 f1c6381a6e33 ("SELinux: remove unused av.decided field") all permissions are always decided.

    $ compute_av staff_u:staff_r:staff_t:s0 sysadm_u:sysadm_r:sysadm_t:s0 process
    allowed= null
    auditdeny= { fork transition sigchld sigkill sigstop signull ptrace getsched setsched getsession getpgid setpgid getcap setcap share getattr setexec setfscreate noatsecure siginh setrlimit rlimitinh dyntransition setcurrent execmem execstack execheap setkeycreate setsockcreate getrlimit 0x80000000 }
    dontaudit= { signal }
---
 libselinux/utils/compute_av.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
libselinux/utils/compute_av.c +11 -3
index cca407d6..09f574a0 100644
@@ -44,10 +44,14 @@ int main(int argc, char **argv)
 	print_access_vector(tclass, avd.allowed);
 	printf("\n");
 
-	if (avd.decided != ~0U) {
+	if (~avd.decided) {
 		printf("decided=");
 		print_access_vector(tclass, avd.decided);
 		printf("\n");
+
+		printf("undecided=");
+		print_access_vector(tclass, ~avd.decided);
+		printf("\n");
 	}
 
 	if (avd.auditallow) {
@@ -56,10 +60,14 @@ int main(int argc, char **argv)
 		printf("\n");
 	}
 
-	if (avd.auditdeny != ~0U) {
-		printf("auditdeny");
+	if (~avd.auditdeny) {
+		printf("auditdeny=");
 		print_access_vector(tclass, avd.auditdeny);
 		printf("\n");
+
+		printf("dontaudit=");
+		print_access_vector(tclass, ~avd.auditdeny);
+		printf("\n");
 	}
 
 	exit(EXIT_SUCCESS);
Signature · 1 line
2.43.0
#4 lore raw

PATCH 5/11 libselinux: align SELABEL_OPT_DIGEST usage with man page

In reply to Christian Göttsche

According to selabel_opn(3) a non-null value for this option enables the generation of an SHA1 digest of the spec files loaded as described in selabel_digest(3).

---
 libselinux/src/label.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/label.c +1 -1
index 1ea9bdcd..4a7c6e6d 100644
@@ -62,7 +62,7 @@ static inline struct selabel_digest *selabel_is_digest_set
 
 	while (n--) {
 		if (opts[n].type == SELABEL_OPT_DIGEST &&
-					    opts[n].value == (char *)1) {
+					    !!opts[n].value) {
 			digest = calloc(1, sizeof(*digest));
 			if (!digest)
 				goto err;
Signature · 1 line
2.43.0
#5 lore raw

PATCH 6/11 libselinux: fail selabel_open(3) on invalid option

In reply to Christian Göttsche

Return an error on invalid selabel_open(3) options, e.g. an option for a different backend was used.

---
 libselinux/src/label_backends_android.c | 7 +++++++
 libselinux/src/label_db.c               | 8 ++++++++
 libselinux/src/label_file.c             | 7 +++++++
 libselinux/src/label_media.c            | 7 +++++++
 libselinux/src/label_x.c                | 7 +++++++
 5 files changed, 36 insertions(+)
libselinux/src/label_backends_android.c +7 -0
index cd3875fc..7ddacdbe 100644
@@ -157,6 +157,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 		case SELABEL_OPT_PATH:
 			path = opts[n].value;
 			break;
+		case SELABEL_OPT_UNUSED:
+		case SELABEL_OPT_VALIDATE:
+		case SELABEL_OPT_DIGEST:
+			break;
+		default:
+			errno = EINVAL;
+			return -1;
 		}
 
 	if (!path)
libselinux/src/label_db.c +8 -0
index 3f803037..2daf1770 100644
@@ -268,6 +268,14 @@ db_init(const struct selinux_opt *opts, unsigned nopts,
 		case SELABEL_OPT_PATH:
 			path = opts[nopts].value;
 			break;
+		case SELABEL_OPT_UNUSED:
+		case SELABEL_OPT_VALIDATE:
+		case SELABEL_OPT_DIGEST:
+			break;
+		default:
+			free(catalog);
+			errno = EINVAL;
+			return NULL;
 		}
 	}
 
libselinux/src/label_file.c +7 -0
index 4778f8f8..315298b3 100644
@@ -812,6 +812,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 		case SELABEL_OPT_BASEONLY:
 			baseonly = !!opts[n].value;
 			break;
+		case SELABEL_OPT_UNUSED:
+		case SELABEL_OPT_VALIDATE:
+		case SELABEL_OPT_DIGEST:
+			break;
+		default:
+			errno = EINVAL;
+			return -1;
 		}
 
 #if !defined(BUILD_HOST) && !defined(ANDROID)
libselinux/src/label_media.c +7 -0
index b3443b47..4c987988 100644
@@ -85,6 +85,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 		case SELABEL_OPT_PATH:
 			path = opts[n].value;
 			break;
+		case SELABEL_OPT_UNUSED:
+		case SELABEL_OPT_VALIDATE:
+		case SELABEL_OPT_DIGEST:
+			break;
+		default:
+			errno = EINVAL;
+			return -1;
 		}
 
 	/* Open the specification file. */
libselinux/src/label_x.c +7 -0
index e15190ca..f332dcb6 100644
@@ -112,6 +112,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 		case SELABEL_OPT_PATH:
 			path = opts[n].value;
 			break;
+		case SELABEL_OPT_UNUSED:
+		case SELABEL_OPT_VALIDATE:
+		case SELABEL_OPT_DIGEST:
+			break;
+		default:
+			errno = EINVAL;
+			return -1;
 		}
 
 	/* Open the specification file. */
Signature · 1 line
2.43.0
#6 lore raw

PATCH 7/11 libselinux: use logging wrapper in getseuser(3) and get_default_context(3) family

In reply to Christian Göttsche

Instead of writing error messages directly to stderr use the wrapper selinux_log(), which by default writes to stderr. This allows applications to redirect or silence messages via selinux_set_callback(3).

---
 libselinux/src/get_context_list.c | 10 ++++++----
 libselinux/src/seusers.c          |  6 ++++--
 2 files changed, 10 insertions(+), 6 deletions(-)
libselinux/src/get_context_list.c +6 -4
index 9dafa519..7e23be05 100644
@@ -7,7 +7,9 @@
 #include <string.h>
 #include <ctype.h>
 #include <pwd.h>
+
 #include "selinux_internal.h"
+#include "callbacks.h"
 #include "context_internal.h"
 #include "get_context_list_internal.h"
 
@@ -224,7 +226,7 @@ static int get_context_user(FILE * fp,
 
 		/* Check whether a new context is valid */
 		if (SIZE_MAX - user_len < strlen(start) + 2) {
-			fprintf(stderr, "%s: one of partial contexts is too big\n", __FUNCTION__);
+			selinux_log(SELINUX_ERROR, "%s: one of partial contexts is too big\n", __FUNCTION__);
 			errno = EINVAL;
 			rc = -1;
 			goto out;
@@ -245,7 +247,7 @@ static int get_context_user(FILE * fp,
 				rc = -1;
 				goto out;
 			}
-			fprintf(stderr,
+			selinux_log(SELINUX_ERROR,
 				"%s: can't create a context from %s, skipping\n",
 				__FUNCTION__, usercon_str);
 			free(usercon_str);
@@ -439,7 +441,7 @@ int get_ordered_context_list(const char *user,
 
 		fclose(fp);
 		if (rc < 0 && errno != ENOENT) {
-			fprintf(stderr,
+			selinux_log(SELINUX_ERROR,
 				"%s:  error in processing configuration file %s\n",
 				__FUNCTION__, fname);
 			/* Fall through, try global config */
@@ -452,7 +454,7 @@ int get_ordered_context_list(const char *user,
 		rc = get_context_user(fp, fromcon, user, &reachable, &nreachable);
 		fclose(fp);
 		if (rc < 0 && errno != ENOENT) {
-			fprintf(stderr,
+			selinux_log(SELINUX_ERROR,
 				"%s:  error in processing configuration file %s\n",
 				__FUNCTION__, selinux_default_context_path());
 			/* Fall through */
libselinux/src/seusers.c +4 -2
index 6da8c318..e5cfd510 100644
@@ -8,7 +8,9 @@
 #include <errno.h>
 #include <selinux/selinux.h>
 #include <selinux/context.h>
+
 #include "selinux_internal.h"
+#include "callbacks.h"
 
 /* Process line from seusers.conf and split into its fields.
    Returns 0 on success, -1 on comments, and -2 on error. */
@@ -197,8 +199,8 @@ int getseuserbyname(const char *name, char **r_seuser, char **r_level)
 		if (rc == -1)
 			continue;	/* comment, skip */
 		if (rc == -2) {
-			fprintf(stderr, "%s:  error on line %lu, skipping...\n",
-				selinux_usersconf_path(), lineno);
+			selinux_log(SELINUX_ERROR, "%s:  error on line %lu, skipping...\n",
+						   selinux_usersconf_path(), lineno);
 			continue;
 		}
 
Signature · 1 line
2.43.0
#7 lore raw

PATCH 8/11 libselinux: support huge passwd/group entries

In reply to Christian Göttsche

getpwnam_r(3) and getgrnam_r(3) might return ERANGE in case the supplied buffer was too short for the passwd/group entry. Retry with a bigger buffer.

Also use a fallback buffer size in case the libc returns -1 for sysconf(3) of _SC_GETPW_R_SIZE_MAX or _SC_GETGR_R_SIZE_MAX, like musl.

---
 libselinux/src/seusers.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
libselinux/src/seusers.c +25 -8
index e5cfd510..16d69347 100644
@@ -6,6 +6,8 @@
 #include <stdio_ext.h>
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
+
 #include <selinux/selinux.h>
 #include <selinux/context.h>
 
@@ -99,15 +101,30 @@ static gid_t get_default_gid(const char *name) {
 	struct passwd pwstorage, *pwent = NULL;
 	gid_t gid = -1;
 	/* Allocate space for the getpwnam_r buffer */
+	char *rbuf = NULL;
 	long rbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
-	if (rbuflen <= 0) return -1;
-	char *rbuf = malloc(rbuflen);
-	if (rbuf == NULL) return -1;
+	if (rbuflen <= 0)
+		rbuflen = 1024;
+
+	for (;;) {
+		int rc;
+
+		rbuf = malloc(rbuflen);
+		if (rbuf == NULL)
+			break;
 
-	int retval = getpwnam_r(name, &pwstorage, rbuf, rbuflen, &pwent);
-	if (retval == 0 && pwent) {
-		gid = pwent->pw_gid;
+		rc = getpwnam_r(name, &pwstorage, rbuf, rbuflen, &pwent);
+		if (rc == ERANGE && rbuflen < LONG_MAX / 2) {
+			free(rbuf);
+			rbuflen *= 2;
+			continue;
+		}
+		if (rc == 0 && pwent)
+			gid = pwent->pw_gid;
+
+		break;
 	}
+
 	free(rbuf);
 	return gid;
 }
@@ -120,7 +137,7 @@ static int check_group(const char *group, const char *name, const gid_t gid) {
 
 	long rbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
 	if (rbuflen <= 0)
-		return 0;
+		rbuflen = 1024;
 	char *rbuf;
 
 	while(1) {
@@ -129,7 +146,7 @@ static int check_group(const char *group, const char *name, const gid_t gid) {
 			return 0;
 		int retval = getgrnam_r(group, &gbuf, rbuf, 
 				rbuflen, &grent);
-		if ( retval == ERANGE )
+		if (retval == ERANGE && rbuflen < LONG_MAX / 2)
 		{
 			free(rbuf);
 			rbuflen = rbuflen * 2;
Signature · 1 line
2.43.0
#8 lore raw

PATCH 9/11 libsemanage: support huge passwd entries

In reply to Christian Göttsche

getpwnam_r(3) might return ERANGE in case the supplied buffer was too short for the passwd entry. Retry with a bigger buffer.

---
 libsemanage/src/genhomedircon.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
libsemanage/src/genhomedircon.c +10 -3
index 66585987..ecb34fc7 100644
@@ -985,9 +985,6 @@ static int add_user(genhomedircon_settings_t * s,
 		rbuflen = 1024;
 	else if (rbuflen <= 0)
 		goto cleanup;
-	rbuf = malloc(rbuflen);
-	if (rbuf == NULL)
-		goto cleanup;
 
 	if (user) {
 		prefix = semanage_user_get_prefix(user);
@@ -1005,7 +1002,17 @@ static int add_user(genhomedircon_settings_t * s,
 		homedir_role = prefix;
 	}
 
+retry:
+	rbuf = malloc(rbuflen);
+	if (rbuf == NULL)
+		goto cleanup;
+
 	retval = getpwnam_r(name, &pwstorage, rbuf, rbuflen, &pwent);
+	if (retval == ERANGE && rbuflen < LONG_MAX / 2) {
+		free(rbuf);
+		rbuflen *= 2;
+		goto retry;
+	}
 	if (retval != 0 || pwent == NULL) {
 		if (retval != 0 && retval != ENOENT) {
 			goto cleanup;
Signature · 1 line
2.43.0
#9 lore raw

PATCH 10/11 libselinux: enable usage with pedantic UB sanitizers

In reply to Christian Göttsche

Clang's undefined behavior sanitizer supports checking for unsigned integer overflow and underflow, and implicit conversions. While those operations are well-defined by the C language they can signal logic mistakes or processing of unchecked user input.

Annotate functions deliberately making use of integer overflow and adopt the remaining code sites.

Example reports:

stringrep.c:348:7: runtime error: left shift of 2147483648 by 1 places cannot be represented in type 'access_vector_t' (aka 'unsigned int') seusers.c:98:14: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'gid_t' (aka 'unsigned int') changed the value to 4294967295 (32-bit, unsigned)

---
 libselinux/src/avc.c                    |  4 +++-
 libselinux/src/avc_sidtab.c             |  1 +
 libselinux/src/label.c                  |  7 +++++--
 libselinux/src/label_backends_android.c |  4 +++-
 libselinux/src/label_db.c               |  3 ++-
 libselinux/src/label_file.c             |  6 ++++--
 libselinux/src/label_media.c            |  4 +++-
 libselinux/src/label_x.c                |  4 +++-
 libselinux/src/selinux_internal.h       | 11 +++++++++++
 libselinux/src/seusers.c                |  2 +-
 libselinux/src/sha1.c                   |  3 +++
 libselinux/src/stringrep.c              |  4 +++-
 12 files changed, 42 insertions(+), 11 deletions(-)
libselinux/src/avc.c +3 -1
index 5e1c036e..ce87ac16 100644
@@ -229,13 +229,15 @@ int avc_open(struct selinux_opt *opts, unsigned nopts)
 {
 	avc_setenforce = 0;
 
-	while (nopts--)
+	while (nopts) {
+		nopts--;
 		switch(opts[nopts].type) {
 		case AVC_OPT_SETENFORCE:
 			avc_setenforce = 1;
 			avc_enforcing = !!opts[nopts].value;
 			break;
 		}
+	}
 
 	return avc_init_internal("avc", NULL, NULL, NULL, NULL);
 }
libselinux/src/avc_sidtab.c +1 -0
index e396a938..3303537b 100644
@@ -13,6 +13,7 @@
 #include "avc_sidtab.h"
 #include "avc_internal.h"
 
+ignore_unsigned_overflow_
 static inline unsigned sidtab_hash(const char * key)
 {
 	unsigned int hash = 5381;
libselinux/src/label.c +5 -2
index 4a7c6e6d..d2e703ef 100644
@@ -60,7 +60,8 @@ static inline struct selabel_digest *selabel_is_digest_set
 {
 	struct selabel_digest *digest = NULL;
 
-	while (n--) {
+	while (n) {
+		n--;
 		if (opts[n].type == SELABEL_OPT_DIGEST &&
 					    !!opts[n].value) {
 			digest = calloc(1, sizeof(*digest));
@@ -112,9 +113,11 @@ static void selabel_digest_fini(struct selabel_digest *ptr)
 static inline int selabel_is_validate_set(const struct selinux_opt *opts,
 					  unsigned n)
 {
-	while (n--)
+	while (n) {
+		n--;
 		if (opts[n].type == SELABEL_OPT_VALIDATE)
 			return !!opts[n].value;
+	}
 
 	return 0;
 }
libselinux/src/label_backends_android.c +3 -1
index 7ddacdbe..33a17236 100644
@@ -152,7 +152,8 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 	struct stat sb;
 
 	/* Process arguments */
-	while (n--)
+	while (n) {
+		n--;
 		switch (opts[n].type) {
 		case SELABEL_OPT_PATH:
 			path = opts[n].value;
@@ -165,6 +166,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 			errno = EINVAL;
 			return -1;
 		}
+	}
 
 	if (!path)
 		return -1;
libselinux/src/label_db.c +2 -1
index 2daf1770..2ff10b2f 100644
@@ -263,7 +263,8 @@ db_init(const struct selinux_opt *opts, unsigned nopts,
 	 *   the default one. If RDBMS is not SE-PostgreSQL, it may need to
 	 *   specify an explicit specfile for database objects.
 	 */
-	while (nopts--) {
+	while (nopts) {
+		nopts--;
 		switch (opts[nopts].type) {
 		case SELABEL_OPT_PATH:
 			path = opts[nopts].value;
libselinux/src/label_file.c +4 -2
index 315298b3..3b2bda97 100644
@@ -68,7 +68,7 @@ static int find_stem_from_file(struct saved_data *data, const char *key)
 /*
  * hash calculation and key comparison of hash table
  */
-
+ignore_unsigned_overflow_
 static unsigned int symhash(hashtab_t h, const_hashtab_key_t key)
 {
 	const struct chkdups_key *k = (const struct chkdups_key *)key;
@@ -801,7 +801,8 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 	int status = -1, baseonly = 0;
 
 	/* Process arguments */
-	while (n--)
+	while (n) {
+		n--;
 		switch(opts[n].type) {
 		case SELABEL_OPT_PATH:
 			path = opts[n].value;
@@ -820,6 +821,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 			errno = EINVAL;
 			return -1;
 		}
+	}
 
 #if !defined(BUILD_HOST) && !defined(ANDROID)
 	char subs_file[PATH_MAX + 1];
libselinux/src/label_media.c +3 -1
index 4c987988..fad5ea6d 100644
@@ -80,7 +80,8 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 	struct stat sb;
 
 	/* Process arguments */
-	while (n--)
+	while (n) {
+		n--;
 		switch(opts[n].type) {
 		case SELABEL_OPT_PATH:
 			path = opts[n].value;
@@ -93,6 +94,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 			errno = EINVAL;
 			return -1;
 		}
+}
 
 	/* Open the specification file. */
 	if (!path)
libselinux/src/label_x.c +3 -1
index f332dcb6..bf569ca5 100644
@@ -107,7 +107,8 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 	struct stat sb;
 
 	/* Process arguments */
-	while (n--)
+	while (n) {
+		n--;
 		switch(opts[n].type) {
 		case SELABEL_OPT_PATH:
 			path = opts[n].value;
@@ -120,6 +121,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 			errno = EINVAL;
 			return -1;
 		}
+	}
 
 	/* Open the specification file. */
 	if (!path)
libselinux/src/selinux_internal.h +11 -0
index af69ff04..b134808e 100644
@@ -102,4 +102,15 @@ size_t strlcpy(char *dest, const char *src, size_t size);
 void *reallocarray(void *ptr, size_t nmemb, size_t size);
 #endif
 
+/* Use to ignore intentional unsigned under- and overflows while running under UBSAN. */
+#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ >= 4)
+#if (__clang_major__ >= 12)
+#define ignore_unsigned_overflow_        __attribute__((no_sanitize("unsigned-integer-overflow", "unsigned-shift-base")))
+#else
+#define ignore_unsigned_overflow_        __attribute__((no_sanitize("unsigned-integer-overflow")))
+#endif
+#else
+#define ignore_unsigned_overflow_
+#endif
+
 #endif /* SELINUX_INTERNAL_H_ */
libselinux/src/seusers.c +1 -1
index 16d69347..5a521f81 100644
@@ -99,7 +99,7 @@ int require_seusers  = 0;
 
 static gid_t get_default_gid(const char *name) {
 	struct passwd pwstorage, *pwent = NULL;
-	gid_t gid = -1;
+	gid_t gid = (gid_t)-1;
 	/* Allocate space for the getpwnam_r buffer */
 	char *rbuf = NULL;
 	long rbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
libselinux/src/sha1.c +3 -0
index 9d51e04a..452b0cc2 100644
@@ -26,6 +26,8 @@
 #include "sha1.h"
 #include <memory.h>
 
+#include "selinux_internal.h"
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //  TYPES
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -62,6 +64,7 @@ typedef union
 //
 //  Hash a single 512-bit block. This is the core of the algorithm
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ignore_unsigned_overflow_
 static
 void
     TransformFunction
libselinux/src/stringrep.c +3 -1
index d2237d1c..1b460224 100644
@@ -337,13 +337,15 @@ void print_access_vector(security_class_t tclass, access_vector_t av)
 
 	printf(" {");
 
-	while (av) {
+	for (;;) {
 		if (av & bit) {
 			permstr = security_av_perm_to_string(tclass, bit);
 			if (!permstr)
 				break;
 			printf(" %s", permstr);
 			av &= ~bit;
+			if (!av)
+				break;
 		}
 		bit <<= 1;
 	}
Signature · 1 line
2.43.0
#10 lore raw

PATCH 11/11 setfiles: avoid unsigned integer underflow

In reply to Christian Göttsche

While well-defined unsigned integer underflow might signal a logic mistake or processing of unchecked user input. Please Clang's undefined behavior sanitizer:

restore.c:91:37: runtime error: unsigned integer overflow: 1 - 2 cannot be represented in type 'unsigned long'

---
 policycoreutils/setfiles/restore.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
policycoreutils/setfiles/restore.c +5 -5
index 6131f46a..d045e948 100644
@@ -77,8 +77,8 @@ int process_glob(char *name, struct restore_opts *opts, size_t nthreads,
 		 long unsigned *skipped_errors)
 {
 	glob_t globbuf;
-	size_t i = 0;
-	int len, rc, errors;
+	size_t i, len;
+	int rc, errors;
 
 	memset(&globbuf, 0, sizeof(globbuf));
 
@@ -88,10 +88,10 @@ int process_glob(char *name, struct restore_opts *opts, size_t nthreads,
 		return errors;
 
 	for (i = 0; i < globbuf.gl_pathc; i++) {
-		len = strlen(globbuf.gl_pathv[i]) - 2;
-		if (len > 0 && strcmp(&globbuf.gl_pathv[i][len--], "/.") == 0)
+		len = strlen(globbuf.gl_pathv[i]);
+		if (len > 2 && strcmp(&globbuf.gl_pathv[i][len - 2], "/.") == 0)
 			continue;
-		if (len > 0 && strcmp(&globbuf.gl_pathv[i][len], "/..") == 0)
+		if (len > 3 && strcmp(&globbuf.gl_pathv[i][len - 3], "/..") == 0)
 			continue;
 		rc = selinux_restorecon_parallel(globbuf.gl_pathv[i],
 						 opts->restorecon_flags,
Signature · 1 line
2.43.0
Acked-by on v1 1/11
#11 lore raw
In reply to Christian Göttsche
Quoted 5 lines · Christian Göttsche

Selabel lookups might fail with errno set to EINVAL in the unlikely case a regular expression from the file context definition failed to compile.

For these 11 patches:

Quoted 37 lines
---
 libselinux/man/man3/selabel_lookup.3            | 3 ++-
 libselinux/man/man3/selabel_lookup_best_match.3 | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
libselinux/man/man3/selabel_lookup.3 +2 -1
index 4e47c3ec..e20345e6 100644
@@ -64,7 +64,8 @@ The
 .I key
 and/or
 .I type
-inputs are invalid, or the context being returned failed validation.
+inputs are invalid, or the context being returned failed validation, or a
+regular expression in the database failed to compile.
 .TP
 .B ENOMEM
 An attempt to allocate memory failed.
libselinux/man/man3/selabel_lookup_best_match.3 +2 -2
index ef2efb4a..985a8600 100644
@@ -78,7 +78,8 @@ The
 .I key
 and/or
 .I type
-inputs are invalid, or the context being returned failed validation.
+inputs are invalid, or the context being returned failed validation, or a
+regular expression in the database failed to compile.
 .TP
 .B ENOMEM
 An attempt to allocate memory failed.
--

2.43.0

#12 lore raw
Quoted 4 lines · James Carter
Quoted 1 line · Christian Göttsche
Quoted 8 lines
Quoted 4 lines

Selabel lookups might fail with errno set to EINVAL in the unlikely case a regular expression from the file context definition failed to compile.

For these 11 patches:

These 11 patches have been merged.
Thanks,
Jim

Quoted 37 lines
Quoted 37 lines
---
 libselinux/man/man3/selabel_lookup.3            | 3 ++-
 libselinux/man/man3/selabel_lookup_best_match.3 | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
libselinux/man/man3/selabel_lookup.3 +2 -1
index 4e47c3ec..e20345e6 100644
@@ -64,7 +64,8 @@ The
 .I key
 and/or
 .I type
-inputs are invalid, or the context being returned failed validation.
+inputs are invalid, or the context being returned failed validation, or a
+regular expression in the database failed to compile.
 .TP
 .B ENOMEM
 An attempt to allocate memory failed.
libselinux/man/man3/selabel_lookup_best_match.3 +2 -2
index ef2efb4a..985a8600 100644
@@ -78,7 +78,8 @@ The
 .I key
 and/or
 .I type
-inputs are invalid, or the context being returned failed validation.
+inputs are invalid, or the context being returned failed validation, or a
+regular expression in the database failed to compile.
 .TP
 .B ENOMEM
 An attempt to allocate memory failed.
--

2.43.0