X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fselinux-util.c;h=7c58985cd26ae29975e4e7fc5458a5e5dd6b12d6;hb=30ab6a0fc1bb950c4dcd90dcd3dfe00a810c7fc1;hp=0d8c6c2f1c2dd8eadbb2f748568c0c86c5c4d853;hpb=ecabcf8b6edcc856ec2fd5bd43fc675a8fe04731;p=elogind.git diff --git a/src/shared/selinux-util.c b/src/shared/selinux-util.c index 0d8c6c2f1..7c58985cd 100644 --- a/src/shared/selinux-util.c +++ b/src/shared/selinux-util.c @@ -20,9 +20,9 @@ ***/ #include -#include #include #include + #ifdef HAVE_SELINUX #include #include @@ -116,6 +116,7 @@ void mac_selinux_finish(void) { return; selabel_close(label_hnd); + label_hnd = NULL; #endif } @@ -145,7 +146,7 @@ int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) { r = lsetfilecon(path, fcon); /* If the FS doesn't support labels, then exit without warning */ - if (r < 0 && errno == ENOTSUP) + if (r < 0 && errno == EOPNOTSUPP) return 0; } } @@ -232,7 +233,7 @@ int mac_selinux_get_our_label(char **label) { return r; } -int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, char **label) { +int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label) { int r = -EOPNOTSUPP; #ifdef HAVE_SELINUX @@ -256,11 +257,7 @@ int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, char **label if (r < 0) return -errno; - r = getexeccon(&fcon); - if (r < 0) - return -errno; - - if (!fcon) { + if (!exec_label) { /* If there is no context set for next exec let's use context of target executable */ r = getfilecon(exe, &fcon); @@ -319,10 +316,25 @@ int mac_selinux_create_file_prepare(const char *path, mode_t mode) { if (!label_hnd) return 0; - r = selabel_lookup_raw(label_hnd, &filecon, path, mode); - if (r < 0 && errno != ENOENT) + if (path_is_absolute(path)) + r = selabel_lookup_raw(label_hnd, &filecon, path, mode); + else { + _cleanup_free_ char *newpath; + + newpath = path_make_absolute_cwd(path); + if (!newpath) + return -ENOMEM; + + r = selabel_lookup_raw(label_hnd, &filecon, newpath, mode); + } + + /* No context specified by the policy? Proceed without setting it. */ + if (r < 0 && errno == ENOENT) + return 0; + + if (r < 0) r = -errno; - else if (r == 0) { + else { r = setfscreatecon(filecon); if (r < 0) { log_enforcing("Failed to set SELinux security context %s for %s: %m", filecon, path); @@ -380,56 +392,6 @@ void mac_selinux_create_socket_clear(void) { #endif } -int mac_selinux_mkdir(const char *path, mode_t mode) { - - /* Creates a directory and labels it according to the SELinux policy */ - -#ifdef HAVE_SELINUX - _cleanup_security_context_free_ security_context_t fcon = NULL; - int r; - - assert(path); - - if (!label_hnd) - goto skipped; - - if (path_is_absolute(path)) - r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFDIR); - else { - _cleanup_free_ char *newpath; - - newpath = path_make_absolute_cwd(path); - if (!newpath) - return -ENOMEM; - - r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFDIR); - } - - if (r == 0) - r = setfscreatecon(fcon); - - if (r < 0 && errno != ENOENT) { - log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon, path); - - if (security_getenforce() == 1) { - r = -errno; - goto finish; - } - } - - r = mkdir(path, mode); - if (r < 0) - r = -errno; - -finish: - setfscreatecon(NULL); - return r; - -skipped: -#endif - return mkdir(path, mode) < 0 ? -errno : 0; -} - int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) { /* Binds a socket and label its file system object according to the SELinux policy */