chiark / gitweb /
security: rework selinux, smack, ima, apparmor detection logic
[elogind.git] / src / journal / journald-server.c
index f417059ec52d963b2448225e6475512fba676e48..9732e1b25ed4cec5d33eb6dc942b791d1e0de21f 100644 (file)
@@ -180,25 +180,6 @@ static uint64_t available_space(Server *s, bool verbose) {
         return s->cached_available_space;
 }
 
-static void server_read_file_gid(Server *s) {
-        const char *g = "systemd-journal";
-        int r;
-
-        assert(s);
-
-        if (s->file_gid_valid)
-                return;
-
-        r = get_group_creds(&g, &s->file_gid);
-        if (r < 0)
-                log_warning("Failed to resolve '%s' group: %s", g, strerror(-r));
-
-        /* if we couldn't read the gid, then it will be 0, but that's
-         * fine and we shouldn't try to resolve the group again, so
-         * let's just pretend it worked right-away. */
-        s->file_gid_valid = true;
-}
-
 void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
         int r;
 #ifdef HAVE_ACL
@@ -209,11 +190,9 @@ void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
 
         assert(f);
 
-        server_read_file_gid(s);
-
-        r = fchmod_and_fchown(f->fd, 0640, 0, s->file_gid);
+        r = fchmod(f->fd, 0640);
         if (r < 0)
-                log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
+                log_warning("Failed to fix access mode on %s, ignoring: %s", f->path, strerror(-r));
 
 #ifdef HAVE_ACL
         if (uid <= 0)
@@ -342,8 +321,10 @@ void server_rotate(Server *s) {
                 if (r < 0)
                         if (f)
                                 log_error("Failed to rotate %s: %s", f->path, strerror(-r));
-                        else
+                        else {
                                 log_error("Failed to create user journal: %s", strerror(-r));
+                                hashmap_remove(s->user_journals, k);
+                        }
                 else {
                         hashmap_replace(s->user_journals, k, f);
                         server_fix_perms(s, f, PTR_TO_UINT32(k));
@@ -638,23 +619,31 @@ static void dispatch_message_real(
                                 IOVEC_SET_STRING(iovec[n++], x);
                         }
 
+                        if (cg_path_get_slice(c, &t) >= 0) {
+                                x = strappenda("_SYSTEMD_SLICE=", t);
+                                free(t);
+                                IOVEC_SET_STRING(iovec[n++], x);
+                        }
+
                         free(c);
                 }
 
 #ifdef HAVE_SELINUX
-                if (label) {
-                        x = alloca(sizeof("_SELINUX_CONTEXT=") + label_len);
+                if (use_selinux()) {
+                        if (label) {
+                                x = alloca(sizeof("_SELINUX_CONTEXT=") + label_len);
 
-                        *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
-                        IOVEC_SET_STRING(iovec[n++], x);
-                } else {
-                        security_context_t con;
+                                *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
+                                IOVEC_SET_STRING(iovec[n++], x);
+                        } else {
+                                security_context_t con;
 
-                        if (getpidcon(ucred->pid, &con) >= 0) {
-                                x = strappenda("_SELINUX_CONTEXT=", con);
+                                if (getpidcon(ucred->pid, &con) >= 0) {
+                                        x = strappenda("_SELINUX_CONTEXT=", con);
 
-                                freecon(con);
-                                IOVEC_SET_STRING(iovec[n++], x);
+                                        freecon(con);
+                                        IOVEC_SET_STRING(iovec[n++], x);
+                                }
                         }
                 }
 #endif
@@ -897,8 +886,10 @@ static int system_journal_open(Server *s) {
         char ids[33];
 
         r = sd_id128_get_machine(&machine);
-        if (r < 0)
+        if (r < 0) {
+                log_error("Failed to get machine id: %s", strerror(-r));
                 return r;
+        }
 
         sd_id128_to_string(machine, ids);
 
@@ -1000,10 +991,8 @@ int server_flush_to_var(Server *s) {
         log_debug("Flushing to /var...");
 
         r = sd_id128_get_machine(&machine);
-        if (r < 0) {
-                log_error("Failed to get machine id: %s", strerror(-r));
+        if (r < 0)
                 return r;
-        }
 
         r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
         if (r < 0) {
@@ -1075,7 +1064,8 @@ int process_event(Server *s, struct epoll_event *ev) {
                 ssize_t n;
 
                 if (ev->events != EPOLLIN) {
-                        log_error("Got invalid event from epoll.");
+                        log_error("Got invalid event from epoll for %s: %"PRIx32,
+                                  "signal fd", ev->events);
                         return -EIO;
                 }
 
@@ -1092,6 +1082,8 @@ int process_event(Server *s, struct epoll_event *ev) {
                 }
 
                 if (sfsi.ssi_signo == SIGUSR1) {
+                        log_info("Received request to flush runtime journal from PID %"PRIu32,
+                                 sfsi.ssi_pid);
                         touch("/run/systemd/journal/flushed");
                         server_flush_to_var(s);
                         server_sync(s);
@@ -1099,6 +1091,8 @@ int process_event(Server *s, struct epoll_event *ev) {
                 }
 
                 if (sfsi.ssi_signo == SIGUSR2) {
+                        log_info("Received request to rotate journal from PID %"PRIu32,
+                                 sfsi.ssi_pid);
                         server_rotate(s);
                         server_vacuum(s);
                         return 1;
@@ -1124,8 +1118,12 @@ int process_event(Server *s, struct epoll_event *ev) {
         } else if (ev->data.fd == s->dev_kmsg_fd) {
                 int r;
 
-                if (ev->events != EPOLLIN) {
-                        log_error("Got invalid event from epoll.");
+                if (ev->events & EPOLLERR)
+                        log_warning("/dev/kmsg buffer overrun, some messages lost.");
+
+                if (!(ev->events & EPOLLIN)) {
+                        log_error("Got invalid event from epoll for %s: %"PRIx32,
+                                  "/dev/kmsg", ev->events);
                         return -EIO;
                 }
 
@@ -1139,7 +1137,9 @@ int process_event(Server *s, struct epoll_event *ev) {
                    ev->data.fd == s->syslog_fd) {
 
                 if (ev->events != EPOLLIN) {
-                        log_error("Got invalid event from epoll.");
+                        log_error("Got invalid event from epoll for %s: %"PRIx32,
+                                  ev->data.fd == s->native_fd ? "native fd" : "syslog fd",
+                                  ev->events);
                         return -EIO;
                 }
 
@@ -1237,8 +1237,6 @@ int process_event(Server *s, struct epoll_event *ev) {
                         }
 
                         if (ev->data.fd == s->syslog_fd) {
-                                char *e;
-
                                 if (n > 0 && n_fds == 0) {
                                         s->buffer[n] = 0;
                                         server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
@@ -1262,7 +1260,8 @@ int process_event(Server *s, struct epoll_event *ev) {
         } else if (ev->data.fd == s->stdout_fd) {
 
                 if (ev->events != EPOLLIN) {
-                        log_error("Got invalid event from epoll.");
+                        log_error("Got invalid event from epoll for %s: %"PRIx32,
+                                  "stdout fd", ev->events);
                         return -EIO;
                 }
 
@@ -1273,7 +1272,8 @@ int process_event(Server *s, struct epoll_event *ev) {
                 StdoutStream *stream;
 
                 if ((ev->events|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
-                        log_error("Got invalid event from epoll.");
+                        log_error("Got invalid event from epoll for %s: %"PRIx32,
+                                  "stdout stream", ev->events);
                         return -EIO;
                 }