libselinux: rework selabel_subs_init() to avoid use-after-free
PATCH 1/2 libselinux: rework selabel_subs_init() to avoid use-after-free
In selabel_subs_init(), when digest_add_specfile() fails, the returned value is a pointer to data which has been freed (because label "err" frees variable "sub" which is equals to the returned variable, "list").
Moreover since since commit fd56c5230cea ("Separate out the calling of local subs and dist subs in selabel_sub"), argument "list" of selabel_subs_init() has always been NULL (rec->subs and rec->dist_subs are both initialized to NULL in selabel_open() before selabel_file_init() is called).
Drop selabel_file_init()'s "list" argument and free all the list items which have been allocated in this function, when the code encounters an error.
--- libselinux/src/label.c | 14 +++++++++++--- libselinux/src/label_file.c | 10 ++++------ libselinux/src/label_internal.h | 1 - 3 files changed, 15 insertions(+), 10 deletions(-)
libselinux/src/label.c +11 -3
@@ -92,12 +92,11 @@ static char *selabel_sub(struct selabel_sub *ptr, const char *src) }struct selabel_sub *selabel_subs_init (const char *path, -struct selabel_sub *list, -struct selabel_digest *digest) +struct selabel_digest *digest) {char buf[1024 ];FILE *cfg =fopen (path,"re" ); -struct selabel_sub *sub =NULL ; +struct selabel_sub *list =NULL , *sub =NULL ;struct stat sb;if (!cfg)
@@ -146,6 +145,7 @@ struct selabel_sub *selabel_subs_init(const char *path, sub->slen =strlen (src); sub->next = list; list = sub; + sub =NULL ; }if (digest_add_specfile (digest, cfg,NULL , sb.st_size, path) <0 )
@@ -158,6 +158,14 @@ err:if (sub)free (sub->src);free (sub); +while (list) { + sub = list->next; +free (list->src); +free (list->dst); +free (list); + list = sub; + } + list =NULL ; goto out; }
libselinux/src/label_file.c +4 -6
@@ -589,17 +589,15 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,if (!path) { rec->dist_subs =selabel_subs_init (selinux_file_context_subs_dist_path (), - rec->dist_subs, rec->digest); + rec->digest); rec->subs =selabel_subs_init (selinux_file_context_subs_path (), - rec->subs, rec->digest); + rec->digest); path =selinux_file_context_path (); }else {snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , path); - rec->dist_subs =selabel_subs_init (subs_file, rec->dist_subs, - rec->digest); + rec->dist_subs =selabel_subs_init (subs_file, rec->digest);snprintf (subs_file,sizeof (subs_file),"%s.subs" , path); - rec->subs =selabel_subs_init (subs_file, rec->subs, - rec->digest); + rec->subs =selabel_subs_init (subs_file, rec->digest); }#endif
libselinux/src/label_internal.h +0 -1
@@ -76,7 +76,6 @@ extern int digest_add_specfile(struct selabel_digest *digest, FILE *fp,extern void digest_gen_hash (struct selabel_digest *digest);extern struct selabel_sub *selabel_subs_init (const char *path, -struct selabel_sub *list,struct selabel_digest *digest);struct selabel_lookup_rec {
Signature · 1 line
2.13.0
PATCH 2/2 libselinux: propagate selabel_subs_init() errors
Memory allocation failures in selabel_subs_init() should be fatal, contrary to failures which come from the non-existence of the substitution files (subs or subs_dist).
Modify selabel_subs_init()'s prototype in order to return the error state. This forces the pointer to the created substitution list to be moved to an output function argument.
--- libselinux/src/label.c | 18 ++++++++++++------ libselinux/src/label_file.c | 24 +++++++++++++++++------- libselinux/src/label_internal.h | 4 ++-- 3 files changed, 31 insertions(+), 15 deletions(-)
libselinux/src/label.c +12 -6
@@ -91,16 +91,20 @@ static char *selabel_sub(struct selabel_sub *ptr, const char *src)return NULL ; } -struct selabel_sub *selabel_subs_init (const char *path, -struct selabel_digest *digest) +int selabel_subs_init (const char *path,struct selabel_digest *digest, +struct selabel_sub **out_subs) {char buf[1024 ];FILE *cfg =fopen (path,"re" );struct selabel_sub *list =NULL , *sub =NULL ;struct stat sb; +int status =-1 ; -if (!cfg) -return list; + *out_subs =NULL ; +if (!cfg) { +/* If the file does not exist, it is not fatal */ +return (errno ==ENOENT ) ?0 :-1 ; + }if (fstat (fileno (cfg), &sb) <0 ) goto out;
@@ -151,9 +155,12 @@ struct selabel_sub *selabel_subs_init(const char *path,if (digest_add_specfile (digest, cfg,NULL , sb.st_size, path) <0 ) goto err; + *out_subs = list; + status =0 ; + out:fclose (cfg); -return list; +return status; err:if (sub)free (sub->src);
@@ -165,7 +172,6 @@ err:free (list); list = sub; } - list =NULL ; goto out; }
libselinux/src/label_file.c +17 -7
@@ -587,17 +587,27 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,char subs_file[PATH_MAX +1 ];/* Process local and distribution substitution files */ if (!path) { - rec->dist_subs = -selabel_subs_init (selinux_file_context_subs_dist_path (), - rec->digest); - rec->subs =selabel_subs_init (selinux_file_context_subs_path (), - rec->digest); + status =selabel_subs_init ( +selinux_file_context_subs_dist_path (), + rec->digest, &rec->dist_subs); +if (status) + goto finish; + status =selabel_subs_init (selinux_file_context_subs_path (), + rec->digest, &rec->subs); +if (status) + goto finish; path =selinux_file_context_path (); }else {snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , path); - rec->dist_subs =selabel_subs_init (subs_file, rec->digest); + status =selabel_subs_init (subs_file, rec->digest, + &rec->dist_subs); +if (status) + goto finish;snprintf (subs_file,sizeof (subs_file),"%s.subs" , path); - rec->subs =selabel_subs_init (subs_file, rec->digest); + status =selabel_subs_init (subs_file, rec->digest, + &rec->subs); +if (status) + goto finish; }#endif
libselinux/src/label_internal.h +2 -2
@@ -75,8 +75,8 @@ extern int digest_add_specfile(struct selabel_digest *digest, FILE *fp, const char *path);extern void digest_gen_hash (struct selabel_digest *digest); -extern struct selabel_sub *selabel_subs_init (const char *path, -struct selabel_digest *digest); +extern int selabel_subs_init (const char *path,struct selabel_digest *digest, +struct selabel_sub **out_subs);struct selabel_lookup_rec {char * ctx_raw;
Signature · 1 line
2.13.0
Stephen Smalley
AppliedRe: [PATCH 2/2] libselinux: propagate selabel_subs_init() errors
Quoted 7 lines · Nicolas Iooss
Memory allocation failures in
selabel_subs_init()should be fatal, contrary to failures which come from the non-existence of the substitution files (subs orsubs_dist).Modify selabel_subs_init()'s prototype in order to return the error state. This forces the pointer to the created substitution list to be moved to an output function argument.
Thanks, applied both.
Quoted 130 lines
Signed-off-by Nicolas Iooss <[email protected]>---
libselinux/src/label.c | 18 ++++++++++++------ libselinux/src/label_file.c | 24 +++++++++++++++++-------libselinux/src/label_internal.h| 4 ++--3 files changed, 31 insertions(+), 15 deletions(-)libselinux/src/label.c +0
-0@@ -91,16 +91,20 @@ static char *selabel_sub(struct selabel_sub *ptr, const char *src) return NULL; }libselinux/src/label.c +1
-2-struct selabel_sub *selabel_subs_init (const char *path, - struct selabel_digest *digest) +int selabel_subs_init(const char *path, struct selabel_digest*digest,
libselinux/src/label.c +1
-0+ struct selabel_sub **out_subs){
char buf[1024];
FILE *cfg =fopen(path, "re");
structselabel_sub*list = NULL, *sub = NULL; struct stat sb;libselinux/src/label.c +1
-0+int status =-1 ;libselinux/src/label.c +5
-2-if (!cfg) -return list; + *out_subs =NULL ; +if (!cfg) { +/* If the file does not exist, it is not fatal */ +return (errno ==ENOENT ) ?0 :-1 ; + }
if (fstat(fileno(cfg), &sb)< 0)
goto out;libselinux/src/label.c +0
-0@@ -151,9 +155,12 @@ struct selabel_sub *selabel_subs_init(const char *path, if (digest_add_specfile(digest, cfg, NULL, sb.st_size, path) < 0) goto err;libselinux/src/label.c +3
-0+ *out_subs = list; + status =0 ; +out:
fclose(cfg);libselinux/src/label.c +1
-1-return list; +return status;err:
if (sub)
free(sub->src);libselinux/src/label.c +0
-1@@ -165,7 +172,6 @@ err: free(list); list = sub; } - list =NULL ; goto out; }libselinux/src/label_file.c +9
-6@@ -587,17 +587,27 @@ static int init(struct selabel_handle *rec,const struct selinux_opt *opts, char subs_file[PATH_MAX + 1]; /* Process local and distribution substitution files */ if (!path) { - rec->dist_subs = - selabel_subs_init(selinux_file_context_subs_dist_path (), - rec->digest); - rec->subs =selabel_subs_init (selinux_file_context_subs_path (), - rec->digest); + status =selabel_subs_init ( +selinux_file_context_subs_dist_path (), + rec->digest, &rec->dist_subs); +if (status) + goto finish; + status =selabel_subs_init (selinux_file_context_subs_path (), + rec->digest, &rec->subs); +if (status) + goto finish; path = selinux_file_context_path(); } else { snprintf(subs_file, sizeof(subs_file),"%s.subs_dist" , path); - rec->dist_subs = selabel_subs_init(subs_file, rec-Quoted 1 line
digest);
libselinux/src/label_file.c +4
-0+ status =selabel_subs_init (subs_file, rec->digest, + &rec->dist_subs); +if (status) + goto finish;
snprintf(subs_file, sizeof(subs_file), "%s.subs", path);libselinux/src/label_file.c +0
-1- rec->subs = selabel_subs_init(subs_file, rec-Quoted 1 line
digest);
libselinux/src/label_file.c +4
-0+ status =selabel_subs_init (subs_file, rec->digest, + &rec->subs); +if (status) + goto finish;}
#endiflibselinux/src/label_internal.h +0
-0@@ -75,8 +75,8 @@ extern int digest_add_specfile(struct selabel_digest *digest, FILE *fp,const char *path);
extern voiddigest_gen_hash(struct selabel_digest *digest);libselinux/src/label_internal.h +1
-2-extern struct selabel_sub *selabel_subs_init (const char *path, - struct selabel_digest *digest); +extern int selabel_subs_init(const char *path, struct selabel_digest*digest,
libselinux/src/label_internal.h +1
-0+ struct selabel_sub **out_subs);
structselabel_lookup_rec{
char *ctx_raw;