libselinux: add a configuration-root search list
Stephen Smalley
maintainercover libselinux: add a configuration-root search list
This series is the first half of addressing
https://github.com/SELinuxProject/selinux/issues/33
It adds a configuration-root search list to libselinux so that it searches an ordered list of configuration roots for each configuration file. The list can be overridden at build time via -DSELINUXFALLBACKDIRS=. There is no change in behavior as long as the files are found in their default locations under /etc/selinux; the fallbacks are only used when those files are missing, e.g. upon a factory reset.
The second half of this series will address libsemanage but is not necessary to merge this half.
Stephen Smalley (4):
libselinux: add a configuration-root search list
libselinux: search for the binary policy across configuration roots
libselinux: fall back through configuration roots
libselinux: search file_contexts files across configuration roots
libselinux/src/booleans.c | 2 +- libselinux/src/get_context_list.c | 6 +- libselinux/src/get_default_type.c | 5 +- libselinux/src/init.c | 12 +- libselinux/src/label_db.c | 2 +- libselinux/src/label_file.c | 50 +++++++-- libselinux/src/label_media.c | 2 +- libselinux/src/label_x.c | 2 +- libselinux/src/load_policy.c | 24 ++-- libselinux/src/matchmediacon.c | 2 +- libselinux/src/selinux_config.c | 176 ++++++++++++++++++++++++++---- libselinux/src/selinux_internal.h | 40 +++++++ libselinux/src/seusers.c | 4 +- 13 files changed, 280 insertions(+), 47 deletions(-)
Signature · 1 line
2.55.0
Stephen Smalley
maintainerPATCH 1/4 libselinux: add a configuration-root search list
Currently every runtime file libselinux consumes -- the config file, policy.NN, contexts/*, seusers, etc -- is looked up under a single hard-coded root, SELINUXDIR = "/etc/selinux". On a hermetic-/usr or factory-reset system where /etc is empty at boot, neither is_selinux_enabled() nor selinux_mkload_policy() find anything and the box comes up with SELinux disabled.
Introduce an ordered list of configuration roots:
/etc/selinux /run/selinux /usr/lib/selinux
(where the tail is overriddable at build time via -DSELINUXFALLBACKDIRS=...) and the internal helpers to search it:
selinux_confdirs[] - the raw root list, for constructor use selinux_policy_roots() - <root>/<SELINUXTYPE> for each root selinux_policy_fopen(), selinux_policy_open() - fopen() the accessor
path, remapping the selinux_policy_root() prefix through the fallback roots on ENOENT.
The existing selinux_*_path() accessors continue to return the primary (/etc-based) path unconditionally so that callers doing pointer arithmetic against selinux_policy_root() - notably semanage_init_final_suffix() - keep working. The search is applied where the file is actually opened; those call sites are converted in the following patches.
selinux_getenforcemode(), init_selinux_config() and the constructor has_selinux_config check now try each root in turn for the config file. selinux_set_policy_root() populates only [0], disabling the fallback search when an explicit root is in effect.
--- libselinux/src/init.c | 12 ++- libselinux/src/selinux_config.c | 121 ++++++++++++++++++++++++++++-- libselinux/src/selinux_internal.h | 30 ++++++++ 3 files changed, 155 insertions(+), 8 deletions(-)
libselinux/src/init.c +11 -1
@@ -103,6 +103,10 @@ static void init_lib(void) __attribute__((constructor));static void init_lib (void ) {long page_size; +#ifndef ANDROID +unsigned int i; +char cfg[PATH_MAX ]; +#endif SELINUX_PROTECT_ERRNO ; page_size =sysconf (_SC_PAGE_SIZE);
@@ -112,7 +116,13 @@ static void init_lib(void)4096 ;init_selinuxmnt ();#ifndef ANDROID - has_selinux_config = (access (SELINUXCONFIG ,F_OK ) ==0 ); +for (i =0 ; selinux_confdirs[i]; i++) { +snprintf (cfg,sizeof (cfg),"%sconfig" , selinux_confdirs[i]); +if (access (cfg,F_OK ) ==0 ) { + has_selinux_config =1 ; +break ; + } + }#endif }
libselinux/src/selinux_config.c +114 -7
172 lines not on this page · raw message
libselinux/src/selinux_internal.h +30 -0
@@ -92,6 +92,36 @@ extern size_t selinux_page_size;#define SELINUXDIR "/etc/selinux/"#define SELINUXCONFIG SELINUXDIR "config" +/* +* Ordered list of configuraiton roots, each with a trailing slash. +* SELINUXDIR is always first: it is the write target and the value the +* existing selinux_*_path() accessors return. The remaining entries are +* read-only fallbacks for hermetic-/usr / factory-reset systems where +* /etc and /var may be empty at boot. Distributions can override the +* fallback list at build time. +*/ +#ifndef SELINUXFALLBACKDIRS +#define SELINUXFALLBACKDIRS "/run/selinux/", "/usr/lib/selinux" +#endif + +extern const char *const selinux_confdirs[]; + +/* +* NULL-terminated list of <confdir><SELINUXTYPE> roots, populated by +* init_selinux_config(); [0] == selinux_policy_root(). Empty (only the +* NULL terminator) after selinux_set_policy_root() so that callers fall +* back to the single overridden root. Internal only. +*/ +const char *const *selinux_policy_roots (void ); + +/* +* Open @path (as returned by one of the selinux_*_path() accessors), +* falling back through selinux_policy_roots() when the primary path +* yields ENOENT. Internal-only. +*/ +FILE *selinux_policy_fopen (const char *path,const char *mode); +int selinux_policy_open (const char *path,int flags); +extern int has_selinux_config;#ifndef HAVE_STRLCPY
Signature · 1 line
2.55.0
Stephen Smalley
maintainerPATCH 2/4 libselinux: search for the binary policy across configuration roots
In reply to Stephen Smalleyselinux_mkload_policy() and selinux_current_policy_path() now try each configuration root at a given policy version before falling back to an older version. An admin-supplied /etc/selinux/<type>/policy/policy.N therefore shadows a vendor /usr/lib copy at the same version, but a newer vendor policy is still preferred over an older admin one - the same behavior a single-root system already has when the running kernel supports a higher version than the on-disk policy.
The load_policy loop now records the actual path opened so that later diagnostics name the file that was used, not the /etc path it was searched under.
--- libselinux/src/load_policy.c | 24 ++++++++++++++++-------- libselinux/src/selinux_config.c | 25 +++++++++++++------------ 2 files changed, 29 insertions(+), 20 deletions(-)
libselinux/src/load_policy.c +16 -8
@@ -133,14 +133,22 @@ int selinux_mkload_policy(int preservebools __attribute__((unused))) vers = maxvers; search: -snprintf (path,sizeof (path),"%s.%d" ,selinux_binary_policy_path (), - vers); - fd =open (path,O_RDONLY |O_CLOEXEC ); -while (fd <0 && errno ==ENOENT && --vers >= minvers) { -/* Check prior versions to see if old policy is available */ -snprintf (path,sizeof (path),"%s.%d" , -selinux_binary_policy_path (), vers); - fd =open (path,O_RDONLY |O_CLOEXEC ); +/* +* For each version, try each policy root in turn: an admin-supplied +* /etc copy at version N shadows a vendor /usr/lib copy at the same +* version, but a newer vendor policy still beats an older admin one. +*/ + fd =-1 ; + errno =ENOENT ; +for (; fd <0 && errno ==ENOENT && vers >= minvers; vers--) { +const char *const *roots =selinux_policy_roots (); +for (; *roots; roots++) { +snprintf (path,sizeof (path),"%s/policy/policy.%d" , + *roots, vers); + fd =open (path,O_RDONLY |O_CLOEXEC ); +if (fd >=0 || errno !=ENOENT ) +break ; + } }if (fd <0 ) { selinux_log(
libselinux/src/selinux_config.c +13 -12
@@ -368,8 +368,8 @@ const char *selinux_binary_policy_path(void)const char *selinux_current_policy_path (void ) { -int rc =0 ; -int vers =0 ; +int vers; +unsigned int i;static char policy_path[PATH_MAX ];if (selinux_mnt) {
@@ -379,16 +379,17 @@ const char *selinux_current_policy_path(void)return policy_path; } } - vers =security_policyvers (); -do { -/* Check prior versions to see if old policy is available */ -snprintf (policy_path,sizeof (policy_path),"%s.%d" , -selinux_binary_policy_path (), vers); - }while ((rc =access (policy_path,F_OK )) && --vers >0 ); - -if (rc) -return NULL ; -return policy_path; +__selinux_once (once, init_selinux_config); +for (vers =security_policyvers (); vers >0 ; vers--) { +for (i =0 ; selinux_policyroots[i]; i++) { +snprintf (policy_path,sizeof (policy_path), +"%s/policy/policy.%d" , selinux_policyroots[i], + vers); +if (access (policy_path,F_OK ) ==0 ) +return policy_path; + } + } +return NULL ; }const char *selinux_file_context_path (void )
Signature · 1 line
2.55.0
Stephen Smalley
maintainerPATCH 3/4 libselinux: fall back through configuration roots
In reply to Stephen SmalleyConvert the internal readers of selinux_*_path() accessors from fopen() to selinux_policy_fopen() so that a missing file in /etc/selinux/<type> is retried under the other configuration roots.
selinux_policy_fopen() only remaps a path that starts with selinux_policy_root() so a caller-supplied path via SELABEL_OPT_PATH in the media/x/db backends still open the requested path.
The label_file backend does its own text/.bin selection in open_file() and cannot go through selinux_policy_fopen(); it is handled in a following patch.
--- libselinux/src/booleans.c | 2 +- libselinux/src/get_context_list.c | 6 +++--- libselinux/src/get_default_type.c | 5 +++-- libselinux/src/label_db.c | 2 +- libselinux/src/label_media.c | 2 +- libselinux/src/label_x.c | 2 +- libselinux/src/matchmediacon.c | 2 +- libselinux/src/seusers.c | 4 ++-- 8 files changed, 13 insertions(+), 12 deletions(-)
libselinux/src/booleans.c +1 -1
@@ -103,7 +103,7 @@ char *selinux_boolean_sub(const char *name)if (!name)return NULL ; - cfg =fopen (selinux_booleans_subs_path (),"re" ); + cfg =selinux_policy_fopen (selinux_booleans_subs_path (),"re" );if (!cfg) goto out;
libselinux/src/get_context_list.c +3 -3
@@ -297,7 +297,7 @@ static int get_failsafe_context(const char *user, char **newcon)size_t plen, nlen;int rc; - fp =fopen (selinux_failsafe_context_path (),"re" ); + fp =selinux_policy_fopen (selinux_failsafe_context_path (),"re" );if (!fp)return -1 ;
@@ -422,7 +422,7 @@ int get_ordered_context_list(const char *user, const char *fromcon,if (!fname) goto failsafe;snprintf (fname, fname_len,"%s%s" , user_contexts_path, user); - fp =fopen (fname,"re" ); + fp =selinux_policy_fopen (fname,"re" );if (fp) {__fsetlocking (fp,FSETLOCKING_BYCALLER ); rc =get_context_user (fp, con, user, &reachable, &nreachable);
@@ -437,7 +437,7 @@ int get_ordered_context_list(const char *user, const char *fromcon, } }free (fname); - fp =fopen (selinux_default_context_path (),"re" ); + fp =selinux_policy_fopen (selinux_default_context_path (),"re" );if (fp) {__fsetlocking (fp,FSETLOCKING_BYCALLER ); rc =get_context_user (fp, con, user, &reachable, &nreachable);
libselinux/src/get_default_type.c +3 -2
@@ -2,8 +2,9 @@#include <stdlib.h> #include <string.h> #include <ctype.h> -#include "get_default_type_internal.h" #include <errno.h> +#include "get_default_type_internal.h" +#include "selinux_internal.h" static int find_default_type (FILE *fp,const char *role,char **type);
@@ -11,7 +12,7 @@ int get_default_type(const char *role, char **type) {FILE *fp =NULL ; - fp =fopen (selinux_default_type_path (),"re" ); + fp =selinux_policy_fopen (selinux_default_type_path (),"re" );if (!fp)return -1 ;
libselinux/src/label_db.c +1 -1
@@ -287,7 +287,7 @@ static catalog_t *db_init(const struct selinux_opt *opts, unsigned nopts,if (!path) path =selinux_sepgsql_context_path (); -if ((filp =fopen (path,"re" )) ==NULL ) { +if ((filp =selinux_policy_fopen (path,"re" )) ==NULL ) {free (catalog);return NULL ; }
libselinux/src/label_media.c +1 -1
@@ -99,7 +99,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,/* Open the specification file. */ if (!path) path =selinux_media_context_path (); -if ((fp =fopen (path,"re" )) ==NULL ) +if ((fp =selinux_policy_fopen (path,"re" )) ==NULL )return -1 ;__fsetlocking (fp,FSETLOCKING_BYCALLER );
libselinux/src/label_x.c +1 -1
@@ -129,7 +129,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,/* Open the specification file. */ if (!path) path =selinux_x_context_path (); -if ((fp =fopen (path,"re" )) ==NULL ) +if ((fp =selinux_policy_fopen (path,"re" )) ==NULL )return -1 ;__fsetlocking (fp,FSETLOCKING_BYCALLER );
libselinux/src/matchmediacon.c +1 -1
@@ -18,7 +18,7 @@ int matchmediacon(const char *media, char **con)char *ptr, *ptr2 =NULL ;int found =0 ;char current_line[PATH_MAX ]; -if ((infile =fopen (path,"re" )) ==NULL ) +if ((infile =selinux_policy_fopen (path,"re" )) ==NULL )return -1 ; while (!feof_unlocked(infile)) { if (!fgets_unlocked(current_line, sizeof(current_line),
libselinux/src/seusers.c +2 -2
@@ -201,7 +201,7 @@ int getseuserbyname(const char *name, char **r_seuser, char **r_level)gid_t gid =get_default_gid (name); - cfg =fopen (selinux_usersconf_path (),"re" ); + cfg =selinux_policy_fopen (selinux_usersconf_path (),"re" );if (!cfg) goto nomatch;
@@ -296,7 +296,7 @@ int getseuser(const char *username, const char *service, char **r_seuser,if (asprintf (&path,"%s/logins/%s" ,selinux_policy_root (), username) <0 ) goto err; - fp =fopen (path,"re" ); + fp =selinux_policy_fopen (path,"re" );free (path);if (fp ==NULL ) goto err;
Signature · 1 line
2.55.0
Stephen Smalley
maintainerPATCH 4/4 libselinux: search file_contexts files across configuration roots
In reply to Stephen SmalleyWhen no SELABEL_OPT_PATH is specified, resolve each of the file_contexts inputs (base, .homedirs, .local, .subs, .subs_dist) independently against the configuration-root list. On a stateless system these have different owners and land in different roots:
- .subs_dist ships with the policy package and is never rewritten by libsemanage, so it stays under the vendor root;
- .subs and a hand-edited .local are admin config under /etc;
- base, .homedirs, and a managed .local are regenerated by libsemanage into whatever root SEMANAGE_FINAL_SELINUX points at.
open_file() already pairs a text file with its .bin sibling by mtime within one directory, so a compiled form under a lower-priority root never shadows a newer text file under a higher one; passing ".bin" as the sibling suffix to selinux_policy_resolve() lets a root that carries only the compiled form still satisfy the search.
The SELABEL_OPT_PATH case keeps the existing behavior: everything is looked up relative to the caller-supplied path.
--- libselinux/src/label_file.c | 50 ++++++++++++++++++++++++++----- libselinux/src/selinux_config.c | 30 +++++++++++++++++++ libselinux/src/selinux_internal.h | 10 +++++++ 3 files changed, 83 insertions(+), 7 deletions(-)
libselinux/src/label_file.c +43 -7
@@ -1594,17 +1594,37 @@ 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_provided) { - rec->spec_files[0 ] =strdup (selinux_file_context_path ()); +char resolved[PATH_MAX +1 ]; + +/* +* Each file_contexts input is resolved independently +* against the configuration-root list. The .local and +* .subs files are admin config. The .homedirs file is +* generated locally and .subs_dist ships with the policy +* package and is not reinstalled by libsemanage, so on a +* stateless system they will not share a directory with the +* base file. Text/.bin pairing stays per-directory in +* open_file(), so a lower-priority compiled form never +* shadows a higher-priority text file. +*/ +selinux_policy_resolve (selinux_file_context_path (),".bin" , + resolved,sizeof (resolved)); + rec->spec_files[0 ] =strdup (resolved);if (rec->spec_files[0 ] ==NULL ) goto finish; - status =selabel_subs_init ( -selinux_file_context_subs_dist_path (), rec->digest, - &data->dist_subs, &data->dist_subs_num, - &data->dist_subs_alloc); + +selinux_policy_resolve (selinux_file_context_subs_dist_path (), +NULL , subs_file,sizeof (subs_file)); + status =selabel_subs_init (subs_file, rec->digest, + &data->dist_subs, + &data->dist_subs_num, + &data->dist_subs_alloc);if (status) goto finish; - status =selabel_subs_init (selinux_file_context_subs_path (), - rec->digest, &data->subs, + +selinux_policy_resolve (selinux_file_context_subs_path (),NULL , + subs_file,sizeof (subs_file)); + status =selabel_subs_init (subs_file, rec->digest, &data->subs, &data->subs_num, &data->subs_alloc);if (status) goto finish;
@@ -1617,6 +1637,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, &data->dist_subs_alloc);if (status) goto finish; +snprintf (subs_file,sizeof (subs_file),"%s.subs" , rec->spec_files[0 ]); status = selabel_subs_init(subs_file, rec->digest, &data->subs,
@@ -1627,6 +1648,21 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,#endif for (i =0 ; i < num_optional_paths; i++) { +#if !defined(BUILD_HOST ) && !defined(ANDROID ) +if (!path_provided) { +char opt_path[PATH_MAX +1 ], resolved[PATH_MAX +1 ]; + +snprintf (opt_path,sizeof (opt_path),"%s.%s" , +selinux_file_context_path (), opt_suffixes[i]); +selinux_policy_resolve (opt_path,".bin" , resolved, +sizeof (resolved)); + rec->spec_files[num_required_paths + i] = +strdup (resolved); +if (rec->spec_files[num_required_paths + i] ==NULL ) + goto finish; +continue ; + } +#endif if (asprintf (&rec->spec_files[num_required_paths + i],"%s.%s" , rec->spec_files[0 ], opt_suffixes[i]) <0 ) { rec->spec_files[num_required_paths + i] =NULL ;
libselinux/src/selinux_config.c +30 -0
@@ -595,3 +595,33 @@ int selinux_policy_open(const char *path, int flags) errno =ENOENT ;return -1 ; } + +int selinux_policy_resolve (const char *path,const char *sibling,char *out, +size_t outlen) +{ +char probe[PATH_MAX ]; +unsigned int i; + +__selinux_once (once, init_selinux_config); + +if (strlcpy (out, path, outlen) >= outlen) + goto noent; +for (i =0 ;; i++) { +if (access (out,F_OK ) ==0 ) +return 0 ; +if (sibling) +snprintf (probe,sizeof (probe),"%s%s" , out, sibling); +if (access (probe,F_OK ) ==0 ) +return 0 ; +if (!selinux_policyroots[i +1 ] || +selinux_policy_remap (path, selinux_policyroots[i +1 ], out, + outlen)) +break ; + } + +/* Nothing found: leave primary path in @out for diagnostics. */ +strlcpy (out, path, outlen); +noent: + errno =ENOENT ; +return -1 ; +}
libselinux/src/selinux_internal.h +10 -0
@@ -122,6 +122,16 @@ const char *const *selinux_policy_roots(void);FILE *selinux_policy_fopen (const char *path,const char *mode);int selinux_policy_open (const char *path,int flags); +/* +* Resolve @path against the configuration-root list: write into @out +* the first remapping under which @out (or @out concatenated with +* @sibling, when the base file is optional but a sibling such as .bin +* may exist alone is present. If no root has the file, @out receives +* @path unchanged and -1 is returned with errno ENOENT. Internal only. +*/ +int selinux_policy_resolve (const char *path,const char *sibling,char *out, +size_t outlen); +extern int has_selinux_config;#ifndef HAVE_STRLCPY
Signature · 1 line
2.55.0