Skip to contentSkip to conversation

libsepol/cil: declare file local function pointer static

PATCH In review Christian Göttsche 2 participants 8 messages Apr 8, 2022 – Apr 12, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 libsepol/cil: declare file local function pointer static

    ../cil/src/cil_log.c:45:8: warning: no previous extern declaration for non-static variable 'cil_log_handler' [-Wmissing-variable-declarations]
    void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
           ^
---
 libsepol/cil/src/cil_log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/cil/src/cil_log.c +1 -1
index e45c58fc..f4c6e415 100644
@@ -42,7 +42,7 @@ static void cil_default_log_handler(__attribute__((unused)) int lvl, const char
 	fprintf(stderr, "%s", msg);
 }
 
-void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
+static void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
 
 void cil_set_log_handler(void (*handler)(int lvl, const char *msg))
 {
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/5 libsepol: check correct pointer for oom

Check the actual pointer which memory was assigned to, not its parent array pointer.

    services.c:810:14: warning: Assigned value is garbage or undefined [core.uninitialized.Assign]
                                            **r_buf = **new_buf;
                                                    ^ ~~~~~~~~~
---
 libsepol/src/services.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/services.c +1 -1
index 29723729..b8fb2704 100644
@@ -803,7 +803,7 @@ mls_ops:
 				if (len < 0 || len >= reason_buf_len - reason_buf_used) {
 					new_buf_len = reason_buf_len + REASON_BUF_SIZE;
 					*new_buf = realloc(*r_buf, new_buf_len);
-					if (!new_buf) {
+					if (!*new_buf) {
 						ERR(NULL, "failed to realloc reason buffer");
 						goto out1;
 					}
Signature · 1 line
2.35.1
#2 lore raw

PATCH 3/5 libsepol: drop unnecessary const discarding casts

In reply to Christian Göttsche

hashtab_search() takes a const_hashtab_key_t, alias const char* as second key parameter type. Do not unnecessarily cast variables of type const char* to hashtab_key_t, alias char*.

policydb.c: In function ‘policydb_string_to_security_class’:
policydb.c:4164:39: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
4164 | (hashtab_key_t) class_name);
| ^
policydb.c: In function ‘policydb_string_to_av_perm’:
policydb.c:4184:25: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
4184 | (hashtab_key_t)perm_name);
| ^
policydb.c:4193:25: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
4193 | (hashtab_key_t)perm_name);
| ^

---
 libsepol/src/policydb.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
libsepol/src/policydb.c +3 -3
index 5c7e35e8..0c71f09f 100644
@@ -4160,7 +4160,7 @@ static sepol_security_class_t policydb_string_to_security_class(
 	class_datum_t *tclass_datum;
 
 	tclass_datum = hashtab_search(policydb->p_classes.table,
-				      (hashtab_key_t) class_name);
+				      class_name);
 	if (!tclass_datum)
 		return 0;
 	return tclass_datum->s.value;
@@ -4180,7 +4180,7 @@ static sepol_access_vector_t policydb_string_to_av_perm(
 
 	perm_datum = (perm_datum_t *)
 			hashtab_search(tclass_datum->permissions.table,
-			(hashtab_key_t)perm_name);
+			perm_name);
 	if (perm_datum != NULL)
 		return UINT32_C(1) << (perm_datum->s.value - 1);
 
@@ -4189,7 +4189,7 @@ static sepol_access_vector_t policydb_string_to_av_perm(
 
 	perm_datum = (perm_datum_t *)
 			hashtab_search(tclass_datum->comdatum->permissions.table,
-			(hashtab_key_t)perm_name);
+			perm_name);
 
 	if (perm_datum != NULL)
 		return UINT32_C(1) << (perm_datum->s.value - 1);
Signature · 1 line
2.35.1
#3 lore raw

PATCH 4/5 libselinux: limit has buffer size

In reply to Christian Göttsche

The struct selabel_digest member hashbuf_size is used to compute hashes via Sha1Update(), which takes uint32_t as length parameter type. Use that same type for hashbuf_size to avoid potential value truncations, as the overflow check in digest_add_specfile() on hashbuf_size is based on it.

label_support.c: In function ‘digest_gen_hash’:
label_support.c:125:53: warning: conversion from ‘size_t’ {aka ‘long unsigned int’} to ‘uint32_t’ {aka ‘unsigned int’} may change value [-Wconversion]
125 | Sha1Update(&context, digest->hashbuf, digest->hashbuf_size);
| ~~~~~~^~~~~~~~~~~~~~

---
 libselinux/src/label_internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/label_internal.h +1 -1
index 782c6aa8..82a762f7 100644
@@ -57,7 +57,7 @@ int selabel_service_init(struct selabel_handle *rec,
 struct selabel_digest {
 	unsigned char *digest;	/* SHA1 digest of specfiles */
 	unsigned char *hashbuf;	/* buffer to hold specfiles */
-	size_t hashbuf_size;	/* buffer size */
+	uint32_t hashbuf_size;	/* buffer size */
 	size_t specfile_cnt;	/* how many specfiles processed */
 	char **specfile_list;	/* and their names */
 };
Signature · 1 line
2.35.1
#4 lore raw

Re: [PATCH 4/5] libselinux: limit has buffer size

Quoted 11 lines · Christian Göttsche

The struct selabel_digest member hashbuf_size is used to compute hashes via Sha1Update(), which takes uint32_t as length parameter type. Use that same type for hashbuf_size to avoid potential value truncations, as the overflow check in digest_add_specfile() on hashbuf_size is based on it.

label_support.c: In function ‘digest_gen_hash’:
label_support.c:125:53: warning: conversion from ‘size_t’ {aka ‘long unsigned int’} to ‘uint32_t’ {aka ‘unsigned int’} may change value [-Wconversion]
125 | Sha1Update(&context, digest->hashbuf, digest->hashbuf_size);
| ~~~~~~^~~~~~~~~~~~~~

An alternative would be to split the Sha1Update() call[1] into multiple, each for a maximum of UINT32_MAX bytes.

[1]: https://github.com/SELinuxProject/selinux/blob/73562de8fc70b21aeb6be86dfdfddb7502d236ea/libselinux/src/label_support.c#L125

Quoted 21 lines
---
 libselinux/src/label_internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/label_internal.h +1 -2
index 782c6aa8..82a762f7 100644
@@ -57,7 +57,7 @@ int selabel_service_init(struct selabel_handle *rec,
 struct selabel_digest {
        unsigned char *digest;  /* SHA1 digest of specfiles */
        unsigned char *hashbuf; /* buffer to hold specfiles */
-       size_t hashbuf_size;    /* buffer size */
+       uint32_t hashbuf_size;  /* buffer size */
        size_t specfile_cnt;    /* how many specfiles processed */
        char **specfile_list;   /* and their names */
 };
--

2.35.1

#5 lore raw

PATCH 5/5 libsemanage: avoid double fclose

In reply to Christian Göttsche

The cleanup goto block in semanage_direct_set_enabled() closes the file stream pointer fp if not NULL. Set the stream to NULL after a manual fclose(3), even on failure.

    direct_api.c: In function ‘semanage_direct_set_enabled’:
    direct_api.c:2130:25: error: pointer ‘fp’ may be used after ‘fclose’ [-Werror=use-after-free]
     2130 |         if (fp != NULL) fclose(fp);
          |                         ^~~~~~~~~~
    direct_api.c:2092:29: note: call to ‘fclose’ here
     2092 |                         if (fclose(fp) != 0) {
          |                             ^~~~~~~~~~
---
 libsemanage/src/direct_api.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
libsemanage/src/direct_api.c +3 -3
index d5716ce5..7206483a 100644
@@ -2089,7 +2089,9 @@ static int semanage_direct_set_enabled(semanage_handle_t *sh,
 				goto cleanup;
 			}
 
-			if (fclose(fp) != 0) {
+			ret = fclose(fp);
+			fp = NULL;
+			if (ret != 0) {
 				ERR(sh,
 				    "Unable to close disabled file for module %s",
 				    modkey->name);
@@ -2097,8 +2099,6 @@ static int semanage_direct_set_enabled(semanage_handle_t *sh,
 				goto cleanup;
 			}
 
-			fp = NULL;
-
 			break;
 		case 1: /* enable the module */
 			if (unlink(fn) < 0) {
Signature · 1 line
2.35.1
Acked-by on v1 1/5
#6 lore raw
In reply to Christian Göttsche
Quoted 6 lines · Christian Göttsche
    ../cil/src/cil_log.c:45:8: warning: no previous extern declaration for non-static variable 'cil_log_handler' [-Wmissing-variable-declarations]
    void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
           ^

For these 5 patches:

Quoted 20 lines
---
 libsepol/cil/src/cil_log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/cil/src/cil_log.c +1 -2
index e45c58fc..f4c6e415 100644
@@ -42,7 +42,7 @@ static void cil_default_log_handler(__attribute__((unused)) int lvl, const char
        fprintf(stderr, "%s", msg);
 }
 
-void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
+static void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
 
 void cil_set_log_handler(void (*handler)(int lvl, const char *msg))
 {
--

2.35.1

#7 lore raw
Quoted 4 lines · James Carter
Quoted 1 line · Christian Göttsche
Quoted 9 lines
Quoted 5 lines
    ../cil/src/cil_log.c:45:8: warning: no previous extern declaration for non-static variable 'cil_log_handler' [-Wmissing-variable-declarations]
    void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
           ^

For these 5 patches:

These 5 patches have been merged.
Thanks,
Jim

Quoted 20 lines
Quoted 20 lines
---
 libsepol/cil/src/cil_log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/cil/src/cil_log.c +1 -2
index e45c58fc..f4c6e415 100644
@@ -42,7 +42,7 @@ static void cil_default_log_handler(__attribute__((unused)) int lvl, const char
        fprintf(stderr, "%s", msg);
 }
 
-void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
+static void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
 
 void cil_set_log_handler(void (*handler)(int lvl, const char *msg))
 {
--

2.35.1