Skip to contentSkip to conversation

libsepol/cil: bail out on snprintf failure

PATCH In review Christian Göttsche 2 participants 3 messages Dec 20, 2021 – Jan 5, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol/cil: bail out on snprintf failure

Do not continue with a negative return value once a string append operation fails to avoid increasing the buffer length variable str_len, potentially leading to an out-of-bounds write.

Found by GitHub CodeQL.

---
 libsepol/cil/src/cil.c | 6 ++++++
 1 file changed, 6 insertions(+)
libsepol/cil/src/cil.c +6 -0
index 9916cbee..38edcf8e 100644
@@ -1456,6 +1456,12 @@ int cil_userprefixes_to_string(struct cil_db *db, char **out, size_t *size)
 
 		buf_pos = snprintf(str_tmp, str_len, "user %s prefix %s;\n", user->datum.fqn,
 									userprefix->prefix_str);
+		if (buf_pos < 0) {
+			free(str_tmp);
+			*size = 0;
+			*out = NULL;
+			goto exit;
+		}
 		str_len -= buf_pos;
 		str_tmp += buf_pos;
 	}
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Christian Göttsche

Do not continue with a negative return value once a string append operation fails to avoid increasing the buffer length variable str_len, potentially leading to an out-of-bounds write.

Found by GitHub CodeQL.

Quoted 24 lines
---
 libsepol/cil/src/cil.c | 6 ++++++
 1 file changed, 6 insertions(+)
libsepol/cil/src/cil.c +6 -1
index 9916cbee..38edcf8e 100644
@@ -1456,6 +1456,12 @@ int cil_userprefixes_to_string(struct cil_db *db, char **out, size_t *size)
 
                buf_pos = snprintf(str_tmp, str_len, "user %s prefix %s;\n", user->datum.fqn,
                                                                        userprefix->prefix_str);
+               if (buf_pos < 0) {
+                       free(str_tmp);
+                       *size = 0;
+                       *out = NULL;
+                       goto exit;
+               }
                str_len -= buf_pos;
                str_tmp += buf_pos;
        }
--

2.34.1

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

Do not continue with a negative return value once a string append operation fails to avoid increasing the buffer length variable str_len, potentially leading to an out-of-bounds write.

Quoted 6 lines
Quoted 3 lines

Found by GitHub CodeQL.

Merged.
Thanks,
Jim

Quoted 24 lines
Quoted 24 lines
---
 libsepol/cil/src/cil.c | 6 ++++++
 1 file changed, 6 insertions(+)
libsepol/cil/src/cil.c +6 -1
index 9916cbee..38edcf8e 100644
@@ -1456,6 +1456,12 @@ int cil_userprefixes_to_string(struct cil_db *db, char **out, size_t *size)
 
                buf_pos = snprintf(str_tmp, str_len, "user %s prefix %s;\n", user->datum.fqn,
                                                                        userprefix->prefix_str);
+               if (buf_pos < 0) {
+                       free(str_tmp);
+                       *size = 0;
+                       *out = NULL;
+                       goto exit;
+               }
                str_len -= buf_pos;
                str_tmp += buf_pos;
        }
--

2.34.1