SELinux Security Module development
 help / color / mirror / Atom feed
* [PATCH v2] libsepol: policydb_read(): use a static string for policydb_str
@ 2026-04-03  6:53 Rahul Sandhu
  2026-04-03  6:56 ` [PATCH v3] " Rahul Sandhu
  0 siblings, 1 reply; 6+ messages in thread
From: Rahul Sandhu @ 2026-04-03  6:53 UTC (permalink / raw)
  To: selinux; +Cc: Rahul Sandhu, Rahul Sandhu

From: Rahul Sandhu <nvraxn@gmail.com>

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.

Signed-off-by: Rahul Sandhu <nvraxn@posteo.uk>
---
 libsepol/src/policydb.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)

diff --git a/libsepol/src/policydb.c b/libsepol/src/policydb.c
index 9760b164..8d290d86 100644
--- a/libsepol/src/policydb.c
+++ b/libsepol/src/policydb.c
@@ -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;
+	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;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-05-13 13:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-03  6:53 [PATCH v2] libsepol: policydb_read(): use a static string for policydb_str Rahul Sandhu
2026-04-03  6:56 ` [PATCH v3] " Rahul Sandhu
2026-04-03  6:58   ` Rahul Sandhu
2026-04-24 20:40   ` Rahul Sandhu
2026-05-12 19:14   ` James Carter
2026-05-13 13:57     ` James Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox