libsepol: create new keys with copy of 'name' variable
PATCH 1/1 libsepol: create new keys with copy of 'name' variable
sepol_*_key_create functions, which create keys based only on name, copy only a pointer to a new key. This pointer doesn't need to be valid during the new key lifetime therefore it's necessary to create a copy of it.
E.g. Python 3 SWIG bindings temporarily allocate memory needed for PyUnicodeObject->char * conversion. This memory is deallocated shortly after underlying C function returns.
--- libsepol/src/boolean_record.c | 7 +++++-- libsepol/src/iface_record.c | 7 +++++-- libsepol/src/user_record.c | 7 +++++-- 3 files changed, 15 insertions(+), 6 deletions(-)
libsepol/src/boolean_record.c +5 -2
@@ -25,12 +25,14 @@ int sepol_bool_key_create(sepol_handle_t * handle,sepol_bool_key_t *tmp_key = (sepol_bool_key_t *)malloc (sizeof (struct sepol_bool_key )); -if (!tmp_key) { +char *tmp_name =strdup (name); + +if (!tmp_key || !tmp_name) {ERR (handle,"out of memory, " "could not create boolean key" );return STATUS_ERR ; } - tmp_key->name = name; + tmp_key->name = tmp_name; *key_ptr = tmp_key;return STATUS_SUCCESS ;
@@ -62,6 +64,7 @@ int sepol_bool_key_extract(sepol_handle_t * handle,void sepol_bool_key_free (sepol_bool_key_t * key) { +free (key->name);free (key); }
libsepol/src/iface_record.c +5 -2
@@ -31,12 +31,14 @@ int sepol_iface_key_create(sepol_handle_t * handle,sepol_iface_key_t *tmp_key = (sepol_iface_key_t *)malloc (sizeof (sepol_iface_key_t)); -if (!tmp_key) { +char *tmp_name =strdup (name); + +if (!tmp_key || !tmp_name) {ERR (handle,"out of memory, could not create interface key" );return STATUS_ERR ; } - tmp_key->name = name; + tmp_key->name = tmp_name; *key_ptr = tmp_key;return STATUS_SUCCESS ;
@@ -68,6 +70,7 @@ int sepol_iface_key_extract(sepol_handle_t * handle,void sepol_iface_key_free (sepol_iface_key_t * key) { +free (key->name);free (key); }
libsepol/src/user_record.c +5 -2
@@ -34,13 +34,15 @@ int sepol_user_key_create(sepol_handle_t * handle,sepol_user_key_t *tmp_key = (sepol_user_key_t *)malloc (sizeof (sepol_user_key_t)); -if (!tmp_key) { +char *tmp_name =strdup (name); + +if (!tmp_key || !tmp_name) {ERR (handle,"out of memory, " "could not create selinux user key" );return STATUS_ERR ; } - tmp_key->name = name; + tmp_key->name = tmp_name; *key_ptr = tmp_key;return STATUS_SUCCESS ;
@@ -71,6 +73,7 @@ int sepol_user_key_extract(sepol_handle_t * handle,void sepol_user_key_free (sepol_user_key_t * key) { +free (key->name);free (key); }
Signature · 1 line
2.5.0
Quoted 22 lines · Petr Lautrbach
sepol_*_key_create functions, which create keys based only on name, copy only a pointer to a new key. This pointer doesn't need to be valid during the new key lifetime therefore it's necessary to create a copy of it.
E.g. Python 3 SWIG bindings temporarily allocate memory needed for
PyUnicodeObject->char* conversion. This memory is deallocated shortly after underlying C function returns.Patch-by Michal Srb <[email protected]>Signed-off-by Petr Lautrbach <[email protected]>--- libsepol/src/boolean_record.c | 7 +++++-- libsepol/src/iface_record.c | 7 +++++-- libsepol/src/user_record.c | 7 +++++-- 3 files changed, 15 insertions(+), 6 deletions(-)libsepol/src/boolean_record.c +0
-0@@ -25,12 +25,14 @@ int sepol_bool_key_create(sepol_handle_t * handle, sepol_bool_key_t *tmp_key =
libsepol/src/boolean_record.c int sepol_bool_key_create(sepol_handle_t * handle, · line 34
Quoted 10 lines
libsepol/src/boolean_record.c +3
-1(sepol_bool_key_t *)malloc (sizeof (struct sepol_bool_key )); -if (!tmp_key) { +char *tmp_name =strdup (name); + +if (!tmp_key || !tmp_name) {ERR (handle,"out of memory, " "could not create boolean key" );return STATUS_ERR ; }
Need to free tmp_key if it is allocated, but the strdup for tmp_name fails. And vice versa.
Quoted 19 lines
libsepol/src/boolean_record.c +2
-1- tmp_key->name = name; + tmp_key->name = tmp_name; *key_ptr = tmp_key;return STATUS_SUCCESS ;@@ -62,6 +64,7 @@ int sepol_bool_key_extract(sepol_handle_t * handle,void sepol_bool_key_free (sepol_bool_key_t * key) { +free (key->name);free (key); }libsepol/src/iface_record.c +0
-0@@ -31,12 +31,14 @@ int sepol_iface_key_create(sepol_handle_t * handle, sepol_iface_key_t *tmp_key =
libsepol/src/iface_record.c int sepol_iface_key_create(sepol_handle_t * handle, · line 40
Quoted 10 lines
libsepol/src/iface_record.c +3
-1(sepol_iface_key_t *)malloc (sizeof (sepol_iface_key_t)); -if (!tmp_key) { +char *tmp_name =strdup (name); + +if (!tmp_key || !tmp_name) {ERR (handle,"out of memory, could not create interface key" );return STATUS_ERR ; }
Same here.
Quoted 20 lines
libsepol/src/iface_record.c +2
-1- tmp_key->name = name; + tmp_key->name = tmp_name; *key_ptr = tmp_key;return STATUS_SUCCESS ;@@ -68,6 +70,7 @@ int sepol_iface_key_extract(sepol_handle_t * handle,void sepol_iface_key_free (sepol_iface_key_t * key) { +free (key->name);free (key); }libsepol/src/user_record.c +0
-0@@ -34,13 +34,15 @@ int sepol_user_key_create(sepol_handle_t * handle,sepol_user_key_t *tmp_key = (sepol_user_key_t *)malloc (sizeof (sepol_user_key_t));
libsepol/src/user_record.c int sepol_user_key_create(sepol_handle_t * handle, · line 44
Quoted 10 lines
libsepol/src/user_record.c +3
-1-if (!tmp_key) { +char *tmp_name =strdup (name); + +if (!tmp_key || !tmp_name) {ERR (handle,"out of memory, " "could not create selinux user key" );return STATUS_ERR ; }
And here.
Quoted 14 lines
libsepol/src/user_record.c +2
-1- tmp_key->name = name; + tmp_key->name = tmp_name; *key_ptr = tmp_key;return STATUS_SUCCESS ;@@ -71,6 +73,7 @@ int sepol_user_key_extract(sepol_handle_t * handle,void sepol_user_key_free (sepol_user_key_t * key) { +free (key->name);free (key); }
Signature · 2 lines
James Carter <[email protected]> National Security Agency
Quoted 7 lines · James Carter
Quoted 6 lines · Petr Lautrbach
libsepol/src/boolean_record.c +0
-0@@ -25,12 +25,14 @@ int sepol_bool_key_create(sepol_handle_t * handle,sepol_bool_key_t *tmp_key = (sepol_bool_key_t *)malloc (sizeof (struct sepol_bool_key ));
Quoted 12 lines
libsepol/src/boolean_record.c int sepol_bool_key_create(sepol_handle_t * handle, · line 34
Quoted 8 lines
libsepol/src/boolean_record.c +3
-1-if (!tmp_key) { +char *tmp_name =strdup (name); + +if (!tmp_key || !tmp_name) {ERR (handle,"out of memory, " "could not create boolean key" );return STATUS_ERR ; }Need to free
tmp_keyif it is allocated, but the strdup fortmp_namefails. And vice versa.
The patch is apparently not ready yet. It can't be even compiled, see bellow. Sorry for the noise.
cc -Werror -Wall -W -Wundef -Wshadow -Wmissing-format-attribute -O2 -I. -I../include -D_GNU_SOURCE -I../cil/include -fPIC -c -o boolean_record.o boolean_record.cboolean_record.c: In function ‘sepol_bool_key_free’:
boolean_record.c:67:7: error: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
free(key->name);
^In file included from boolean_record.c:2:0:/usr/include/stdlib.h:483:13: note: expected ‘void *’ but argument is of type ‘const char *’
extern void free (void *__ptr) __THROW;
Signature · 1 line
Petr Lautrbach