libsepol: policydb_read(): use a static string for policydb_str
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.
Signed-off-by Rahul Sandhu <[email protected]>
--- libsepol/src/policydb.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
libsepol/src/policydb.c +1 -14
@@ -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
Quoted 16 lines · Rahul Sandhu
We know the maximum possible size of
policydb_strat compile time; it'sPOLICYDB_STRING_MAX_LENGTH+ 1 (with + 1 accounting for the null term). AsPOLICYDB_STRING_MAX_LENGTHis trivially small, make it a static str, avoiding an extra allocation.Signed-off-by Rahul Sandhu <[email protected]>--- libsepol/src/policydb.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)libsepol/src/policydb.c +0
-0
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
-14const 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