libselinux: fall through on EACCES when searching configuration roots
Stephen Smalley
maintainerPATCH v2 1/2 libselinux: fall through on EACCES when searching configuration roots
When searching the configuration roots for a given configuration file, rather than failing immediately on EACCES, continue searching lower-priority roots for the file and succeed if we find one that is readable.
This prevents an unreadable file under /etc from hiding a readable one under /usr/lib.
---
v2 removes the double S from EACCES in the comments.
libselinux/src/load_policy.c | 19 +++++++++++++--- libselinux/src/selinux_config.c | 36 ++++++++++++++++++++++++------- libselinux/src/selinux_internal.h | 9 +++++--- 3 files changed, 50 insertions(+), 14 deletions(-)
libselinux/src/load_policy.c +16 -3
@@ -137,18 +137,31 @@ search:* 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. +* EACCES is treated as absent so an unreadable file in a writable +* root cannot block the search from reaching a readable vendor copy. */ fd =-1 ; - errno =ENOENT ; -for (; fd <0 && errno ==ENOENT && vers >= minvers; vers--) { +for (; fd <0 && 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 ) +if (fd >=0 )break ; +if (errno ==ENOENT ) +continue ; +if (errno ==EACCES ) { +selinux_log ( +SELINUX_WARNING , +"SELinux: Skipping unreadable policy file %s: %m\n" , + path); +continue ; + } +break ; } +if (fd <0 && errno !=ENOENT && errno !=EACCES ) +break ; }if (fd <0 ) { selinux_log(
libselinux/src/selinux_config.c +28 -8
@@ -90,6 +90,13 @@ static const uint16_t file_path_suffixes_idx[NEL] = {#undef L1#undef L2 +/* +* Open the SELinux configuration file, trying each confdir in turn. +* ENOENT and EACCES fall through: an unreadable copy in a +* higher-priority root must not mask a readable one in a +* lower-priority root. Any other error is reported against the +* location that produced it. +*/ static FILE *open_selinux_config (void ) {char path[PATH_MAX ];
@@ -99,9 +106,10 @@ static FILE *open_selinux_config(void)for (i =0 ; selinux_confdirs[i]; i++) {snprintf (path,sizeof (path),"%sconfig" , selinux_confdirs[i]); fp =fopen (path,"re" ); -if (fp || errno !=ENOENT ) +if (fp || (errno !=ENOENT && errno !=EACCES ))return fp; } + errno =ENOENT ;return NULL ; }
@@ -550,15 +558,24 @@ static int selinux_policy_remap(const char *path, const char *root, char *out,return 0 ; } +/* +* ENOENT and EACCES fall through to the next configuration root. +* EACCES is treated the same as ENOENT so that a file the caller cannot +* read cannot mask a readable copy in a lower-priority root; any other +* error is returned against the root that produced it. If nothing was +* readable, errno is EACCES if any root refused, otherwise ENOENT. +*/ FILE *selinux_policy_fopen (const char *path,const char *mode) {char alt[PATH_MAX ];unsigned int i; +int refused =0 ;FILE *fp; fp =fopen (path, mode); -if (fp || errno !=ENOENT ) +if (fp || (errno !=ENOENT && errno !=EACCES ))return fp; + refused |= (errno ==EACCES );__selinux_once (once, init_selinux_config);for (i =1 ; selinux_policyroots[i]; i++) {
@@ -566,10 +583,11 @@ FILE *selinux_policy_fopen(const char *path, const char *mode) sizeof(alt)))break ; fp =fopen (alt, mode); -if (fp || errno !=ENOENT ) +if (fp || (errno !=ENOENT && errno !=EACCES ))return fp; + refused |= (errno ==EACCES ); } - errno =ENOENT ; + errno = refused ?EACCES :ENOENT ;return NULL ; }
@@ -577,11 +595,12 @@ int selinux_policy_open(const char *path, int flags) {char alt[PATH_MAX ];unsigned int i; -int fd; +int fd, refused =0 ; fd =open (path, flags); -if (fd >=0 || errno !=ENOENT ) +if (fd >=0 || (errno !=ENOENT && errno !=EACCES ))return fd; + refused |= (errno ==EACCES );__selinux_once (once, init_selinux_config);for (i =1 ; selinux_policyroots[i]; i++) {
@@ -589,10 +608,11 @@ int selinux_policy_open(const char *path, int flags) sizeof(alt)))break ; fd =open (alt, flags); -if (fd >=0 || errno !=ENOENT ) +if (fd >=0 || (errno !=ENOENT && errno !=EACCES ))return fd; + refused |= (errno ==EACCES ); } - errno =ENOENT ; + errno = refused ?EACCES :ENOENT ;return -1 ; }
libselinux/src/selinux_internal.h +6 -3
@@ -117,7 +117,7 @@ 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. +* yields ENOENT or EACCES. Internal-only. */ FILE *selinux_policy_fopen (const char *path,const char *mode);int selinux_policy_open (const char *path,int flags);
@@ -126,8 +126,11 @@ 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. +* may exist alone) is present. If no root has the file, @out receives +* @path unchanged and -1 is returned with errno ENOENT, or if a root +* contains the file but it is un-readable and there is no readable +* fallback, @out receives @path unchanged and -1 is returned with errno +* EACCES. Internal only. */ int selinux_policy_resolve (const char *path,const char *sibling,char *out,size_t outlen);
Signature · 1 line
2.55.0
Stephen Smalley
maintainerPATCH v2 2/2 libsemanage: fall through on an unreadable semanage.conf
When searching the configuration roots for semanage.conf, check that the file is readable, not just that it exists, and fall through to the next configuration root if not.
This prevents an unreadable /etc/selinux/semanage.conf from causing libsemanage to fall back to the built-in defaults instead of using /usr/lib/selinux/semanage.conf.
--- libsemanage/src/semanage_store.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
libsemanage/src/semanage_store.c +6 -3
@@ -564,14 +564,17 @@ char *semanage_conf_path(void)/* * Rooted at each configuration directory, then (for a chroot -* build) fall back to the host directory. +* build) fall back to the host directory. Probe R_OK rather +* than F_OK: semanage_conf_parse() treats a fopen() failure as +* "use compiled-in defaults", so an unreadable higher-priority +* copy would otherwise mask a readable vendor copy under /usr/lib. */ for (i =0 ; dirs[i]; i++) {free (conf);if (asprintf (&conf,"%s%s%s" ,semanage_root (), dirs[i],SEMANAGE_CONF_FILE ) <0 )return NULL ; -if (access (conf,F_OK ) ==0 ) +if (access (conf,R_OK ) ==0 )return conf; }if (semanage_root ()[0 ] !='\0' ) {
@@ -580,7 +583,7 @@ char *semanage_conf_path(void)if (asprintf (&conf,"%s%s" , dirs[i],SEMANAGE_CONF_FILE ) <0 )return NULL ; -if (access (conf,F_OK ) ==0 ) +if (access (conf,R_OK ) ==0 )return conf; } }
Signature · 1 line
2.55.0
Stephen Smalley
maintainerQuoted 6 lines · Stephen Smalley
When searching the configuration roots for a given configuration file, rather than failing immediately on EACCES, continue searching lower-priority roots for the file and succeed if we find one that is readable.
Quoted 6 lines
This prevents an unreadable file under /etc from hiding a readable one under
/usr/lib.Signed-off-by Stephen Smalley <[email protected]>---
v2 removes the double S from EACCES in the comments.
This series has been merged.