Skip to contentSkip to conversation

libselinux: cast to unsigned char for character handling function

PATCH In review Christian Göttsche 2 participants 5 messages Nov 1, 2023 – Nov 7, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 libselinux: cast to unsigned char for character handling function

Character handling functions, like isspace(3), expect a value representable as unsigned char or equal to EOF. Otherwise the behavior is undefined.

See https://wiki.sei.cmu.edu/confluence/display/c/STR37-C.+Arguments+to+character-handling+functions+must+be+representable+as+an+unsigned+char

---
v3:
- compute_create.c: avoid cast by changing type of variable
- drop RFC and split from original patch set

---
 libselinux/src/booleans.c                        |  8 ++++----
 libselinux/src/compute_create.c                  |  2 +-
 libselinux/src/get_context_list.c                | 12 ++++++------
 libselinux/src/get_default_type.c                |  2 +-
 libselinux/src/label_file.c                      |  8 ++++----
 libselinux/src/label_media.c                     |  2 +-
 libselinux/src/label_support.c                   |  8 ++++----
 libselinux/src/label_x.c                         |  2 +-
 libselinux/src/load_policy.c                     |  2 +-
 libselinux/src/matchmediacon.c                   |  6 +++---
 libselinux/src/selinux_check_securetty_context.c |  4 ++--
 libselinux/src/selinux_config.c                  | 12 ++++++------
 libselinux/src/seusers.c                         |  6 +++---
 13 files changed, 37 insertions(+), 37 deletions(-)
libselinux/src/booleans.c +4 -4
index e34b39ff..c557df65 100644
@@ -107,7 +107,7 @@ char *selinux_boolean_sub(const char *name)
 		char *ptr;
 		char *src = line_buf;
 		char *dst;
-		while (*src && isspace(*src))
+		while (*src && isspace((unsigned char)*src))
 			src++;
 		if (!*src)
 			continue;
@@ -115,19 +115,19 @@ char *selinux_boolean_sub(const char *name)
 			continue;
 
 		ptr = src;
-		while (*ptr && !isspace(*ptr))
+		while (*ptr && !isspace((unsigned char)*ptr))
 			ptr++;
 		*ptr++ = '\0';
 		if (strcmp(src, name) != 0)
 			continue;
 
 		dst = ptr;
-		while (*dst && isspace(*dst))
+		while (*dst && isspace((unsigned char)*dst))
 			dst++;
 		if (!*dst)
 			continue;
 		ptr = dst;
-		while (*ptr && !isspace(*ptr))
+		while (*ptr && !isspace((unsigned char)*ptr))
 			ptr++;
 		*ptr = '\0';
 
libselinux/src/compute_create.c +1 -1
index e9f3c96a..d19baa0b 100644
@@ -13,7 +13,7 @@
 
 static int object_name_encode(const char *objname, char *buffer, size_t buflen)
 {
-	int	code;
+	unsigned char code;
 	size_t	offset = 0;
 
 	if (buflen - offset < 1)
libselinux/src/get_context_list.c +6 -6
index d774b9cf..0342823c 100644
@@ -170,27 +170,27 @@ static int get_context_user(FILE * fp,
 
 		/* Skip leading whitespace. */
 		start = line;
-		while (*start && isspace(*start))
+		while (*start && isspace((unsigned char)*start))
 			start++;
 		if (!(*start))
 			continue;
 
 		/* Find the end of the (partial) fromcon in the line. */
 		end = start;
-		while (*end && !isspace(*end))
+		while (*end && !isspace((unsigned char)*end))
 			end++;
 		if (!(*end))
 			continue;
 
 		/* Check for a match. */
 		linerole = start;
-		while (*start && !isspace(*start) && *start != ':')
+		while (*start && !isspace((unsigned char)*start) && *start != ':')
 			start++;
 		if (*start != ':')
 			continue;
 		*start = 0;
 		linetype = ++start;
-		while (*start && !isspace(*start) && *start != ':')
+		while (*start && !isspace((unsigned char)*start) && *start != ':')
 			start++;
 		if (!(*start))
 			continue;
@@ -210,14 +210,14 @@ static int get_context_user(FILE * fp,
 	start = ++end;
 	while (*start) {
 		/* Skip leading whitespace */
-		while (*start && isspace(*start))
+		while (*start && isspace((unsigned char)*start))
 			start++;
 		if (!(*start))
 			break;
 
 		/* Find the end of this partial context. */
 		end = start;
-		while (*end && !isspace(*end))
+		while (*end && !isspace((unsigned char)*end))
 			end++;
 		if (*end)
 			*end++ = 0;
libselinux/src/get_default_type.c +1 -1
index 766ea4b7..87e9c72c 100644
@@ -42,7 +42,7 @@ static int find_default_type(FILE * fp, const char *role, char **type)
 			buf[strlen(buf) - 1] = 0;
 
 		ptr = buf;
-		while (*ptr && isspace(*ptr))
+		while (*ptr && isspace((unsigned char)*ptr))
 			ptr++;
 		if (!(*ptr))
 			continue;
libselinux/src/label_file.c +4 -4
index a665c9b4..4778f8f8 100644
@@ -708,20 +708,20 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 		char *src = buf;
 		char *dst = NULL;
 
-		while (*src && isspace(*src))
+		while (*src && isspace((unsigned char)*src))
 			src++;
 		if (src[0] == '#') continue;
 		ptr = src;
-		while (*ptr && ! isspace(*ptr))
+		while (*ptr && ! isspace((unsigned char)*ptr))
 			ptr++;
 		*ptr++ = '\0';
 		if (! *src) continue;
 
 		dst = ptr;
-		while (*dst && isspace(*dst))
+		while (*dst && isspace((unsigned char)*dst))
 			dst++;
 		ptr = dst;
-		while (*ptr && ! isspace(*ptr))
+		while (*ptr && ! isspace((unsigned char)*ptr))
 			ptr++;
 		*ptr = '\0';
 		if (! *dst)
libselinux/src/label_media.c +1 -1
index d33b614a..b3443b47 100644
@@ -39,7 +39,7 @@ static int process_line(const char *path, char *line_buf, int pass,
 	char *key, *context;
 
 	buf_p = line_buf;
-	while (isspace(*buf_p))
+	while (isspace((unsigned char)*buf_p))
 		buf_p++;
 	/* Skip comment lines and empty lines. */
 	if (*buf_p == '#' || *buf_p == 0)
libselinux/src/label_support.c +4 -4
index 49f72b1c..f7ab9292 100644
@@ -26,14 +26,14 @@ static inline int read_spec_entry(char **entry, char **ptr, int *len, const char
 	*entry = NULL;
 	char *tmp_buf = NULL;
 
-	while (isspace(**ptr) && **ptr != '\0')
+	while (isspace((unsigned char)**ptr) && **ptr != '\0')
 		(*ptr)++;
 
 	tmp_buf = *ptr;
 	*len = 0;
 
-	while (!isspace(**ptr) && **ptr != '\0') {
-		if (!isascii(**ptr)) {
+	while (!isspace((unsigned char)**ptr) && **ptr != '\0') {
+		if (!isascii((unsigned char)**ptr)) {
 			errno = EINVAL;
 			*errbuf = "Non-ASCII characters found";
 			return -1;
@@ -80,7 +80,7 @@ int  read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)
 		len++;
 
 	buf_p = line_buf;
-	while (isspace(*buf_p))
+	while (isspace((unsigned char)*buf_p))
 		buf_p++;
 
 	/* Skip comment lines and empty lines. */
libselinux/src/label_x.c +1 -1
index 4b8f524f..e15190ca 100644
@@ -41,7 +41,7 @@ static int process_line(const char *path, char *line_buf, int pass,
 	char *type, *key, *context;
 
 	buf_p = line_buf;
-	while (isspace(*buf_p))
+	while (isspace((unsigned char)*buf_p))
 		buf_p++;
 	/* Skip comment lines and empty lines. */
 	if (*buf_p == '#' || *buf_p == 0)
libselinux/src/load_policy.c +1 -1
index 17918e8b..57d7aaef 100644
@@ -252,7 +252,7 @@ int selinux_init_load_policy(int *enforce)
 		}
 		if (fgets(buf, selinux_page_size, cfg) &&
 		    (tmp = strstr(buf, "enforcing="))) {
-			if (tmp == buf || isspace(*(tmp - 1))) {
+			if (tmp == buf || isspace((unsigned char)*(tmp - 1))) {
 				secmdline =
 				    atoi(tmp + sizeof("enforcing=") - 1);
 			}
libselinux/src/matchmediacon.c +3 -3
index d3d95043..6ba87b99 100644
@@ -29,7 +29,7 @@ int matchmediacon(const char *media, char ** con)
 			current_line[strlen(current_line) - 1] = 0;
 		/* Skip leading whitespace before the partial context. */
 		ptr = current_line;
-		while (*ptr && isspace(*ptr))
+		while (*ptr && isspace((unsigned char)*ptr))
 			ptr++;
 
 		if (!(*ptr))
@@ -37,7 +37,7 @@ int matchmediacon(const char *media, char ** con)
 
 		/* Find the end of the media context. */
 		ptr2 = ptr;
-		while (*ptr2 && !isspace(*ptr2))
+		while (*ptr2 && !isspace((unsigned char)*ptr2))
 			ptr2++;
 		if (!(*ptr2))
 			continue;
@@ -53,7 +53,7 @@ int matchmediacon(const char *media, char ** con)
 		return -1;
 
 	/* Skip whitespace. */
-	while (*ptr2 && isspace(*ptr2))
+	while (*ptr2 && isspace((unsigned char)*ptr2))
 		ptr2++;
 	if (!(*ptr2)) {
 		return -1;
libselinux/src/selinux_check_securetty_context.c +2 -2
index c5c557fd..7609752e 100644
@@ -26,13 +26,13 @@ int selinux_check_securetty_context(const char * tty_context)
 
 				/* Skip leading whitespace. */
 				start = line;
-				while (*start && isspace(*start))
+				while (*start && isspace((unsigned char)*start))
 					start++;
 				if (!(*start))
 					continue;
 
 				end = start;
-				while (*end && !isspace(*end))
+				while (*end && !isspace((unsigned char)*end))
 					end++;
 				if (*end)
 					*end++ = 0;
libselinux/src/selinux_config.c +6 -6
index d2e49ee1..1d8cfb71 100644
@@ -103,7 +103,7 @@ int selinux_getenforcemode(int *enforce)
 			if (strncmp(buf, SELINUXTAG, len))
 				continue;
 			tag = buf+len;
-			while (isspace(*tag))
+			while (isspace((unsigned char)*tag))
 				tag++;
 			if (!strncasecmp
 			    (tag, "enforcing", sizeof("enforcing") - 1)) {
@@ -173,7 +173,7 @@ static void init_selinux_config(void)
 			if (line_buf[len - 1] == '\n')
 				line_buf[len - 1] = 0;
 			buf_p = line_buf;
-			while (isspace(*buf_p))
+			while (isspace((unsigned char)*buf_p))
 				buf_p++;
 			if (*buf_p == '#' || *buf_p == 0)
 				continue;
@@ -181,7 +181,7 @@ static void init_selinux_config(void)
 			if (!strncasecmp(buf_p, SELINUXTYPETAG,
 					 sizeof(SELINUXTYPETAG) - 1)) {
 				buf_p += sizeof(SELINUXTYPETAG) - 1;
-				while (isspace(*buf_p))
+				while (isspace((unsigned char)*buf_p))
 					buf_p++;
 				type = strdup(buf_p);
 				if (!type) {
@@ -191,7 +191,7 @@ static void init_selinux_config(void)
 				}
 				end = type + strlen(type) - 1;
 				while ((end > type) &&
-				       (isspace(*end) || iscntrl(*end))) {
+				       (isspace((unsigned char)*end) || iscntrl((unsigned char)*end))) {
 					*end = 0;
 					end--;
 				}
@@ -206,14 +206,14 @@ static void init_selinux_config(void)
 			} else if (!strncmp(buf_p, REQUIRESEUSERS,
 					    sizeof(REQUIRESEUSERS) - 1)) {
 				value = buf_p + sizeof(REQUIRESEUSERS) - 1;
-				while (isspace(*value))
+				while (isspace((unsigned char)*value))
 					value++;
 				intptr = &require_seusers;
 			} else {
 				continue;
 			}
 
-			if (isdigit(*value))
+			if (isdigit((unsigned char)*value))
 				*intptr = atoi(value);
 			else if (strncasecmp(value, "true", sizeof("true") - 1))
 				*intptr = 1;
libselinux/src/seusers.c +3 -3
index fff80c1a..6da8c318 100644
@@ -25,7 +25,7 @@ static int process_seusers(const char *buffer,
 		goto err;
 
 	start = newbuf;
-	while (isspace(*start))
+	while (isspace((unsigned char)*start))
 		start++;
 	if (*start == '#' || *start == 0) {
 		free(newbuf);
@@ -46,7 +46,7 @@ static int process_seusers(const char *buffer,
 		mls_found = 0;
 
 		end = start;
-		while (*end && !isspace(*end))
+		while (*end && !isspace((unsigned char)*end))
 			end++;
 	}
 	*end = 0;
@@ -63,7 +63,7 @@ static int process_seusers(const char *buffer,
 		goto out;
 
 	start = ++end;
-	while (*end && !isspace(*end))
+	while (*end && !isspace((unsigned char)*end))
 		end++;
 	*end = 0;
 
Signature · 1 line
2.42.0
#1 lore raw

PATCH v3 2/2 libselinux: introduce reallocarray(3)

Introduce reallocarray(3), a realloc(3) wrapper incorporating a multiplication overflow check.

Add private implementation in case the function is not provided by the standard C library.

Use in appropriate locations.

---
v3:
- use SIZE_MAX and NULL instead of -1 and 0
- fix indentation
- drop RFC and split from original patch set

---
 libselinux/src/Makefile             |  6 ++++++
 libselinux/src/get_context_list.c   |  2 +-
 libselinux/src/matchpathcon.c       |  4 ++--
 libselinux/src/selinux_internal.c   | 16 ++++++++++++++++
 libselinux/src/selinux_internal.h   |  4 ++++
 libselinux/src/selinux_restorecon.c |  3 +--
 6 files changed, 30 insertions(+), 5 deletions(-)
libselinux/src/Makefile +6 -0
index cf830046..7aadb822 100644
@@ -108,6 +108,12 @@ ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlc
 override CFLAGS += -DHAVE_STRLCPY
 endif
 
+# check for reallocarray(3) availability
+H := \#
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){reallocarray(NULL, 0, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+override CFLAGS += -DHAVE_REALLOCARRAY
+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/get_context_list.c +1 -1
index 0342823c..9dafa519 100644
@@ -272,7 +272,7 @@ static int get_context_user(FILE * fp,
 			continue;
 		}
 		if (security_check_context(usercon_str2) == 0) {
-			new_reachable = realloc(*reachable, (*nreachable + 2) * sizeof(char *));
+			new_reachable = reallocarray(*reachable, *nreachable + 2, sizeof(char *));
 			if (!new_reachable) {
 				context_free(usercon);
 				rc = -1;
libselinux/src/matchpathcon.c +2 -2
index b98849d9..e44734c3 100644
@@ -96,8 +96,8 @@ static int add_array_elt(char *con)
 	if (con_array_size) {
 		while (con_array_used >= con_array_size) {
 			con_array_size *= 2;
-			tmp = (char **)realloc(con_array, sizeof(char*) *
-						     con_array_size);
+			tmp = (char **)reallocarray(con_array, con_array_size,
+						    sizeof(char*));
 			if (!tmp) {
 				free_array_elts();
 				return -1;
libselinux/src/selinux_internal.c +16 -0
index c2be7c0a..678bd99c 100644
@@ -1,5 +1,7 @@
 #include "selinux_internal.h"
 
+#include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
 
@@ -16,3 +18,17 @@ size_t strlcpy(char *dest, const char *src, size_t size)
 	return ret;
 }
 #endif /* HAVE_STRLCPY */
+
+#ifndef HAVE_REALLOCARRAY
+void *reallocarray(void *ptr, size_t nmemb, size_t size)
+{
+	
+	if (size && nmemb > SIZE_MAX / size) {
+		errno = ENOMEM;
+		return NULL;
+	}
+
+	return realloc(ptr, nmemb * size);
+
+}
+#endif /* HAVE_REALLOCARRAY */
libselinux/src/selinux_internal.h +4 -0
index 06f2c038..af69ff04 100644
@@ -98,4 +98,8 @@ extern int has_selinux_config ;
 size_t strlcpy(char *dest, const char *src, size_t size);
 #endif
 
+#ifndef HAVE_REALLOCARRAY
+void *reallocarray(void *ptr, size_t nmemb, size_t size);
+#endif
+
 #endif /* SELINUX_INTERNAL_H_ */
libselinux/src/selinux_restorecon.c +1 -2
index 7ef2d45d..38f10f1c 100644
@@ -175,8 +175,7 @@ static int add_exclude(const char *directory, bool who)
 		return -1;
 	}
 
-	tmp_list = realloc(exclude_lst,
-			   sizeof(struct edir) * (exclude_count + 1));
+	tmp_list = reallocarray(exclude_lst, exclude_count + 1, sizeof(struct edir));
 	if (!tmp_list)
 		goto oom;
 
Signature · 1 line
2.42.0
Acked-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] libselinux: introduce reallocarray(3)

Quoted 10 lines · Christian Göttsche

Introduce reallocarray(3), a realloc(3) wrapper incorporating a multiplication overflow check.

Add private implementation in case the function is not provided by the standard C library.

Use in appropriate locations.

Quoted 119 lines

---
v3:
- use SIZE_MAX and NULL instead of -1 and 0
- fix indentation
- drop RFC and split from original patch set

---
 libselinux/src/Makefile             |  6 ++++++
 libselinux/src/get_context_list.c   |  2 +-
 libselinux/src/matchpathcon.c       |  4 ++--
 libselinux/src/selinux_internal.c   | 16 ++++++++++++++++
 libselinux/src/selinux_internal.h   |  4 ++++
 libselinux/src/selinux_restorecon.c |  3 +--
 6 files changed, 30 insertions(+), 5 deletions(-)
libselinux/src/Makefile +6 -0
index cf830046..7aadb822 100644
@@ -108,6 +108,12 @@ ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlc
 override CFLAGS += -DHAVE_STRLCPY
 endif
 
+# check for reallocarray(3) availability
+H := \#
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){reallocarray(NULL, 0, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+override CFLAGS += -DHAVE_REALLOCARRAY
+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/get_context_list.c +1 -1
index 0342823c..9dafa519 100644
@@ -272,7 +272,7 @@ static int get_context_user(FILE * fp,
                        continue;
                }
                if (security_check_context(usercon_str2) == 0) {
-                       new_reachable = realloc(*reachable, (*nreachable + 2) * sizeof(char *));
+                       new_reachable = reallocarray(*reachable, *nreachable + 2, sizeof(char *));
                        if (!new_reachable) {
                                context_free(usercon);
                                rc = -1;
libselinux/src/matchpathcon.c +2 -2
index b98849d9..e44734c3 100644
@@ -96,8 +96,8 @@ static int add_array_elt(char *con)
        if (con_array_size) {
                while (con_array_used >= con_array_size) {
                        con_array_size *= 2;
-                       tmp = (char **)realloc(con_array, sizeof(char*) *
-                                                    con_array_size);
+                       tmp = (char **)reallocarray(con_array, con_array_size,
+                                                   sizeof(char*));
                        if (!tmp) {
                                free_array_elts();
                                return -1;
libselinux/src/selinux_internal.c +16 -0
index c2be7c0a..678bd99c 100644
@@ -1,5 +1,7 @@
 #include "selinux_internal.h"
 
+#include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
 
@@ -16,3 +18,17 @@ size_t strlcpy(char *dest, const char *src, size_t size)
        return ret;
 }
 #endif /* HAVE_STRLCPY */
+
+#ifndef HAVE_REALLOCARRAY
+void *reallocarray(void *ptr, size_t nmemb, size_t size)
+{
+
+       if (size && nmemb > SIZE_MAX / size) {
+               errno = ENOMEM;
+               return NULL;
+       }
+
+       return realloc(ptr, nmemb * size);
+
+}
+#endif /* HAVE_REALLOCARRAY */
libselinux/src/selinux_internal.h +4 -0
index 06f2c038..af69ff04 100644
@@ -98,4 +98,8 @@ extern int has_selinux_config ;
 size_t strlcpy(char *dest, const char *src, size_t size);
 #endif
 
+#ifndef HAVE_REALLOCARRAY
+void *reallocarray(void *ptr, size_t nmemb, size_t size);
+#endif
+
 #endif /* SELINUX_INTERNAL_H_ */
libselinux/src/selinux_restorecon.c +1 -3
index 7ef2d45d..38f10f1c 100644
@@ -175,8 +175,7 @@ static int add_exclude(const char *directory, bool who)
                return -1;
        }
 
-       tmp_list = realloc(exclude_lst,
-                          sizeof(struct edir) * (exclude_count + 1));
+       tmp_list = reallocarray(exclude_lst, exclude_count + 1, sizeof(struct edir));
        if (!tmp_list)
                goto oom;
 
--

2.42.0

Acked-by on v3 1/2
#3 lore raw
In reply to Christian Göttsche
Quoted 8 lines · Christian Göttsche

Character handling functions, like isspace(3), expect a value representable as unsigned char or equal to EOF. Otherwise the behavior is undefined.

See https://wiki.sei.cmu.edu/confluence/display/c/STR37-C.+Arguments+to+character-handling+functions+must+be+representable+as+an+unsigned+char

Quoted 376 lines

---
v3:
- compute_create.c: avoid cast by changing type of variable
- drop RFC and split from original patch set

---
 libselinux/src/booleans.c                        |  8 ++++----
 libselinux/src/compute_create.c                  |  2 +-
 libselinux/src/get_context_list.c                | 12 ++++++------
 libselinux/src/get_default_type.c                |  2 +-
 libselinux/src/label_file.c                      |  8 ++++----
 libselinux/src/label_media.c                     |  2 +-
 libselinux/src/label_support.c                   |  8 ++++----
 libselinux/src/label_x.c                         |  2 +-
 libselinux/src/load_policy.c                     |  2 +-
 libselinux/src/matchmediacon.c                   |  6 +++---
 libselinux/src/selinux_check_securetty_context.c |  4 ++--
 libselinux/src/selinux_config.c                  | 12 ++++++------
 libselinux/src/seusers.c                         |  6 +++---
 13 files changed, 37 insertions(+), 37 deletions(-)
libselinux/src/booleans.c +4 -4
index e34b39ff..c557df65 100644
@@ -107,7 +107,7 @@ char *selinux_boolean_sub(const char *name)
                char *ptr;
                char *src = line_buf;
                char *dst;
-               while (*src && isspace(*src))
+               while (*src && isspace((unsigned char)*src))
                        src++;
                if (!*src)
                        continue;
@@ -115,19 +115,19 @@ char *selinux_boolean_sub(const char *name)
                        continue;
 
                ptr = src;
-               while (*ptr && !isspace(*ptr))
+               while (*ptr && !isspace((unsigned char)*ptr))
                        ptr++;
                *ptr++ = '\0';
                if (strcmp(src, name) != 0)
                        continue;
 
                dst = ptr;
-               while (*dst && isspace(*dst))
+               while (*dst && isspace((unsigned char)*dst))
                        dst++;
                if (!*dst)
                        continue;
                ptr = dst;
-               while (*ptr && !isspace(*ptr))
+               while (*ptr && !isspace((unsigned char)*ptr))
                        ptr++;
                *ptr = '\0';
 
libselinux/src/compute_create.c +1 -1
index e9f3c96a..d19baa0b 100644
@@ -13,7 +13,7 @@
 
 static int object_name_encode(const char *objname, char *buffer, size_t buflen)
 {
-       int     code;
+       unsigned char code;
        size_t  offset = 0;
 
        if (buflen - offset < 1)
libselinux/src/get_context_list.c +6 -6
index d774b9cf..0342823c 100644
@@ -170,27 +170,27 @@ static int get_context_user(FILE * fp,
 
                /* Skip leading whitespace. */
                start = line;
-               while (*start && isspace(*start))
+               while (*start && isspace((unsigned char)*start))
                        start++;
                if (!(*start))
                        continue;
 
                /* Find the end of the (partial) fromcon in the line. */
                end = start;
-               while (*end && !isspace(*end))
+               while (*end && !isspace((unsigned char)*end))
                        end++;
                if (!(*end))
                        continue;
 
                /* Check for a match. */
                linerole = start;
-               while (*start && !isspace(*start) && *start != ':')
+               while (*start && !isspace((unsigned char)*start) && *start != ':')
                        start++;
                if (*start != ':')
                        continue;
                *start = 0;
                linetype = ++start;
-               while (*start && !isspace(*start) && *start != ':')
+               while (*start && !isspace((unsigned char)*start) && *start != ':')
                        start++;
                if (!(*start))
                        continue;
@@ -210,14 +210,14 @@ static int get_context_user(FILE * fp,
        start = ++end;
        while (*start) {
                /* Skip leading whitespace */
-               while (*start && isspace(*start))
+               while (*start && isspace((unsigned char)*start))
                        start++;
                if (!(*start))
                        break;
 
                /* Find the end of this partial context. */
                end = start;
-               while (*end && !isspace(*end))
+               while (*end && !isspace((unsigned char)*end))
                        end++;
                if (*end)
                        *end++ = 0;
libselinux/src/get_default_type.c +1 -1
index 766ea4b7..87e9c72c 100644
@@ -42,7 +42,7 @@ static int find_default_type(FILE * fp, const char *role, char **type)
                        buf[strlen(buf) - 1] = 0;
 
                ptr = buf;
-               while (*ptr && isspace(*ptr))
+               while (*ptr && isspace((unsigned char)*ptr))
                        ptr++;
                if (!(*ptr))
                        continue;
libselinux/src/label_file.c +4 -4
index a665c9b4..4778f8f8 100644
@@ -708,20 +708,20 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
                char *src = buf;
                char *dst = NULL;
 
-               while (*src && isspace(*src))
+               while (*src && isspace((unsigned char)*src))
                        src++;
                if (src[0] == '#') continue;
                ptr = src;
-               while (*ptr && ! isspace(*ptr))
+               while (*ptr && ! isspace((unsigned char)*ptr))
                        ptr++;
                *ptr++ = '\0';
                if (! *src) continue;
 
                dst = ptr;
-               while (*dst && isspace(*dst))
+               while (*dst && isspace((unsigned char)*dst))
                        dst++;
                ptr = dst;
-               while (*ptr && ! isspace(*ptr))
+               while (*ptr && ! isspace((unsigned char)*ptr))
                        ptr++;
                *ptr = '\0';
                if (! *dst)
libselinux/src/label_media.c +1 -1
index d33b614a..b3443b47 100644
@@ -39,7 +39,7 @@ static int process_line(const char *path, char *line_buf, int pass,
        char *key, *context;
 
        buf_p = line_buf;
-       while (isspace(*buf_p))
+       while (isspace((unsigned char)*buf_p))
                buf_p++;
        /* Skip comment lines and empty lines. */
        if (*buf_p == '#' || *buf_p == 0)
libselinux/src/label_support.c +4 -4
index 49f72b1c..f7ab9292 100644
@@ -26,14 +26,14 @@ static inline int read_spec_entry(char **entry, char **ptr, int *len, const char
        *entry = NULL;
        char *tmp_buf = NULL;
 
-       while (isspace(**ptr) && **ptr != '\0')
+       while (isspace((unsigned char)**ptr) && **ptr != '\0')
                (*ptr)++;
 
        tmp_buf = *ptr;
        *len = 0;
 
-       while (!isspace(**ptr) && **ptr != '\0') {
-               if (!isascii(**ptr)) {
+       while (!isspace((unsigned char)**ptr) && **ptr != '\0') {
+               if (!isascii((unsigned char)**ptr)) {
                        errno = EINVAL;
                        *errbuf = "Non-ASCII characters found";
                        return -1;
@@ -80,7 +80,7 @@ int  read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)
                len++;
 
        buf_p = line_buf;
-       while (isspace(*buf_p))
+       while (isspace((unsigned char)*buf_p))
                buf_p++;
 
        /* Skip comment lines and empty lines. */
libselinux/src/label_x.c +1 -1
index 4b8f524f..e15190ca 100644
@@ -41,7 +41,7 @@ static int process_line(const char *path, char *line_buf, int pass,
        char *type, *key, *context;
 
        buf_p = line_buf;
-       while (isspace(*buf_p))
+       while (isspace((unsigned char)*buf_p))
                buf_p++;
        /* Skip comment lines and empty lines. */
        if (*buf_p == '#' || *buf_p == 0)
libselinux/src/load_policy.c +1 -1
index 17918e8b..57d7aaef 100644
@@ -252,7 +252,7 @@ int selinux_init_load_policy(int *enforce)
                }
                if (fgets(buf, selinux_page_size, cfg) &&
                    (tmp = strstr(buf, "enforcing="))) {
-                       if (tmp == buf || isspace(*(tmp - 1))) {
+                       if (tmp == buf || isspace((unsigned char)*(tmp - 1))) {
                                secmdline =
                                    atoi(tmp + sizeof("enforcing=") - 1);
                        }
libselinux/src/matchmediacon.c +3 -3
index d3d95043..6ba87b99 100644
@@ -29,7 +29,7 @@ int matchmediacon(const char *media, char ** con)
                        current_line[strlen(current_line) - 1] = 0;
                /* Skip leading whitespace before the partial context. */
                ptr = current_line;
-               while (*ptr && isspace(*ptr))
+               while (*ptr && isspace((unsigned char)*ptr))
                        ptr++;
 
                if (!(*ptr))
@@ -37,7 +37,7 @@ int matchmediacon(const char *media, char ** con)
 
                /* Find the end of the media context. */
                ptr2 = ptr;
-               while (*ptr2 && !isspace(*ptr2))
+               while (*ptr2 && !isspace((unsigned char)*ptr2))
                        ptr2++;
                if (!(*ptr2))
                        continue;
@@ -53,7 +53,7 @@ int matchmediacon(const char *media, char ** con)
                return -1;
 
        /* Skip whitespace. */
-       while (*ptr2 && isspace(*ptr2))
+       while (*ptr2 && isspace((unsigned char)*ptr2))
                ptr2++;
        if (!(*ptr2)) {
                return -1;
libselinux/src/selinux_check_securetty_context.c +2 -2
index c5c557fd..7609752e 100644
@@ -26,13 +26,13 @@ int selinux_check_securetty_context(const char * tty_context)
 
                                /* Skip leading whitespace. */
                                start = line;
-                               while (*start && isspace(*start))
+                               while (*start && isspace((unsigned char)*start))
                                        start++;
                                if (!(*start))
                                        continue;
 
                                end = start;
-                               while (*end && !isspace(*end))
+                               while (*end && !isspace((unsigned char)*end))
                                        end++;
                                if (*end)
                                        *end++ = 0;
libselinux/src/selinux_config.c +6 -6
index d2e49ee1..1d8cfb71 100644
@@ -103,7 +103,7 @@ int selinux_getenforcemode(int *enforce)
                        if (strncmp(buf, SELINUXTAG, len))
                                continue;
                        tag = buf+len;
-                       while (isspace(*tag))
+                       while (isspace((unsigned char)*tag))
                                tag++;
                        if (!strncasecmp
                            (tag, "enforcing", sizeof("enforcing") - 1)) {
@@ -173,7 +173,7 @@ static void init_selinux_config(void)
                        if (line_buf[len - 1] == '\n')
                                line_buf[len - 1] = 0;
                        buf_p = line_buf;
-                       while (isspace(*buf_p))
+                       while (isspace((unsigned char)*buf_p))
                                buf_p++;
                        if (*buf_p == '#' || *buf_p == 0)
                                continue;
@@ -181,7 +181,7 @@ static void init_selinux_config(void)
                        if (!strncasecmp(buf_p, SELINUXTYPETAG,
                                         sizeof(SELINUXTYPETAG) - 1)) {
                                buf_p += sizeof(SELINUXTYPETAG) - 1;
-                               while (isspace(*buf_p))
+                               while (isspace((unsigned char)*buf_p))
                                        buf_p++;
                                type = strdup(buf_p);
                                if (!type) {
@@ -191,7 +191,7 @@ static void init_selinux_config(void)
                                }
                                end = type + strlen(type) - 1;
                                while ((end > type) &&
-                                      (isspace(*end) || iscntrl(*end))) {
+                                      (isspace((unsigned char)*end) || iscntrl((unsigned char)*end))) {
                                        *end = 0;
                                        end--;
                                }
@@ -206,14 +206,14 @@ static void init_selinux_config(void)
                        } else if (!strncmp(buf_p, REQUIRESEUSERS,
                                            sizeof(REQUIRESEUSERS) - 1)) {
                                value = buf_p + sizeof(REQUIRESEUSERS) - 1;
-                               while (isspace(*value))
+                               while (isspace((unsigned char)*value))
                                        value++;
                                intptr = &require_seusers;
                        } else {
                                continue;
                        }
 
-                       if (isdigit(*value))
+                       if (isdigit((unsigned char)*value))
                                *intptr = atoi(value);
                        else if (strncasecmp(value, "true", sizeof("true") - 1))
                                *intptr = 1;
libselinux/src/seusers.c +3 -4
index fff80c1a..6da8c318 100644
@@ -25,7 +25,7 @@ static int process_seusers(const char *buffer,
                goto err;
 
        start = newbuf;
-       while (isspace(*start))
+       while (isspace((unsigned char)*start))
                start++;
        if (*start == '#' || *start == 0) {
                free(newbuf);
@@ -46,7 +46,7 @@ static int process_seusers(const char *buffer,
                mls_found = 0;
 
                end = start;
-               while (*end && !isspace(*end))
+               while (*end && !isspace((unsigned char)*end))
                        end++;
        }
        *end = 0;
@@ -63,7 +63,7 @@ static int process_seusers(const char *buffer,
                goto out;
 
        start = ++end;
-       while (*end && !isspace(*end))
+       while (*end && !isspace((unsigned char)*end))
                end++;
        *end = 0;
 
--

2.42.0

#4 lore raw
Quoted 8 lines · James Carter
Quoted 5 lines · Christian Göttsche

Character handling functions, like isspace(3), expect a value representable as unsigned char or equal to EOF. Otherwise the behavior is undefined.

Quoted 6 lines
Quoted 3 lines

See https://wiki.sei.cmu.edu/confluence/display/c/STR37-C.+Arguments+to+character-handling+functions+must+be+representable+as+an+unsigned+char

These two patches have been merged.
Thanks,
Jim

Quoted 376 lines
Quoted 376 lines

---
v3:
- compute_create.c: avoid cast by changing type of variable
- drop RFC and split from original patch set

---
 libselinux/src/booleans.c                        |  8 ++++----
 libselinux/src/compute_create.c                  |  2 +-
 libselinux/src/get_context_list.c                | 12 ++++++------
 libselinux/src/get_default_type.c                |  2 +-
 libselinux/src/label_file.c                      |  8 ++++----
 libselinux/src/label_media.c                     |  2 +-
 libselinux/src/label_support.c                   |  8 ++++----
 libselinux/src/label_x.c                         |  2 +-
 libselinux/src/load_policy.c                     |  2 +-
 libselinux/src/matchmediacon.c                   |  6 +++---
 libselinux/src/selinux_check_securetty_context.c |  4 ++--
 libselinux/src/selinux_config.c                  | 12 ++++++------
 libselinux/src/seusers.c                         |  6 +++---
 13 files changed, 37 insertions(+), 37 deletions(-)
libselinux/src/booleans.c +4 -4
index e34b39ff..c557df65 100644
@@ -107,7 +107,7 @@ char *selinux_boolean_sub(const char *name)
                char *ptr;
                char *src = line_buf;
                char *dst;
-               while (*src && isspace(*src))
+               while (*src && isspace((unsigned char)*src))
                        src++;
                if (!*src)
                        continue;
@@ -115,19 +115,19 @@ char *selinux_boolean_sub(const char *name)
                        continue;
 
                ptr = src;
-               while (*ptr && !isspace(*ptr))
+               while (*ptr && !isspace((unsigned char)*ptr))
                        ptr++;
                *ptr++ = '\0';
                if (strcmp(src, name) != 0)
                        continue;
 
                dst = ptr;
-               while (*dst && isspace(*dst))
+               while (*dst && isspace((unsigned char)*dst))
                        dst++;
                if (!*dst)
                        continue;
                ptr = dst;
-               while (*ptr && !isspace(*ptr))
+               while (*ptr && !isspace((unsigned char)*ptr))
                        ptr++;
                *ptr = '\0';
 
libselinux/src/compute_create.c +1 -1
index e9f3c96a..d19baa0b 100644
@@ -13,7 +13,7 @@
 
 static int object_name_encode(const char *objname, char *buffer, size_t buflen)
 {
-       int     code;
+       unsigned char code;
        size_t  offset = 0;
 
        if (buflen - offset < 1)
libselinux/src/get_context_list.c +6 -6
index d774b9cf..0342823c 100644
@@ -170,27 +170,27 @@ static int get_context_user(FILE * fp,
 
                /* Skip leading whitespace. */
                start = line;
-               while (*start && isspace(*start))
+               while (*start && isspace((unsigned char)*start))
                        start++;
                if (!(*start))
                        continue;
 
                /* Find the end of the (partial) fromcon in the line. */
                end = start;
-               while (*end && !isspace(*end))
+               while (*end && !isspace((unsigned char)*end))
                        end++;
                if (!(*end))
                        continue;
 
                /* Check for a match. */
                linerole = start;
-               while (*start && !isspace(*start) && *start != ':')
+               while (*start && !isspace((unsigned char)*start) && *start != ':')
                        start++;
                if (*start != ':')
                        continue;
                *start = 0;
                linetype = ++start;
-               while (*start && !isspace(*start) && *start != ':')
+               while (*start && !isspace((unsigned char)*start) && *start != ':')
                        start++;
                if (!(*start))
                        continue;
@@ -210,14 +210,14 @@ static int get_context_user(FILE * fp,
        start = ++end;
        while (*start) {
                /* Skip leading whitespace */
-               while (*start && isspace(*start))
+               while (*start && isspace((unsigned char)*start))
                        start++;
                if (!(*start))
                        break;
 
                /* Find the end of this partial context. */
                end = start;
-               while (*end && !isspace(*end))
+               while (*end && !isspace((unsigned char)*end))
                        end++;
                if (*end)
                        *end++ = 0;
libselinux/src/get_default_type.c +1 -1
index 766ea4b7..87e9c72c 100644
@@ -42,7 +42,7 @@ static int find_default_type(FILE * fp, const char *role, char **type)
                        buf[strlen(buf) - 1] = 0;
 
                ptr = buf;
-               while (*ptr && isspace(*ptr))
+               while (*ptr && isspace((unsigned char)*ptr))
                        ptr++;
                if (!(*ptr))
                        continue;
libselinux/src/label_file.c +4 -4
index a665c9b4..4778f8f8 100644
@@ -708,20 +708,20 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
                char *src = buf;
                char *dst = NULL;
 
-               while (*src && isspace(*src))
+               while (*src && isspace((unsigned char)*src))
                        src++;
                if (src[0] == '#') continue;
                ptr = src;
-               while (*ptr && ! isspace(*ptr))
+               while (*ptr && ! isspace((unsigned char)*ptr))
                        ptr++;
                *ptr++ = '\0';
                if (! *src) continue;
 
                dst = ptr;
-               while (*dst && isspace(*dst))
+               while (*dst && isspace((unsigned char)*dst))
                        dst++;
                ptr = dst;
-               while (*ptr && ! isspace(*ptr))
+               while (*ptr && ! isspace((unsigned char)*ptr))
                        ptr++;
                *ptr = '\0';
                if (! *dst)
libselinux/src/label_media.c +1 -1
index d33b614a..b3443b47 100644
@@ -39,7 +39,7 @@ static int process_line(const char *path, char *line_buf, int pass,
        char *key, *context;
 
        buf_p = line_buf;
-       while (isspace(*buf_p))
+       while (isspace((unsigned char)*buf_p))
                buf_p++;
        /* Skip comment lines and empty lines. */
        if (*buf_p == '#' || *buf_p == 0)
libselinux/src/label_support.c +4 -4
index 49f72b1c..f7ab9292 100644
@@ -26,14 +26,14 @@ static inline int read_spec_entry(char **entry, char **ptr, int *len, const char
        *entry = NULL;
        char *tmp_buf = NULL;
 
-       while (isspace(**ptr) && **ptr != '\0')
+       while (isspace((unsigned char)**ptr) && **ptr != '\0')
                (*ptr)++;
 
        tmp_buf = *ptr;
        *len = 0;
 
-       while (!isspace(**ptr) && **ptr != '\0') {
-               if (!isascii(**ptr)) {
+       while (!isspace((unsigned char)**ptr) && **ptr != '\0') {
+               if (!isascii((unsigned char)**ptr)) {
                        errno = EINVAL;
                        *errbuf = "Non-ASCII characters found";
                        return -1;
@@ -80,7 +80,7 @@ int  read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)
                len++;
 
        buf_p = line_buf;
-       while (isspace(*buf_p))
+       while (isspace((unsigned char)*buf_p))
                buf_p++;
 
        /* Skip comment lines and empty lines. */
libselinux/src/label_x.c +1 -1
index 4b8f524f..e15190ca 100644
@@ -41,7 +41,7 @@ static int process_line(const char *path, char *line_buf, int pass,
        char *type, *key, *context;
 
        buf_p = line_buf;
-       while (isspace(*buf_p))
+       while (isspace((unsigned char)*buf_p))
                buf_p++;
        /* Skip comment lines and empty lines. */
        if (*buf_p == '#' || *buf_p == 0)
libselinux/src/load_policy.c +1 -1
index 17918e8b..57d7aaef 100644
@@ -252,7 +252,7 @@ int selinux_init_load_policy(int *enforce)
                }
                if (fgets(buf, selinux_page_size, cfg) &&
                    (tmp = strstr(buf, "enforcing="))) {
-                       if (tmp == buf || isspace(*(tmp - 1))) {
+                       if (tmp == buf || isspace((unsigned char)*(tmp - 1))) {
                                secmdline =
                                    atoi(tmp + sizeof("enforcing=") - 1);
                        }
libselinux/src/matchmediacon.c +3 -3
index d3d95043..6ba87b99 100644
@@ -29,7 +29,7 @@ int matchmediacon(const char *media, char ** con)
                        current_line[strlen(current_line) - 1] = 0;
                /* Skip leading whitespace before the partial context. */
                ptr = current_line;
-               while (*ptr && isspace(*ptr))
+               while (*ptr && isspace((unsigned char)*ptr))
                        ptr++;
 
                if (!(*ptr))
@@ -37,7 +37,7 @@ int matchmediacon(const char *media, char ** con)
 
                /* Find the end of the media context. */
                ptr2 = ptr;
-               while (*ptr2 && !isspace(*ptr2))
+               while (*ptr2 && !isspace((unsigned char)*ptr2))
                        ptr2++;
                if (!(*ptr2))
                        continue;
@@ -53,7 +53,7 @@ int matchmediacon(const char *media, char ** con)
                return -1;
 
        /* Skip whitespace. */
-       while (*ptr2 && isspace(*ptr2))
+       while (*ptr2 && isspace((unsigned char)*ptr2))
                ptr2++;
        if (!(*ptr2)) {
                return -1;
libselinux/src/selinux_check_securetty_context.c +2 -2
index c5c557fd..7609752e 100644
@@ -26,13 +26,13 @@ int selinux_check_securetty_context(const char * tty_context)
 
                                /* Skip leading whitespace. */
                                start = line;
-                               while (*start && isspace(*start))
+                               while (*start && isspace((unsigned char)*start))
                                        start++;
                                if (!(*start))
                                        continue;
 
                                end = start;
-                               while (*end && !isspace(*end))
+                               while (*end && !isspace((unsigned char)*end))
                                        end++;
                                if (*end)
                                        *end++ = 0;
libselinux/src/selinux_config.c +6 -6
index d2e49ee1..1d8cfb71 100644
@@ -103,7 +103,7 @@ int selinux_getenforcemode(int *enforce)
                        if (strncmp(buf, SELINUXTAG, len))
                                continue;
                        tag = buf+len;
-                       while (isspace(*tag))
+                       while (isspace((unsigned char)*tag))
                                tag++;
                        if (!strncasecmp
                            (tag, "enforcing", sizeof("enforcing") - 1)) {
@@ -173,7 +173,7 @@ static void init_selinux_config(void)
                        if (line_buf[len - 1] == '\n')
                                line_buf[len - 1] = 0;
                        buf_p = line_buf;
-                       while (isspace(*buf_p))
+                       while (isspace((unsigned char)*buf_p))
                                buf_p++;
                        if (*buf_p == '#' || *buf_p == 0)
                                continue;
@@ -181,7 +181,7 @@ static void init_selinux_config(void)
                        if (!strncasecmp(buf_p, SELINUXTYPETAG,
                                         sizeof(SELINUXTYPETAG) - 1)) {
                                buf_p += sizeof(SELINUXTYPETAG) - 1;
-                               while (isspace(*buf_p))
+                               while (isspace((unsigned char)*buf_p))
                                        buf_p++;
                                type = strdup(buf_p);
                                if (!type) {
@@ -191,7 +191,7 @@ static void init_selinux_config(void)
                                }
                                end = type + strlen(type) - 1;
                                while ((end > type) &&
-                                      (isspace(*end) || iscntrl(*end))) {
+                                      (isspace((unsigned char)*end) || iscntrl((unsigned char)*end))) {
                                        *end = 0;
                                        end--;
                                }
@@ -206,14 +206,14 @@ static void init_selinux_config(void)
                        } else if (!strncmp(buf_p, REQUIRESEUSERS,
                                            sizeof(REQUIRESEUSERS) - 1)) {
                                value = buf_p + sizeof(REQUIRESEUSERS) - 1;
-                               while (isspace(*value))
+                               while (isspace((unsigned char)*value))
                                        value++;
                                intptr = &require_seusers;
                        } else {
                                continue;
                        }
 
-                       if (isdigit(*value))
+                       if (isdigit((unsigned char)*value))
                                *intptr = atoi(value);
                        else if (strncasecmp(value, "true", sizeof("true") - 1))
                                *intptr = 1;
libselinux/src/seusers.c +3 -4
index fff80c1a..6da8c318 100644
@@ -25,7 +25,7 @@ static int process_seusers(const char *buffer,
                goto err;
 
        start = newbuf;
-       while (isspace(*start))
+       while (isspace((unsigned char)*start))
                start++;
        if (*start == '#' || *start == 0) {
                free(newbuf);
@@ -46,7 +46,7 @@ static int process_seusers(const char *buffer,
                mls_found = 0;
 
                end = start;
-               while (*end && !isspace(*end))
+               while (*end && !isspace((unsigned char)*end))
                        end++;
        }
        *end = 0;
@@ -63,7 +63,7 @@ static int process_seusers(const char *buffer,
                goto out;
 
        start = ++end;
-       while (*end && !isspace(*end))
+       while (*end && !isspace((unsigned char)*end))
                end++;
        *end = 0;
 
--

2.42.0