Skip to contentSkip to conversation

libsepol: constify tokenized input

PATCH In review Christian Göttsche 2 participants 5 messages Dec 11, 2023 – Dec 12, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libsepol: constify tokenized input

The input string to be tokenized is not modified.

---
 libsepol/include/sepol/policydb/util.h | 2 +-
 libsepol/src/util.c                    | 9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
libsepol/include/sepol/policydb/util.h +1 -1
index db8da213..4ea5a346 100644
@@ -40,7 +40,7 @@ char *sepol_extended_perms_to_string(avtab_extended_perms_t *xperms);
  * The tokenize function may be used to
  * replace sscanf
  */
-extern int tokenize(char *line_buf, char delim, int num_args, ...);
+extern int tokenize(const char *line_buf, char delim, int num_args, ...);
 
 #ifdef __cplusplus
 }
libsepol/src/util.c +5 -4
index 2f877920..571f6c93 100644
@@ -221,9 +221,9 @@ err:
  */
 
 /* Read a token from a buffer */
-static inline int tokenize_str(char delim, char **str, char **ptr, size_t *len)
+static inline int tokenize_str(char delim, char **str, const char **ptr, size_t *len)
 {
-	char *tmp_buf = *ptr;
+	const char *tmp_buf = *ptr;
 	*str = NULL;
 
 	while (**ptr != '\0') {
@@ -273,9 +273,10 @@ static inline int tokenize_str(char delim, char **str, char **ptr, size_t *len)
  * contain the remaining content of line_buf. If the delimiter is any whitespace
  * character, then all whitespace will be squashed.
  */
-int tokenize(char *line_buf, char delim, int num_args, ...)
+int tokenize(const char *line_buf, char delim, int num_args, ...)
 {
-	char **arg, *buf_p;
+	char **arg;
+	const char *buf_p;
 	int rc, items;
 	size_t arg_len = 0;
 	va_list ap;
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/3 libsepol: avoid integer overflow in add_i_to_a()

---
 libsepol/src/util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/util.c +1 -1
index 571f6c93..4a6f7d11 100644
@@ -44,7 +44,7 @@ int add_i_to_a(uint32_t i, uint32_t * cnt, uint32_t ** a)
 {
 	uint32_t *new;
 
-	if (cnt == NULL || a == NULL)
+	if (cnt == NULL || *cnt == UINT32_MAX || a == NULL)
 		return -1;
 
 	/* FIX ME: This is not very elegant! We use an array that we
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/3 libsepol: extended permission formatting cleanup

In reply to Christian Göttsche

Declare the read-only permission parameter const.
Use a more readable overflow check, which is also resilient against changes of the growth factor or initial size.

---
 libsepol/include/sepol/policydb/util.h | 2 +-
 libsepol/src/kernel_to_cil.c           | 5 ++---
 libsepol/src/util.c                    | 5 ++---
 3 files changed, 5 insertions(+), 7 deletions(-)
libsepol/include/sepol/policydb/util.h +1 -1
index 4ea5a346..abaff698 100644
@@ -34,7 +34,7 @@ extern int add_i_to_a(uint32_t i, uint32_t * cnt, uint32_t ** a);
 extern char *sepol_av_to_string(policydb_t * policydbp, uint32_t tclass,
 				sepol_access_vector_t av);
 
-char *sepol_extended_perms_to_string(avtab_extended_perms_t *xperms);
+char *sepol_extended_perms_to_string(const avtab_extended_perms_t *xperms);
 
 /*
  * The tokenize function may be used to
libsepol/src/kernel_to_cil.c +2 -3
index bcb58eee..119b657b 100644
@@ -1627,7 +1627,7 @@ exit:
 
 #define next_bit_in_range(i, p) (((i) + 1 < sizeof(p)*8) && xperm_test(((i) + 1), p))
 
-static char *xperms_to_str(avtab_extended_perms_t *xperms)
+static char *xperms_to_str(const avtab_extended_perms_t *xperms)
 {
 	uint16_t value;
 	uint16_t low_bit;
@@ -1644,8 +1644,7 @@ static char *xperms_to_str(avtab_extended_perms_t *xperms)
 	}
 
 retry:
-	size *= 2;
-	if (size == 0)
+	if (__builtin_mul_overflow(size, 2, &size))
 		goto err;
 	p = realloc(buffer, size);
 	if (!p)
libsepol/src/util.c +2 -3
index 4a6f7d11..687336c8 100644
@@ -126,7 +126,7 @@ char *sepol_av_to_string(policydb_t * policydbp, uint32_t tclass,
 
 #define next_bit_in_range(i, p) (((i) + 1 < sizeof(p)*8) && xperm_test(((i) + 1), p))
 
-char *sepol_extended_perms_to_string(avtab_extended_perms_t *xperms)
+char *sepol_extended_perms_to_string(const avtab_extended_perms_t *xperms)
 {
 	uint16_t value;
 	uint16_t low_bit;
@@ -142,8 +142,7 @@ char *sepol_extended_perms_to_string(avtab_extended_perms_t *xperms)
 		return NULL;
 
 retry:
-	size *= 2;
-	if (size == 0)
+	if (__builtin_mul_overflow(size, 2, &size))
 		goto err;
 	p = realloc(buffer, size);
 	if (!p)
Signature · 1 line
2.43.0
Acked-by on v1 1/3
#3 lore raw
In reply to Christian Göttsche
Quoted 4 lines · Christian Göttsche

The input string to be tokenized is not modified.

For these three patches:

Quoted 51 lines
---
 libsepol/include/sepol/policydb/util.h | 2 +-
 libsepol/src/util.c                    | 9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
libsepol/include/sepol/policydb/util.h +1 -1
index db8da213..4ea5a346 100644
@@ -40,7 +40,7 @@ char *sepol_extended_perms_to_string(avtab_extended_perms_t *xperms);
  * The tokenize function may be used to
  * replace sscanf
  */
-extern int tokenize(char *line_buf, char delim, int num_args, ...);
+extern int tokenize(const char *line_buf, char delim, int num_args, ...);
 
 #ifdef __cplusplus
 }
libsepol/src/util.c +5 -5
index 2f877920..571f6c93 100644
@@ -221,9 +221,9 @@ err:
  */
 
 /* Read a token from a buffer */
-static inline int tokenize_str(char delim, char **str, char **ptr, size_t *len)
+static inline int tokenize_str(char delim, char **str, const char **ptr, size_t *len)
 {
-       char *tmp_buf = *ptr;
+       const char *tmp_buf = *ptr;
        *str = NULL;
 
        while (**ptr != '\0') {
@@ -273,9 +273,10 @@ static inline int tokenize_str(char delim, char **str, char **ptr, size_t *len)
  * contain the remaining content of line_buf. If the delimiter is any whitespace
  * character, then all whitespace will be squashed.
  */
-int tokenize(char *line_buf, char delim, int num_args, ...)
+int tokenize(const char *line_buf, char delim, int num_args, ...)
 {
-       char **arg, *buf_p;
+       char **arg;
+       const char *buf_p;
        int rc, items;
        size_t arg_len = 0;
        va_list ap;
--

2.43.0

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

The input string to be tokenized is not modified.

For these three patches:

These three patches have been merged.
Thanks,
Jim

Quoted 51 lines
Quoted 51 lines
---
 libsepol/include/sepol/policydb/util.h | 2 +-
 libsepol/src/util.c                    | 9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
libsepol/include/sepol/policydb/util.h +1 -1
index db8da213..4ea5a346 100644
@@ -40,7 +40,7 @@ char *sepol_extended_perms_to_string(avtab_extended_perms_t *xperms);
  * The tokenize function may be used to
  * replace sscanf
  */
-extern int tokenize(char *line_buf, char delim, int num_args, ...);
+extern int tokenize(const char *line_buf, char delim, int num_args, ...);
 
 #ifdef __cplusplus
 }
libsepol/src/util.c +5 -5
index 2f877920..571f6c93 100644
@@ -221,9 +221,9 @@ err:
  */
 
 /* Read a token from a buffer */
-static inline int tokenize_str(char delim, char **str, char **ptr, size_t *len)
+static inline int tokenize_str(char delim, char **str, const char **ptr, size_t *len)
 {
-       char *tmp_buf = *ptr;
+       const char *tmp_buf = *ptr;
        *str = NULL;
 
        while (**ptr != '\0') {
@@ -273,9 +273,10 @@ static inline int tokenize_str(char delim, char **str, char **ptr, size_t *len)
  * contain the remaining content of line_buf. If the delimiter is any whitespace
  * character, then all whitespace will be squashed.
  */
-int tokenize(char *line_buf, char delim, int num_args, ...)
+int tokenize(const char *line_buf, char delim, int num_args, ...)
 {
-       char **arg, *buf_p;
+       char **arg;
+       const char *buf_p;
        int rc, items;
        size_t arg_len = 0;
        va_list ap;
--

2.43.0