libselinux: support multiple spec_files
PATCH 1/2 libselinux: support multiple spec_files
Update the existing backends to potentially allocate multiple spec_files. This commit simply adds a level of indirection but does not change any current behaviour. It will facilitate the gradual migration of backends to multiple context files.
In selabel_fini(), compat_validate() is updated to use the first spec_file only, for legacy validation error reporting. selabel_open() is already performing context validation if requested against individual file paths (see insert_spec).
--- libselinux/src/label.c | 10 ++++++++-- libselinux/src/label_db.c | 11 +++++++++-- libselinux/src/label_file.c | 8 ++++++-- libselinux/src/label_internal.h | 5 +++-- libselinux/src/label_media.c | 8 +++++++- libselinux/src/label_x.c | 8 +++++++- 6 files changed, 40 insertions(+), 10 deletions(-)
libselinux/src/label.c +8 -2
@@ -162,7 +162,7 @@ static int selabel_fini(const struct selabel_handle *rec,char *ctx_trans;int rc; -if (compat_validate (rec, lr, rec->spec_file, lr->lineno)) +if (compat_validate (rec, lr, rec->spec_files[0 ], lr->lineno))return -1 ;if (!translating)
@@ -398,11 +398,17 @@ int selabel_digest(struct selabel_handle *rec, unsigned char **digest,void selabel_close (struct selabel_handle *rec) { +size_t i; +if (rec->digest)selabel_digest_fini (rec->digest);if (rec->func_close) rec->func_close (rec); -free (rec->spec_file); +if (rec->spec_files) { +for (i =0 ; i < rec->spec_files_len; i++) +free (rec->spec_files[i]); +free (rec->spec_files); + }free (rec); }
libselinux/src/label_db.c +9 -2
@@ -302,12 +302,19 @@ static catalog_t *db_init(const struct selinux_opt *opts, unsigned nopts, errno =EINVAL ;return NULL ; } - rec->spec_file =strdup (path); -if (!rec->spec_file) { + rec->spec_files =calloc (1 ,sizeof (path)); +if (!rec->spec_files) {free (catalog);fclose (filp);return NULL ; } + rec->spec_files[0 ] =strdup (path); +if (!rec->spec_files[0 ]) { +free (catalog); +fclose (filp); +return NULL ; + } + rec->spec_files_len =1 ;/* * Parse for each lines
libselinux/src/label_file.c +6 -2
@@ -1549,9 +1549,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, goto finish; } - rec->spec_file =strdup (path); -if (!rec->spec_file) + rec->spec_files =calloc (1 ,sizeof (path)); +if (!rec->spec_files) goto finish; + rec->spec_files[0 ] =strdup (path); +if (!rec->spec_files[0 ]) + goto finish; + rec->spec_files_len =1 ;/* * The do detailed validation of the input and fill the spec array
libselinux/src/label_internal.h +3 -2
@@ -96,10 +96,11 @@ struct selabel_handle {void *data;/* -* The main spec file used. Note for file contexts the local and/or +* The spec files used. Note for file contexts the local and/or * homedirs could also have been used to resolve a context. */ -char *spec_file; +size_t spec_files_len; +char **spec_files;/* ptr to SHA1 hash information if SELABEL_OPT_DIGEST set */ struct selabel_digest *digest;
libselinux/src/label_media.c +7 -1
@@ -109,7 +109,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, errno =EINVAL ; goto finish; } - rec->spec_file =strdup (path); + rec->spec_files =calloc (1 ,sizeof (path)); +if (!rec->spec_files) + goto finish; + rec->spec_files[0 ] =strdup (path); +if (!rec->spec_files[0 ]) + goto finish; + rec->spec_files_len =1 ;/* * Perform two passes over the specification file.
libselinux/src/label_x.c +7 -1
@@ -139,7 +139,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, errno =EINVAL ; goto finish; } - rec->spec_file =strdup (path); + rec->spec_files =calloc (1 ,sizeof (path)); +if (!rec->spec_files) + goto finish; + rec->spec_files[0 ] =strdup (path); +if (!rec->spec_files[0 ]) + goto finish; + rec->spec_files_len =1 ;/* * Perform two passes over the specification file.
Signature · 1 line
2.55.0.229.g6434b31f56-goog
PATCH 2/2 libselinux: support multiple contexts for the file backend
Update the file labeling backend to support specifying multiple SELABEL_OPT_PATH options in selabel_open().
All provided paths are stored in rec->spec_files and processed during initialization. Derived files, such as substitutions (.subs, .subs_dist) and auxiliary contexts (.homedirs, .local), continue to be based on the first path (or default selinux_file_context_path()) when enabled.
Duplicate checking logic is refactored into a helper function report_dups(). A duplicate with the same specification result is not fatal anymore, but a warning is still logged.
Additionally, duplicate validation is moved after all files (including .homedirs and .local) are loaded and sorted. For single file context setups, the only difference in behavior is that duplicate validation now extends to fc.homedirs and fc.local.
A similar multiple files setup has been used in Android for 9+ years.
--- libselinux/src/label_file.c | 216 ++++++++++++++++++++++-------------- 1 file changed, 132 insertions(+), 84 deletions(-)
libselinux/src/label_file.c +132 -84
@@ -104,9 +104,59 @@ void sort_spec_node(struct spec_node *node, struct spec_node *parent) }/* -* Warn about duplicate specifications. +* Report a duplicate in the log. +* +* If the duplicate is considered fatal (i.e., if the specifications of both +* entries do not match), return -1 and set errno to EINVAL. +*/ +static int report_dups (struct selabel_handle *rec,uint8_t inputno1, +uint8_t inputno2,const char *ctx_raw1, +const char *ctx_raw2,const char *match, +const char *file_kind) +{ +if (inputno1 >= rec->spec_files_len) + inputno1 =0 ; +if (inputno2 >= rec->spec_files_len) + inputno2 =0 ; + +const char *path1 = rec->spec_files[inputno1]; +const char *path2 = rec->spec_files[inputno2]; + +if (strcmp (ctx_raw1, ctx_raw2) !=0 ) { +if (inputno1 == inputno2) +COMPAT_LOG ( +SELINUX_ERROR , +"Multiple different specifications for %s %s in %s (%s and %s).\n" , + file_kind, match, path1, ctx_raw1, ctx_raw2); +else +COMPAT_LOG ( +SELINUX_ERROR , +"Multiple different specifications for %s %s in %s and %s (%s and %s).\n" , + file_kind, match, path1, path2, ctx_raw1, + ctx_raw2); + errno =EINVAL ; +return -1 ; + + }else { +if (inputno1 == inputno2) +COMPAT_LOG ( +SELINUX_WARNING , +"Multiple same specifications for %s %s in %s.\n" , + file_kind, match, path1); +else +COMPAT_LOG ( +SELINUX_WARNING , +"Multiple same specifications for %s %s in %s and %s.\n" , + file_kind, match, path1, path2); + } +return 0 ; +} + +/* +* Find duplicate specifications. */ -static int nodups_spec_node (const struct spec_node *node,const char *path) +static int nodups_spec_node (struct selabel_handle *rec, +const struct spec_node *node) {int rc =0 ;
@@ -126,24 +176,10 @@ static int nodups_spec_node(const struct spec_node *node, const char *path) node1->file_kind != node2->file_kind)continue ; - rc =-1 ; - errno =EINVAL ; -if (strcmp (node1->lr.ctx_raw, node2->lr.ctx_raw) !=0 ) { -COMPAT_LOG ( -SELINUX_ERROR , -"%s: Multiple different specifications for %s %s (%s and %s).\n" , - path, -file_kind_to_string (node1->file_kind), - node1->literal_match, node1->lr.ctx_raw, - node2->lr.ctx_raw); - }else { -COMPAT_LOG ( -SELINUX_ERROR , -"%s: Multiple same specifications for %s %s.\n" , - path, -file_kind_to_string (node1->file_kind), - node1->literal_match); - } + rc =report_dups (rec, node1->inputno, node2->inputno, + node1->lr.ctx_raw, node2->lr.ctx_raw, + node1->literal_match, +file_kind_to_string (node1->file_kind)); } }
@@ -168,28 +204,11 @@ static int nodups_spec_node(const struct spec_node *node, const char *path) node1->file_kind != node2->file_kind)continue ; - rc =-1 ; - errno =EINVAL ; -if (strcmp (node1->lr.ctx_raw, - node2->lr.ctx_raw) !=0 ) { -COMPAT_LOG ( -SELINUX_ERROR , -"%s: Multiple different specifications for %s %s (%s and %s).\n" , - path, -file_kind_to_string ( - node1->file_kind), - node1->regex_str, - node1->lr.ctx_raw, - node2->lr.ctx_raw); - }else { -COMPAT_LOG ( -SELINUX_ERROR , -"%s: Multiple same specifications for %s %s.\n" , - path, -file_kind_to_string ( - node1->file_kind), - node1->regex_str); - } + rc =report_dups ( + rec, node1->inputno, node2->inputno, + node1->lr.ctx_raw, node2->lr.ctx_raw, + node1->regex_str, +file_kind_to_string (node1->file_kind)); } } }
@@ -197,7 +216,7 @@ static int nodups_spec_node(const struct spec_node *node, const char *path)for (uint32_t i =0 ; i < node->children_num; i++) {int rc2; - rc2 =nodups_spec_node (&node->children[i], path); + rc2 =nodups_spec_node (rec, &node->children[i]);if (rc2) rc = rc2; }
@@ -1484,22 +1503,27 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, unsigned n) {struct saved_data *data = rec->data; -const char *path =NULL ; +size_t num_paths =0 , i; +char **paths =NULL ;const char *prefix =NULL ; -int status =-1 , baseonly =0 ; +int status =-1 ; +bool baseonly = false, path_provided = false;/* Process arguments */ -while (n) { - n--; -switch (opts[n].type) { + i = n; +while (i--) { +switch (opts[i].type) {case SELABEL_OPT_PATH : - path = opts[n].value; +if (opts[i].value) { + num_paths++; + path_provided = true; + }break ;case SELABEL_OPT_SUBSET : - prefix = opts[n].value; + prefix = opts[i].value;break ;case SELABEL_OPT_BASEONLY : - baseonly = !!opts[n].value; + baseonly = !!opts[i].value;break ;case SELABEL_OPT_UNUSED :case SELABEL_OPT_VALIDATE :
@@ -1511,10 +1535,44 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, } } +/* If no paths were provided, we will use the default path or fail, depending on the target. */ +if (!path_provided) { +#if !defined(BUILD_HOST ) && !defined(ANDROID ) + num_paths =1 ; +#else +selinux_log (SELINUX_ERROR , +"No path given to file labeling backend\n" ); + goto finish; +#endif + } + +/* Allocate the paths */ + paths =calloc (num_paths,sizeof (*paths)); +if (paths ==NULL ) { + goto finish; + } + rec->spec_files = paths; + rec->spec_files_len = num_paths; + +if (path_provided) { +for (i =0 ; i < n; i++) { +switch (opts[i].type) { +case SELABEL_OPT_PATH : + *paths =strdup (opts[i].value); +if (*paths ==NULL ) + goto finish; + paths++; +break ; +default : +break ; + } + } + } +#if !defined(BUILD_HOST ) && !defined(ANDROID )char subs_file[PATH_MAX +1 ];/* Process local and distribution substitution files */ -if (!path) { +if (!path_provided) { status = selabel_subs_init( selinux_file_context_subs_dist_path(), rec->digest, &data->dist_subs, &data->dist_subs_num,
@@ -1526,66 +1584,56 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, &data->subs_num, &data->subs_alloc);if (status) goto finish; - path = selinux_file_context_path(); + rec->spec_files[0 ] =strdup (selinux_file_context_path ()); +if (rec->spec_files[0 ] ==NULL ) + goto finish; } else { -snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , path); +snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , + rec->spec_files[0 ]); status =selabel_subs_init (subs_file, rec->digest, &data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);if (status) goto finish; -snprintf (subs_file,sizeof (subs_file),"%s.subs" , path); +snprintf (subs_file,sizeof (subs_file),"%s.subs" , + rec->spec_files[0 ]); status =selabel_subs_init (subs_file, rec->digest, &data->subs, &data->subs_num, &data->subs_alloc);if (status) goto finish; } -#endif -if (!path) { - errno =EINVAL ; - goto finish; - } - - rec->spec_files =calloc (1 ,sizeof (path)); -if (!rec->spec_files) - goto finish; - rec->spec_files[0 ] =strdup (path); -if (!rec->spec_files[0 ]) - goto finish; - rec->spec_files_len =1 ; -/* -* The do detailed validation of the input and fill the spec array +* Process each input file. */ - status =process_file (path,NULL , rec, prefix, rec->digest,0 ); -if (status) - goto finish; - -if (rec->validating) { -sort_specs (data); - - status =nodups_spec_node (data->root, path); +for (i =0 ; i < num_paths; i++) { + status =process_file (rec->spec_files[i],NULL , rec, prefix, + rec->digest, i);if (status) goto finish; }if (!baseonly) { - status =process_file (path,"homedirs" , rec, prefix, - rec->digest,1 ); + status =process_file (rec->spec_files[0 ],"homedirs" , rec, + prefix, rec->digest, num_paths +1 );if (status && errno !=ENOENT ) goto finish; - status =process_file (path,"local" , rec, prefix, rec->digest, -2 ); + status =process_file (rec->spec_files[0 ],"local" , rec, prefix, + rec->digest, num_paths +2 );if (status && errno !=ENOENT ) goto finish; } -if (!rec->validating || !baseonly) -sort_specs (data); +sort_specs (data); + +if (rec->validating) { + status =nodups_spec_node (rec, data->root); +if (status) + goto finish; + }digest_gen_hash (rec->digest);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
Stephen Smalley
maintainerRe: [PATCH 2/2] libselinux: support multiple contexts for the file backend
Quoted 44 lines · Thiébaud Weksteen
Update the file labeling backend to support specifying multiple
SELABEL_OPT_PATHoptions inselabel_open().All provided paths are stored in
rec->spec_filesand processed during initialization. Derived files, such as substitutions (.subs, .subs_dist) and auxiliary contexts (.homedirs, .local), continue to be based on the first path (or defaultselinux_file_context_path()) when enabled.Duplicate checking logic is refactored into a helper function
report_dups(). A duplicate with the same specification result is not fatal anymore, but a warning is still logged.Additionally, duplicate validation is moved after all files (including .homedirs and .local) are loaded and sorted. For single file context setups, the only difference in behavior is that duplicate validation now extends to fc.homedirs and fc.local.
A similar multiple files setup has been used in Android for 9+ years.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/label_file.c | 216 ++++++++++++++++++++++-------------- 1 file changed, 132 insertions(+), 84 deletions(-)libselinux/src/label_file.c +4
-4@@ -1484,22 +1503,27 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, unsigned n) {struct saved_data *data = rec->data; -const char *path =NULL ; +size_t num_paths =0 , i; +char **paths =NULL ;const char *prefix =NULL ; -int status =-1 , baseonly =0 ; +int status =-1 ; +bool baseonly = false, path_provided = false;/* Process arguments */ -while (n) { - n--;
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1520
Quoted 10 lines
libselinux/src/label_file.c +7
-2-switch (opts[n].type) { + i = n; +while (i--) { +switch (opts[i].type) {case SELABEL_OPT_PATH : - path = opts[n].value; +if (opts[i].value) { + num_paths++; + path_provided = true; + }
Here we count only non-NULL values, counting from the end.
Quoted 14 lines
libselinux/src/label_file.c +2
-2break ;case SELABEL_OPT_SUBSET : - prefix = opts[n].value; + prefix = opts[i].value;break ;case SELABEL_OPT_BASEONLY : - baseonly = !!opts[n].value; + baseonly = !!opts[i].value;break ;case SELABEL_OPT_UNUSED :case SELABEL_OPT_VALIDATE :@@ -1511,10 +1535,44 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, } }
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1546
Quoted 10 lines
libselinux/src/label_file.c +9
-0+/* If no paths were provided, we will use the default path or fail, depending on the target. */ +if (!path_provided) { +#if !defined(BUILD_HOST ) && !defined(ANDROID ) + num_paths =1 ; +#else +selinux_log (SELINUX_ERROR , +"No path given to file labeling backend\n" ); + goto finish; +#endif
Do we really need/want differing behavior here for different build configs? Effectively it means a different user-visible API?
Quoted 9 lines
libselinux/src/label_file.c +9
-0+ } + +/* Allocate the paths */ + paths =calloc (num_paths,sizeof (*paths)); +if (paths ==NULL ) { + goto finish; + } + rec->spec_files = paths; + rec->spec_files_len = num_paths;
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1565
Quoted 10 lines
libselinux/src/label_file.c +10
-0+ +if (path_provided) { +for (i =0 ; i < n; i++) { +switch (opts[i].type) { +case SELABEL_OPT_PATH : + *paths =strdup (opts[i].value); +if (*paths ==NULL ) + goto finish; + paths++; +break ;
Here we would end up calling strdup(NULL) if any of the opts[i].value are NULL instead of skipping them as above.
Quoted 106 lines
libselinux/src/label_file.c +29
-34+default : +break ; + } + } + } +#if !defined(BUILD_HOST ) && !defined(ANDROID )char subs_file[PATH_MAX +1 ];/* Process local and distribution substitution files */ -if (!path) { +if (!path_provided) { status = selabel_subs_init( selinux_file_context_subs_dist_path(), rec->digest, &data->dist_subs, &data->dist_subs_num,@@ -1526,66 +1584,56 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, &data->subs_num, &data->subs_alloc);if (status) goto finish; - path = selinux_file_context_path(); + rec->spec_files[0 ] =strdup (selinux_file_context_path ()); +if (rec->spec_files[0 ] ==NULL ) + goto finish; } else { -snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , path); +snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , + rec->spec_files[0 ]); status =selabel_subs_init (subs_file, rec->digest, &data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);if (status) goto finish; -snprintf (subs_file,sizeof (subs_file),"%s.subs" , path); +snprintf (subs_file,sizeof (subs_file),"%s.subs" , + rec->spec_files[0 ]); status =selabel_subs_init (subs_file, rec->digest, &data->subs, &data->subs_num, &data->subs_alloc);if (status) goto finish; } -#endif -if (!path) { - errno =EINVAL ; - goto finish; - } - - rec->spec_files =calloc (1 ,sizeof (path)); -if (!rec->spec_files) - goto finish; - rec->spec_files[0 ] =strdup (path); -if (!rec->spec_files[0 ]) - goto finish; - rec->spec_files_len =1 ; -/* -* The do detailed validation of the input and fill the spec array +* Process each input file. */ - status =process_file (path,NULL , rec, prefix, rec->digest,0 ); -if (status) - goto finish; - -if (rec->validating) { -sort_specs (data); - - status =nodups_spec_node (data->root, path); +for (i =0 ; i < num_paths; i++) { + status =process_file (rec->spec_files[i],NULL , rec, prefix, + rec->digest, i);if (status) goto finish; }if (!baseonly) { - status =process_file (path,"homedirs" , rec, prefix, - rec->digest,1 ); + status =process_file (rec->spec_files[0 ],"homedirs" , rec, + prefix, rec->digest, num_paths +1 );if (status && errno !=ENOENT ) goto finish; - status =process_file (path,"local" , rec, prefix, rec->digest, -2 ); + status =process_file (rec->spec_files[0 ],"local" , rec, prefix, + rec->digest, num_paths +2 );if (status && errno !=ENOENT ) goto finish; } -if (!rec->validating || !baseonly) -sort_specs (data); +sort_specs (data); + +if (rec->validating) { + status =nodups_spec_node (rec, data->root); +if (status) + goto finish; + }digest_gen_hash (rec->digest); --2.55.0.229.g6434b31f56-goog
Stephen Smalley
maintainerRe: [PATCH 2/2] libselinux: support multiple contexts for the file backend
In reply to Thiébaud WeksteenQuoted 76 lines · Thiébaud Weksteen
Update the file labeling backend to support specifying multiple
SELABEL_OPT_PATHoptions inselabel_open().All provided paths are stored in
rec->spec_filesand processed during initialization. Derived files, such as substitutions (.subs, .subs_dist) and auxiliary contexts (.homedirs, .local), continue to be based on the first path (or defaultselinux_file_context_path()) when enabled.Duplicate checking logic is refactored into a helper function
report_dups(). A duplicate with the same specification result is not fatal anymore, but a warning is still logged.Additionally, duplicate validation is moved after all files (including .homedirs and .local) are loaded and sorted. For single file context setups, the only difference in behavior is that duplicate validation now extends to fc.homedirs and fc.local.
A similar multiple files setup has been used in Android for 9+ years.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/label_file.c | 216 ++++++++++++++++++++++-------------- 1 file changed, 132 insertions(+), 84 deletions(-)libselinux/src/label_file.c +8
-19@@ -1526,66 +1584,56 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, &data->subs_num, &data->subs_alloc);if (status) goto finish; - path = selinux_file_context_path(); + rec->spec_files[0 ] =strdup (selinux_file_context_path ()); +if (rec->spec_files[0 ] ==NULL ) + goto finish; }else { -snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , path); +snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , + rec->spec_files[0 ]); status =selabel_subs_init (subs_file, rec->digest, &data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);if (status) goto finish; -snprintf (subs_file,sizeof (subs_file),"%s.subs" , path); +snprintf (subs_file,sizeof (subs_file),"%s.subs" , + rec->spec_files[0 ]); status =selabel_subs_init (subs_file, rec->digest, &data->subs, &data->subs_num, &data->subs_alloc);if (status) goto finish; } -#endif -if (!path) { - errno =EINVAL ; - goto finish; - } - - rec->spec_files =calloc (1 ,sizeof (path)); -if (!rec->spec_files) - goto finish; - rec->spec_files[0 ] =strdup (path); -if (!rec->spec_files[0 ]) - goto finish; - rec->spec_files_len =1 ; -/* -* The do detailed validation of the input and fill the spec array +* Process each input file. */ - status =process_file (path,NULL , rec, prefix, rec->digest,0 );
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1613
Quoted 10 lines
libselinux/src/label_file.c +3
-7-if (status) - goto finish; - -if (rec->validating) { -sort_specs (data); - - status =nodups_spec_node (data->root, path); +for (i =0 ; i < num_paths; i++) { + status =process_file (rec->spec_files[i],NULL , rec, prefix, + rec->digest, i);
process_file() last argument is uint8_t; i and num_paths are size_t. Need to cap the max num_paths somewhere or change the type.
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1620
Quoted 9 lines
libselinux/src/label_file.c +2
-2if (status) goto finish; }if (!baseonly) { - status =process_file (path,"homedirs" , rec, prefix, - rec->digest,1 ); + status =process_file (rec->spec_files[0 ],"homedirs" , rec, + prefix, rec->digest, num_paths +1 );
Ditto.
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1625
Quoted 7 lines
libselinux/src/label_file.c +2
-2if (status && errno !=ENOENT ) goto finish; - status =process_file (path,"local" , rec, prefix, rec->digest, -2 ); + status =process_file (rec->spec_files[0 ],"local" , rec, prefix, + rec->digest, num_paths +2 );
Ditto
Quoted 19 lines
libselinux/src/label_file.c +7
-3if (status && errno !=ENOENT ) goto finish; } -if (!rec->validating || !baseonly) -sort_specs (data); +sort_specs (data); + +if (rec->validating) { + status =nodups_spec_node (rec, data->root); +if (status) + goto finish; + }digest_gen_hash (rec->digest); --2.55.0.229.g6434b31f56-goog
Stephen Smalley
maintainerRe: [PATCH 2/2] libselinux: support multiple contexts for the file backend
Quoted 96 lines · Stephen Smalley
Quoted 76 lines · Thiébaud Weksteen
Update the file labeling backend to support specifying multiple
SELABEL_OPT_PATHoptions inselabel_open().All provided paths are stored in
rec->spec_filesand processed during initialization. Derived files, such as substitutions (.subs, .subs_dist) and auxiliary contexts (.homedirs, .local), continue to be based on the first path (or defaultselinux_file_context_path()) when enabled.Duplicate checking logic is refactored into a helper function
report_dups(). A duplicate with the same specification result is not fatal anymore, but a warning is still logged.Additionally, duplicate validation is moved after all files (including .homedirs and .local) are loaded and sorted. For single file context setups, the only difference in behavior is that duplicate validation now extends to fc.homedirs and fc.local.
A similar multiple files setup has been used in Android for 9+ years.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/label_file.c | 216 ++++++++++++++++++++++-------------- 1 file changed, 132 insertions(+), 84 deletions(-)libselinux/src/label_file.c +8
-19@@ -1526,66 +1584,56 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, &data->subs_num, &data->subs_alloc);if (status) goto finish; - path = selinux_file_context_path(); + rec->spec_files[0 ] =strdup (selinux_file_context_path ()); +if (rec->spec_files[0 ] ==NULL ) + goto finish; }else { -snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , path); +snprintf (subs_file,sizeof (subs_file),"%s.subs_dist" , + rec->spec_files[0 ]); status =selabel_subs_init (subs_file, rec->digest, &data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);if (status) goto finish; -snprintf (subs_file,sizeof (subs_file),"%s.subs" , path); +snprintf (subs_file,sizeof (subs_file),"%s.subs" , + rec->spec_files[0 ]); status =selabel_subs_init (subs_file, rec->digest, &data->subs, &data->subs_num, &data->subs_alloc);if (status) goto finish; } -#endif -if (!path) { - errno =EINVAL ; - goto finish; - } - - rec->spec_files =calloc (1 ,sizeof (path)); -if (!rec->spec_files) - goto finish; - rec->spec_files[0 ] =strdup (path); -if (!rec->spec_files[0 ]) - goto finish; - rec->spec_files_len =1 ; -/* -* The do detailed validation of the input and fill the spec array +* Process each input file. */ - status =process_file (path,NULL , rec, prefix, rec->digest,0 );libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1613
Quoted 10 lines
libselinux/src/label_file.c +3
-7-if (status) - goto finish; - -if (rec->validating) { -sort_specs (data); - - status =nodups_spec_node (data->root, path); +for (i =0 ; i < num_paths; i++) { + status =process_file (rec->spec_files[i],NULL , rec, prefix, + rec->digest, i);
process_file()last argument isuint8_t; i andnum_pathsaresize_t. Need to cap the maxnum_pathssomewhere or change the type.Quoted 4 lines
libselinux/src/label_file.c +0
-0if (status) goto finish; }
Quoted 7 lines
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1620
Quoted 5 lines
libselinux/src/label_file.c +2
-2if (!baseonly) { - status =process_file (path,"homedirs" , rec, prefix, - rec->digest,1 ); + status =process_file (rec->spec_files[0 ],"homedirs" , rec, + prefix, rec->digest, num_paths +1 );Ditto.
Also, could use num_paths here since i ends at num_paths - 1.
Quoted 10 lines
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1625
Quoted 8 lines
libselinux/src/label_file.c +2
-2if (status && errno !=ENOENT ) goto finish; - status =process_file (path,"local" , rec, prefix, rec->digest, -2 ); + status =process_file (rec->spec_files[0 ],"local" , rec, prefix, + rec->digest, num_paths +2 );Ditto
And this could be num_path + 1.
Quoted 20 lines
Quoted 20 lines
libselinux/src/label_file.c +7
-3if (status && errno !=ENOENT ) goto finish; } -if (!rec->validating || !baseonly) -sort_specs (data); +sort_specs (data); + +if (rec->validating) { + status =nodups_spec_node (rec, data->root); +if (status) + goto finish; + }digest_gen_hash (rec->digest); --2.55.0.229.g6434b31f56-goog
Stephen Smalley
maintainerRe: [PATCH 2/2] libselinux: support multiple contexts for the file backend
In reply to Thiébaud WeksteenQuoted 50 lines · Thiébaud Weksteen
Update the file labeling backend to support specifying multiple
SELABEL_OPT_PATHoptions inselabel_open().All provided paths are stored in
rec->spec_filesand processed during initialization. Derived files, such as substitutions (.subs, .subs_dist) and auxiliary contexts (.homedirs, .local), continue to be based on the first path (or defaultselinux_file_context_path()) when enabled.Duplicate checking logic is refactored into a helper function
report_dups(). A duplicate with the same specification result is not fatal anymore, but a warning is still logged.Additionally, duplicate validation is moved after all files (including .homedirs and .local) are loaded and sorted. For single file context setups, the only difference in behavior is that duplicate validation now extends to fc.homedirs and fc.local.
A similar multiple files setup has been used in Android for 9+ years.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/label_file.c | 216 ++++++++++++++++++++++-------------- 1 file changed, 132 insertions(+), 84 deletions(-)libselinux/src/label_file.c +0
-17@@ -168,28 +204,11 @@ static int nodups_spec_node(const struct spec_node *node, const char *path) node1->file_kind != node2->file_kind)continue ; - rc =-1 ; - errno =EINVAL ; -if (strcmp (node1->lr.ctx_raw, - node2->lr.ctx_raw) !=0 ) { -COMPAT_LOG ( -SELINUX_ERROR , -"%s: Multiple different specifications for %s %s (%s and %s).\n" , - path, -file_kind_to_string ( - node1->file_kind), - node1->regex_str, - node1->lr.ctx_raw, - node2->lr.ctx_raw); - }else { - COMPAT_LOG( - SELINUX_ERROR, - "%s: Multiple same specifications for %s %s.\n",
libselinux/src/label_file.c static int nodups_spec_node(const struct spec_node *node, const char *path) · line 211
Quoted 10 lines
libselinux/src/label_file.c +5
-5- path, -file_kind_to_string ( - node1->file_kind), - node1->regex_str); - } + rc =report_dups ( + rec, node1->inputno, node2->inputno, + node1->lr.ctx_raw, node2->lr.ctx_raw, + node1->regex_str, +file_kind_to_string (node1->file_kind));
This will clobber prior errors; need to preserve those.
Quoted 4 lines
libselinux/src/label_file.c +0
-0} } }
Stephen Smalley
maintainerRe: [PATCH 2/2] libselinux: support multiple contexts for the file backend
In reply to Thiébaud WeksteenQuoted 9 lines · Thiébaud Weksteen
Update the file labeling backend to support specifying multiple
SELABEL_OPT_PATHoptions inselabel_open().All provided paths are stored in
rec->spec_filesand processed during initialization. Derived files, such as substitutions (.subs, .subs_dist) and auxiliary contexts (.homedirs, .local), continue to be based on the first path (or defaultselinux_file_context_path()) when enabled.
Quoted 8 lines
Duplicate checking logic is refactored into a helper function
report_dups(). A duplicate with the same specification result is not fatal anymore, but a warning is still logged.Additionally, duplicate validation is moved after all files (including .homedirs and .local) are loaded and sorted. For single file context setups, the only difference in behavior is that duplicate validation now extends to fc.homedirs and fc.local.
Not sure if this change in behavior is desired - it seemingly precludes .local from intentionally overriding an entry's context. Defer to distro maintainers.
Quoted 21 lines
A similar multiple files setup has been used in Android for 9+ years.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/label_file.c | 216 ++++++++++++++++++++++-------------- 1 file changed, 132 insertions(+), 84 deletions(-)libselinux/src/label_file.c +4
-1@@ -104,9 +104,59 @@ void sort_spec_node(struct spec_node *node, struct spec_node *parent) }/* -* Warn about duplicate specifications. +* Report a duplicate in the log. +* +* If the duplicate is considered fatal (i.e., if the specifications of both +* entries do not match), return -1 and set errno to EINVAL.
libselinux/src/label_file.c void sort_spec_node(struct spec_node *node, struct spec_node *parent) · line 120
Quoted 10 lines
libselinux/src/label_file.c +10
-0+*/ +static int report_dups (struct selabel_handle *rec,uint8_t inputno1, +uint8_t inputno2,const char *ctx_raw1, +const char *ctx_raw2,const char *match, +const char *file_kind) +{ +if (inputno1 >= rec->spec_files_len) + inputno1 =0 ; +if (inputno2 >= rec->spec_files_len) + inputno2 =0 ;
Since you are intentionally passing in num_paths + 1 and num_paths + 2 for the homedirs and local files, this will incorrectly attribute any duplicates to the wrong file. Not sure we want cross-file duplicate checking anyway.
Re: [PATCH 2/2] libselinux: support multiple contexts for the file backend
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · Thiébaud Weksteen
Update the file labeling backend to support specifying multiple
SELABEL_OPT_PATHoptions inselabel_open().All provided paths are stored in
rec->spec_filesand processed during initialization. Derived files, such as substitutions (.subs, .subs_dist) and auxiliary contexts (.homedirs, .local), continue to be based on the first path (or defaultselinux_file_context_path()) when enabled.Duplicate checking logic is refactored into a helper function
report_dups(). A duplicate with the same specification result is not fatal anymore, but a warning is still logged.
Quoted 13 lines
Quoted 4 lines
Additionally, duplicate validation is moved after all files (including .homedirs and .local) are loaded and sorted. For single file context setups, the only difference in behavior is that duplicate validation now extends to fc.homedirs and fc.local.
Not sure if this change in behavior is desired - it seemingly precludes .local from intentionally overriding an entry's context. Defer to distro maintainers.
Quoted 4 lines
A similar multiple files setup has been used in Android for 9+ years.
Signed-off-by Thiébaud Weksteen <[email protected]>
Thanks for the quick review Stephen. Overall I agree with all your feedback and will send an updated patch.
Quoted 11 lines
libselinux/src/label_file.c static int init(struct selabel_handle *rec, const struct selinux_opt *opts, · line 1546
Quoted 9 lines
libselinux/src/label_file.c +9
-0+/* If no paths were provided, we will use the default path or fail, depending on the target. */ +if (!path_provided) { +#if !defined(BUILD_HOST ) && !defined(ANDROID ) + num_paths =1 ; +#else +selinux_log (SELINUX_ERROR , +"No path given to file labeling backend\n" ); + goto finish; +#endif Do we really need/want differing behavior here for different build configs? Effectively it means a different user-visible API?
If I understand correctly, this difference already exists. If ANDROID is defined and if no SELABEL_OPT_PATH were supplied, path is NULL and selinux_file_context_path() is never called; which means that if (!path) below will be executed and return EINVAL (I should have kept that return value in my change).
More generally, we had to implement a layer on top of selabel_open for Android-specific file_contexts locations [1]. I am wondering if we could improve the design and abstract any standard-distro details (e.g., homedirs). Fundamentally, selabel_open for file_contexts would
either: use the file paths provided as argument; or otherwise use the "platform default". Right now, "platform default" means (selinux_file_context_path + homedirs + local + subs). What if we define a callback that returns a set of files to use and optional subs? We could keep any Android-specific changes in our own "platform default" implementation (which would be behind a build configuration option).
Quoted 17 lines
Quoted 7 lines
/*
- * Warn about duplicate specifications.
+ * Report a duplicate in the log.
+ *
+ * If the duplicate is considered fatal (i.e., if the specifications of both
+ * entries do not match), return -1 and set errno to EINVAL.
+ */libselinux/src/label_file.c void sort_spec_node(struct spec_node *node, struct spec_node *parent) · line 120
Quoted 9 lines
libselinux/src/label_file.c +9
-0+static int report_dups (struct selabel_handle *rec,uint8_t inputno1, +uint8_t inputno2,const char *ctx_raw1, +const char *ctx_raw2,const char *match, +const char *file_kind) +{ +if (inputno1 >= rec->spec_files_len) + inputno1 =0 ; +if (inputno2 >= rec->spec_files_len) + inputno2 =0 ;
Quoted 7 lines
Not sure if this change in behavior is desired - it seemingly precludes .local from intentionally overriding an entry's context. Defer to distro maintainers.
Since you are intentionally passing innum_paths+ 1 andnum_paths+ 2 for the homedirs and local files, this will incorrectly attribute any duplicates to the wrong file. Not sure we want cross-file duplicate checking anyway.
Thanks, I wasn't sure about this as I don't know how these files are used in other distributions. I agree, keeping the existing behaviour is better.
Stephen Smalley
maintainerQuoted 58 lines · Thiébaud Weksteen
Update the existing backends to potentially allocate multiple
spec_files. This commit simply adds a level of indirection but does not change any current behaviour. It will facilitate the gradual migration of backends to multiple context files.In
selabel_fini(),compat_validate()is updated to use the firstspec_fileonly, for legacy validation error reporting.selabel_open()is already performing context validation if requested against individual file paths (seeinsert_spec).Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/label.c | 10 ++++++++-- libselinux/src/label_db.c | 11 +++++++++-- libselinux/src/label_file.c | 8 ++++++-- libselinux/src/label_internal.h | 5 +++-- libselinux/src/label_media.c | 8 +++++++- libselinux/src/label_x.c | 8 +++++++- 6 files changed, 40 insertions(+), 10 deletions(-)libselinux/src/label.c +8
-2@@ -162,7 +162,7 @@ static int selabel_fini(const struct selabel_handle *rec,char *ctx_trans;int rc; -if (compat_validate (rec, lr, rec->spec_file, lr->lineno)) +if (compat_validate (rec, lr, rec->spec_files[0 ], lr->lineno))return -1 ;if (!translating)@@ -398,11 +398,17 @@ int selabel_digest(struct selabel_handle *rec, unsigned char **digest,void selabel_close (struct selabel_handle *rec) { +size_t i; +if (rec->digest)selabel_digest_fini (rec->digest);if (rec->func_close) rec->func_close (rec); -free (rec->spec_file); +if (rec->spec_files) { +for (i =0 ; i < rec->spec_files_len; i++) +free (rec->spec_files[i]); +free (rec->spec_files); + }free (rec); }libselinux/src/label_db.c +0
-0
libselinux/src/label_db.c static catalog_t *db_init(const struct selinux_opt *opts, unsigned nopts, · line 305
Quoted 6 lines
libselinux/src/label_db.c +1
-2@@ -302,12 +302,19 @@ static catalog_t *db_init(const struct selinux_opt *opts, unsigned nopts, errno =EINVAL ;return NULL ; } - rec->spec_file =strdup (path); -if (!rec->spec_file) { + rec->spec_files =calloc (1 ,sizeof (path));
Here and below, any particular reason you didn't use sizeof(*rec->spec_files) instead of sizeof(path)? It's not wrong, just not what I usually see.
Quoted 94 lines
libselinux/src/label_db.c +8
-0+if (!rec->spec_files) {free (catalog);fclose (filp);return NULL ; } + rec->spec_files[0 ] =strdup (path); +if (!rec->spec_files[0 ]) { +free (catalog); +fclose (filp); +return NULL ; + } + rec->spec_files_len =1 ;/* * Parse for each lines libselinux/src/label_file.c +6
-2@@ -1549,9 +1549,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, goto finish; } - rec->spec_file =strdup (path); -if (!rec->spec_file) + rec->spec_files =calloc (1 ,sizeof (path)); +if (!rec->spec_files) goto finish; + rec->spec_files[0 ] =strdup (path); +if (!rec->spec_files[0 ]) + goto finish; + rec->spec_files_len =1 ;/* * The do detailed validation of the input and fill the spec array libselinux/src/label_internal.h +3
-2@@ -96,10 +96,11 @@ struct selabel_handle {void *data;/* -* The main spec file used. Note for file contexts the local and/or +* The spec files used. Note for file contexts the local and/or * homedirs could also have been used to resolve a context. */ -char *spec_file; +size_t spec_files_len; +char **spec_files;/* ptr to SHA1 hash information if SELABEL_OPT_DIGEST set */ struct selabel_digest *digest;libselinux/src/label_media.c +7
-1@@ -109,7 +109,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, errno =EINVAL ; goto finish; } - rec->spec_file =strdup (path); + rec->spec_files =calloc (1 ,sizeof (path)); +if (!rec->spec_files) + goto finish; + rec->spec_files[0 ] =strdup (path); +if (!rec->spec_files[0 ]) + goto finish; + rec->spec_files_len =1 ;/* * Perform two passes over the specification file. libselinux/src/label_x.c +7
-2@@ -139,7 +139,13 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, errno =EINVAL ; goto finish; } - rec->spec_file =strdup (path); + rec->spec_files =calloc (1 ,sizeof (path)); +if (!rec->spec_files) + goto finish; + rec->spec_files[0 ] =strdup (path); +if (!rec->spec_files[0 ]) + goto finish; + rec->spec_files_len =1 ;/* * Perform two passes over the specification file. -- 2.55.0.229.g6434b31f56-goog
Quoted 16 lines · Stephen Smalley
Quoted 13 lines · Thiébaud Weksteen
Update the existing backends to potentially allocate multiple
spec_files. This commit simply adds a level of indirection but does not change any current behaviour. It will facilitate the gradual migration of backends to multiple context files.In
selabel_fini(),compat_validate()is updated to use the firstspec_fileonly, for legacy validation error reporting.selabel_open()is already performing context validation if requested against individual file paths (seeinsert_spec).Signed-off-by Thiébaud Weksteen <[email protected]>---
Quoted 8 lines
libselinux/src/label_db.c static catalog_t *db_init(const struct selinux_opt *opts, unsigned nopts, · line 305
Quoted 3 lines
libselinux/src/label_db.c +1
-2- rec->spec_file =strdup (path); -if (!rec->spec_file) { + rec->spec_files =calloc (1 ,sizeof (path));Here and below, any particular reason you didn't use
sizeof(*rec->spec_files)instead of sizeof(path)? It's not wrong, just not what I usually see.
No particular reason. I'll update this in the next patch version. Thanks for the review Stephen.