Skip to contentSkip to conversation

libsemanage: Perform access check using euid instead of uid

PATCH In review Vit Mojzis 4 participants 41 messages Feb 14, 2017 – Mar 1, 2018 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

PATCH 1/1 libsemanage: Perform access check using euid instead of uid

Use faccessat() with AT_EACCESS instead of accesss() in order to check permissions of effective user. access() calls checking existence of a file (F_OK) were left untouched since they work correctly.

This enables setuid programs to use libsemanage.

---
 libsemanage/src/conf-parse.y     |  7 ++++---
 libsemanage/src/semanage_store.c | 18 +++++++++---------
 2 files changed, 13 insertions(+), 12 deletions(-)
libsemanage/src/conf-parse.y +4 -3
index b527e89..d72a0c2 100644
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
 
 extern int semanage_lex(void);                /* defined in conf-scan.c */
 extern int semanage_lex_destroy(void);        /* defined in conf-scan.c */
@@ -361,7 +362,7 @@ static int semanage_conf_init(semanage_conf_t * conf)
 		return -1;
 	}
 
-	if (access("/sbin/load_policy", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/load_policy", X_OK, AT_EACCESS) == 0) {
 		conf->load_policy->path = strdup("/sbin/load_policy");
 	} else {
 		conf->load_policy->path = strdup("/usr/sbin/load_policy");
@@ -375,7 +376,7 @@ static int semanage_conf_init(semanage_conf_t * conf)
 	     calloc(1, sizeof(*(current_conf->setfiles)))) == NULL) {
 		return -1;
 	}
-	if (access("/sbin/setfiles", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/setfiles", X_OK, AT_EACCESS) == 0) {
 		conf->setfiles->path = strdup("/sbin/setfiles");
 	} else {
 		conf->setfiles->path = strdup("/usr/sbin/setfiles");
@@ -389,7 +390,7 @@ static int semanage_conf_init(semanage_conf_t * conf)
 	     calloc(1, sizeof(*(current_conf->sefcontext_compile)))) == NULL) {
 		return -1;
 	}
-	if (access("/sbin/sefcontext_compile", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/sefcontext_compile", X_OK, AT_EACCESS) == 0) {
 		conf->sefcontext_compile->path = strdup("/sbin/sefcontext_compile");
 	} else {
 		conf->sefcontext_compile->path = strdup("/usr/sbin/sefcontext_compile");
libsemanage/src/semanage_store.c +9 -9
index f468fab..805bd60 100644
@@ -517,7 +517,7 @@ char *semanage_conf_path(void)
 	snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
 		 SEMANAGE_CONF_FILE);
 
-	if (access(semanage_conf, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, semanage_conf, R_OK, AT_EACCESS) != 0) {
 		snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
 	}
 
@@ -552,7 +552,7 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD, path, mode_mask, AT_EACCESS) == -1) {
 			ERR(sh,
 			    "Could not access module store at %s, or it is not a directory.",
 			    path);
@@ -575,7 +575,7 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD, path, mode_mask, AT_EACCESS) == -1) {
 			ERR(sh,
 			    "Could not access module store active subdirectory at %s, or it is not a directory.",
 			    path);
@@ -598,7 +598,7 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD, path, mode_mask, AT_EACCESS) == -1) {
 			ERR(sh,
 			    "Could not access module store active modules subdirectory at %s, or it is not a directory.",
 			    path);
@@ -619,7 +619,7 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK | W_OK) == -1) {
+		if (!S_ISREG(sb.st_mode) || faccessat(AT_FDCWD, path, R_OK | W_OK, AT_EACCESS) == -1) {
 			ERR(sh, "Could not access lock file at %s.", path);
 			return -1;
 		}
@@ -639,7 +639,7 @@ int semanage_store_access_check(void)
 
 	/* read access on active store */
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);
-	if (access(path, R_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | X_OK, AT_EACCESS) != 0)
 		goto out;
 
 	/* we can read the active store meaning it is managed
@@ -650,13 +650,13 @@ int semanage_store_access_check(void)
 	 * write access necessary if the lock file does not exist
 	 */
 	path = semanage_files[SEMANAGE_READ_LOCK];
-	if (access(path, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS) != 0) {
 		if (access(path, F_OK) == 0) {
 			goto out;
 		}
 
 		path = semanage_files[SEMANAGE_ROOT];
-		if (access(path, R_OK | W_OK | X_OK) != 0) {
+		if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK, AT_EACCESS) != 0) {
 			goto out;
 		}
 	}
@@ -666,7 +666,7 @@ int semanage_store_access_check(void)
 
 	/* check the modules directory */
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_MODULES);
-	if (access(path, R_OK | W_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK, AT_EACCESS) != 0)
 		goto out;
 
 	rc = SEMANAGE_CAN_WRITE;
Signature · 1 line
2.9.3
#1 lore raw
Quoted 6 lines · Vit Mojzis

Use faccessat() with AT_EACCESS instead of accesss() in order to check
permissions of effective user. access() calls checking existence of a file (F_OK) were left untouched since they work correctly.

This enables setuid programs to use libsemanage.

Not sure we want setuid programs using libsemanage.  Is there a use case for that?  I wouldn't warrant it to be safe.

AT_EACCESS is not implemented by the kernel, so this seems to rely on some libc magic to support?  Is that done in a safe way?

libsemanage usage of access() is not for security purposes; it is just to test for existence/location of various files and to confirm that the policy store is writable by the caller before beginning any real work. So arguments about access() being insecure are not relevant here.

Quoted 168 lines

---
 libsemanage/src/conf-parse.y     |  7 ++++--- libsemanage/src/semanage_store.c | 18 +++++++++---------

 2 files changed, 13 insertions(+), 12 deletions(-)
libsemanage/src/conf- renamed from libsemanage/src/conf-parse.y +0 -0

parse.y
index b527e89..d72a0c2 100644

libsemanage/src/conf-parse.y +1 -0
@@ -30,6 +30,7 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
+#include <fcntl.h>


 extern int semanage_lex(void);                /* defined in conf- scan.c */
 extern int semanage_lex_destroy(void);        /* defined in conf- scan.c */

libsemanage/src/conf-parse.y +0 -0
@@ -361,7 +362,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)
  		return -1;
  	}

libsemanage/src/conf-parse.y +1 -1
-	if (access("/sbin/load_policy", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/load_policy", X_OK,

AT_EACCESS) == 0) {
  conf->load_policy->path =
strdup("/sbin/load_policy");
  } else {
  conf->load_policy->path =
strdup("/usr/sbin/load_policy");

libsemanage/src/conf-parse.y +1 -1
@@ -375,7 +376,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)
  	     calloc(1, sizeof(*(current_conf->setfiles)))) == NULL)
 {
  		return -1;
  	}
-	if (access("/sbin/setfiles", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/setfiles", X_OK, AT_EACCESS)
 == 0) {

  conf->setfiles->path = strdup("/sbin/setfiles"); } else {
  conf->setfiles->path = strdup("/usr/sbin/setfiles");

libsemanage/src/conf-parse.y +1 -1
@@ -389,7 +390,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)
  	     calloc(1, sizeof(*(current_conf->sefcontext_compile)))) 
 == NULL) {
  		return -1;
  	}
-	if (access("/sbin/sefcontext_compile", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/sefcontext_compile", X_OK,
 AT_EACCESS) == 0) {

  conf->sefcontext_compile->path =
strdup("/sbin/sefcontext_compile");
  } else {
  conf->sefcontext_compile->path =
strdup("/usr/sbin/sefcontext_compile");

libsemanage/src/semanage_store.c +0 -0
index f468fab..805bd60 100644
@@ -517,7 +517,7 @@ char *semanage_conf_path(void)
  	snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(),
 selinux_path(),
  		 SEMANAGE_CONF_FILE);

libsemanage/src/semanage_store.c +1 -1
-	if (access(semanage_conf, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, semanage_conf, R_OK, AT_EACCESS) !=

0) {
  snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
  }

libsemanage/src/semanage_store.c +1 -1
@@ -552,7 +552,7 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,
 path, mode_mask, AT_EACCESS) == -1) {

  ERR(sh,
      "Could not access module store at %s, or it is not a directory.",
      path);

libsemanage/src/semanage_store.c +1 -1
@@ -575,7 +575,7 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,
 path, mode_mask, AT_EACCESS) == -1) {

  ERR(sh,
      "Could not access module store active subdirectory at %s, or it is not a directory.",
      path);

libsemanage/src/semanage_store.c +1 -1
@@ -598,7 +598,7 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,
 path, mode_mask, AT_EACCESS) == -1) {

  ERR(sh,
      "Could not access module store active modules subdirectory at %s, or it is not a directory.", path);

libsemanage/src/semanage_store.c +1 -1
@@ -619,7 +619,7 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK |
 W_OK) == -1) {
+		if (!S_ISREG(sb.st_mode) || faccessat(AT_FDCWD,
 path, R_OK | W_OK, AT_EACCESS) == -1) {

  ERR(sh, "Could not access lock file at %s.", path);
  return -1;
  }

libsemanage/src/semanage_store.c +0 -0
@@ -639,7 +639,7 @@ int semanage_store_access_check(void)


  /* read access on active store */
  path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);

libsemanage/src/semanage_store.c +1 -1
-	if (access(path, R_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | X_OK, AT_EACCESS) != 0)

  goto out;

  /* we can read the active store meaning it is managed

libsemanage/src/semanage_store.c +1 -1
@@ -650,13 +650,13 @@ int semanage_store_access_check(void)
  	 * write access necessary if the lock file does not exist
  	 */
  	path = semanage_files[SEMANAGE_READ_LOCK];
-	if (access(path, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS) != 0) {
  		if (access(path, F_OK) == 0) {
  			goto out;
  		}


  path = semanage_files[SEMANAGE_ROOT];

libsemanage/src/semanage_store.c +1 -1
-		if (access(path, R_OK | W_OK | X_OK) != 0) {
+		if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK,

AT_EACCESS) != 0) {
  goto out;
  }
  }

libsemanage/src/semanage_store.c +0 -0
@@ -666,7 +666,7 @@ int semanage_store_access_check(void)


  /* check the modules directory */
  path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_MODULES);

libsemanage/src/semanage_store.c +1 -1
-	if (access(path, R_OK | W_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK,

AT_EACCESS) != 0)
  goto out;

  rc = SEMANAGE_CAN_WRITE;

#2 lore raw
Quoted 7 lines · Stephen Smalley
Quoted 6 lines · Vit Mojzis

Use faccessat() with AT_EACCESS instead of accesss() in order to check
permissions of effective user. access() calls checking existence of a file (F_OK) were left untouched since they work correctly.

Quoted 7 lines
Quoted 1 line

This enables setuid programs to use libsemanage.

Not sure we want setuid programs using libsemanage.  Is there a use case for that?  I wouldn't warrant it to be safe.

AT_EACCESS is not implemented by the kernel, so this seems to rely on some libc magic to support?  Is that done in a safe way?

Per the man page glibc notes, AT_EACCESS is implemented by the glibc wrapper function by using fstatat(2) and emulating the permission check in userspace.  This however is not equivalent to access(2), since the latter will perform SELinux checks too.  It is also seemingly glibc- specific and thus non-portable.

Quoted 200 lines

libsemanage usage of access() is not for security purposes; it is just
to test for existence/location of various files and to confirm that the
policy store is writable by the caller before beginning any real work.
 So arguments about access() being insecure are not relevant here.

Quoted 181 lines

---
 libsemanage/src/conf-parse.y     |  7 ++++--- libsemanage/src/semanage_store.c | 18 +++++++++---------

 2 files changed, 13 insertions(+), 12 deletions(-)
libsemanage/src/conf- renamed from libsemanage/src/conf-parse.y +0 -0

parse.y
index b527e89..d72a0c2 100644

libsemanage/src/conf-parse.y +1 -0
@@ -30,6 +30,7 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
+#include <fcntl.h>


 extern int semanage_lex(void);                /* defined in conf- scan.c */
 extern int semanage_lex_destroy(void);        /* defined in conf- scan.c */

libsemanage/src/conf-parse.y +0 -0
@@ -361,7 +362,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)
  		return -1;
  	}

libsemanage/src/conf-parse.y +1 -1
-	if (access("/sbin/load_policy", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/load_policy", X_OK,

AT_EACCESS) == 0) {
  conf->load_policy->path =
strdup("/sbin/load_policy");
  } else {
  conf->load_policy->path =
strdup("/usr/sbin/load_policy");

libsemanage/src/conf-parse.y +0 -0
@@ -375,7 +376,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)

       calloc(1, sizeof(*(current_conf->setfiles)))) == NULL)
{
  return -1;
  }

libsemanage/src/conf-parse.y +1 -1
-	if (access("/sbin/setfiles", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/setfiles", X_OK,

AT_EACCESS)
== 0) {
  conf->setfiles->path = strdup("/sbin/setfiles"); } else {
  conf->setfiles->path = strdup("/usr/sbin/setfiles");

libsemanage/src/conf-parse.y +0 -0
@@ -389,7 +390,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)

       calloc(1, sizeof(*(current_conf-

Quoted 1 line

sefcontext_compile))))

== NULL) {
  return -1;
  }

libsemanage/src/conf-parse.y +1 -1
-	if (access("/sbin/sefcontext_compile", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/sefcontext_compile", X_OK,

AT_EACCESS) == 0) {
  conf->sefcontext_compile->path =
strdup("/sbin/sefcontext_compile");
  } else {
  conf->sefcontext_compile->path =
strdup("/usr/sbin/sefcontext_compile");

libsemanage/src/semanage_store.c +0 -0
index f468fab..805bd60 100644
@@ -517,7 +517,7 @@ char *semanage_conf_path(void)
  	snprintf(semanage_conf, len + 1, "%s%s%s",
 semanage_root(),
 selinux_path(),
  		 SEMANAGE_CONF_FILE);

libsemanage/src/semanage_store.c +1 -1
-	if (access(semanage_conf, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, semanage_conf, R_OK, AT_EACCESS)

!=
0) {
  snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
  }

libsemanage/src/semanage_store.c +0 -1
@@ -552,7 +552,7 @@ int semanage_create_store(semanage_handle_t *
 sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path,
 mode_mask)

== -1) {

libsemanage/src/semanage_store.c +1 -0
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,

path, mode_mask, AT_EACCESS) == -1) {
  ERR(sh,
      "Could not access module store at %s, or
it is not a directory.",
      path);

libsemanage/src/semanage_store.c +0 -1
@@ -575,7 +575,7 @@ int semanage_create_store(semanage_handle_t *
 sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path,
 mode_mask)

== -1) {

libsemanage/src/semanage_store.c +1 -0
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,

path, mode_mask, AT_EACCESS) == -1) {
  ERR(sh,
      "Could not access module store active subdirectory at %s, or it is not a directory.",
      path);

libsemanage/src/semanage_store.c +0 -1
@@ -598,7 +598,7 @@ int semanage_create_store(semanage_handle_t *
 sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path,
 mode_mask)

== -1) {

libsemanage/src/semanage_store.c +1 -0
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,

path, mode_mask, AT_EACCESS) == -1) {
  ERR(sh,
      "Could not access module store active modules subdirectory at %s, or it is not a directory.", path);

libsemanage/src/semanage_store.c +1 -1
@@ -619,7 +619,7 @@ int semanage_create_store(semanage_handle_t *
 sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK |
 W_OK) == -1) {
+		if (!S_ISREG(sb.st_mode) || faccessat(AT_FDCWD,

path, R_OK | W_OK, AT_EACCESS) == -1) {
  ERR(sh, "Could not access lock file at %s.",
path);
  return -1;
  }

libsemanage/src/semanage_store.c +0 -0
@@ -639,7 +639,7 @@ int semanage_store_access_check(void)


  /* read access on active store */
  path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);

libsemanage/src/semanage_store.c +1 -1
-	if (access(path, R_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | X_OK, AT_EACCESS) !=

0)
  goto out;

  /* we can read the active store meaning it is managed

libsemanage/src/semanage_store.c +1 -1
@@ -650,13 +650,13 @@ int semanage_store_access_check(void)
  	 * write access necessary if the lock file does not exist
  	 */
  	path = semanage_files[SEMANAGE_READ_LOCK];
-	if (access(path, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS) != 0) {
  		if (access(path, F_OK) == 0) {
  			goto out;
  		}


  path = semanage_files[SEMANAGE_ROOT];

libsemanage/src/semanage_store.c +1 -1
-		if (access(path, R_OK | W_OK | X_OK) != 0) {
+		if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK,

AT_EACCESS) != 0) {
  goto out;
  }
  }

libsemanage/src/semanage_store.c +0 -0
@@ -666,7 +666,7 @@ int semanage_store_access_check(void)


  /* check the modules directory */
  path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_MODULES);

libsemanage/src/semanage_store.c +1 -1
-	if (access(path, R_OK | W_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK,
Quoted 4 lines

AT_EACCESS) != 0)
  goto out;

  rc = SEMANAGE_CAN_WRITE;

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to Selinux-request@tycho .nsa.gov.

#3 lore raw
Quoted 20 lines · Stephen Smalley
Quoted 10 lines · Stephen Smalley
Quoted 8 lines · Vit Mojzis

Use faccessat() with AT_EACCESS instead of accesss() in order to check
permissions of effective user. access() calls checking existence of
a file (F_OK) were left untouched since they work correctly.

Quoted 8 lines
Quoted 1 line

This enables setuid programs to use libsemanage.

Not sure we want setuid programs using libsemanage.  Is there a use case for that?  I wouldn't warrant it to be safe.

AT_EACCESS is not implemented by the kernel, so this seems to rely on
some libc magic to support?  Is that done in a safe way?

Quoted 6 lines

Per the man page glibc notes, AT_EACCESS is implemented by the glibc wrapper function by using fstatat(2) and emulating the permission check
in userspace.  This however is not equivalent to access(2), since the latter will perform SELinux checks too.  It is also seemingly glibc- specific and thus non-portable.

Looked at the glibc implementation and it seems very broken; it hardcodes legacy Unix permission checking without consideration of Linux capabilities, POSIX ACLs, or anything else including SELinux permissions.  Someone really ought to consider removing that.

musl implementation is perhaps more reliable but has to use a complex workaround (clones a child that call setuid(), setgid() and then faccessat() under the new uid/gid and reports the status to the parent via a pipe).

Quoted 224 lines
Quoted 210 lines

libsemanage usage of access() is not for security purposes; it is just
to test for existence/location of various files and to confirm that the
policy store is writable by the caller before beginning any real work.
 So arguments about access() being insecure are not relevant here.

Quoted 199 lines

---
 libsemanage/src/conf-parse.y     |  7 ++++--- libsemanage/src/semanage_store.c | 18 +++++++++---------

 2 files changed, 13 insertions(+), 12 deletions(-)
libsemanage/src/conf- renamed from libsemanage/src/conf-parse.y +0 -0

parse.y
index b527e89..d72a0c2 100644

libsemanage/src/conf-parse.y +1 -0
@@ -30,6 +30,7 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
+#include <fcntl.h>


 extern int semanage_lex(void);                /* defined in conf-
scan.c */
 extern int semanage_lex_destroy(void);        /* defined in conf-
scan.c */

libsemanage/src/conf-parse.y +0 -0
@@ -361,7 +362,7 @@ static int semanage_conf_init(semanage_conf_t
 *
 conf)
  		return -1;
  	}

libsemanage/src/conf-parse.y +1 -1
-	if (access("/sbin/load_policy", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/load_policy", X_OK,

AT_EACCESS) == 0) {
  conf->load_policy->path =
strdup("/sbin/load_policy");
  } else {
  conf->load_policy->path =
strdup("/usr/sbin/load_policy");

libsemanage/src/conf-parse.y +0 -0
@@ -375,7 +376,7 @@ static int semanage_conf_init(semanage_conf_t
 *
 conf)

       calloc(1, sizeof(*(current_conf->setfiles)))) == NULL)
{
  return -1;
  }

libsemanage/src/conf-parse.y +1 -1
-	if (access("/sbin/setfiles", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/setfiles", X_OK,

AT_EACCESS)
== 0) {
  conf->setfiles->path = strdup("/sbin/setfiles"); } else {
  conf->setfiles->path = strdup("/usr/sbin/setfiles");

libsemanage/src/conf-parse.y +0 -0
@@ -389,7 +390,7 @@ static int semanage_conf_init(semanage_conf_t
 *
 conf)

       calloc(1, sizeof(*(current_conf-

Quoted 2 lines

sefcontext_compile))))

== NULL) {
  return -1;
  }

libsemanage/src/conf-parse.y +1 -1
-	if (access("/sbin/sefcontext_compile", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/sefcontext_compile",

X_OK,
AT_EACCESS) == 0) {
  conf->sefcontext_compile->path =
strdup("/sbin/sefcontext_compile");
  } else {
  conf->sefcontext_compile->path =
strdup("/usr/sbin/sefcontext_compile");

libsemanage/src/semanage_store.c +0 -0
index f468fab..805bd60 100644
@@ -517,7 +517,7 @@ char *semanage_conf_path(void)
  	snprintf(semanage_conf, len + 1, "%s%s%s",
 semanage_root(),
 selinux_path(),
  		 SEMANAGE_CONF_FILE);

libsemanage/src/semanage_store.c +1 -1
-	if (access(semanage_conf, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, semanage_conf, R_OK, AT_EACCESS)

!=
0) {
  snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
  }

libsemanage/src/semanage_store.c +0 -1
@@ -552,7 +552,7 @@ int semanage_create_store(semanage_handle_t *
 sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path,
 mode_mask)

== -1) {

libsemanage/src/semanage_store.c +1 -0
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,

path, mode_mask, AT_EACCESS) == -1) {
  ERR(sh,
      "Could not access module store at %s,
or
it is not a directory.",
      path);

libsemanage/src/semanage_store.c +0 -1
@@ -575,7 +575,7 @@ int semanage_create_store(semanage_handle_t *
 sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path,
 mode_mask)

== -1) {

libsemanage/src/semanage_store.c +1 -0
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,

path, mode_mask, AT_EACCESS) == -1) {
  ERR(sh,
      "Could not access module store active
subdirectory at %s, or it is not a directory.", path);

libsemanage/src/semanage_store.c +0 -1
@@ -598,7 +598,7 @@ int semanage_create_store(semanage_handle_t *
 sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path,
 mode_mask)

== -1) {

libsemanage/src/semanage_store.c +1 -0
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,

path, mode_mask, AT_EACCESS) == -1) {
  ERR(sh,
      "Could not access module store active
modules subdirectory at %s, or it is not a directory.", path);

libsemanage/src/semanage_store.c +1 -1
@@ -619,7 +619,7 @@ int semanage_create_store(semanage_handle_t *
 sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK |
 W_OK) == -1) {
+		if (!S_ISREG(sb.st_mode) || faccessat(AT_FDCWD,

path, R_OK | W_OK, AT_EACCESS) == -1) {
  ERR(sh, "Could not access lock file at %s.",
path);
  return -1;
  }

libsemanage/src/semanage_store.c +0 -0
@@ -639,7 +639,7 @@ int semanage_store_access_check(void)


  /* read access on active store */
  path = semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_TOPLEVEL);

libsemanage/src/semanage_store.c +1 -1
-	if (access(path, R_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | X_OK, AT_EACCESS)

!=
0)
  goto out;

  /* we can read the active store meaning it is managed

libsemanage/src/semanage_store.c +0 -0
@@ -650,13 +650,13 @@ int semanage_store_access_check(void)
  	 * write access necessary if the lock file does not

exist
   */
  path = semanage_files[SEMANAGE_READ_LOCK];

libsemanage/src/semanage_store.c +1 -1
-	if (access(path, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS) != 0) {

  if (access(path, F_OK) == 0) { goto out;
  }

  path = semanage_files[SEMANAGE_ROOT];

libsemanage/src/semanage_store.c +1 -1
-		if (access(path, R_OK | W_OK | X_OK) != 0) {
+		if (faccessat(AT_FDCWD, path, R_OK | W_OK |

X_OK,
AT_EACCESS) != 0) {
  goto out;
  }
  }

libsemanage/src/semanage_store.c +0 -0
@@ -666,7 +666,7 @@ int semanage_store_access_check(void)


  /* check the modules directory */
  path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_MODULES);

libsemanage/src/semanage_store.c +1 -1
-	if (access(path, R_OK | W_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK,

AT_EACCESS) != 0)
  goto out;

Quoted 8 lines
Quoted 1 line

  rc = SEMANAGE_CAN_WRITE;

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to Selinux-request@tyc ho
.nsa.gov.

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to Selinux-request@tycho .nsa.gov.

#4 lore raw
Quoted 43 lines · Stephen Smalley
Quoted 27 lines · Stephen Smalley
Quoted 16 lines · Stephen Smalley
Quoted 11 lines · Vit Mojzis

Use faccessat() with AT_EACCESS instead of accesss() in order to
check
permissions of effective user. access() calls checking existence
of
a file (F_OK) were left untouched since they work correctly.

Quoted 1 line

This enables setuid programs to use libsemanage.

Quoted 8 lines

Not sure we want setuid programs using libsemanage.  Is there a use
case for that?  I wouldn't warrant it to be safe.

AT_EACCESS is not implemented by the kernel, so this seems to rely
on
some libc magic to support?  Is that done in a safe way?

Quoted 9 lines

Per the man page glibc notes, AT_EACCESS is implemented by the glibc
wrapper function by using fstatat(2) and emulating the permission check
in userspace.  This however is not equivalent to access(2), since the
latter will perform SELinux checks too.  It is also seemingly glibc-
specific and thus non-portable.

Looked at the glibc implementation and it seems very broken; it hardcodes legacy Unix permission checking without consideration of Linux capabilities, POSIX ACLs, or anything else including SELinux permissions.  Someone really ought to consider removing that.

Quoted 5 lines

musl implementation is perhaps more reliable but has to use a complex workaround (clones a child that call setuid(), setgid() and then faccessat() under the new uid/gid and reports the status to the parent
via a pipe).

This seems to be a known issue,
https://bugzilla.redhat.com/show_bug.cgi?id=1333764

#5 lore raw
In reply to Stephen Smalley
Quoted 10 lines · Stephen Smalley
Quoted 6 lines · Vit Mojzis

Use faccessat() with AT_EACCESS instead of accesss() in order to check
permissions of effective user. access() calls checking existence of a file (F_OK) were left untouched since they work correctly.

This enables setuid programs to use libsemanage.

Not sure we want setuid programs using libsemanage. Is there a use case for that? I wouldn't warrant it to be safe.

The motivation is described in the response from the original reporter in the bug:

---
I think the reason why I filed the bug back then was
https://fedorahosted.org/sssd/ticket/2564

In general I don't think the bug is a big deal to us and if upstream is reluctant to this change, just close the bug. I just found it odd to check if a file exists before acting on it instead of just trying to work with the file and failing on errors..the current approach seems a bit racy to me.

About the question in the thread that asks why do we use the selinux libraries in a setuid library..the reason is that in order to pass certain certifications, no code in SSSD that deals with network connections should run as root. Therefore, the SSSD itself runs as a nonprivileged user and for actions that require root privileges (like setting a selinux context for a user) we fork our a setgid helper that actually does the work.
---

I think it's about situations when a process can create e.g. /var/lib/selinux/targeted/semanage.read.LOCK001 using fopen(), but libsemanage denies to work as it thinks it can't access the store.

Petr

Quoted 183 lines

AT_EACCESS is not implemented by the kernel, so this seems to rely on some libc magic to support? Is that done in a safe way?

libsemanage usage of access() is not for security purposes; it is just to test for existence/location of various files and to confirm that the policy store is writable by the caller before beginning any real work.
So arguments about access() being insecure are not relevant here.

Quoted 167 lines
---
 libsemanage/src/conf-parse.y     |  7 ++++---
 libsemanage/src/semanage_store.c | 18 +++++++++---------
 2 files changed, 13 insertions(+), 12 deletions(-)
libsemanage/src/conf- renamed from libsemanage/src/conf-parse.y +0 -0

parse.y
index b527e89..d72a0c2 100644

libsemanage/src/conf-parse.y +1 -0
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
 
 extern int semanage_lex(void);                /* defined in conf-

scan.c */
extern int semanage_lex_destroy(void); /* defined in conf-
scan.c */

libsemanage/src/conf-parse.y +1 -1
@@ -361,7 +362,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)
 		return -1;
 	}
 
-	if (access("/sbin/load_policy", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/load_policy", X_OK,
 AT_EACCESS) == 0) {
 		conf->load_policy->path =

strdup("/sbin/load_policy");

libsemanage/src/conf-parse.y +0 -0
 	} else {
 		conf->load_policy->path =

strdup("/usr/sbin/load_policy");

libsemanage/src/conf-parse.y +1 -1
@@ -375,7 +376,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)
 	     calloc(1, sizeof(*(current_conf->setfiles)))) == NULL)
 {
 		return -1;
 	}
-	if (access("/sbin/setfiles", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/setfiles", X_OK, AT_EACCESS)
 == 0) {
libsemanage/src/conf-parse.y +1 -1
 		conf->setfiles->path = strdup("/sbin/setfiles");
 	} else {
 		conf->setfiles->path = strdup("/usr/sbin/setfiles");
@@ -389,7 +390,7 @@ static int semanage_conf_init(semanage_conf_t *
 conf)
 	     calloc(1, sizeof(*(current_conf->sefcontext_compile)))) 
 == NULL) {
 		return -1;
 	}
-	if (access("/sbin/sefcontext_compile", X_OK) == 0) {
+	if (faccessat(AT_FDCWD, "/sbin/sefcontext_compile", X_OK,
 AT_EACCESS) == 0) {

conf->sefcontext_compile->path =
strdup("/sbin/sefcontext_compile");

libsemanage/src/conf-parse.y +0 -0
 	} else {
 		conf->sefcontext_compile->path =

strdup("/usr/sbin/sefcontext_compile");

libsemanage/src/semanage_store.c +1 -1
index f468fab..805bd60 100644
@@ -517,7 +517,7 @@ char *semanage_conf_path(void)
 	snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(),
 selinux_path(),
 		 SEMANAGE_CONF_FILE);
 
-	if (access(semanage_conf, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, semanage_conf, R_OK, AT_EACCESS) !=
 0) {
 		snprintf(semanage_conf, len + 1, "%s%s",

selinux_path(), SEMANAGE_CONF_FILE);

libsemanage/src/semanage_store.c +1 -1
 	}
@@ -552,7 +552,7 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,
 path, mode_mask, AT_EACCESS) == -1) {
libsemanage/src/semanage_store.c +0 -0
 			ERR(sh,
 			    "Could not access module store at %s, or

it is not a directory.",

libsemanage/src/semanage_store.c +1 -1
 			    path);
@@ -575,7 +575,7 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,
 path, mode_mask, AT_EACCESS) == -1) {
libsemanage/src/semanage_store.c +0 -0
 			ERR(sh,
 			    "Could not access module store active

subdirectory at %s, or it is not a directory.",

libsemanage/src/semanage_store.c +1 -1
 			    path);
@@ -598,7 +598,7 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode) || faccessat(AT_FDCWD,
 path, mode_mask, AT_EACCESS) == -1) {
libsemanage/src/semanage_store.c +0 -0
 			ERR(sh,
 			    "Could not access module store active

modules subdirectory at %s, or it is not a directory.",

libsemanage/src/semanage_store.c +1 -1
 			    path);
@@ -619,7 +619,7 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK |
 W_OK) == -1) {
+		if (!S_ISREG(sb.st_mode) || faccessat(AT_FDCWD,
 path, R_OK | W_OK, AT_EACCESS) == -1) {

ERR(sh, "Could not access lock file at %s.",
path);

libsemanage/src/semanage_store.c +3 -3
 			return -1;
 		}
@@ -639,7 +639,7 @@ int semanage_store_access_check(void)
 
 	/* read access on active store */
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);
-	if (access(path, R_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | X_OK, AT_EACCESS) != 0)
 		goto out;
 
 	/* we can read the active store meaning it is managed
@@ -650,13 +650,13 @@ int semanage_store_access_check(void)
 	 * write access necessary if the lock file does not exist
 	 */
 	path = semanage_files[SEMANAGE_READ_LOCK];
-	if (access(path, R_OK) != 0) {
+	if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS) != 0) {
 		if (access(path, F_OK) == 0) {
 			goto out;
 		}
 
 		path = semanage_files[SEMANAGE_ROOT];
-		if (access(path, R_OK | W_OK | X_OK) != 0) {
+		if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK,
 AT_EACCESS) != 0) {
 			goto out;
 		}
libsemanage/src/semanage_store.c +1 -1
 	}
@@ -666,7 +666,7 @@ int semanage_store_access_check(void)
 
 	/* check the modules directory */
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_MODULES);
-	if (access(path, R_OK | W_OK | X_OK) != 0)
+	if (faccessat(AT_FDCWD, path, R_OK | W_OK | X_OK,
 AT_EACCESS) != 0)
 		goto out;
 
Quoted 1 line

rc = SEMANAGE_CAN_WRITE;

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

Signature · 1 line
Petr Lautrbach
#6 lore raw
Quoted 41 lines · Petr Lautrbach
Quoted 9 lines · Stephen Smalley
Quoted 7 lines · Vit Mojzis

Use faccessat() with AT_EACCESS instead of accesss() in order to check
permissions of effective user. access() calls checking existence of
a file (F_OK) were left untouched since they work correctly.

Quoted 4 lines
Quoted 1 line

This enables setuid programs to use libsemanage.

Not sure we want setuid programs using libsemanage.  Is there a use case for that?  I wouldn't warrant it to be safe.

The motivation is described in the response from the original reporter
in the bug:

---
I think the reason why I filed the bug back then was
https://fedorahosted.org/sssd/ticket/2564

In general I don't think the bug is a big deal to us and if upstream is
reluctant to this change, just close the bug. I just found it odd to check if a file exists before acting on it instead of just trying to work with the file and failing on errors..the current approach seems a
bit racy to me.

About the question in the thread that asks why do we use the selinux libraries in a setuid library..the reason is that in order to pass certain certifications, no code in SSSD that deals with network connections should run as root. Therefore, the SSSD itself runs as a nonprivileged user and for actions that require root privileges (like setting a selinux context for a user) we fork our a setgid helper that
actually does the work.
---

Quoted 3 lines

I think it's about situations when a process can create e.g. /var/lib/selinux/targeted/semanage.read.LOCK001 using fopen(), but libsemanage denies to work as it thinks it can't access the store.

So then I would suggest looking at removing the use of access() checks altogether, aside from trivial F_OK and X_OK tests that should be harmless.

author
#7 lore raw

libsemanage: remove/replace access() checks to make setuid programs work

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.

Following patches remove redundant access checks (where the access check was followed by open, write,etc. call and the return value is checked), and replace necessary "access(, F_OK)" checks by "stats()" (e.g. in case where existence of a file determines if hll module compilation is necessary, or represents some setting - such as preserve_tunables).

RHBZ #1186431

libsemanage/src/direct_api.c | 79 ++++++++++++++++++++++++++++++++++++-------------------------------------------
libsemanage/src/semanage_store.c | 17 ++++++++---------

2 files changed, 44 insertions(+), 52 deletions(-)
author
#8 lore raw

PATCH 1/3 libsemanage: remove access() check to make setuid programs work

access() uses real UID instead of effective UID which causes false negative checks in setuid programs (except for F_OK which works properly). fopen() return values are always checked, which makes access() checks redundant.

---
 libsemanage/src/direct_api.c     |  3 ---
 libsemanage/src/semanage_store.c | 17 ++++++++---------
 2 files changed, 8 insertions(+), 12 deletions(-)
libsemanage/src/direct_api.c +0 -3
index f4b0416..dc46f8f 100644
@@ -146,9 +146,6 @@ int semanage_direct_connect(semanage_handle_t * sh)
 		if (semanage_create_store(sh, 1))
 			goto err;
 
-	if (semanage_access_check(sh) < SEMANAGE_CAN_READ)
-		goto err;
-
 	sh->u.direct.translock_file_fd = -1;
 	sh->u.direct.activelock_file_fd = -1;
 
libsemanage/src/semanage_store.c +8 -9
index 6b75002..0a10032 100644
@@ -535,7 +535,6 @@ char *semanage_conf_path(void)
 int semanage_create_store(semanage_handle_t * sh, int create)
 {
 	struct stat sb;
-	int mode_mask = R_OK | W_OK | X_OK;
 	const char *path = semanage_files[SEMANAGE_ROOT];
 	int fd;
 
@@ -554,9 +553,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store at %s, or it is not a directory.",
+			    "Module store at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -577,9 +576,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store active subdirectory at %s, or it is not a directory.",
+			    "Module store active subdirectory at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -600,9 +599,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store active modules subdirectory at %s, or it is not a directory.",
+			    "Module store active modules subdirectory at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -621,8 +620,8 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK | W_OK) == -1) {
-			ERR(sh, "Could not access lock file at %s.", path);
+		if (!S_ISREG(sb.st_mode)) {
+			ERR(sh, "Lock file at %s missing.", path);
 			return -1;
 		}
 	}
Signature · 1 line
2.9.3
#9 lore raw

Re: [PATCH 1/3] libsemanage: remove access() check to make setuid programs work

Quoted 89 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs (except for F_OK which works properly). fopen() return values are always checked, which makes access() checks redundant.

---
 libsemanage/src/direct_api.c     |  3 --- libsemanage/src/semanage_store.c | 17 ++++++++---------

 2 files changed, 8 insertions(+), 12 deletions(-)
libsemanage/src/direct_api.c +0 -0
index f4b0416..dc46f8f 100644
@@ -146,9 +146,6 @@ int semanage_direct_connect(semanage_handle_t *
 sh)
  		if (semanage_create_store(sh, 1))
  			goto err;

libsemanage/src/direct_api.c +0 -3
-	if (semanage_access_check(sh) < SEMANAGE_CAN_READ)
-		goto err;
-

  sh->u.direct.translock_file_fd = -1;
  sh->u.direct.activelock_file_fd = -1;

libsemanage/src/semanage_store.c +0 -1
index 6b75002..0a10032 100644
@@ -535,7 +535,6 @@ char *semanage_conf_path(void)
  int semanage_create_store(semanage_handle_t * sh, int create)
  {
  	struct stat sb;
-	int mode_mask = R_OK | W_OK | X_OK;
  	const char *path = semanage_files[SEMANAGE_ROOT];
  	int fd;

libsemanage/src/semanage_store.c +2 -2
@@ -554,9 +553,9 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
  			ERR(sh,
-			    "Could not access module store at %s, or
 it is not a directory.",
+			    "Module store at %s is not a

directory.",
      path);
  return -1;
  }

libsemanage/src/semanage_store.c +2 -2
@@ -577,9 +576,9 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
  			ERR(sh,
-			    "Could not access module store active
 subdirectory at %s, or it is not a directory.",
+			    "Module store active subdirectory at %s

is not a directory.",
      path);
  return -1;
  }

libsemanage/src/semanage_store.c +2 -2
@@ -600,9 +599,9 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask)
 == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
  			ERR(sh,
-			    "Could not access module store active
 modules subdirectory at %s, or it is not a directory.",
+			    "Module store active modules

subdirectory at %s is not a directory.",
      path);
  return -1;
  }

libsemanage/src/semanage_store.c int semanage_create_store(semanage_handle_t * sh, · line 627

Quoted 10 lines
libsemanage/src/semanage_store.c +2 -2
@@ -621,8 +620,8 @@ int semanage_create_store(semanage_handle_t * sh,
 int create)
  			return -1;
  		}
  	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK |
 W_OK) == -1) {
-			ERR(sh, "Could not access lock file at %s.",
 path);
+		if (!S_ISREG(sb.st_mode)) {
+			ERR(sh, "Lock file at %s missing.", path);

It can't be missing or the stat() would have failed. Lock file is not a regular file?
Not sure how useful these tests are though...

Quoted 3 lines

  return -1;
  }
  }

author
#10 lore raw

PATCH 2/3 libsemanage: remove access() check to make setuid programs work

In reply to Vit Mojzis

F_OK access checks only work properly as long as all directories along the path are accessible to real user running the program. Replace F_OK access checks by testing return value of open, write, etc.

---
 libsemanage/src/direct_api.c | 40 +++++++++++++++-------------------------
 1 file changed, 15 insertions(+), 25 deletions(-)
libsemanage/src/direct_api.c +15 -25
index dc46f8f..508277d 100644
@@ -342,10 +342,6 @@ static int semanage_direct_disconnect(semanage_handle_t * sh)
 
 static int semanage_direct_begintrans(semanage_handle_t * sh)
 {
-
-	if (semanage_access_check(sh) != SEMANAGE_CAN_WRITE) {
-		return -1;
-	}
 	if (semanage_get_trans_lock(sh) < 0) {
 		return -1;
 	}
@@ -1491,33 +1487,27 @@ rebuild:
 	}
 
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
 	/* run genhomedircon if its enabled, this should be the last operation
Signature · 1 line
2.9.3
#11 lore raw

Re: [PATCH 2/3] libsemanage: remove access() check to make setuid programs work

Quoted 5 lines · Vit Mojzis

F_OK access checks only work properly as long as all directories along
the path are accessible to real user running the program. Replace F_OK access checks by testing return value of open, write, etc.

The patch description talks about F_OK tests, but...

Quoted 17 lines

---
 libsemanage/src/direct_api.c | 40 +++++++++++++++-------------------
------

 1 file changed, 15 insertions(+), 25 deletions(-)
libsemanage/src/direct_api.c +0 -0
index dc46f8f..508277d 100644
@@ -342,10 +342,6 @@ static int
 semanage_direct_disconnect(semanage_handle_t * sh)


 static int semanage_direct_begintrans(semanage_handle_t * sh) {

libsemanage/src/direct_api.c static int semanage_direct_disconnect(semanage_handle_t * sh) · line 348

Quoted 4 lines
libsemanage/src/direct_api.c +0 -4
-
-	if (semanage_access_check(sh) != SEMANAGE_CAN_WRITE) {
-		return -1;
-	}

The patch has an unrelated change (possibly belonging in the first patch?).

Quoted 73 lines

  if (semanage_get_trans_lock(sh) < 0) {
  return -1;
  }

libsemanage/src/direct_api.c +0 -0
@@ -1491,33 +1487,27 @@ rebuild:
  	}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);

libsemanage/src/direct_api.c +0 -2
-	if (access(path, F_OK) == 0) {
-		retval =

semanage_copy_file(semanage_path(SEMANAGE_TMP,
SEMANAGE_STORE_FC_LOCAL),

libsemanage/src/direct_api.c +0 -1
-							semanage_fin

al_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),

libsemanage/src/direct_api.c +0 -1
-							sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +1 -3
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP,

SEMANAGE_STORE_FC_LOCAL),

libsemanage/src/direct_api.c +1 -0
+						semanage_final_path(

SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),

libsemanage/src/direct_api.c +1 -0
+						sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +2 -0
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;

  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);

libsemanage/src/direct_api.c +0 -2
-	if (access(path, F_OK) == 0) {
-		retval =

semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),

libsemanage/src/direct_api.c +0 -1
-							semanage_fin

al_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),

libsemanage/src/direct_api.c +0 -1
-							sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +1 -3
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP,

SEMANAGE_STORE_FC),

libsemanage/src/direct_api.c +1 -0
+						semanage_final_path(

SEMANAGE_FINAL_TMP, SEMANAGE_FC),

libsemanage/src/direct_api.c +1 -0
+						sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +2 -0
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;

  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);

libsemanage/src/direct_api.c +0 -2
-	if (access(path, F_OK) == 0) {
-		retval =

semanage_copy_file(semanage_path(SEMANAGE_TMP,
SEMANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +0 -1
-							semanage_fin

al_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),

libsemanage/src/direct_api.c +0 -1
-							sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +1 -3
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP,

SEMANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +1 -0
+						semanage_final_path(

SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),

libsemanage/src/direct_api.c +1 -0
+						sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +2 -0
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;

  }

  /* run genhomedircon if its enabled, this should be the last operation

author
#12 lore raw

PATCH 3/3 libsemanage: replace access(, F_OK) checks to make setuid programs work

In reply to Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat().

---
 libsemanage/src/direct_api.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
libsemanage/src/direct_api.c +21 -15
index 508277d..35ca1b0 100644
@@ -272,7 +272,9 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
 	/* set the disable dontaudit value */
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+
+	struct stat sb;
+	if (stat(path, &sb) == 0)
 		sepol_set_disable_dontaudit(sh->sepolh, 1);
 	else
 		sepol_set_disable_dontaudit(sh->sepolh, 0);
@@ -1101,8 +1103,9 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
 			goto cleanup;
 		}
 
+		struct stat sb;
 		if (semanage_get_ignore_module_cache(sh) == 0 &&
-				access(cil_path, F_OK) == 0) {
+				stat(cil_path, &sb) == 0) {
 			continue;
 		}
 
@@ -1165,7 +1168,8 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
 	/* Create or remove the disable_dontaudit flag file. */
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+	struct stat sb;
+	if (stat(path, &sb) == 0)
 		do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
 	else
 		do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
@@ -1190,7 +1194,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
 	/* Create or remove the preserve_tunables flag file. */
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)
 		do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
 	else
 		do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
@@ -1231,37 +1235,37 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 	 */
 	if (!do_rebuild) {
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
@@ -1395,7 +1399,7 @@ rebuild:
 			goto cleanup;
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			retval = semanage_copy_file(path,
 						    semanage_path(SEMANAGE_TMP,
 								  SEMANAGE_STORE_SEUSERS),
@@ -1408,7 +1412,7 @@ rebuild:
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			retval = semanage_copy_file(path,
 						    semanage_path(SEMANAGE_TMP,
 								  SEMANAGE_USERS_EXTRA),
@@ -1732,7 +1736,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 		goto cleanup;
 	}
 
-	if (access(module_path, F_OK) != 0) {
+	struct stat sb;
+	if (stat(module_path, &sb) != 0) {
 		ERR(sh, "Module does not exist: %s", module_path);
 		rc = -1;
 		goto cleanup;
@@ -1762,7 +1767,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 		goto cleanup;
 	}
 
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
 		rc = semanage_compile_module(sh, _modinfo);
 		if (rc < 0) {
 			goto cleanup;
@@ -2737,7 +2742,8 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
 			goto cleanup;
 		}
 
-		if (access(path, F_OK) == 0) {
+		struct stat sb;
+		if (stat(path, &sb) == 0) {
 			ret = unlink(path);
 			if (ret != 0) {
 				ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
Signature · 1 line
2.9.3
#13 lore raw

Re: [PATCH 3/3] libsemanage: replace access(, F_OK) checks to make setuid programs work

Quoted 23 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat().

---
 libsemanage/src/direct_api.c | 36 +++++++++++++++++++++------------- --

 1 file changed, 21 insertions(+), 15 deletions(-)
libsemanage/src/direct_api.c +0 -0
index 508277d..35ca1b0 100644
@@ -272,7 +272,9 @@ int semanage_direct_connect(semanage_handle_t *
 sh)


  /* set the disable dontaudit value */
  path = semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c int semanage_direct_connect(semanage_handle_t * sh) · line 277

Quoted 4 lines
libsemanage/src/direct_api.c +3 -1
-	if (access(path, F_OK) == 0)
+
+	struct stat sb;
+	if (stat(path, &sb) == 0)

Need to check errno as well to ensure that it is just ENOENT when stat() returns non-zero; anything else is an error.

stat() can produce a SELinux getattr denial, whereas access(F_OK) doesn't perform any SELinux check beyond directory search access.

Quoted 151 lines

  sepol_set_disable_dontaudit(sh->sepolh, 1); else sepol_set_disable_dontaudit(sh->sepolh, 0);

libsemanage/src/direct_api.c +0 -0
@@ -1101,8 +1103,9 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
  			goto cleanup;
  		}

libsemanage/src/direct_api.c +1 -0
+		struct stat sb;

  if (semanage_get_ignore_module_cache(sh) == 0 &&

libsemanage/src/direct_api.c +1 -1
-				access(cil_path, F_OK) == 0) {
+				stat(cil_path, &sb) == 0) {

  continue;
  }

libsemanage/src/direct_api.c +0 -0
@@ -1165,7 +1168,8 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


  /* Create or remove the disable_dontaudit flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c +2 -1
-	if (access(path, F_OK) == 0)
+	struct stat sb;
+	if (stat(path, &sb) == 0)

  do_rebuild |= !(sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

  else
  do_rebuild |= (sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +0 -0
@@ -1190,7 +1194,7 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


  /* Create or remove the preserve_tunables flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);

libsemanage/src/direct_api.c +1 -1
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)

  do_rebuild |= !(sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

  else
  do_rebuild |= (sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +1 -1
@@ -1231,37 +1235,37 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)
  	 */
  	if (!do_rebuild) {
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1;
  goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1;
  goto rebuild;
  }

libsemanage/src/direct_api.c +0 -0
@@ -1395,7 +1399,7 @@ rebuild:
  			goto cleanup;


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

  retval = semanage_copy_file(path, semanage_path(SE MANAGE_TMP,
    SE MANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +0 -0
@@ -1408,7 +1412,7 @@ rebuild:
  		}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

  retval = semanage_copy_file(path, semanage_path(SE MANAGE_TMP,
    SE MANAGE_USERS_EXTRA),

libsemanage/src/direct_api.c +0 -0
@@ -1732,7 +1736,8 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}

libsemanage/src/direct_api.c +2 -1
-	if (access(module_path, F_OK) != 0) {
+	struct stat sb;
+	if (stat(module_path, &sb) != 0) {

  ERR(sh, "Module does not exist: %s", module_path); rc = -1;
  goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -1762,7 +1767,7 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}

libsemanage/src/direct_api.c +0 -1
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&

access(input_file, F_OK) != 0) {

libsemanage/src/direct_api.c +1 -0
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&

stat(input_file, &sb) != 0) {
  rc = semanage_compile_module(sh, _modinfo); if (rc < 0) {
  goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -2737,7 +2742,8 @@ static int
 semanage_direct_install_info(semanage_handle_t *sh,
  			goto cleanup;
  		}

libsemanage/src/direct_api.c +2 -1
-		if (access(path, F_OK) == 0) {
+		struct stat sb;
+		if (stat(path, &sb) == 0) {

  ret = unlink(path);
  if (ret != 0) {
  ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));

author
#14 lore raw

Re: [PATCH 3/3] libsemanage: replace access(, F_OK) checks to make setuid programs work

Quoted 21 lines · Stephen Smalley
Quoted 20 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat().

---
  libsemanage/src/direct_api.c | 36 +++++++++++++++++++++-------------
--
  1 file changed, 21 insertions(+), 15 deletions(-)
libsemanage/src/direct_api.c +0 -0
index 508277d..35ca1b0 100644
@@ -272,7 +272,9 @@ int semanage_direct_connect(semanage_handle_t *
 sh)
 
Quoted 9 lines

libsemanage/src/direct_api.c int semanage_direct_connect(semanage_handle_t * sh) · line 277

Quoted 7 lines
libsemanage/src/direct_api.c +3 -1
  	/* set the disable dontaudit value */
  	path = semanage_path(SEMANAGE_ACTIVE,
 SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+
+	struct stat sb;
+	if (stat(path, &sb) == 0)

Need to check errno as well to ensure that it is just ENOENT when stat() returns non-zero; anything else is an error.

Thank you for the feedback. I was trying to keep behavioral changes to a minimum. In case "access" call failed (it can encounter all the errors that "stat" can except for EOVERFLOW) the code assumed that the file doesn't exist and this patch preserves said behavior. But if you think think that the calling function should fail instead, I can rework the patch.

Quoted 3 lines

stat() can produce a SELinux getattr denial, whereas access(F_OK) doesn't perform any SELinux check beyond directory search access.

That is a fair point and I don't have a solution for it. However in my understanding the calling process should have the "getattr" access permission (and it should even be able to create the file in most cases).

Quoted 152 lines
Quoted 152 lines
libsemanage/src/direct_api.c +0 -0
  		sepol_set_disable_dontaudit(sh->sepolh, 1);
  	else
  		sepol_set_disable_dontaudit(sh->sepolh, 0);
libsemanage/src/direct_api.c +4 -2
@@ -1101,8 +1103,9 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
  			goto cleanup;
  		}
 
+		struct stat sb;
  		if (semanage_get_ignore_module_cache(sh) == 0 &&
-				access(cil_path, F_OK) == 0) {
+				stat(cil_path, &sb) == 0) {
  			continue;
  		}
@@ -1165,7 +1168,8 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)
 
  	/* Create or remove the disable_dontaudit flag file. */
  	path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+	struct stat sb;
+	if (stat(path, &sb) == 0)
  		do_rebuild |= !(sepol_get_disable_dontaudit(sh-
Quoted 1 line

sepolh) == 1);

else
do_rebuild |= (sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +1 -1
@@ -1190,7 +1194,7 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)
 
  	/* Create or remove the preserve_tunables flag file. */
  	path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_PRESERVE_TUNABLES);
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)
  		do_rebuild |= !(sepol_get_preserve_tunables(sh-
Quoted 1 line

sepolh) == 1);

else
do_rebuild |= (sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +5 -5
@@ -1231,37 +1235,37 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)
  	 */
  	if (!do_rebuild) {
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}
 
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_STORE_FC);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}
 
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_STORE_SEUSERS);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}
 
  		path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}
 
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}
 

path = semanage_path(SEMANAGE_TMP,
SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +2 -2
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}
@@ -1395,7 +1399,7 @@ rebuild:
  			goto cleanup;
 
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
  			retval = semanage_copy_file(path,
  						    semanage_path(SE

MANAGE_TMP,
SE
MANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +1 -1
@@ -1408,7 +1412,7 @@ rebuild:
  		}
 
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
  			retval = semanage_copy_file(path,
  						    semanage_path(SE

MANAGE_TMP,
SE
MANAGE_USERS_EXTRA),

libsemanage/src/direct_api.c +2 -1
@@ -1732,7 +1736,8 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}
 
-	if (access(module_path, F_OK) != 0) {
+	struct stat sb;
+	if (stat(module_path, &sb) != 0) {
  		ERR(sh, "Module does not exist: %s", module_path);
  		rc = -1;

goto cleanup;

libsemanage/src/direct_api.c +1 -1
@@ -1762,7 +1767,7 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}
 
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&
 access(input_file, F_OK) != 0) {
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&
 stat(input_file, &sb) != 0) {
  		rc = semanage_compile_module(sh, _modinfo);
  		if (rc < 0) {
  			goto cleanup;
libsemanage/src/direct_api.c +2 -1
@@ -2737,7 +2742,8 @@ static int
 semanage_direct_install_info(semanage_handle_t *sh,
  			goto cleanup;
  		}
 
-		if (access(path, F_OK) == 0) {
+		struct stat sb;
+		if (stat(path, &sb) == 0) {
  			ret = unlink(path);
  			if (ret != 0) {

ERR(sh, "Error while removing cached
CIL file %s: %s", path, strerror(errno));

#15 lore raw

Re: [PATCH 3/3] libsemanage: replace access(, F_OK) checks to make setuid programs work

Quoted 36 lines · Vit Mojzis
Quoted 26 lines · Stephen Smalley
Quoted 25 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false
negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat().

---
  libsemanage/src/direct_api.c | 36 +++++++++++++++++++++------
-------
--

  1 file changed, 21 insertions(+), 15 deletions(-)
libsemanage/src/direct_api.c +0 -0
index 508277d..35ca1b0 100644
@@ -272,7 +272,9 @@ int semanage_direct_connect(semanage_handle_t
 *
 sh)


   /* set the disable dontaudit value */
   path = semanage_path(SEMANAGE_ACTIVE,

Quoted 8 lines

libsemanage/src/direct_api.c int semanage_direct_connect(semanage_handle_t * sh) · line 277

Quoted 5 lines

SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c +3 -1
-	if (access(path, F_OK) == 0)
+
+	struct stat sb;
+	if (stat(path, &sb) == 0)

Need to check errno as well to ensure that it is just ENOENT when stat() returns non-zero; anything else is an error.

Quoted 9 lines

Thank you for the feedback. I was trying to keep behavioral changes to a
minimum. In case "access" call failed (it can encounter all the errors
that "stat" can except for EOVERFLOW) the code assumed that the file doesn't exist and this patch preserves said behavior.
But if you think think that the calling function should fail instead, I
can rework the patch.

Except that use of stat() does introduce one additional potential failure case, i.e. SELinux getattr permission denial on the file. Unlikely, of course, but possible. And since you removed the semanage_access_check() in the earlier patch, we are no longer checking that we can even read/search the active directory prior to making this call. So our first permission denial (on either the active directory or the file itself) might occur here, and we would wrongly interpret it as the file not existing after your change.

It is also more correct to explicitly test errno for ENOENT regardless, so we might as well fix that while we are making these changes.

Quoted 8 lines
Quoted 2 lines

stat() can produce a SELinux getattr denial, whereas access(F_OK) doesn't perform any SELinux check beyond directory search access.

That is a fair point and I don't have a solution for it. However in my
understanding the calling process should have the "getattr" access permission (and it should even be able to create the file in most cases).

Yes, I'm not concerned about needing getattr permission itself; I just want to ensure that we correctly handle it when it is denied.

Quoted 166 lines
Quoted 166 lines
Quoted 166 lines

   sepol_set_disable_dontaudit(sh->sepolh, 1); else sepol_set_disable_dontaudit(sh->sepolh, 0);

libsemanage/src/direct_api.c +0 -0
@@ -1101,8 +1103,9 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
   			goto cleanup;
   		}

libsemanage/src/direct_api.c +1 -0
+		struct stat sb;

   if (semanage_get_ignore_module_cache(sh) == 0 &&

libsemanage/src/direct_api.c +1 -1
-				access(cil_path, F_OK) == 0) {
+				stat(cil_path, &sb) == 0) {

   continue;
   }

libsemanage/src/direct_api.c +0 -0
@@ -1165,7 +1168,8 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


   /* Create or remove the disable_dontaudit flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c +2 -1
-	if (access(path, F_OK) == 0)
+	struct stat sb;
+	if (stat(path, &sb) == 0)

   do_rebuild |= !(sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

   else
   do_rebuild |= (sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +0 -0
@@ -1190,7 +1194,7 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


   /* Create or remove the preserve_tunables flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);

libsemanage/src/direct_api.c +1 -1
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)

   do_rebuild |= !(sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

   else
   do_rebuild |= (sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +1 -1
@@ -1231,37 +1235,37 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)
   	 */
   	if (!do_rebuild) {
   		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
   			do_rebuild = 1;
   			goto rebuild;
   		}


   path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

   do_rebuild = 1; goto rebuild;
   }

   path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

   do_rebuild = 1; goto rebuild;
   }

   path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

   do_rebuild = 1; goto rebuild;
   }

   path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

   do_rebuild = 1; goto rebuild;
   }

   path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

   do_rebuild = 1;
   goto rebuild;
   }

libsemanage/src/direct_api.c +0 -0
@@ -1395,7 +1399,7 @@ rebuild:
   			goto cleanup;


   path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

   retval = semanage_copy_file(path, semanage_pa th(SE
MANAGE_TMP,

  SE
MANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +0 -0
@@ -1408,7 +1412,7 @@ rebuild:
   		}


   path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

   retval = semanage_copy_file(path, semanage_pa th(SE
MANAGE_TMP,

  SE
MANAGE_USERS_EXTRA),

libsemanage/src/direct_api.c +0 -0
@@ -1732,7 +1736,8 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
   		goto cleanup;
   	}

libsemanage/src/direct_api.c +2 -1
-	if (access(module_path, F_OK) != 0) {
+	struct stat sb;
+	if (stat(module_path, &sb) != 0) {

   ERR(sh, "Module does not exist: %s", module_path);
   rc = -1;
   goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -1762,7 +1767,7 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
   		goto cleanup;
   	}

libsemanage/src/direct_api.c +0 -1
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext,

"cil") &&
access(input_file, F_OK) != 0) {

libsemanage/src/direct_api.c +1 -0
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext,

"cil") &&
stat(input_file, &sb) != 0) {
   rc = semanage_compile_module(sh, _modinfo); if (rc < 0) {
   goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -2737,7 +2742,8 @@ static int
 semanage_direct_install_info(semanage_handle_t *sh,
   			goto cleanup;
   		}

libsemanage/src/direct_api.c +2 -1
-		if (access(path, F_OK) == 0) {
+		struct stat sb;
+		if (stat(path, &sb) == 0) {

   ret = unlink(path);
   if (ret != 0) {
   ERR(sh, "Error while removing cached
CIL file %s: %s", path, strerror(errno));

author
#16 lore raw

PATCH 1/3 libsemanage: remove access() check to make setuid programs work

access() uses real UID instead of effective UID which causes false negative checks in setuid programs. Remove redundant access() checks

---
 libsemanage/src/direct_api.c     |  7 -------
 libsemanage/src/semanage_store.c | 17 ++++++++---------
 2 files changed, 8 insertions(+), 16 deletions(-)
libsemanage/src/direct_api.c +0 -7
index 65842df..b761b68 100644
@@ -148,9 +148,6 @@ int semanage_direct_connect(semanage_handle_t * sh)
 		if (semanage_create_store(sh, 1))
 			goto err;
 
-	if (semanage_access_check(sh) < SEMANAGE_CAN_READ)
-		goto err;
-
 	sh->u.direct.translock_file_fd = -1;
 	sh->u.direct.activelock_file_fd = -1;
 
@@ -373,10 +370,6 @@ static int semanage_direct_disconnect(semanage_handle_t * sh)
 
 static int semanage_direct_begintrans(semanage_handle_t * sh)
 {
-
-	if (semanage_access_check(sh) != SEMANAGE_CAN_WRITE) {
-		return -1;
-	}
 	if (semanage_get_trans_lock(sh) < 0) {
 		return -1;
 	}
libsemanage/src/semanage_store.c +8 -9
index 6158d08..ac84349 100644
@@ -537,7 +537,6 @@ char *semanage_conf_path(void)
 int semanage_create_store(semanage_handle_t * sh, int create)
 {
 	struct stat sb;
-	int mode_mask = R_OK | W_OK | X_OK;
 	const char *path = semanage_files[SEMANAGE_ROOT];
 	int fd;
 
@@ -556,9 +555,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store at %s, or it is not a directory.",
+			    "Module store at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -579,9 +578,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store active subdirectory at %s, or it is not a directory.",
+			    "Module store active subdirectory at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -602,9 +601,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store active modules subdirectory at %s, or it is not a directory.",
+			    "Module store active modules subdirectory at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -623,8 +622,8 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK | W_OK) == -1) {
-			ERR(sh, "Could not access lock file at %s.", path);
+		if (!S_ISREG(sb.st_mode)) {
+			ERR(sh, "Object at %s is not a lock file.", path);
 			return -1;
 		}
 	}
Signature · 1 line
2.9.4
author
#17 lore raw

PATCH 2/3 libsemanage: remove access() check to make setuid programs work

F_OK access checks only work properly as long as all directories along the path are accessible to real user running the program. Replace F_OK access checks by testing return value of open, write, etc.

---
 libsemanage/src/direct_api.c | 36 +++++++++++++++---------------------
 1 file changed, 15 insertions(+), 21 deletions(-)
libsemanage/src/direct_api.c +15 -21
index b761b68..ed11a7c 100644
@@ -1538,33 +1538,27 @@ rebuild:
 	}
 
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
 	/* run genhomedircon if its enabled, this should be the last operation
Signature · 1 line
2.9.4
#18 lore raw

Re: [PATCH 2/3] libsemanage: remove access() check to make setuid programs work

Quoted 82 lines · Vit Mojzis

F_OK access checks only work properly as long as all directories along
the path are accessible to real user running the program. Replace F_OK access checks by testing return value of open, write, etc.

---
 libsemanage/src/direct_api.c | 36 +++++++++++++++------------------- --

 1 file changed, 15 insertions(+), 21 deletions(-)
libsemanage/src/direct_api.c +0 -0
index b761b68..ed11a7c 100644
@@ -1538,33 +1538,27 @@ rebuild:
  	}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);

libsemanage/src/direct_api.c +0 -2
-	if (access(path, F_OK) == 0) {
-		retval =

semanage_copy_file(semanage_path(SEMANAGE_TMP,
SEMANAGE_STORE_FC_LOCAL),

libsemanage/src/direct_api.c +0 -1
-							semanage_fin

al_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),

libsemanage/src/direct_api.c +0 -1
-							sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +1 -3
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP,

SEMANAGE_STORE_FC_LOCAL),

libsemanage/src/direct_api.c +1 -0
+						semanage_final_path(

SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),

libsemanage/src/direct_api.c +1 -0
+						sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +2 -0
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;

  }
  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);

libsemanage/src/direct_api.c +0 -2
-	if (access(path, F_OK) == 0) {
-		retval =

semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),

libsemanage/src/direct_api.c +0 -1
-							semanage_fin

al_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),

libsemanage/src/direct_api.c +0 -1
-							sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +1 -3
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP,

SEMANAGE_STORE_FC),

libsemanage/src/direct_api.c +1 -0
+						semanage_final_path(

SEMANAGE_FINAL_TMP, SEMANAGE_FC),

libsemanage/src/direct_api.c +1 -0
+						sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +2 -0
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;

  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);

libsemanage/src/direct_api.c +0 -2
-	if (access(path, F_OK) == 0) {
-		retval =

semanage_copy_file(semanage_path(SEMANAGE_TMP,
SEMANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +0 -1
-							semanage_fin

al_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),

libsemanage/src/direct_api.c +0 -1
-							sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +1 -3
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP,

SEMANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +1 -0
+						semanage_final_path(

SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),

libsemanage/src/direct_api.c +1 -0
+						sh->conf-
Quoted 1 line

file_mode);

libsemanage/src/direct_api.c +2 -0
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
Quoted 4 lines

  }

  /* run genhomedircon if its enabled, this should be the last operation

Isn't the assignment to path in each of the sequences above rendered moot by your removal of the call to access()? So you could either remove the assignment to path each time, or alternatively retain it but pass path as the first argument to semanage_copy_file() rather than calling semanage_path() again.

author
#19 lore raw

PATCH 3/3 libsemanage: replace access(, F_OK) checks to make setuid programs work

In reply to Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat().

---
 libsemanage/src/direct_api.c | 50 +++++++++++++++++++++++++++++---------------
 1 file changed, 33 insertions(+), 17 deletions(-)
libsemanage/src/direct_api.c +33 -17
index ed11a7c..3d1c354 100644
@@ -296,10 +296,19 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
 	/* set the disable dontaudit value */
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+
+	struct stat sb;
+	if (stat(path, &sb) == 0)
 		sepol_set_disable_dontaudit(sh->sepolh, 1);
-	else
-		sepol_set_disable_dontaudit(sh->sepolh, 0);
+	else{
+		if (errno == ENOENT){
+			sepol_set_disable_dontaudit(sh->sepolh, 0);
+		}
+		else{
+			ERR(sh, "Unable to access disable_dontaudit file at %s: %s\n",path , strerror(errno));
+			goto err;
+		}
+	}
 
 	return STATUS_SUCCESS;
 
@@ -1114,6 +1123,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
 	int status = 0;
 	int i;
 	char cil_path[PATH_MAX];
+	struct stat sb;
 
 	assert(sh);
 	assert(modinfos);
@@ -1130,9 +1140,12 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
 		}
 
 		if (semanage_get_ignore_module_cache(sh) == 0 &&
-				access(cil_path, F_OK) == 0) {
+				(status = stat(cil_path, &sb)) == 0) {
 			continue;
 		}
+		if (status != 0 && status != ENOENT) {
+			goto cleanup; //an error in the "stat" call
+		}
 
 		status = semanage_compile_module(sh, &modinfos[i]);
 		if (status < 0) {
@@ -1200,7 +1213,8 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
 	/* Create or remove the disable_dontaudit flag file. */
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+	struct stat sb;
+	if (stat(path, &sb) == 0)
 		do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
 	else
 		do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
@@ -1225,7 +1239,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
 	/* Create or remove the preserve_tunables flag file. */
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)
 		do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
 	else
 		do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
@@ -1266,37 +1280,37 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 	 */
 	if (!do_rebuild) {
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
 			do_rebuild = 1;
 			goto rebuild;
 		}
@@ -1431,7 +1445,7 @@ rebuild:
 			goto cleanup;
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			retval = semanage_copy_file(path,
 						    semanage_path(SEMANAGE_TMP,
 								  SEMANAGE_STORE_SEUSERS),
@@ -1444,7 +1458,7 @@ rebuild:
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			retval = semanage_copy_file(path,
 						    semanage_path(SEMANAGE_TMP,
 								  SEMANAGE_USERS_EXTRA),
@@ -1785,7 +1799,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 		goto cleanup;
 	}
 
-	if (access(module_path, F_OK) != 0) {
+	struct stat sb;
+	if (stat(module_path, &sb) != 0) {
 		ERR(sh, "Module does not exist: %s", module_path);
 		rc = -1;
 		goto cleanup;
@@ -1815,7 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 		goto cleanup;
 	}
 
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
 		rc = semanage_compile_module(sh, _modinfo);
 		if (rc < 0) {
 			goto cleanup;
@@ -2790,7 +2805,8 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
 			goto cleanup;
 		}
 
-		if (access(path, F_OK) == 0) {
+		struct stat sb;
+		if (stat(path, &sb) == 0) {
 			ret = unlink(path);
 			if (ret != 0) {
 				ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
Signature · 1 line
2.9.4
#20 lore raw

Re: [PATCH 3/3] libsemanage: replace access(, F_OK) checks to make setuid programs work

Quoted 23 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat().

---
 libsemanage/src/direct_api.c | 50 +++++++++++++++++++++++++++++-----
----------

 1 file changed, 33 insertions(+), 17 deletions(-)
libsemanage/src/direct_api.c +0 -0
index ed11a7c..3d1c354 100644
@@ -296,10 +296,19 @@ int semanage_direct_connect(semanage_handle_t *
 sh)


  /* set the disable dontaudit value */
  path = semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c int semanage_direct_connect(semanage_handle_t * sh) · line 300

Quoted 3 lines
libsemanage/src/direct_api.c +2 -1
-	if (access(path, F_OK) == 0)
+
+	struct stat sb;

Please add a blank line after declarations before code, and move this up to the beginning of the function.

libsemanage/src/direct_api.c int semanage_direct_connect(semanage_handle_t * sh) · line 303

Quoted 5 lines
libsemanage/src/direct_api.c +1 -0
+	if (stat(path, &sb) == 0)

  sepol_set_disable_dontaudit(sh->sepolh, 1);

libsemanage/src/direct_api.c +1 -2
-	else
-		sepol_set_disable_dontaudit(sh->sepolh, 0);
+	else{

Please include a space between else and {.

libsemanage/src/direct_api.c int semanage_direct_connect(semanage_handle_t * sh) · line 304

Quoted 1 line
libsemanage/src/direct_api.c +1 -0
+		if (errno == ENOENT){

Please include a space between ) and {.

libsemanage/src/direct_api.c int semanage_direct_connect(semanage_handle_t * sh) · line 307

Quoted 3 lines
libsemanage/src/direct_api.c +3 -0
+			sepol_set_disable_dontaudit(sh->sepolh, 0);
+		}
+		else{

This should be } else { on a single line, with space between else and {.

Quoted 27 lines
libsemanage/src/direct_api.c +1 -0
+			ERR(sh, "Unable to access disable_dontaudit

file at %s: %s\n",path , strerror(errno));

libsemanage/src/direct_api.c +3 -0
+			goto err;
+		}
+	}


  return STATUS_SUCCESS;

libsemanage/src/direct_api.c +1 -0
@@ -1114,6 +1123,7 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
  	int status = 0;
  	int i;
  	char cil_path[PATH_MAX];
+	struct stat sb;


  assert(sh);
  assert(modinfos);

libsemanage/src/direct_api.c +0 -0
@@ -1130,9 +1140,12 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
  		}


  if (semanage_get_ignore_module_cache(sh) == 0 &&

libsemanage/src/direct_api.c +1 -1
-				access(cil_path, F_OK) == 0) {
+				(status = stat(cil_path, &sb)) == 0)

{
  continue;
  }

libsemanage/src/direct_api.c static int semanage_compile_hll_modules(semanage_handle_t *sh, · line 1146

Quoted 1 line
libsemanage/src/direct_api.c +1 -0
+		if (status != 0 && status != ENOENT) {

errno != ENOENT

Also, we should ERR() here since otherwise we won't get any diagnostic output (in the other error cases, the called function handles this, but here we have a direct failure in this function).

libsemanage/src/direct_api.c

Quoted 13 lines
libsemanage/src/direct_api.c +2 -0
+			goto cleanup; //an error in the "stat" call
+		}


  status = semanage_compile_module(sh, &modinfos[i]); if (status < 0) {

libsemanage/src/direct_api.c +0 -0
@@ -1200,7 +1213,8 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


  /* Create or remove the disable_dontaudit flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c +1 -1
-	if (access(path, F_OK) == 0)
+	struct stat sb;

Empty line between declarations and code, move decls to beginning of function.

Quoted 6 lines
libsemanage/src/direct_api.c +1 -0
+	if (stat(path, &sb) == 0)

  do_rebuild |= !(sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

  else
  do_rebuild |= (sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

No errno check here or below?

Quoted 122 lines
libsemanage/src/direct_api.c +0 -0
@@ -1225,7 +1239,7 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


  /* Create or remove the preserve_tunables flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);

libsemanage/src/direct_api.c +1 -1
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)

  do_rebuild |= !(sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

  else
  do_rebuild |= (sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +1 -1
@@ -1266,37 +1280,37 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)
  	 */
  	if (!do_rebuild) {
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1;
  goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1;
  goto rebuild;
  }

libsemanage/src/direct_api.c +0 -0
@@ -1431,7 +1445,7 @@ rebuild:
  			goto cleanup;


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

  retval = semanage_copy_file(path, semanage_path(SE MANAGE_TMP,
    SE MANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +0 -0
@@ -1444,7 +1458,7 @@ rebuild:
  		}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

  retval = semanage_copy_file(path, semanage_path(SE MANAGE_TMP,
    SE MANAGE_USERS_EXTRA),

libsemanage/src/direct_api.c +0 -0
@@ -1785,7 +1799,8 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}

libsemanage/src/direct_api.c +2 -1
-	if (access(module_path, F_OK) != 0) {
+	struct stat sb;
+	if (stat(module_path, &sb) != 0) {

  ERR(sh, "Module does not exist: %s", module_path); rc = -1;
  goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -1815,7 +1830,7 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}

libsemanage/src/direct_api.c +0 -1
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&

access(input_file, F_OK) != 0) {

libsemanage/src/direct_api.c +1 -0
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&

stat(input_file, &sb) != 0) {
  rc = semanage_compile_module(sh, _modinfo); if (rc < 0) {
  goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -2790,7 +2805,8 @@ static int
 semanage_direct_install_info(semanage_handle_t *sh,
  			goto cleanup;
  		}

libsemanage/src/direct_api.c +2 -1
-		if (access(path, F_OK) == 0) {
+		struct stat sb;
+		if (stat(path, &sb) == 0) {

  ret = unlink(path);
  if (ret != 0) {
  ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));

author
#21 lore raw

PATCH 1/3 libsemanage: remove access() check to make setuid programs work

access() uses real UID instead of effective UID which causes false negative checks in setuid programs. Remove redundant access() checks

---
 libsemanage/src/direct_api.c     |  7 -------
 libsemanage/src/semanage_store.c | 17 ++++++++---------
 2 files changed, 8 insertions(+), 16 deletions(-)
libsemanage/src/direct_api.c +0 -7
index 88873c43..b7899d68 100644
@@ -148,9 +148,6 @@ int semanage_direct_connect(semanage_handle_t * sh)
 		if (semanage_create_store(sh, 1))
 			goto err;
 
-	if (semanage_access_check(sh) < SEMANAGE_CAN_READ)
-		goto err;
-
 	sh->u.direct.translock_file_fd = -1;
 	sh->u.direct.activelock_file_fd = -1;
 
@@ -398,10 +395,6 @@ static int semanage_direct_disconnect(semanage_handle_t *sh)
 
 static int semanage_direct_begintrans(semanage_handle_t * sh)
 {
-
-	if (semanage_access_check(sh) != SEMANAGE_CAN_WRITE) {
-		return -1;
-	}
 	if (semanage_get_trans_lock(sh) < 0) {
 		return -1;
 	}
libsemanage/src/semanage_store.c +8 -9
index 936e6495..4bd1d651 100644
@@ -538,7 +538,6 @@ char *semanage_conf_path(void)
 int semanage_create_store(semanage_handle_t * sh, int create)
 {
 	struct stat sb;
-	int mode_mask = R_OK | W_OK | X_OK;
 	const char *path = semanage_files[SEMANAGE_ROOT];
 	int fd;
 
@@ -557,9 +556,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store at %s, or it is not a directory.",
+			    "Module store at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -580,9 +579,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store active subdirectory at %s, or it is not a directory.",
+			    "Module store active subdirectory at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -603,9 +602,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+		if (!S_ISDIR(sb.st_mode)) {
 			ERR(sh,
-			    "Could not access module store active modules subdirectory at %s, or it is not a directory.",
+			    "Module store active modules subdirectory at %s is not a directory.",
 			    path);
 			return -1;
 		}
@@ -624,8 +623,8 @@ int semanage_create_store(semanage_handle_t * sh, int create)
 			return -1;
 		}
 	} else {
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK | W_OK) == -1) {
-			ERR(sh, "Could not access lock file at %s.", path);
+		if (!S_ISREG(sb.st_mode)) {
+			ERR(sh, "Object at %s is not a lock file.", path);
 			return -1;
 		}
 	}
Signature · 1 line
2.14.3
author
#22 lore raw

PATCH 2/3 libsemanage: remove access() check to make setuid programs work

F_OK access checks only work properly as long as all directories along the path are accessible to real user running the program. Replace F_OK access checks by testing return value of open, write, etc.

---
 libsemanage/src/direct_api.c | 39 +++++++++++++++------------------------
 1 file changed, 15 insertions(+), 24 deletions(-)
libsemanage/src/direct_api.c +15 -24
index b7899d68..f4d57cf8 100644
@@ -1563,34 +1563,25 @@ rebuild:
 		goto cleanup;
 	}
 
-	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
-	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
-	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
 	/* run genhomedircon if its enabled, this should be the last operation
Signature · 1 line
2.14.3
#23 lore raw

Re: [PATCH 2/3] libsemanage: remove access() check to make setuid programs work

Quoted 66 lines · Vit Mojzis

F_OK access checks only work properly as long as all directories along the path are accessible to real user running the program. Replace F_OK access checks by testing return value of open, write, etc.

---
 libsemanage/src/direct_api.c | 39 +++++++++++++++------------------------
 1 file changed, 15 insertions(+), 24 deletions(-)
libsemanage/src/direct_api.c +15 -25
index b7899d68..f4d57cf8 100644
@@ -1563,34 +1563,25 @@ rebuild:
                goto cleanup;
        }
 
-       path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);
-       if (access(path, F_OK) == 0) {
-               retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
-                                                       semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
-                                                       sh->conf->file_mode);
-               if (retval < 0) {
-                       goto cleanup;
-               }
+       retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
+                                               semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
+                                               sh->conf->file_mode);
+       if (retval < 0 && errno != ENOENT) {
+               goto cleanup;
        }
 
-       path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-       if (access(path, F_OK) == 0) {
-               retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
-                                                       semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
-                                                       sh->conf->file_mode);
-               if (retval < 0) {
-                       goto cleanup;
-               }
+       retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
+                                               semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
+                                               sh->conf->file_mode);
+       if (retval < 0 && errno != ENOENT) {
+               goto cleanup;
        }
 
-       path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-       if (access(path, F_OK) == 0) {
-               retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
-                                                       semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
-                                                       sh->conf->file_mode);
-               if (retval < 0) {
-                       goto cleanup;
-               }
+       retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
+                                               semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
+                                               sh->conf->file_mode);
+       if (retval < 0 && errno != ENOENT) {
+               goto cleanup;
        }
 
        /* run genhomedircon if its enabled, this should be the last operation
--
Quoted 3 lines

2.14.3

tentative ack. I need to run tests on this one, but it looks fine. I suspect this should work looking at semanage_copy_file() internals.

#24 lore raw

Re: [PATCH 2/3] libsemanage: remove access() check to make setuid programs work

In reply to Vit Mojzis
Quoted 53 lines · Vit Mojzis

F_OK access checks only work properly as long as all directories along the path are accessible to real user running the program. Replace F_OK access checks by testing return value of open, write, etc.

---
 libsemanage/src/direct_api.c | 39 +++++++++++++++------------------------
 1 file changed, 15 insertions(+), 24 deletions(-)
libsemanage/src/direct_api.c +10 -20
index b7899d68..f4d57cf8 100644
@@ -1563,34 +1563,25 @@ rebuild:
 		goto cleanup;
 	}
 
-	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
-	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}
 
-	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-	if (access(path, F_OK) == 0) {
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),

libsemanage/src/direct_api.c rebuild: · line 1585

Quoted 10 lines
libsemanage/src/direct_api.c +5 -4
-							sh->conf->file_mode);
-		if (retval < 0) {
-			goto cleanup;
-		}
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
+						sh->conf->file_mode);
+	if (retval < 0 && errno != ENOENT) {
+		goto cleanup;
 	}

I think we need to clear retval here to ensure that we don't later misinterpret a negative retval as an error. Otherwise, if disable_genhomedircon && !do_install, we'll fall through to the end of the function without ever setting retval again, so the mere absence of a seusers file will cause a fatal error from semanage_direct_commit.

Quoted 2 lines
libsemanage/src/direct_api.c +0 -0
 	/* run genhomedircon if its enabled, this should be the last operation
#25 lore raw

Re: [PATCH 2/3] libsemanage: remove access() check to make setuid programs work

Quoted 62 lines · Stephen Smalley
Quoted 61 lines · Vit Mojzis

F_OK access checks only work properly as long as all directories along the path are accessible to real user running the program. Replace F_OK access checks by testing return value of open, write, etc.

---
 libsemanage/src/direct_api.c | 39 +++++++++++++++------------------------
 1 file changed, 15 insertions(+), 24 deletions(-)
libsemanage/src/direct_api.c +14 -24
index b7899d68..f4d57cf8 100644
@@ -1563,34 +1563,25 @@ rebuild:
              goto cleanup;
      }
 
-     path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);
-     if (access(path, F_OK) == 0) {
-             retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
-                                                     semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
-                                                     sh->conf->file_mode);
-             if (retval < 0) {
-                     goto cleanup;
-             }
+     retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
+                                             semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
+                                             sh->conf->file_mode);
+     if (retval < 0 && errno != ENOENT) {
+             goto cleanup;
      }
 
-     path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-     if (access(path, F_OK) == 0) {
-             retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
-                                                     semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
-                                                     sh->conf->file_mode);
-             if (retval < 0) {
-                     goto cleanup;
-             }
+     retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
+                                             semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
+                                             sh->conf->file_mode);
+     if (retval < 0 && errno != ENOENT) {
+             goto cleanup;
      }
 
-     path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-     if (access(path, F_OK) == 0) {
-             retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
-                                                     semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
-                                                     sh->conf->file_mode);
-             if (retval < 0) {
-                     goto cleanup;
-             }
+     retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
+                                             semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
+                                             sh->conf->file_mode);
+     if (retval < 0 && errno != ENOENT) {
Quoted 9 lines

libsemanage/src/direct_api.c rebuild: · line 1585

Quoted 2 lines
libsemanage/src/direct_api.c +1 -0
+             goto cleanup;
      }

I think we need to clear retval here to ensure that we don't later misinterpret a negative retval as an error. Otherwise, if disable_genhomedircon && !do_install, we'll fall through to the end of the function without ever setting retval again, so the mere absence of a seusers file will cause a fatal error from semanage_direct_commit.

That's spot on. That is precisely what happens. In the case retval is -1 and errno IS
ENOENT, retval needs to be set to 0.

Maybe just make a static helper so this way you don't have to even worry about errno here:

static int copy_file(...) {
int rc = semanage_copy_file(...);
return !(rc < 0 && errno != ENOENT);
}

It would make that main code simpler to read and then retval should be 0 on ENOENT case.

Quoted 2 lines
Quoted 2 lines
libsemanage/src/direct_api.c +0 -0
      /* run genhomedircon if its enabled, this should be the last operation
Signature · 3 lines
Respectfully,

William C Roberts
author
#26 lore raw

PATCH 3/3 libsemanage: replace access() checks to make setuid programs work

In reply to Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +86 -46
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
 	const char *path;
+	struct stat sb;
 
 	if (semanage_check_init(sh, sh->conf->store_root_path))
 		goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
 	/* set the disable dontaudit value */
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+
+	if (stat(path, &sb) == 0)
 		sepol_set_disable_dontaudit(sh->sepolh, 1);
-	else
+	else {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			goto err;
+		}
+
 		sepol_set_disable_dontaudit(sh->sepolh, 0);
+	}
 
 	return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
 	int status = 0;
 	int i;
 	char cil_path[PATH_MAX];
+	struct stat sb;
 
 	assert(sh);
 	assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
 		}
 
 		if (semanage_get_ignore_module_cache(sh) == 0 &&
-				access(cil_path, F_OK) == 0) {
+				(status = stat(cil_path, &sb)) == 0) {
 			continue;
 		}
+		if (status != 0 && errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+			goto cleanup; //an error in the "stat" call
+		}
 
 		status = semanage_compile_module(sh, &modinfos[i]);
 		if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 	struct cil_db *cildb = NULL;
 	semanage_module_info_t *modinfos = NULL;
 	mode_t mask = umask(0077);
+	struct stat sb;
 
 	int do_rebuild, do_write_kernel, do_install;
 	int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
 	/* Create or remove the disable_dontaudit flag file. */
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)
 		do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-	else
+	else {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			goto cleanup;
+		}
+
 		do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+	}
 	if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
 		FILE *touch;
 		touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
 	/* Create or remove the preserve_tunables flag file. */
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)
 		do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-	else
+	else {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			goto cleanup;
+		}
+
 		do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+	}
+
 	if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
 		FILE *touch;
 		touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 	 * a rebuild.
 	 */
 	if (!do_rebuild) {
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
-
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
-
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
-
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
-
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
+		int files[] = {SEMANAGE_STORE_KERNEL,
+					   SEMANAGE_STORE_FC,
+					   SEMANAGE_STORE_SEUSERS,
+					   SEMANAGE_LINKED,
+					   SEMANAGE_SEUSERS_LINKED,
+					   SEMANAGE_USERS_EXTRA_LINKED};
+
+		for (i = 0; i < (int) sizeof(files); i++) {
+			path = semanage_path(SEMANAGE_TMP, files[i]);
+			if (stat(path, &sb) != 0) {
+				if (errno != ENOENT) {
+					ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+					goto cleanup;
+				}
 
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
+				do_rebuild = 1;
+				goto rebuild;
+			}
 		}
 	}
 
@@ -1457,7 +1468,7 @@ rebuild:
 			goto cleanup;
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			retval = semanage_copy_file(path,
 						    semanage_path(SEMANAGE_TMP,
 								  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
 				goto cleanup;
 			pseusers->dtable->drop_cache(pseusers->dbase);
 		} else {
+			if (errno != ENOENT) {
+				ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+				goto cleanup;
+			}
+
 			pseusers->dtable->clear(sh, pseusers->dbase);
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			retval = semanage_copy_file(path,
 						    semanage_path(SEMANAGE_TMP,
 								  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
 				goto cleanup;
 			pusers_extra->dtable->drop_cache(pusers_extra->dbase);
 		} else {
+			if (errno != ENOENT) {
+				ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+				goto cleanup;
+			}
+
 			pusers_extra->dtable->clear(sh, pusers_extra->dbase);
 		}
 	}
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 	ssize_t _data_len;
 	char *_data;
 	int compressed;
+	struct stat sb;
 
 	/* get path of module */
 	rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 		goto cleanup;
 	}
 
-	if (access(module_path, F_OK) != 0) {
-		ERR(sh, "Module does not exist: %s", module_path);
+	if (stat(module_path, &sb) != 0) {
+		ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
 		rc = -1;
 		goto cleanup;
 	}
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 		goto cleanup;
 	}
 
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+			goto cleanup;
+		}
+
 		rc = semanage_compile_module(sh, _modinfo);
 		if (rc < 0) {
 			goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
 	}
 
 	if (stat(path, &sb) < 0) {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			status = -1;
+			goto cleanup;
+		}
+
 		*enabled = 1;
 	}
 	else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
 	/* set enabled/disabled status */
 	if (stat(fn, &sb) < 0) {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+			status = -1;
+			goto cleanup;
+		}
+
 		ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
 		if (ret != 0) {
 			status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
 	int status = 0;
 	int ret = 0;
 	int type;
+	struct stat sb;
 
 	char path[PATH_MAX];
 	mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
 			goto cleanup;
 		}
 
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			ret = unlink(path);
 			if (ret != 0) {
 				ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +12 -2
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
 	char *semanage_conf = NULL;
 	int len;
+	FILE *fp = NULL;
 
 	len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
 	semanage_conf = calloc(len + 1, sizeof(char));
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
 	snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
 		 SEMANAGE_CONF_FILE);
 
-	if (access(semanage_conf, R_OK) != 0) {
+	//the following replaces access(semanage_conf, R_OK) check
+	if ((fp = fopen(semanage_conf, "r")) != NULL) {
+		fclose(fp);
+	} else {
 		snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
 	}
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
 	int r;
+	struct stat sb;
+
+	if (stat(path, &sb) < 0) {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			return -1;
+		}
 
-	if (access(path, F_OK) != 0) {
 		return 0;
 	}
 
Signature · 1 line
2.14.3
#27 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Quoted 80 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +17 -3
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
        const char *path;
+       struct stat sb;
 
        if (semanage_check_init(sh, sh->conf->store_root_path))
                goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
        /* set the disable dontaudit value */
        path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-       if (access(path, F_OK) == 0)
+
+       if (stat(path, &sb) == 0)
                sepol_set_disable_dontaudit(sh->sepolh, 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto err;
+               }
+
                sepol_set_disable_dontaudit(sh->sepolh, 0);
+       }
 
        return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
        int status = 0;
        int i;
        char cil_path[PATH_MAX];
+       struct stat sb;
 
        assert(sh);
        assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
                }
 
                if (semanage_get_ignore_module_cache(sh) == 0 &&
-                               access(cil_path, F_OK) == 0) {
+                               (status = stat(cil_path, &sb)) == 0) {
                        continue;
                }
+               if (status != 0 && errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+                       goto cleanup; //an error in the "stat" call
+               }
 
                status = semanage_compile_module(sh, &modinfos[i]);
                if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
        struct cil_db *cildb = NULL;
        semanage_module_info_t *modinfos = NULL;
        mode_t mask = umask(0077);
+       struct stat sb;
 
        int do_rebuild, do_write_kernel, do_install;
        int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the disable_dontaudit flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c static int semanage_direct_commit(semanage_handle_t * sh) · line 1250

Quoted 10 lines
libsemanage/src/direct_api.c +7 -2
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+

I am not a huge fan of this if under else block of if (errno !=ENOENT).

maybe this is a bit cleaner:

if (stat() == 0)
//exists
else if (errno == ENOENT)
// doesn't exist
else
//fail

Quoted 242 lines
libsemanage/src/direct_api.c +62 -41
                do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+       }
        if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the preserve_tunables flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+
                do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+       }
+
        if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
         * a rebuild.
         */
        if (!do_rebuild) {
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
+               int files[] = {SEMANAGE_STORE_KERNEL,
+                                          SEMANAGE_STORE_FC,
+                                          SEMANAGE_STORE_SEUSERS,
+                                          SEMANAGE_LINKED,
+                                          SEMANAGE_SEUSERS_LINKED,
+                                          SEMANAGE_USERS_EXTRA_LINKED};
+
+               for (i = 0; i < (int) sizeof(files); i++) {
+                       path = semanage_path(SEMANAGE_TMP, files[i]);
+                       if (stat(path, &sb) != 0) {
+                               if (errno != ENOENT) {
+                                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                                       goto cleanup;
+                               }
 
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
+                               do_rebuild = 1;
+                               goto rebuild;
+                       }
                }
        }
 
@@ -1457,7 +1468,7 @@ rebuild:
                        goto cleanup;
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
                                goto cleanup;
                        pseusers->dtable->drop_cache(pseusers->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pseusers->dtable->clear(sh, pseusers->dbase);
                }
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
                                goto cleanup;
                        pusers_extra->dtable->drop_cache(pusers_extra->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pusers_extra->dtable->clear(sh, pusers_extra->dbase);
                }
        }
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
        ssize_t _data_len;
        char *_data;
        int compressed;
+       struct stat sb;
 
        /* get path of module */
        rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (access(module_path, F_OK) != 0) {
-               ERR(sh, "Module does not exist: %s", module_path);
+       if (stat(module_path, &sb) != 0) {
+               ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
                rc = -1;
                goto cleanup;
        }
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+                       goto cleanup;
+               }
+
                rc = semanage_compile_module(sh, _modinfo);
                if (rc < 0) {
                        goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
        }
 
        if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                *enabled = 1;
        }
        else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
        /* set enabled/disabled status */
        if (stat(fn, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
                if (ret != 0) {
                        status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
        int status = 0;
        int ret = 0;
        int type;
+       struct stat sb;
 
        char path[PATH_MAX];
        mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
                        goto cleanup;
                }
 
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        ret = unlink(path);
                        if (ret != 0) {
                                ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +12 -3
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
        char *semanage_conf = NULL;
        int len;
+       FILE *fp = NULL;
 
        len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
        semanage_conf = calloc(len + 1, sizeof(char));
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
        snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
                 SEMANAGE_CONF_FILE);
 
-       if (access(semanage_conf, R_OK) != 0) {
+       //the following replaces access(semanage_conf, R_OK) check
+       if ((fp = fopen(semanage_conf, "r")) != NULL) {
+               fclose(fp);
+       } else {
                snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
        }
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
        int r;
+       struct stat sb;
+
+       if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       return -1;
+               }
 
-       if (access(path, F_OK) != 0) {
                return 0;
        }
 
--

2.14.3

Signature · 3 lines
Respectfully,

William C Roberts
#28 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Where is patch 2/2, I have yet to see it?

Did something get screwy and is it: [PATCH] libsemanage: Improve warning for installing disabled module

Quoted 352 lines · William Roberts
Quoted 80 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +17 -3
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
        const char *path;
+       struct stat sb;
 
        if (semanage_check_init(sh, sh->conf->store_root_path))
                goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
        /* set the disable dontaudit value */
        path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-       if (access(path, F_OK) == 0)
+
+       if (stat(path, &sb) == 0)
                sepol_set_disable_dontaudit(sh->sepolh, 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto err;
+               }
+
                sepol_set_disable_dontaudit(sh->sepolh, 0);
+       }
 
        return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
        int status = 0;
        int i;
        char cil_path[PATH_MAX];
+       struct stat sb;
 
        assert(sh);
        assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
                }
 
                if (semanage_get_ignore_module_cache(sh) == 0 &&
-                               access(cil_path, F_OK) == 0) {
+                               (status = stat(cil_path, &sb)) == 0) {
                        continue;
                }
+               if (status != 0 && errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+                       goto cleanup; //an error in the "stat" call
+               }
 
                status = semanage_compile_module(sh, &modinfos[i]);
                if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
        struct cil_db *cildb = NULL;
        semanage_module_info_t *modinfos = NULL;
        mode_t mask = umask(0077);
+       struct stat sb;
 
        int do_rebuild, do_write_kernel, do_install;
        int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the disable_dontaudit flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c static int semanage_direct_commit(semanage_handle_t * sh) · line 1250

Quoted 10 lines
libsemanage/src/direct_api.c +7 -2
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+

I am not a huge fan of this if under else block of if (errno !=ENOENT).

maybe this is a bit cleaner:

if (stat() == 0)
//exists
else if (errno == ENOENT)
// doesn't exist
else
//fail

Quoted 239 lines
libsemanage/src/direct_api.c +62 -41
                do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+       }
        if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the preserve_tunables flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+
                do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+       }
+
        if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
         * a rebuild.
         */
        if (!do_rebuild) {
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
+               int files[] = {SEMANAGE_STORE_KERNEL,
+                                          SEMANAGE_STORE_FC,
+                                          SEMANAGE_STORE_SEUSERS,
+                                          SEMANAGE_LINKED,
+                                          SEMANAGE_SEUSERS_LINKED,
+                                          SEMANAGE_USERS_EXTRA_LINKED};
+
+               for (i = 0; i < (int) sizeof(files); i++) {
+                       path = semanage_path(SEMANAGE_TMP, files[i]);
+                       if (stat(path, &sb) != 0) {
+                               if (errno != ENOENT) {
+                                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                                       goto cleanup;
+                               }
 
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
+                               do_rebuild = 1;
+                               goto rebuild;
+                       }
                }
        }
 
@@ -1457,7 +1468,7 @@ rebuild:
                        goto cleanup;
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
                                goto cleanup;
                        pseusers->dtable->drop_cache(pseusers->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pseusers->dtable->clear(sh, pseusers->dbase);
                }
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
                                goto cleanup;
                        pusers_extra->dtable->drop_cache(pusers_extra->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pusers_extra->dtable->clear(sh, pusers_extra->dbase);
                }
        }
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
        ssize_t _data_len;
        char *_data;
        int compressed;
+       struct stat sb;
 
        /* get path of module */
        rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (access(module_path, F_OK) != 0) {
-               ERR(sh, "Module does not exist: %s", module_path);
+       if (stat(module_path, &sb) != 0) {
+               ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
                rc = -1;
                goto cleanup;
        }
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+                       goto cleanup;
+               }
+
                rc = semanage_compile_module(sh, _modinfo);
                if (rc < 0) {
                        goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
        }
 
        if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                *enabled = 1;
        }
        else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
        /* set enabled/disabled status */
        if (stat(fn, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
                if (ret != 0) {
                        status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
        int status = 0;
        int ret = 0;
        int type;
+       struct stat sb;
 
        char path[PATH_MAX];
        mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
                        goto cleanup;
                }
 
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        ret = unlink(path);
                        if (ret != 0) {
                                ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +12 -3
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
        char *semanage_conf = NULL;
        int len;
+       FILE *fp = NULL;
 
        len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
        semanage_conf = calloc(len + 1, sizeof(char));
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
        snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
                 SEMANAGE_CONF_FILE);
 
-       if (access(semanage_conf, R_OK) != 0) {
+       //the following replaces access(semanage_conf, R_OK) check
+       if ((fp = fopen(semanage_conf, "r")) != NULL) {
+               fclose(fp);
+       } else {
                snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
        }
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
        int r;
+       struct stat sb;
+
+       if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       return -1;
+               }
 
-       if (access(path, F_OK) != 0) {
                return 0;
        }
 
--
Quoted 3 lines

2.14.3

--
Respectfully,

William C Roberts

Signature · 3 lines
Respectfully,

William C Roberts
#29 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Quoted 4 lines · William Roberts

Where is patch 2/2, I have yet to see it?

Did something get screwy and is it: [PATCH] libsemanage: Improve warning for installing disabled module

No, 2/3 was a separate patch and had the 2/3 in the subject line. I received all three from the list, both locally and on my gmail.

Quoted 359 lines
Quoted 352 lines · William Roberts
Quoted 80 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +17 -3
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
        const char *path;
+       struct stat sb;
 
        if (semanage_check_init(sh, sh->conf->store_root_path))
                goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
        /* set the disable dontaudit value */
        path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-       if (access(path, F_OK) == 0)
+
+       if (stat(path, &sb) == 0)
                sepol_set_disable_dontaudit(sh->sepolh, 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto err;
+               }
+
                sepol_set_disable_dontaudit(sh->sepolh, 0);
+       }
 
        return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
        int status = 0;
        int i;
        char cil_path[PATH_MAX];
+       struct stat sb;
 
        assert(sh);
        assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
                }
 
                if (semanage_get_ignore_module_cache(sh) == 0 &&
-                               access(cil_path, F_OK) == 0) {
+                               (status = stat(cil_path, &sb)) == 0) {
                        continue;
                }
+               if (status != 0 && errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+                       goto cleanup; //an error in the "stat" call
+               }
 
                status = semanage_compile_module(sh, &modinfos[i]);
                if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
        struct cil_db *cildb = NULL;
        semanage_module_info_t *modinfos = NULL;
        mode_t mask = umask(0077);
+       struct stat sb;
 
        int do_rebuild, do_write_kernel, do_install;
        int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the disable_dontaudit flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c static int semanage_direct_commit(semanage_handle_t * sh) · line 1250

Quoted 10 lines
libsemanage/src/direct_api.c +7 -2
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+

I am not a huge fan of this if under else block of if (errno !=ENOENT).

maybe this is a bit cleaner:

if (stat() == 0)
//exists
else if (errno == ENOENT)
// doesn't exist
else
//fail

Quoted 239 lines
libsemanage/src/direct_api.c +62 -41
                do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+       }
        if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the preserve_tunables flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+
                do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+       }
+
        if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
         * a rebuild.
         */
        if (!do_rebuild) {
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
+               int files[] = {SEMANAGE_STORE_KERNEL,
+                                          SEMANAGE_STORE_FC,
+                                          SEMANAGE_STORE_SEUSERS,
+                                          SEMANAGE_LINKED,
+                                          SEMANAGE_SEUSERS_LINKED,
+                                          SEMANAGE_USERS_EXTRA_LINKED};
+
+               for (i = 0; i < (int) sizeof(files); i++) {
+                       path = semanage_path(SEMANAGE_TMP, files[i]);
+                       if (stat(path, &sb) != 0) {
+                               if (errno != ENOENT) {
+                                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                                       goto cleanup;
+                               }
 
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
+                               do_rebuild = 1;
+                               goto rebuild;
+                       }
                }
        }
 
@@ -1457,7 +1468,7 @@ rebuild:
                        goto cleanup;
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
                                goto cleanup;
                        pseusers->dtable->drop_cache(pseusers->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pseusers->dtable->clear(sh, pseusers->dbase);
                }
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
                                goto cleanup;
                        pusers_extra->dtable->drop_cache(pusers_extra->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pusers_extra->dtable->clear(sh, pusers_extra->dbase);
                }
        }
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
        ssize_t _data_len;
        char *_data;
        int compressed;
+       struct stat sb;
 
        /* get path of module */
        rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (access(module_path, F_OK) != 0) {
-               ERR(sh, "Module does not exist: %s", module_path);
+       if (stat(module_path, &sb) != 0) {
+               ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
                rc = -1;
                goto cleanup;
        }
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+                       goto cleanup;
+               }
+
                rc = semanage_compile_module(sh, _modinfo);
                if (rc < 0) {
                        goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
        }
 
        if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                *enabled = 1;
        }
        else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
        /* set enabled/disabled status */
        if (stat(fn, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
                if (ret != 0) {
                        status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
        int status = 0;
        int ret = 0;
        int type;
+       struct stat sb;
 
        char path[PATH_MAX];
        mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
                        goto cleanup;
                }
 
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        ret = unlink(path);
                        if (ret != 0) {
                                ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +12 -3
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
        char *semanage_conf = NULL;
        int len;
+       FILE *fp = NULL;
 
        len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
        semanage_conf = calloc(len + 1, sizeof(char));
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
        snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
                 SEMANAGE_CONF_FILE);
 
-       if (access(semanage_conf, R_OK) != 0) {
+       //the following replaces access(semanage_conf, R_OK) check
+       if ((fp = fopen(semanage_conf, "r")) != NULL) {
+               fclose(fp);
+       } else {
                snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
        }
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
        int r;
+       struct stat sb;
+
+       if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       return -1;
+               }
 
-       if (access(path, F_OK) != 0) {
                return 0;
        }
 
--
Quoted 3 lines

2.14.3

--
Respectfully,

William C Roberts

#30 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Quoted 9 lines · Stephen Smalley
Quoted 4 lines · William Roberts

Where is patch 2/2, I have yet to see it?

Did something get screwy and is it: [PATCH] libsemanage: Improve warning for installing disabled module

No, 2/3 was a separate patch and had the 2/3 in the subject line. I received all three from the list, both locally and on my gmail.

Thanks gmail for folding the patch 1/3 and 2/3 into the same "conversation" or whatever it does.

Quoted 349 lines
Quoted 349 lines
Quoted 345 lines · William Roberts
Quoted 80 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +17 -3
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
        const char *path;
+       struct stat sb;
 
        if (semanage_check_init(sh, sh->conf->store_root_path))
                goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
        /* set the disable dontaudit value */
        path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-       if (access(path, F_OK) == 0)
+
+       if (stat(path, &sb) == 0)
                sepol_set_disable_dontaudit(sh->sepolh, 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto err;
+               }
+
                sepol_set_disable_dontaudit(sh->sepolh, 0);
+       }
 
        return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
        int status = 0;
        int i;
        char cil_path[PATH_MAX];
+       struct stat sb;
 
        assert(sh);
        assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
                }
 
                if (semanage_get_ignore_module_cache(sh) == 0 &&
-                               access(cil_path, F_OK) == 0) {
+                               (status = stat(cil_path, &sb)) == 0) {
                        continue;
                }
+               if (status != 0 && errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+                       goto cleanup; //an error in the "stat" call
+               }
 
                status = semanage_compile_module(sh, &modinfos[i]);
                if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
        struct cil_db *cildb = NULL;
        semanage_module_info_t *modinfos = NULL;
        mode_t mask = umask(0077);
+       struct stat sb;
 
        int do_rebuild, do_write_kernel, do_install;
        int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the disable_dontaudit flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c static int semanage_direct_commit(semanage_handle_t * sh) · line 1250

Quoted 10 lines
libsemanage/src/direct_api.c +7 -2
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+

I am not a huge fan of this if under else block of if (errno !=ENOENT).

maybe this is a bit cleaner:

if (stat() == 0)
//exists
else if (errno == ENOENT)
// doesn't exist
else
//fail

Quoted 242 lines
libsemanage/src/direct_api.c +62 -41
                do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+       }
        if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the preserve_tunables flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+
                do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+       }
+
        if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
         * a rebuild.
         */
        if (!do_rebuild) {
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
+               int files[] = {SEMANAGE_STORE_KERNEL,
+                                          SEMANAGE_STORE_FC,
+                                          SEMANAGE_STORE_SEUSERS,
+                                          SEMANAGE_LINKED,
+                                          SEMANAGE_SEUSERS_LINKED,
+                                          SEMANAGE_USERS_EXTRA_LINKED};
+
+               for (i = 0; i < (int) sizeof(files); i++) {
+                       path = semanage_path(SEMANAGE_TMP, files[i]);
+                       if (stat(path, &sb) != 0) {
+                               if (errno != ENOENT) {
+                                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                                       goto cleanup;
+                               }
 
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
+                               do_rebuild = 1;
+                               goto rebuild;
+                       }
                }
        }
 
@@ -1457,7 +1468,7 @@ rebuild:
                        goto cleanup;
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
                                goto cleanup;
                        pseusers->dtable->drop_cache(pseusers->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pseusers->dtable->clear(sh, pseusers->dbase);
                }
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
                                goto cleanup;
                        pusers_extra->dtable->drop_cache(pusers_extra->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pusers_extra->dtable->clear(sh, pusers_extra->dbase);
                }
        }
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
        ssize_t _data_len;
        char *_data;
        int compressed;
+       struct stat sb;
 
        /* get path of module */
        rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (access(module_path, F_OK) != 0) {
-               ERR(sh, "Module does not exist: %s", module_path);
+       if (stat(module_path, &sb) != 0) {
+               ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
                rc = -1;
                goto cleanup;
        }
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+                       goto cleanup;
+               }
+
                rc = semanage_compile_module(sh, _modinfo);
                if (rc < 0) {
                        goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
        }
 
        if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                *enabled = 1;
        }
        else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
        /* set enabled/disabled status */
        if (stat(fn, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
                if (ret != 0) {
                        status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
        int status = 0;
        int ret = 0;
        int type;
+       struct stat sb;
 
        char path[PATH_MAX];
        mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
                        goto cleanup;
                }
 
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        ret = unlink(path);
                        if (ret != 0) {
                                ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +12 -3
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
        char *semanage_conf = NULL;
        int len;
+       FILE *fp = NULL;
 
        len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
        semanage_conf = calloc(len + 1, sizeof(char));
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
        snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
                 SEMANAGE_CONF_FILE);
 
-       if (access(semanage_conf, R_OK) != 0) {
+       //the following replaces access(semanage_conf, R_OK) check
+       if ((fp = fopen(semanage_conf, "r")) != NULL) {
+               fclose(fp);
+       } else {
                snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
        }
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
        int r;
+       struct stat sb;
+
+       if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       return -1;
+               }
 
-       if (access(path, F_OK) != 0) {
                return 0;
        }
 
--

2.14.3

#31 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

So peeking through the code base, I see:

int semanage_direct_is_managed(semanage_handle_t * sh) {
if (semanage_check_init(sh, sh->conf->store_root_path)) goto err;

if (semanage_access_check(sh) < 0)
return 0;

return 1;

err:
ERR(sh, "could not check whether policy is managed"); return STATUS_ERR;
}

Which semanage_access_check eventually gets down to a raw access check.

Which is only ever used in test_fcontext

semanage_access_check is also the only consumer of semanage_direct_access_check

which is the semanage_store_access_check is only consumed by the former and test case and
the same could be said for semanage_store_access_check

I think this is a good time to roll in patch 4 and drop everything relying on semanage_store_access_check.

Thoughts?

Quoted 364 lines · William Roberts
Quoted 9 lines · Stephen Smalley
Quoted 4 lines · William Roberts

Where is patch 2/2, I have yet to see it?

Did something get screwy and is it: [PATCH] libsemanage: Improve warning for installing disabled module

No, 2/3 was a separate patch and had the 2/3 in the subject line. I received all three from the list, both locally and on my gmail.

Thanks gmail for folding the patch 1/3 and 2/3 into the same "conversation" or whatever it does.

Quoted 349 lines
Quoted 349 lines
Quoted 345 lines · William Roberts
Quoted 80 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +17 -3
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
        const char *path;
+       struct stat sb;
 
        if (semanage_check_init(sh, sh->conf->store_root_path))
                goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
        /* set the disable dontaudit value */
        path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-       if (access(path, F_OK) == 0)
+
+       if (stat(path, &sb) == 0)
                sepol_set_disable_dontaudit(sh->sepolh, 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto err;
+               }
+
                sepol_set_disable_dontaudit(sh->sepolh, 0);
+       }
 
        return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
        int status = 0;
        int i;
        char cil_path[PATH_MAX];
+       struct stat sb;
 
        assert(sh);
        assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
                }
 
                if (semanage_get_ignore_module_cache(sh) == 0 &&
-                               access(cil_path, F_OK) == 0) {
+                               (status = stat(cil_path, &sb)) == 0) {
                        continue;
                }
+               if (status != 0 && errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+                       goto cleanup; //an error in the "stat" call
+               }
 
                status = semanage_compile_module(sh, &modinfos[i]);
                if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
        struct cil_db *cildb = NULL;
        semanage_module_info_t *modinfos = NULL;
        mode_t mask = umask(0077);
+       struct stat sb;
 
        int do_rebuild, do_write_kernel, do_install;
        int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the disable_dontaudit flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c static int semanage_direct_commit(semanage_handle_t * sh) · line 1250

Quoted 10 lines
libsemanage/src/direct_api.c +7 -2
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+

I am not a huge fan of this if under else block of if (errno !=ENOENT).

maybe this is a bit cleaner:

if (stat() == 0)
//exists
else if (errno == ENOENT)
// doesn't exist
else
//fail

Quoted 242 lines
libsemanage/src/direct_api.c +62 -41
                do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+       }
        if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the preserve_tunables flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+
                do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+       }
+
        if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
         * a rebuild.
         */
        if (!do_rebuild) {
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
+               int files[] = {SEMANAGE_STORE_KERNEL,
+                                          SEMANAGE_STORE_FC,
+                                          SEMANAGE_STORE_SEUSERS,
+                                          SEMANAGE_LINKED,
+                                          SEMANAGE_SEUSERS_LINKED,
+                                          SEMANAGE_USERS_EXTRA_LINKED};
+
+               for (i = 0; i < (int) sizeof(files); i++) {
+                       path = semanage_path(SEMANAGE_TMP, files[i]);
+                       if (stat(path, &sb) != 0) {
+                               if (errno != ENOENT) {
+                                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                                       goto cleanup;
+                               }
 
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
+                               do_rebuild = 1;
+                               goto rebuild;
+                       }
                }
        }
 
@@ -1457,7 +1468,7 @@ rebuild:
                        goto cleanup;
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
                                goto cleanup;
                        pseusers->dtable->drop_cache(pseusers->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pseusers->dtable->clear(sh, pseusers->dbase);
                }
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
                                goto cleanup;
                        pusers_extra->dtable->drop_cache(pusers_extra->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pusers_extra->dtable->clear(sh, pusers_extra->dbase);
                }
        }
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
        ssize_t _data_len;
        char *_data;
        int compressed;
+       struct stat sb;
 
        /* get path of module */
        rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (access(module_path, F_OK) != 0) {
-               ERR(sh, "Module does not exist: %s", module_path);
+       if (stat(module_path, &sb) != 0) {
+               ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
                rc = -1;
                goto cleanup;
        }
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+                       goto cleanup;
+               }
+
                rc = semanage_compile_module(sh, _modinfo);
                if (rc < 0) {
                        goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
        }
 
        if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                *enabled = 1;
        }
        else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
        /* set enabled/disabled status */
        if (stat(fn, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
                if (ret != 0) {
                        status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
        int status = 0;
        int ret = 0;
        int type;
+       struct stat sb;
 
        char path[PATH_MAX];
        mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
                        goto cleanup;
                }
 
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        ret = unlink(path);
                        if (ret != 0) {
                                ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +12 -3
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
        char *semanage_conf = NULL;
        int len;
+       FILE *fp = NULL;
 
        len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
        semanage_conf = calloc(len + 1, sizeof(char));
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
        snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
                 SEMANAGE_CONF_FILE);
 
-       if (access(semanage_conf, R_OK) != 0) {
+       //the following replaces access(semanage_conf, R_OK) check
+       if ((fp = fopen(semanage_conf, "r")) != NULL) {
+               fclose(fp);
+       } else {
                snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
        }
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
        int r;
+       struct stat sb;
+
+       if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       return -1;
+               }
 
-       if (access(path, F_OK) != 0) {
                return 0;
        }
 
--

2.14.3

Signature · 3 lines
Respectfully,

William C Roberts
#32 lore raw

Re: Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Quoted 23 lines · William Roberts

So peeking through the code base, I see:

int semanage_direct_is_managed(semanage_handle_t * sh) {
if (semanage_check_init(sh, sh->conf->store_root_path)) goto err;

if (semanage_access_check(sh) < 0)
return 0;

return 1;

err:
ERR(sh, "could not check whether policy is managed"); return STATUS_ERR;
}

Which semanage_access_check eventually gets down to a raw access check.

Which is only ever used in test_fcontext

semanage_access_check is also the only consumer of semanage_direct_access_check

Quoted 8 lines

which is the semanage_store_access_check is only consumed by the former and test case and
the same could be said for semanage_store_access_check

I think this is a good time to roll in patch 4 and drop everything relying on semanage_store_access_check.

Thoughts?

semanage_access_check() is part of the shared library ABI. Can't be removed. Used by seobject.py via the python bindings. At most, we can kill all internal users but the ABI has to remain.

Quoted 370 lines
Quoted 364 lines · William Roberts
Quoted 9 lines · Stephen Smalley
Quoted 4 lines · William Roberts

Where is patch 2/2, I have yet to see it?

Did something get screwy and is it: [PATCH] libsemanage: Improve warning for installing disabled module

No, 2/3 was a separate patch and had the 2/3 in the subject line. I received all three from the list, both locally and on my gmail.

Thanks gmail for folding the patch 1/3 and 2/3 into the same "conversation" or whatever it does.

Quoted 349 lines
Quoted 349 lines
Quoted 345 lines · William Roberts
Quoted 80 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +17 -3
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
        const char *path;
+       struct stat sb;
 
        if (semanage_check_init(sh, sh->conf->store_root_path))
                goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
        /* set the disable dontaudit value */
        path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-       if (access(path, F_OK) == 0)
+
+       if (stat(path, &sb) == 0)
                sepol_set_disable_dontaudit(sh->sepolh, 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto err;
+               }
+
                sepol_set_disable_dontaudit(sh->sepolh, 0);
+       }
 
        return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
        int status = 0;
        int i;
        char cil_path[PATH_MAX];
+       struct stat sb;
 
        assert(sh);
        assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
                }
 
                if (semanage_get_ignore_module_cache(sh) == 0 &&
-                               access(cil_path, F_OK) == 0) {
+                               (status = stat(cil_path, &sb)) == 0) {
                        continue;
                }
+               if (status != 0 && errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+                       goto cleanup; //an error in the "stat" call
+               }
 
                status = semanage_compile_module(sh, &modinfos[i]);
                if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
        struct cil_db *cildb = NULL;
        semanage_module_info_t *modinfos = NULL;
        mode_t mask = umask(0077);
+       struct stat sb;
 
        int do_rebuild, do_write_kernel, do_install;
        int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the disable_dontaudit flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c static int semanage_direct_commit(semanage_handle_t * sh) · line 1250

Quoted 10 lines
libsemanage/src/direct_api.c +7 -2
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+

I am not a huge fan of this if under else block of if (errno !=ENOENT).

maybe this is a bit cleaner:

if (stat() == 0)
//exists
else if (errno == ENOENT)
// doesn't exist
else
//fail

Quoted 242 lines
libsemanage/src/direct_api.c +62 -41
                do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+       }
        if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the preserve_tunables flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+
                do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+       }
+
        if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
         * a rebuild.
         */
        if (!do_rebuild) {
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
+               int files[] = {SEMANAGE_STORE_KERNEL,
+                                          SEMANAGE_STORE_FC,
+                                          SEMANAGE_STORE_SEUSERS,
+                                          SEMANAGE_LINKED,
+                                          SEMANAGE_SEUSERS_LINKED,
+                                          SEMANAGE_USERS_EXTRA_LINKED};
+
+               for (i = 0; i < (int) sizeof(files); i++) {
+                       path = semanage_path(SEMANAGE_TMP, files[i]);
+                       if (stat(path, &sb) != 0) {
+                               if (errno != ENOENT) {
+                                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                                       goto cleanup;
+                               }
 
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
+                               do_rebuild = 1;
+                               goto rebuild;
+                       }
                }
        }
 
@@ -1457,7 +1468,7 @@ rebuild:
                        goto cleanup;
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
                                goto cleanup;
                        pseusers->dtable->drop_cache(pseusers->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pseusers->dtable->clear(sh, pseusers->dbase);
                }
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
                                goto cleanup;
                        pusers_extra->dtable->drop_cache(pusers_extra->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pusers_extra->dtable->clear(sh, pusers_extra->dbase);
                }
        }
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
        ssize_t _data_len;
        char *_data;
        int compressed;
+       struct stat sb;
 
        /* get path of module */
        rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (access(module_path, F_OK) != 0) {
-               ERR(sh, "Module does not exist: %s", module_path);
+       if (stat(module_path, &sb) != 0) {
+               ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
                rc = -1;
                goto cleanup;
        }
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+                       goto cleanup;
+               }
+
                rc = semanage_compile_module(sh, _modinfo);
                if (rc < 0) {
                        goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
        }
 
        if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                *enabled = 1;
        }
        else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
        /* set enabled/disabled status */
        if (stat(fn, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
                if (ret != 0) {
                        status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
        int status = 0;
        int ret = 0;
        int type;
+       struct stat sb;
 
        char path[PATH_MAX];
        mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
                        goto cleanup;
                }
 
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        ret = unlink(path);
                        if (ret != 0) {
                                ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +12 -3
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
        char *semanage_conf = NULL;
        int len;
+       FILE *fp = NULL;
 
        len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
        semanage_conf = calloc(len + 1, sizeof(char));
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
        snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
                 SEMANAGE_CONF_FILE);
 
-       if (access(semanage_conf, R_OK) != 0) {
+       //the following replaces access(semanage_conf, R_OK) check
+       if ((fp = fopen(semanage_conf, "r")) != NULL) {
+               fclose(fp);
+       } else {
                snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
        }
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
        int r;
+       struct stat sb;
+
+       if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       return -1;
+               }
 
-       if (access(path, F_OK) != 0) {
                return 0;
        }
 
--

2.14.3

#33 lore raw

Re: Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Quoted 28 lines · Stephen Smalley
Quoted 27 lines · William Roberts

So peeking through the code base, I see:

int semanage_direct_is_managed(semanage_handle_t * sh) {
if (semanage_check_init(sh, sh->conf->store_root_path)) goto err;

if (semanage_access_check(sh) < 0)
return 0;

return 1;

err:
ERR(sh, "could not check whether policy is managed"); return STATUS_ERR;
}

Which semanage_access_check eventually gets down to a raw access check.

Which is only ever used in test_fcontext

semanage_access_check is also the only consumer of semanage_direct_access_check

which is the semanage_store_access_check is only consumed by the former and test case and
the same could be said for semanage_store_access_check

Quoted 8 lines
Quoted 4 lines

I think this is a good time to roll in patch 4 and drop everything relying on semanage_store_access_check.

Thoughts?

semanage_access_check() is part of the shared library ABI. Can't be removed. Used by seobject.py via the python bindings. At most, we can kill all internal users but the ABI has to remain.

Ahh yes, duh.

Outside of just killing off internal users of it, should we modify it to not use access?
So it at least works under setuid?

Quoted 372 lines
Quoted 371 lines
Quoted 364 lines · William Roberts
Quoted 9 lines · Stephen Smalley
Quoted 4 lines · William Roberts

Where is patch 2/2, I have yet to see it?

Did something get screwy and is it: [PATCH] libsemanage: Improve warning for installing disabled module

No, 2/3 was a separate patch and had the 2/3 in the subject line. I received all three from the list, both locally and on my gmail.

Thanks gmail for folding the patch 1/3 and 2/3 into the same "conversation" or whatever it does.

Quoted 349 lines
Quoted 349 lines
Quoted 345 lines · William Roberts
Quoted 80 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +17 -3
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
        const char *path;
+       struct stat sb;
 
        if (semanage_check_init(sh, sh->conf->store_root_path))
                goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
        /* set the disable dontaudit value */
        path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-       if (access(path, F_OK) == 0)
+
+       if (stat(path, &sb) == 0)
                sepol_set_disable_dontaudit(sh->sepolh, 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto err;
+               }
+
                sepol_set_disable_dontaudit(sh->sepolh, 0);
+       }
 
        return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
        int status = 0;
        int i;
        char cil_path[PATH_MAX];
+       struct stat sb;
 
        assert(sh);
        assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
                }
 
                if (semanage_get_ignore_module_cache(sh) == 0 &&
-                               access(cil_path, F_OK) == 0) {
+                               (status = stat(cil_path, &sb)) == 0) {
                        continue;
                }
+               if (status != 0 && errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+                       goto cleanup; //an error in the "stat" call
+               }
 
                status = semanage_compile_module(sh, &modinfos[i]);
                if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
        struct cil_db *cildb = NULL;
        semanage_module_info_t *modinfos = NULL;
        mode_t mask = umask(0077);
+       struct stat sb;
 
        int do_rebuild, do_write_kernel, do_install;
        int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the disable_dontaudit flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c static int semanage_direct_commit(semanage_handle_t * sh) · line 1250

Quoted 10 lines
libsemanage/src/direct_api.c +7 -2
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+

I am not a huge fan of this if under else block of if (errno !=ENOENT).

maybe this is a bit cleaner:

if (stat() == 0)
//exists
else if (errno == ENOENT)
// doesn't exist
else
//fail

Quoted 242 lines
libsemanage/src/direct_api.c +62 -41
                do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+       }
        if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
        /* Create or remove the preserve_tunables flag file. */
        path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-       if (access(path, F_OK) == 0)
+       if (stat(path, &sb) == 0)
                do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-       else
+       else {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       goto cleanup;
+               }
+
                do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+       }
+
        if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
                FILE *touch;
                touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
         * a rebuild.
         */
        if (!do_rebuild) {
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
-
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
-               }
+               int files[] = {SEMANAGE_STORE_KERNEL,
+                                          SEMANAGE_STORE_FC,
+                                          SEMANAGE_STORE_SEUSERS,
+                                          SEMANAGE_LINKED,
+                                          SEMANAGE_SEUSERS_LINKED,
+                                          SEMANAGE_USERS_EXTRA_LINKED};
+
+               for (i = 0; i < (int) sizeof(files); i++) {
+                       path = semanage_path(SEMANAGE_TMP, files[i]);
+                       if (stat(path, &sb) != 0) {
+                               if (errno != ENOENT) {
+                                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                                       goto cleanup;
+                               }
 
-               path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) != 0) {
-                       do_rebuild = 1;
-                       goto rebuild;
+                               do_rebuild = 1;
+                               goto rebuild;
+                       }
                }
        }
 
@@ -1457,7 +1468,7 @@ rebuild:
                        goto cleanup;
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
                                goto cleanup;
                        pseusers->dtable->drop_cache(pseusers->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pseusers->dtable->clear(sh, pseusers->dbase);
                }
 
                path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        retval = semanage_copy_file(path,
                                                    semanage_path(SEMANAGE_TMP,
                                                                  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
                                goto cleanup;
                        pusers_extra->dtable->drop_cache(pusers_extra->dbase);
                } else {
+                       if (errno != ENOENT) {
+                               ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                               goto cleanup;
+                       }
+
                        pusers_extra->dtable->clear(sh, pusers_extra->dbase);
                }
        }
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
        ssize_t _data_len;
        char *_data;
        int compressed;
+       struct stat sb;
 
        /* get path of module */
        rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (access(module_path, F_OK) != 0) {
-               ERR(sh, "Module does not exist: %s", module_path);
+       if (stat(module_path, &sb) != 0) {
+               ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
                rc = -1;
                goto cleanup;
        }
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
                goto cleanup;
        }
 
-       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+       if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+                       goto cleanup;
+               }
+
                rc = semanage_compile_module(sh, _modinfo);
                if (rc < 0) {
                        goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
        }
 
        if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                *enabled = 1;
        }
        else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
        /* set enabled/disabled status */
        if (stat(fn, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+                       status = -1;
+                       goto cleanup;
+               }
+
                ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
                if (ret != 0) {
                        status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
        int status = 0;
        int ret = 0;
        int type;
+       struct stat sb;
 
        char path[PATH_MAX];
        mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
                        goto cleanup;
                }
 
-               if (access(path, F_OK) == 0) {
+               if (stat(path, &sb) == 0) {
                        ret = unlink(path);
                        if (ret != 0) {
                                ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +12 -3
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
        char *semanage_conf = NULL;
        int len;
+       FILE *fp = NULL;
 
        len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
        semanage_conf = calloc(len + 1, sizeof(char));
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
        snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
                 SEMANAGE_CONF_FILE);
 
-       if (access(semanage_conf, R_OK) != 0) {
+       //the following replaces access(semanage_conf, R_OK) check
+       if ((fp = fopen(semanage_conf, "r")) != NULL) {
+               fclose(fp);
+       } else {
                snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
        }
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
        int r;
+       struct stat sb;
+
+       if (stat(path, &sb) < 0) {
+               if (errno != ENOENT) {
+                       ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+                       return -1;
+               }
 
-       if (access(path, F_OK) != 0) {
                return 0;
        }
 
--

2.14.3

Signature · 3 lines
Respectfully,

William C Roberts
#34 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Quoted 38 lines · William Roberts
Quoted 28 lines · Stephen Smalley
Quoted 27 lines · William Roberts

So peeking through the code base, I see:

int semanage_direct_is_managed(semanage_handle_t * sh) {
if (semanage_check_init(sh, sh->conf->store_root_path)) goto err;

if (semanage_access_check(sh) < 0)
return 0;

return 1;

err:
ERR(sh, "could not check whether policy is managed"); return STATUS_ERR;
}

Which semanage_access_check eventually gets down to a raw access check.

Which is only ever used in test_fcontext

semanage_access_check is also the only consumer of semanage_direct_access_check

which is the semanage_store_access_check is only consumed by the former and test case and
the same could be said for semanage_store_access_check

Quoted 8 lines
Quoted 4 lines

I think this is a good time to roll in patch 4 and drop everything relying on semanage_store_access_check.

Thoughts?

semanage_access_check() is part of the shared library ABI. Can't be removed. Used by seobject.py via the python bindings. At most, we can kill all internal users but the ABI has to remain.

Quoted 5 lines

Ahh yes, duh.

Outside of just killing off internal users of it, should we modify it to not use access?
So it at least works under setuid?

I don't think it is worthwhile, and it isn't clear how one would test the directory writability case. Plus semanage_access_check() is only advisory and the caller is free to ignore the result or not call it in the first place. It is really only intended to allow a program to save itself some work if it isn't going to be able to access the policy store at all, not as a security feature. Also, as I've said before, I wouldn't warrant libsemanage to be safe if called from a setuid program; no one has ever audited it for such to my knowledge.

#35 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Quoted 45 lines · Stephen Smalley
Quoted 38 lines · William Roberts
Quoted 28 lines · Stephen Smalley
Quoted 27 lines · William Roberts

So peeking through the code base, I see:

int semanage_direct_is_managed(semanage_handle_t * sh) {
if (semanage_check_init(sh, sh->conf->store_root_path)) goto err;

if (semanage_access_check(sh) < 0)
return 0;

return 1;

err:
ERR(sh, "could not check whether policy is managed"); return STATUS_ERR;
}

Which semanage_access_check eventually gets down to a raw access check.

Which is only ever used in test_fcontext

semanage_access_check is also the only consumer of semanage_direct_access_check

which is the semanage_store_access_check is only consumed by the former and test case and
the same could be said for semanage_store_access_check

Quoted 8 lines
Quoted 4 lines

I think this is a good time to roll in patch 4 and drop everything relying on semanage_store_access_check.

Thoughts?

semanage_access_check() is part of the shared library ABI. Can't be removed. Used by seobject.py via the python bindings. At most, we can kill all internal users but the ABI has to remain.

Quoted 5 lines

Ahh yes, duh.

Outside of just killing off internal users of it, should we modify it to not use access?
So it at least works under setuid?

Quoted 8 lines

I don't think it is worthwhile, and it isn't clear how one would test the directory writability case. Plus semanage_access_check() is only advisory and the caller is free to ignore the result or not call it in the first place. It is really only intended to allow a program to save itself some work if it isn't going to be able to access the policy store at all, not as a security feature. Also, as I've said before, I wouldn't warrant libsemanage to be safe if called from a setuid program; no one has ever audited it for such to my knowledge.

By the way, please test that these patches do not end up yielding silent failures or confusing error messages when users run semanage or semodule commands with insufficient permissions, e.g. semanage login -l or semodule -l as non-root. That's what semanage_access_check() was for, to check up front and provide a useful error message before doing any other work.

#36 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

Quoted 55 lines · Stephen Smalley
Quoted 45 lines · Stephen Smalley
Quoted 38 lines · William Roberts
Quoted 28 lines · Stephen Smalley
Quoted 27 lines · William Roberts

So peeking through the code base, I see:

int semanage_direct_is_managed(semanage_handle_t * sh) {
if (semanage_check_init(sh, sh->conf->store_root_path)) goto err;

if (semanage_access_check(sh) < 0)
return 0;

return 1;

err:
ERR(sh, "could not check whether policy is managed"); return STATUS_ERR;
}

Which semanage_access_check eventually gets down to a raw access check.

Which is only ever used in test_fcontext

semanage_access_check is also the only consumer of semanage_direct_access_check

which is the semanage_store_access_check is only consumed by the former and test case and
the same could be said for semanage_store_access_check

Quoted 8 lines
Quoted 4 lines

I think this is a good time to roll in patch 4 and drop everything relying on semanage_store_access_check.

Thoughts?

semanage_access_check() is part of the shared library ABI. Can't be removed. Used by seobject.py via the python bindings. At most, we can kill all internal users but the ABI has to remain.

Quoted 5 lines

Ahh yes, duh.

Outside of just killing off internal users of it, should we modify it to not use access?
So it at least works under setuid?

Quoted 8 lines

I don't think it is worthwhile, and it isn't clear how one would test the directory writability case. Plus semanage_access_check() is only advisory and the caller is free to ignore the result or not call it in the first place. It is really only intended to allow a program to save itself some work if it isn't going to be able to access the policy store at all, not as a security feature. Also, as I've said before, I wouldn't warrant libsemanage to be safe if called from a setuid program; no one has ever audited it for such to my knowledge.

Quoted 6 lines

By the way, please test that these patches do not end up yielding silent failures or confusing error messages when users run semanage or semodule commands with insufficient permissions, e.g. semanage login -l or semodule -l as non-root. That's what semanage_access_check() was for, to check up front and provide a useful error message before doing any other work.

And for comparison, this is what we get today before these patches:

$ semanage login -l
ValueError: SELinux policy is not managed or store cannot be accessed.
$ semodule -l
libsemanage.semanage_create_store: Could not access module store at
/var/lib/selinux/targeted, or it is not a directory. (Permission denied).
libsemanage.semanage_direct_connect: could not establish direct
connection (Permission denied).
semodule:  Could not connect to policy handler

The messages don't have to be the same, but we need to make sure we have some useful diagnostic that points to the underlying problem.

#37 lore raw

Re: [PATCH 3/3] libsemanage: replace access() checks to make setuid programs work

In reply to Vit Mojzis
Quoted 301 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat(). And access(,R_OK) by fopen(,"r")

---
 libsemanage/src/direct_api.c     | 132 +++++++++++++++++++++++++--------------
 libsemanage/src/semanage_store.c |  14 ++++-
 2 files changed, 98 insertions(+), 48 deletions(-)
libsemanage/src/direct_api.c +86 -46
index f4d57cf8..d42a51cd 100644
@@ -140,6 +140,7 @@ int semanage_direct_is_managed(semanage_handle_t * sh)
 int semanage_direct_connect(semanage_handle_t * sh)
 {
 	const char *path;
+	struct stat sb;
 
 	if (semanage_check_init(sh, sh->conf->store_root_path))
 		goto err;
@@ -302,10 +303,17 @@ int semanage_direct_connect(semanage_handle_t * sh)
 
 	/* set the disable dontaudit value */
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+
+	if (stat(path, &sb) == 0)
 		sepol_set_disable_dontaudit(sh->sepolh, 1);
-	else
+	else {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			goto err;
+		}
+
 		sepol_set_disable_dontaudit(sh->sepolh, 0);
+	}
 
 	return STATUS_SUCCESS;
 
@@ -1139,6 +1147,7 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
 	int status = 0;
 	int i;
 	char cil_path[PATH_MAX];
+	struct stat sb;
 
 	assert(sh);
 	assert(modinfos);
@@ -1155,9 +1164,13 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
 		}
 
 		if (semanage_get_ignore_module_cache(sh) == 0 &&
-				access(cil_path, F_OK) == 0) {
+				(status = stat(cil_path, &sb)) == 0) {
 			continue;
 		}
+		if (status != 0 && errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
+			goto cleanup; //an error in the "stat" call
+		}
 
 		status = semanage_compile_module(sh, &modinfos[i]);
 		if (status < 0) {
@@ -1188,6 +1201,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 	struct cil_db *cildb = NULL;
 	semanage_module_info_t *modinfos = NULL;
 	mode_t mask = umask(0077);
+	struct stat sb;
 
 	int do_rebuild, do_write_kernel, do_install;
 	int fcontexts_modified, ports_modified, seusers_modified,
@@ -1226,10 +1240,16 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
 	/* Create or remove the disable_dontaudit flag file. */
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)
 		do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
-	else
+	else {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			goto cleanup;
+		}
+
 		do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
+	}
 	if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
 		FILE *touch;
 		touch = fopen(path, "w");
@@ -1251,10 +1271,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 
 	/* Create or remove the preserve_tunables flag file. */
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)
 		do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
-	else
+	else {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			goto cleanup;
+		}
+
 		do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
+	}
+
 	if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
 		FILE *touch;
 		touch = fopen(path, "w");
@@ -1291,40 +1318,24 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 	 * a rebuild.
 	 */
 	if (!do_rebuild) {
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
-
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
-
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
-
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
-
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
-		}
+		int files[] = {SEMANAGE_STORE_KERNEL,
+					   SEMANAGE_STORE_FC,
+					   SEMANAGE_STORE_SEUSERS,
+					   SEMANAGE_LINKED,
+					   SEMANAGE_SEUSERS_LINKED,
+					   SEMANAGE_USERS_EXTRA_LINKED};
+
+		for (i = 0; i < (int) sizeof(files); i++) {
+			path = semanage_path(SEMANAGE_TMP, files[i]);
+			if (stat(path, &sb) != 0) {
+				if (errno != ENOENT) {
+					ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+					goto cleanup;
+				}
 
-		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) != 0) {
-			do_rebuild = 1;
-			goto rebuild;
+				do_rebuild = 1;
+				goto rebuild;
+			}
 		}
 	}
 
@@ -1457,7 +1468,7 @@ rebuild:
 			goto cleanup;
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			retval = semanage_copy_file(path,
 						    semanage_path(SEMANAGE_TMP,
 								  SEMANAGE_STORE_SEUSERS),
@@ -1466,11 +1477,16 @@ rebuild:
 				goto cleanup;
 			pseusers->dtable->drop_cache(pseusers->dbase);
 		} else {
+			if (errno != ENOENT) {
+				ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+				goto cleanup;
+			}
+
 			pseusers->dtable->clear(sh, pseusers->dbase);
 		}
 
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			retval = semanage_copy_file(path,
 						    semanage_path(SEMANAGE_TMP,
 								  SEMANAGE_USERS_EXTRA),
@@ -1479,6 +1495,11 @@ rebuild:
 				goto cleanup;
 			pusers_extra->dtable->drop_cache(pusers_extra->dbase);
 		} else {
+			if (errno != ENOENT) {
+				ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+				goto cleanup;
+			}
+
 			pusers_extra->dtable->clear(sh, pusers_extra->dbase);
 		}
 	}
@@ -1809,6 +1830,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 	ssize_t _data_len;
 	char *_data;
 	int compressed;
+	struct stat sb;
 
 	/* get path of module */
 	rc = semanage_module_get_path(
@@ -1821,8 +1843,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 		goto cleanup;
 	}
 
-	if (access(module_path, F_OK) != 0) {
-		ERR(sh, "Module does not exist: %s", module_path);
+	if (stat(module_path, &sb) != 0) {
+		ERR(sh, "Unable to access %s: %s\n", module_path, strerror(errno));
 		rc = -1;
 		goto cleanup;
 	}
@@ -1851,7 +1873,12 @@ static int semanage_direct_extract(semanage_handle_t * sh,
 		goto cleanup;
 	}
 
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", input_file, strerror(errno));
+			goto cleanup;
+		}
+
 		rc = semanage_compile_module(sh, _modinfo);
 		if (rc < 0) {
 			goto cleanup;
@@ -1996,6 +2023,12 @@ static int semanage_direct_get_enabled(semanage_handle_t *sh,
 	}
 
 	if (stat(path, &sb) < 0) {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			status = -1;
+			goto cleanup;
+		}
+
 		*enabled = 1;
 	}
 	else {
@@ -2322,6 +2355,12 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 
 	/* set enabled/disabled status */
 	if (stat(fn, &sb) < 0) {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", fn, strerror(errno));
+			status = -1;
+			goto cleanup;
+		}
+
 		ret = semanage_module_info_set_enabled(sh, *modinfo, 1);
 		if (ret != 0) {
 			status = -1;
@@ -2730,6 +2769,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
 	int status = 0;
 	int ret = 0;
 	int type;
+	struct stat sb;
 
 	char path[PATH_MAX];
 	mode_t mask = umask(0077);
@@ -2830,7 +2870,7 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
 			goto cleanup;
 		}
 
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {
 			ret = unlink(path);
 			if (ret != 0) {
 				ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
libsemanage/src/semanage_store.c +1 -0
index 4bd1d651..83c188dc 100644
@@ -514,6 +514,7 @@ char *semanage_conf_path(void)
 {
 	char *semanage_conf = NULL;
 	int len;
+	FILE *fp = NULL;
 
 	len = strlen(semanage_root()) + strlen(selinux_path()) + strlen(SEMANAGE_CONF_FILE);
 	semanage_conf = calloc(len + 1, sizeof(char));

libsemanage/src/semanage_store.c char *semanage_conf_path(void) · line 527

Quoted 6 lines
libsemanage/src/semanage_store.c +2 -1
@@ -522,7 +523,10 @@ char *semanage_conf_path(void)
 	snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(), selinux_path(),
 		 SEMANAGE_CONF_FILE);
 
-	if (access(semanage_conf, R_OK) != 0) {
+	//the following replaces access(semanage_conf, R_OK) check
+	if ((fp = fopen(semanage_conf, "r")) != NULL) {

Arguably the test should have just been F_OK (i.e. does it exist) and you can just make it a stat() instead.

Quoted 22 lines
libsemanage/src/semanage_store.c +9 -1
+		fclose(fp);
+	} else {
 		snprintf(semanage_conf, len + 1, "%s%s", selinux_path(), SEMANAGE_CONF_FILE);
 	}
 
@@ -1508,8 +1512,14 @@ int semanage_split_fc(semanage_handle_t * sh)
 static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
 
 	int r;
+	struct stat sb;
+
+	if (stat(path, &sb) < 0) {
+		if (errno != ENOENT) {
+			ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
+			return -1;
+		}
 
-	if (access(path, F_OK) != 0) {
 		return 0;
 	}
 
#38 lore raw

Re: [PATCH 1/3] libsemanage: remove access() check to make setuid programs work

In reply to Vit Mojzis
Quoted 96 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs. Remove redundant access() checks

---
 libsemanage/src/direct_api.c     |  7 -------
 libsemanage/src/semanage_store.c | 17 ++++++++---------
 2 files changed, 8 insertions(+), 16 deletions(-)
libsemanage/src/direct_api.c +0 -7
index 88873c43..b7899d68 100644
@@ -148,9 +148,6 @@ int semanage_direct_connect(semanage_handle_t * sh)
                if (semanage_create_store(sh, 1))
                        goto err;
 
-       if (semanage_access_check(sh) < SEMANAGE_CAN_READ)
-               goto err;
-
        sh->u.direct.translock_file_fd = -1;
        sh->u.direct.activelock_file_fd = -1;
 
@@ -398,10 +395,6 @@ static int semanage_direct_disconnect(semanage_handle_t *sh)
 
 static int semanage_direct_begintrans(semanage_handle_t * sh)
 {
-
-       if (semanage_access_check(sh) != SEMANAGE_CAN_WRITE) {
-               return -1;
-       }
        if (semanage_get_trans_lock(sh) < 0) {
                return -1;
        }
libsemanage/src/semanage_store.c +8 -10
index 936e6495..4bd1d651 100644
@@ -538,7 +538,6 @@ char *semanage_conf_path(void)
 int semanage_create_store(semanage_handle_t * sh, int create)
 {
        struct stat sb;
-       int mode_mask = R_OK | W_OK | X_OK;
        const char *path = semanage_files[SEMANAGE_ROOT];
        int fd;
 
@@ -557,9 +556,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
                        return -1;
                }
        } else {
-               if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+               if (!S_ISDIR(sb.st_mode)) {
                        ERR(sh,
-                           "Could not access module store at %s, or it is not a directory.",
+                           "Module store at %s is not a directory.",
                            path);
                        return -1;
                }
@@ -580,9 +579,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
                        return -1;
                }
        } else {
-               if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+               if (!S_ISDIR(sb.st_mode)) {
                        ERR(sh,
-                           "Could not access module store active subdirectory at %s, or it is not a directory.",
+                           "Module store active subdirectory at %s is not a directory.",
                            path);
                        return -1;
                }
@@ -603,9 +602,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
                        return -1;
                }
        } else {
-               if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
+               if (!S_ISDIR(sb.st_mode)) {
                        ERR(sh,
-                           "Could not access module store active modules subdirectory at %s, or it is not a directory.",
+                           "Module store active modules subdirectory at %s is not a directory.",
                            path);
                        return -1;
                }
@@ -624,8 +623,8 @@ int semanage_create_store(semanage_handle_t * sh, int create)
                        return -1;
                }
        } else {
-               if (!S_ISREG(sb.st_mode) || access(path, R_OK | W_OK) == -1) {
-                       ERR(sh, "Could not access lock file at %s.", path);
+               if (!S_ISREG(sb.st_mode)) {
+                       ERR(sh, "Object at %s is not a lock file.", path);
                        return -1;
                }
        }
--
Quoted 3 lines

2.14.3

Tenative ack on testing. This routine semanage_create_store() has some crazy indenting,
a lot of that could be organized to be way-less horizontal.

#39 lore raw

Re: [PATCH 3/3] libsemanage: replace access(, F_OK) checks to make setuid programs work

In reply to Vit Mojzis
Quoted 7 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat().

Also, even with all three patches applied, there are still a number of residual access() calls within semanage_store.c. Is that intentional?

Quoted 197 lines

---
 libsemanage/src/direct_api.c | 50 +++++++++++++++++++++++++++++-----
----------

 1 file changed, 33 insertions(+), 17 deletions(-)
libsemanage/src/direct_api.c +0 -0
index ed11a7c..3d1c354 100644
@@ -296,10 +296,19 @@ int semanage_direct_connect(semanage_handle_t *
 sh)


  /* set the disable dontaudit value */
  path = semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c +3 -1
-	if (access(path, F_OK) == 0)
+
+	struct stat sb;
+	if (stat(path, &sb) == 0)

  sepol_set_disable_dontaudit(sh->sepolh, 1);

libsemanage/src/direct_api.c +6 -2
-	else
-		sepol_set_disable_dontaudit(sh->sepolh, 0);
+	else{
+		if (errno == ENOENT){
+			sepol_set_disable_dontaudit(sh->sepolh, 0);
+		}
+		else{
+			ERR(sh, "Unable to access disable_dontaudit

file at %s: %s\n",path , strerror(errno));

libsemanage/src/direct_api.c +3 -0
+			goto err;
+		}
+	}


  return STATUS_SUCCESS;

libsemanage/src/direct_api.c +1 -0
@@ -1114,6 +1123,7 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
  	int status = 0;
  	int i;
  	char cil_path[PATH_MAX];
+	struct stat sb;


  assert(sh);
  assert(modinfos);

libsemanage/src/direct_api.c +0 -0
@@ -1130,9 +1140,12 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
  		}


  if (semanage_get_ignore_module_cache(sh) == 0 &&

libsemanage/src/direct_api.c +1 -1
-				access(cil_path, F_OK) == 0) {
+				(status = stat(cil_path, &sb)) == 0)

{
  continue;
  }

libsemanage/src/direct_api.c +3 -0
+		if (status != 0 && status != ENOENT) {
+			goto cleanup; //an error in the "stat" call
+		}


  status = semanage_compile_module(sh, &modinfos[i]); if (status < 0) {

libsemanage/src/direct_api.c +0 -0
@@ -1200,7 +1213,8 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


  /* Create or remove the disable_dontaudit flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c +2 -1
-	if (access(path, F_OK) == 0)
+	struct stat sb;
+	if (stat(path, &sb) == 0)

  do_rebuild |= !(sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

  else
  do_rebuild |= (sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +0 -0
@@ -1225,7 +1239,7 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


  /* Create or remove the preserve_tunables flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);

libsemanage/src/direct_api.c +1 -1
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)

  do_rebuild |= !(sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

  else
  do_rebuild |= (sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +1 -1
@@ -1266,37 +1280,37 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)
  	 */
  	if (!do_rebuild) {
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1;
  goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1;
  goto rebuild;
  }

libsemanage/src/direct_api.c +0 -0
@@ -1431,7 +1445,7 @@ rebuild:
  			goto cleanup;


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

  retval = semanage_copy_file(path, semanage_path(SE MANAGE_TMP,
    SE MANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +0 -0
@@ -1444,7 +1458,7 @@ rebuild:
  		}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

  retval = semanage_copy_file(path, semanage_path(SE MANAGE_TMP,
    SE MANAGE_USERS_EXTRA),

libsemanage/src/direct_api.c +0 -0
@@ -1785,7 +1799,8 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}

libsemanage/src/direct_api.c +2 -1
-	if (access(module_path, F_OK) != 0) {
+	struct stat sb;
+	if (stat(module_path, &sb) != 0) {

  ERR(sh, "Module does not exist: %s", module_path); rc = -1;
  goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -1815,7 +1830,7 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}

libsemanage/src/direct_api.c +0 -1
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&

access(input_file, F_OK) != 0) {

libsemanage/src/direct_api.c +1 -0
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&

stat(input_file, &sb) != 0) {
  rc = semanage_compile_module(sh, _modinfo); if (rc < 0) {
  goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -2790,7 +2805,8 @@ static int
 semanage_direct_install_info(semanage_handle_t *sh,
  			goto cleanup;
  		}

libsemanage/src/direct_api.c +2 -1
-		if (access(path, F_OK) == 0) {
+		struct stat sb;
+		if (stat(path, &sb) == 0) {

  ret = unlink(path);
  if (ret != 0) {
  ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));

#40 lore raw

Re: [PATCH 3/3] libsemanage: replace access(, F_OK) checks to make setuid programs work

In reply to Vit Mojzis
Quoted 7 lines · Vit Mojzis

access() uses real UID instead of effective UID which causes false negative checks in setuid programs.
Replace access(,F_OK) (i.e. tests for file existence) by stat().

Did you decide to not pursue this further? I didn't see any follow-up since my last set of comments in June.

Quoted 197 lines

---
 libsemanage/src/direct_api.c | 50 +++++++++++++++++++++++++++++-----
----------

 1 file changed, 33 insertions(+), 17 deletions(-)
libsemanage/src/direct_api.c +0 -0
index ed11a7c..3d1c354 100644
@@ -296,10 +296,19 @@ int semanage_direct_connect(semanage_handle_t *
 sh)


  /* set the disable dontaudit value */
  path = semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c +3 -1
-	if (access(path, F_OK) == 0)
+
+	struct stat sb;
+	if (stat(path, &sb) == 0)

  sepol_set_disable_dontaudit(sh->sepolh, 1);

libsemanage/src/direct_api.c +6 -2
-	else
-		sepol_set_disable_dontaudit(sh->sepolh, 0);
+	else{
+		if (errno == ENOENT){
+			sepol_set_disable_dontaudit(sh->sepolh, 0);
+		}
+		else{
+			ERR(sh, "Unable to access disable_dontaudit

file at %s: %s\n",path , strerror(errno));

libsemanage/src/direct_api.c +3 -0
+			goto err;
+		}
+	}


  return STATUS_SUCCESS;

libsemanage/src/direct_api.c +1 -0
@@ -1114,6 +1123,7 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
  	int status = 0;
  	int i;
  	char cil_path[PATH_MAX];
+	struct stat sb;


  assert(sh);
  assert(modinfos);

libsemanage/src/direct_api.c +0 -0
@@ -1130,9 +1140,12 @@ static int
 semanage_compile_hll_modules(semanage_handle_t *sh,
  		}


  if (semanage_get_ignore_module_cache(sh) == 0 &&

libsemanage/src/direct_api.c +1 -1
-				access(cil_path, F_OK) == 0) {
+				(status = stat(cil_path, &sb)) == 0)

{
  continue;
  }

libsemanage/src/direct_api.c +3 -0
+		if (status != 0 && status != ENOENT) {
+			goto cleanup; //an error in the "stat" call
+		}


  status = semanage_compile_module(sh, &modinfos[i]); if (status < 0) {

libsemanage/src/direct_api.c +0 -0
@@ -1200,7 +1213,8 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


  /* Create or remove the disable_dontaudit flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);

libsemanage/src/direct_api.c +2 -1
-	if (access(path, F_OK) == 0)
+	struct stat sb;
+	if (stat(path, &sb) == 0)

  do_rebuild |= !(sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

  else
  do_rebuild |= (sepol_get_disable_dontaudit(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +0 -0
@@ -1225,7 +1239,7 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)


  /* Create or remove the preserve_tunables flag file. */ path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);

libsemanage/src/direct_api.c +1 -1
-	if (access(path, F_OK) == 0)
+	if (stat(path, &sb) == 0)

  do_rebuild |= !(sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

  else
  do_rebuild |= (sepol_get_preserve_tunables(sh-

Quoted 1 line

sepolh) == 1);

libsemanage/src/direct_api.c +1 -1
@@ -1266,37 +1280,37 @@ static int
 semanage_direct_commit(semanage_handle_t * sh)
  	 */
  	if (!do_rebuild) {
  		path = semanage_path(SEMANAGE_TMP,
 SEMANAGE_STORE_KERNEL);
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {
  			do_rebuild = 1;
  			goto rebuild;
  		}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1;
  goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1; goto rebuild;
  }

  path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) != 0) {
+		if (stat(path, &sb) != 0) {

  do_rebuild = 1;
  goto rebuild;
  }

libsemanage/src/direct_api.c +0 -0
@@ -1431,7 +1445,7 @@ rebuild:
  			goto cleanup;


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

  retval = semanage_copy_file(path, semanage_path(SE MANAGE_TMP,
    SE MANAGE_STORE_SEUSERS),

libsemanage/src/direct_api.c +0 -0
@@ -1444,7 +1458,7 @@ rebuild:
  		}


  path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);

libsemanage/src/direct_api.c +1 -1
-		if (access(path, F_OK) == 0) {
+		if (stat(path, &sb) == 0) {

  retval = semanage_copy_file(path, semanage_path(SE MANAGE_TMP,
    SE MANAGE_USERS_EXTRA),

libsemanage/src/direct_api.c +0 -0
@@ -1785,7 +1799,8 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}

libsemanage/src/direct_api.c +2 -1
-	if (access(module_path, F_OK) != 0) {
+	struct stat sb;
+	if (stat(module_path, &sb) != 0) {

  ERR(sh, "Module does not exist: %s", module_path); rc = -1;
  goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -1815,7 +1830,7 @@ static int
 semanage_direct_extract(semanage_handle_t * sh,
  		goto cleanup;
  	}

libsemanage/src/direct_api.c +0 -1
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&

access(input_file, F_OK) != 0) {

libsemanage/src/direct_api.c +1 -0
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") &&

stat(input_file, &sb) != 0) {
  rc = semanage_compile_module(sh, _modinfo); if (rc < 0) {
  goto cleanup;

libsemanage/src/direct_api.c +0 -0
@@ -2790,7 +2805,8 @@ static int
 semanage_direct_install_info(semanage_handle_t *sh,
  			goto cleanup;
  		}

libsemanage/src/direct_api.c +2 -1
-		if (access(path, F_OK) == 0) {
+		struct stat sb;
+		if (stat(path, &sb) == 0) {

  ret = unlink(path);
  if (ret != 0) {
  ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));