Skip to content

Commit f9610d1

Browse files
libsepol: cast to unsigned char in ctype calls
Passing a plain char to the <ctype.h> functions is undefined when the value is negative (C11 7.4p1). On platforms where char is signed, a byte >= 0x80 from a module name, seusers/file_contexts entry, CIL identifier or IP token sign-extends to a negative int. Cast to unsigned char before each isspace/isalnum/isalpha/isdigit call, as libselinux already does. Signed-off-by: netliomax25-code <netliomax25@gmail.com>
1 parent 6548d84 commit f9610d1

5 files changed

Lines changed: 21 additions & 17 deletions

File tree

libsepol/cil/src/cil_build_ast.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4507,7 +4507,7 @@ int cil_gen_nodecon(struct cil_db *db, struct cil_tree_node *parse_current,
45074507
} else {
45084508
char *addr = parse_current->next->data;
45094509
if (strchr(addr, ':') ||
4510-
(strchr(addr, '.') && isdigit(addr[0]))) {
4510+
(strchr(addr, '.') && isdigit((unsigned char)addr[0]))) {
45114511
cil_ipaddr_init(&nodecon->addr);
45124512
rc = cil_fill_ipaddr(parse_current->next,
45134513
nodecon->addr);
@@ -4529,7 +4529,7 @@ int cil_gen_nodecon(struct cil_db *db, struct cil_tree_node *parse_current,
45294529
} else {
45304530
char *mask = parse_current->next->next->data;
45314531
if (strchr(mask, ':') ||
4532-
(strchr(mask, '.') && isdigit(mask[0]))) {
4532+
(strchr(mask, '.') && isdigit((unsigned char)mask[0]))) {
45334533
cil_ipaddr_init(&nodecon->mask);
45344534
rc = cil_fill_ipaddr(parse_current->next->next,
45354535
nodecon->mask);
@@ -5749,7 +5749,8 @@ int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr)
57495749
addr_str = addr_node->data;
57505750
if (strchr(addr_str, ':')) {
57515751
addr->family = AF_INET6;
5752-
} else if (strchr(addr_str, '.') && isdigit(addr_str[0])) {
5752+
} else if (strchr(addr_str, '.') &&
5753+
isdigit((unsigned char)addr_str[0])) {
57535754
addr->family = AF_INET;
57545755
} else {
57555756
goto exit;

libsepol/cil/src/cil_verify.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ int cil_verify_name(const struct cil_db *db, const char *name,
115115
goto exit;
116116
}
117117

118-
if (!isalpha(name[0])) {
118+
if (!isalpha((unsigned char)name[0])) {
119119
cil_log(CIL_ERR, "First character in %s is not a letter\n",
120120
name);
121121
goto exit;
122122
}
123123

124124
if (db->qualified_names == CIL_FALSE) {
125125
for (i = 1; i < len; i++) {
126-
if (!isalnum(name[i]) && name[i] != '_' &&
127-
name[i] != '-') {
126+
if (!isalnum((unsigned char)name[i]) &&
127+
name[i] != '_' && name[i] != '-') {
128128
cil_log(CIL_ERR,
129129
"Invalid character \"%c\" in %s\n",
130130
name[i], name);
@@ -133,8 +133,9 @@ int cil_verify_name(const struct cil_db *db, const char *name,
133133
}
134134
} else {
135135
for (i = 1; i < len; i++) {
136-
if (!isalnum(name[i]) && name[i] != '_' &&
137-
name[i] != '-' && name[i] != '.') {
136+
if (!isalnum((unsigned char)name[i]) &&
137+
name[i] != '_' && name[i] != '-' &&
138+
name[i] != '.') {
138139
cil_log(CIL_ERR,
139140
"Invalid character \"%c\" in %s\n",
140141
name[i], name);

libsepol/cil/src/cil_write_ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ static int __write_parse_ast_node_helper(struct cil_tree_node *node,
17081708
size_t i;
17091709

17101710
for (i = 0; i < len; i++) {
1711-
if (isspace(str[i])) {
1711+
if (isspace((unsigned char)str[i])) {
17121712
fprintf(args->out, "\"%s\"\n", str);
17131713
return SEPOL_OK;
17141714
}

libsepol/src/module_to_cil.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static int get_line(char **start, char *end, char **line)
118118

119119
*line = NULL;
120120

121-
for (p = *start; p < end && isspace(*p); p++)
121+
for (p = *start; p < end && isspace((unsigned char)*p); p++)
122122
;
123123

124124
*start = p;
@@ -3534,7 +3534,7 @@ static int seusers_to_cil(struct sepol_module_package *mod_pkg)
35343534

35353535
while ((rc = get_line(&cur, end, &line)) > 0) {
35363536
tmp = line;
3537-
while (isspace(*tmp)) {
3537+
while (isspace((unsigned char)*tmp)) {
35383538
tmp++;
35393539
}
35403540

@@ -3627,7 +3627,7 @@ static int user_extra_to_cil(struct sepol_module_package *mod_pkg)
36273627

36283628
while ((rc = get_line(&cur, end, &line)) > 0) {
36293629
tmp = line;
3630-
while (isspace(*tmp)) {
3630+
while (isspace((unsigned char)*tmp)) {
36313631
tmp++;
36323632
}
36333633

@@ -3699,7 +3699,7 @@ static int file_contexts_to_cil(struct sepol_module_package *mod_pkg)
36993699

37003700
while ((rc = get_line(&cur, end, &line)) > 0) {
37013701
tmp = line;
3702-
while (isspace(*tmp)) {
3702+
while (isspace((unsigned char)*tmp)) {
37033703
tmp++;
37043704
}
37053705

@@ -4411,7 +4411,7 @@ static int fix_module_name(struct policydb *pdb)
44114411
// CIL is more restrictive in module names than checkmodule. Convert bad
44124412
// characters to underscores
44134413
for (letter = pdb->name; *letter != '\0'; letter++) {
4414-
if (isalnum(*letter)) {
4414+
if (isalnum((unsigned char)*letter)) {
44154415
continue;
44164416
}
44174417

libsepol/src/util.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,11 @@ static inline int tokenize_str(char delim, char **str, const char **ptr,
248248
*str = NULL;
249249

250250
while (**ptr != '\0') {
251-
if (isspace(delim) && isspace(**ptr)) {
251+
if (isspace((unsigned char)delim) &&
252+
isspace((unsigned char)**ptr)) {
252253
(*ptr)++;
253254
break;
254-
} else if (!isspace(delim) && **ptr == delim) {
255+
} else if (!isspace((unsigned char)delim) && **ptr == delim) {
255256
(*ptr)++;
256257
break;
257258
}
@@ -273,7 +274,8 @@ static inline int tokenize_str(char delim, char **str, const char **ptr,
273274
}
274275

275276
/* Squash spaces if the delimiter is a whitespace character */
276-
while (**ptr != '\0' && isspace(delim) && isspace(**ptr)) {
277+
while (**ptr != '\0' && isspace((unsigned char)delim) &&
278+
isspace((unsigned char)**ptr)) {
277279
(*ptr)++;
278280
}
279281

0 commit comments

Comments
 (0)