Skip to contentSkip to conversation

libsepol: policydb_read(): use a static string for policydb_str

PATCH In review Rahul Sandhu 2 participants 2 messages Jan 12 – Jan 14 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: policydb_read(): use a static string for policydb_str

We know the maximum possible size of policydb_str at compile time; it's POLICYDB_STRING_MAX_LENGTH + 1 (with + 1 accounting for the null term). As POLICYDB_STRING_MAX_LENGTH is trivially small, make it a static str, avoiding an extra allocation.

---
 libsepol/src/policydb.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
libsepol/src/policydb.c +1 -14
index 6aaa18f2..2b0e32f7 100644
@@ -4192,7 +4192,7 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
 	unsigned int i, j, r_policyvers;
 	uint32_t buf[5], nprim;
 	size_t len, nel;
-	char *policydb_str;
+	static char policydb_str[POLICYDB_STRING_MAX_LENGTH + 1];
 	const struct policydb_compat_info *info;
 	unsigned int policy_type, bufindex;
 	ebitmap_node_t *tnode;
@@ -4222,16 +4222,9 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
 		return POLICYDB_ERROR;
 	}
 
-	policydb_str = malloc(len + 1);
-	if (!policydb_str) {
-		ERR(fp->handle, "unable to allocate memory for policydb "
-		    "string of length %zu", len);
-		return POLICYDB_ERROR;
-	}
 	rc = next_entry(policydb_str, fp, len);
 	if (rc < 0) {
 		ERR(fp->handle, "truncated policydb string identifier");
-		free(policydb_str);
 		return POLICYDB_ERROR;
 	}
 	policydb_str[len] = 0;
@@ -4248,22 +4241,16 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
 		if (i == POLICYDB_TARGET_SZ) {
 			ERR(fp->handle, "cannot find a valid target for policy "
 				"string %s", policydb_str);
-			free(policydb_str);
 			return POLICYDB_ERROR;
 		}
 	} else {
 		if (strcmp(policydb_str, POLICYDB_MOD_STRING)) {
 			ERR(fp->handle, "invalid string identifier %s",
 				policydb_str);
-			free(policydb_str);
 			return POLICYDB_ERROR;
 		}
 	}
 
-	/* Done with policydb_str. */
-	free(policydb_str);
-	policydb_str = NULL;
-
 	/* Read the version, config, and table sizes (and policy type if it's a module). */
 	if (policy_type == POLICY_KERN)
 		nel = 4;
Signature · 1 line
2.52.0
#1 lore raw
Quoted 16 lines · Rahul Sandhu

We know the maximum possible size of policydb_str at compile time; it's POLICYDB_STRING_MAX_LENGTH + 1 (with + 1 accounting for the null term). As POLICYDB_STRING_MAX_LENGTH is trivially small, make it a static str, avoiding an extra allocation.

---
 libsepol/src/policydb.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
libsepol/src/policydb.c +0 -0
index 6aaa18f2..2b0e32f7 100644

libsepol/src/policydb.c int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose) · line 4195

Quoted 5 lines
libsepol/src/policydb.c +1 -1
@@ -4192,7 +4192,7 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
        unsigned int i, j, r_policyvers;
        uint32_t buf[5], nprim;
        size_t len, nel;
-       char *policydb_str;
+       static char policydb_str[POLICYDB_STRING_MAX_LENGTH + 1];

Using a static buffer will make this function non-thread-safe.

Quoted 47 lines
libsepol/src/policydb.c +0 -14
        const struct policydb_compat_info *info;
        unsigned int policy_type, bufindex;
        ebitmap_node_t *tnode;
@@ -4222,16 +4222,9 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
                return POLICYDB_ERROR;
        }
 
-       policydb_str = malloc(len + 1);
-       if (!policydb_str) {
-               ERR(fp->handle, "unable to allocate memory for policydb "
-                   "string of length %zu", len);
-               return POLICYDB_ERROR;
-       }
        rc = next_entry(policydb_str, fp, len);
        if (rc < 0) {
                ERR(fp->handle, "truncated policydb string identifier");
-               free(policydb_str);
                return POLICYDB_ERROR;
        }
        policydb_str[len] = 0;
@@ -4248,22 +4241,16 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
                if (i == POLICYDB_TARGET_SZ) {
                        ERR(fp->handle, "cannot find a valid target for policy "
                                "string %s", policydb_str);
-                       free(policydb_str);
                        return POLICYDB_ERROR;
                }
        } else {
                if (strcmp(policydb_str, POLICYDB_MOD_STRING)) {
                        ERR(fp->handle, "invalid string identifier %s",
                                policydb_str);
-                       free(policydb_str);
                        return POLICYDB_ERROR;
                }
        }
 
-       /* Done with policydb_str. */
-       free(policydb_str);
-       policydb_str = NULL;
-
        /* Read the version, config, and table sizes (and policy type if it's a module). */
        if (policy_type == POLICY_KERN)
                nel = 4;
--

2.52.0