]> err.no Git - systemd/commitdiff
label: introduce label_bind() and make use of it where necessary
authorLennart Poettering <lennart@poettering.net>
Tue, 30 Aug 2011 23:02:00 +0000 (01:02 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 30 Aug 2011 23:02:00 +0000 (01:02 +0200)
https://bugzilla.redhat.com/show_bug.cgi?id=729997

src/label.c
src/label.h
src/socket-util.c

index fb9a1b426210c473385bba4e0f62daa6f90c943b..30653a213fcb4aed8899819566959f2d41ced8c8 100644 (file)
@@ -23,6 +23,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <malloc.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 
 #include "label.h"
 #include "util.h"
@@ -273,9 +275,7 @@ void label_free(const char *label) {
 #endif
 }
 
-int label_mkdir(
-        const char *path,
-        mode_t mode) {
+int label_mkdir(const char *path, mode_t mode) {
 
         /* Creates a directory and labels it according to the SELinux policy */
 
@@ -283,43 +283,123 @@ int label_mkdir(
         int r;
         security_context_t fcon = NULL;
 
-        if (use_selinux() && label_hnd) {
+        if (!use_selinux() || label_hnd)
+                goto skipped;
 
-                if (path_is_absolute(path))
-                        r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
-                else {
-                        char *newpath = NULL;
+        if (path_is_absolute(path))
+                r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
+        else {
+                char *newpath;
 
-                        if (!(newpath = path_make_absolute_cwd(path)))
-                                return -ENOMEM;
+                newpath = path_make_absolute_cwd(path);
+                if (!newpath)
+                        return -ENOMEM;
 
-                        r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
-                        free(newpath);
-                }
+                r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
+                free(newpath);
+        }
+
+        if (r == 0)
+                r = setfscreatecon(fcon);
 
-                if (r == 0)
-                        r = setfscreatecon(fcon);
+        if (r < 0 && errno != ENOENT) {
+                log_error("Failed to set security context %s for %s: %m", fcon, path);
 
-                if (r < 0 && errno != ENOENT) {
-                        log_error("Failed to set security context %s for %s: %m", fcon, path);
+                if (security_getenforce() == 1) {
                         r = -errno;
-
-                        if (security_getenforce() == 1)
-                                goto finish;
+                        goto finish;
                 }
         }
 
-        if ((r = mkdir(path, mode)) < 0)
+        r = mkdir(path, mode);
+        if (r < 0)
                 r = -errno;
 
 finish:
-        if (use_selinux() && label_hnd) {
-                setfscreatecon(NULL);
-                freecon(fcon);
+        setfscreatecon(NULL);
+        freecon(fcon);
+
+        return r;
+
+skipped:
+#endif
+        return mkdir(path, mode) < 0 ? -errno : 0;
+}
+
+int label_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
+
+        /* Binds a socket and label its file system object according to the SELinux policy */
+
+#ifdef HAVE_SELINUX
+        int r;
+        security_context_t fcon = NULL;
+        const struct sockaddr_un *un;
+        char *path = NULL;
+
+        assert(fd >= 0);
+        assert(addr);
+        assert(addrlen >= sizeof(sa_family_t));
+
+        if (!use_selinux() || !label_hnd)
+                goto skipped;
+
+        /* Filter out non-local sockets */
+        if (addr->sa_family != AF_UNIX)
+                goto skipped;
+
+        /* Filter out anonymous sockets */
+        if (addrlen < sizeof(sa_family_t) + 1)
+                goto skipped;
+
+        /* Filter out abstract namespace sockets */
+        un = (const struct sockaddr_un*) addr;
+        if (un->sun_path[0] == 0)
+                goto skipped;
+
+        path = strndup(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path));
+        if (!path)
+                return -ENOMEM;
+
+        if (path_is_absolute(path))
+                r = selabel_lookup_raw(label_hnd, &fcon, path, 0777);
+        else {
+                char *newpath;
+
+                newpath = path_make_absolute_cwd(path);
+
+                if (!newpath) {
+                        free(path);
+                        return -ENOMEM;
+                }
+
+                r = selabel_lookup_raw(label_hnd, &fcon, newpath, 0777);
+                free(newpath);
         }
 
+        if (r == 0)
+                r = setfscreatecon(fcon);
+
+        if (r < 0 && errno != ENOENT) {
+                log_error("Failed to set security context %s for %s: %m", fcon, path);
+
+                if (security_getenforce() == 1) {
+                        r = -errno;
+                        goto finish;
+                }
+        }
+
+        r = bind(fd, addr, addrlen);
+        if (r < 0)
+                r = -errno;
+
+finish:
+        setfscreatecon(NULL);
+        freecon(fcon);
+        free(path);
+
         return r;
-#else
-        return mkdir(path, mode);
+
+skipped:
 #endif
+        return bind(fd, addr, addrlen) < 0 ? -errno : 0;
 }
index 6e48efaa0755aef129eb9e01d00949e8daeb28c5..ead44837a429c7e3cb8f13dc97a616013a95f06a 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <sys/types.h>
 #include <stdbool.h>
+#include <sys/socket.h>
 
 int label_init(void);
 void label_finish(void);
@@ -45,4 +46,6 @@ int label_mkdir(const char *path, mode_t mode);
 
 void label_retest_selinux(void);
 
+int label_bind(int fd, const struct sockaddr *addr, socklen_t addrlen);
+
 #endif
index e2e89886d49e43c26e2ba29e804e18a6cf8ac6da..acc4d33372badb098b8f7713e7608537b083c6f1 100644 (file)
@@ -458,7 +458,7 @@ int socket_address_listen(
                 /* Include the original umask in our mask */
                 umask(~socket_mode | old_mask);
 
-                r = bind(fd, &a->sockaddr.sa, a->size);
+                r = label_bind(fd, &a->sockaddr.sa, a->size);
 
                 if (r < 0 && errno == EADDRINUSE) {
                         /* Unlink and try again */