803fb7
From e9bef2f8146ccf152459248775eec8e8ce123865 Mon Sep 17 00:00:00 2001
803fb7
From: Lennart Poettering <lennart@poettering.net>
803fb7
Date: Wed, 22 Apr 2015 22:54:23 +0200
803fb7
Subject: [PATCH] journalctl: rework code that checks whether we have access to
803fb7
 /var/log/journal
803fb7
803fb7
- fix some memory leaks on error conditions
803fb7
803fb7
- handle all error cases properly, and log about failures
803fb7
803fb7
- move HAVE_ACL and no-HAVE_ACL code closer to each other
803fb7
803fb7
Cherry-picked from: e346512c684e9efae84c6442f7e6a5781564ecde
803fb7
Related: #1318994
803fb7
---
de8967
 src/journal/journalctl.c | 120 ++++++++++++++++++++-------------------
de8967
 src/shared/acl-util.c    | 102 ++++++++++++++++++---------------
803fb7
 src/shared/acl-util.h    |   2 +-
803fb7
 3 files changed, 119 insertions(+), 105 deletions(-)
803fb7
803fb7
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
803fb7
index 6ba884779..f60e6415f 100644
803fb7
--- a/src/journal/journalctl.c
803fb7
+++ b/src/journal/journalctl.c
803fb7
@@ -1680,61 +1680,76 @@ static int verify(sd_journal *j) {
803fb7
         return r;
803fb7
 }
803fb7
 
803fb7
-#ifdef HAVE_ACL
803fb7
 static int access_check_var_log_journal(sd_journal *j) {
803fb7
+#ifdef HAVE_ACL
803fb7
         _cleanup_strv_free_ char **g = NULL;
803fb7
-        bool have_access;
803fb7
+        const char* dir;
803fb7
+#endif
803fb7
         int r;
803fb7
 
803fb7
         assert(j);
803fb7
 
803fb7
-        have_access = in_group("systemd-journal") > 0;
803fb7
+        if (arg_quiet)
803fb7
+                return 0;
803fb7
 
803fb7
-        if (!have_access) {
803fb7
-                const char* dir;
803fb7
+        /* If we are root, we should have access, don't warn. */
803fb7
+        if (getuid() == 0)
803fb7
+                return 0;
803fb7
 
803fb7
-                if (access("/run/log/journal", F_OK) >= 0)
803fb7
-                        dir = "/run/log/journal";
803fb7
-                else
803fb7
-                        dir = "/var/log/journal";
803fb7
+        /* If we are in the 'systemd-journal' group, we should have
803fb7
+         * access too. */
803fb7
+        r = in_group("systemd-journal");
803fb7
+        if (r < 0)
803fb7
+                return log_error_errno(r, "Failed to check if we are in the 'systemd-journal' group: %m");
803fb7
+        if (r > 0)
803fb7
+                return 0;
803fb7
 
803fb7
-                /* Let's enumerate all groups from the default ACL of
803fb7
-                 * the directory, which generally should allow access
803fb7
-                 * to most journal files too */
803fb7
-                r = search_acl_groups(&g, dir, &have_access);
803fb7
-                if (r < 0)
803fb7
-                        return r;
803fb7
-        }
803fb7
+#ifdef HAVE_ACL
803fb7
+        if (laccess("/run/log/journal", F_OK) >= 0)
803fb7
+                dir = "/run/log/journal";
803fb7
+        else
803fb7
+                dir = "/var/log/journal";
803fb7
 
803fb7
-        if (!have_access) {
803fb7
+        /* If we are in any of the groups listed in the journal ACLs,
803fb7
+         * then all is good, too. Let's enumerate all groups from the
803fb7
+         * default ACL of the directory, which generally should allow
803fb7
+         * access to most journal files too. */
803fb7
+        r = acl_search_groups(dir, &g);
803fb7
+        if (r < 0)
803fb7
+                return log_error_errno(r, "Failed to search journal ACL: %m");
803fb7
+        if (r > 0)
803fb7
+                return 0;
803fb7
 
803fb7
-                if (strv_isempty(g))
803fb7
-                        log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
803fb7
-                                   "      Users in the 'systemd-journal' group can see all messages. Pass -q to\n"
803fb7
-                                   "      turn off this notice.");
803fb7
-                else {
803fb7
-                        _cleanup_free_ char *s = NULL;
803fb7
+        /* Print a pretty list, if there were ACLs set. */
803fb7
+        if (!strv_isempty(g)) {
803fb7
+                _cleanup_free_ char *s = NULL;
803fb7
 
803fb7
-                        r = strv_extend(&g, "systemd-journal");
803fb7
-                        if (r < 0)
803fb7
-                                return log_oom();
803fb7
+                /* Thre are groups in the ACL, let's list them */
803fb7
+                r = strv_extend(&g, "systemd-journal");
803fb7
+                if (r < 0)
803fb7
+                        return log_oom();
803fb7
 
803fb7
-                        strv_sort(g);
803fb7
-                        strv_uniq(g);
803fb7
+                strv_sort(g);
803fb7
+                strv_uniq(g);
803fb7
 
803fb7
-                        s = strv_join(g, "', '");
803fb7
-                        if (!s)
803fb7
-                                return log_oom();
803fb7
+                s = strv_join(g, "', '");
803fb7
+                if (!s)
803fb7
+                        return log_oom();
803fb7
 
803fb7
-                        log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
803fb7
-                                   "      Users in groups '%s' can see all messages.\n"
803fb7
-                                   "      Pass -q to turn off this notice.", s);
803fb7
-                }
803fb7
+                log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
803fb7
+                           "      Users in groups '%s' can see all messages.\n"
803fb7
+                           "      Pass -q to turn off this notice.", s);
803fb7
+                return 1;
803fb7
         }
803fb7
+#endif
803fb7
 
803fb7
-        return 0;
803fb7
+        /* If no ACLs were found, print a short version of the message. */
803fb7
+        log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
803fb7
+                   "      Users in the 'systemd-journal' group can see all messages. Pass -q to\n"
803fb7
+                   "      turn off this notice.");
803fb7
+
803fb7
+        return 1;
803fb7
 }
803fb7
-#endif
803fb7
 
803fb7
 static int access_check(sd_journal *j) {
803fb7
         Iterator it;
803fb7
@@ -1746,30 +1761,15 @@ static int access_check(sd_journal *j) {
803fb7
         if (set_isempty(j->errors)) {
803fb7
                 if (ordered_hashmap_isempty(j->files))
803fb7
                         log_notice("No journal files were found.");
803fb7
+
803fb7
                 return 0;
803fb7
         }
803fb7
 
803fb7
         if (set_contains(j->errors, INT_TO_PTR(-EACCES))) {
803fb7
-#ifdef HAVE_ACL
803fb7
-                /* If /run/log/journal or /var/log/journal exist, try
803fb7
-                   to pring a nice notice if the user lacks access to it. */
803fb7
-                if (!arg_quiet && geteuid() != 0) {
803fb7
-                        r = access_check_var_log_journal(j);
803fb7
-                        if (r < 0)
803fb7
-                                return r;
803fb7
-                }
803fb7
-#else
803fb7
-                if (geteuid() != 0 && in_group("systemd-journal") <= 0) {
803fb7
-                        log_error("Unprivileged users cannot access messages. Users in the 'systemd-journal' group\n"
803fb7
-                                  "group may access messages.");
803fb7
-                        return -EACCES;
803fb7
-                }
803fb7
-#endif
803fb7
+                (void) access_check_var_log_journal(j);
803fb7
 
803fb7
-                if (ordered_hashmap_isempty(j->files)) {
803fb7
-                        log_error("No journal files were opened due to insufficient permissions.");
803fb7
-                        r = -EACCES;
803fb7
-                }
803fb7
+                if (ordered_hashmap_isempty(j->files))
803fb7
+                        r = log_error_errno(EACCES, "No journal files were opened due to insufficient permissions.");
803fb7
         }
803fb7
 
803fb7
         SET_FOREACH(code, j->errors, it) {
803fb7
@@ -1778,8 +1778,12 @@ static int access_check(sd_journal *j) {
803fb7
                 err = -PTR_TO_INT(code);
803fb7
                 assert(err > 0);
803fb7
 
803fb7
-                if (err != EACCES)
803fb7
-                        log_warning_errno(err, "Error was encountered while opening journal files: %m");
803fb7
+                if (err == EACCES)
803fb7
+                        continue;
803fb7
+
803fb7
+                log_warning_errno(err, "Error was encountered while opening journal files: %m");
803fb7
+                if (r == 0)
803fb7
+                        r = -err;
803fb7
         }
803fb7
 
803fb7
         return r;
803fb7
diff --git a/src/shared/acl-util.c b/src/shared/acl-util.c
803fb7
index e67e9acb6..d18a02f50 100644
803fb7
--- a/src/shared/acl-util.c
803fb7
+++ b/src/shared/acl-util.c
803fb7
@@ -82,17 +82,18 @@ int calc_acl_mask_if_needed(acl_t *acl_p) {
803fb7
 
803fb7
                 if (tag == ACL_MASK)
803fb7
                         return 0;
803fb7
-                if (IN_SET(tag, ACL_USER, ACL_GROUP))
803fb7
-                        goto calc;
803fb7
+
803fb7
+                if (IN_SET(tag, ACL_USER, ACL_GROUP)) {
803fb7
+                        if (acl_calc_mask(acl_p) < 0)
803fb7
+                                return -errno;
803fb7
+
803fb7
+                        return 1;
803fb7
+                }
803fb7
         }
803fb7
         if (r < 0)
803fb7
                 return -errno;
803fb7
-        return 0;
803fb7
 
803fb7
-calc:
803fb7
-        if (acl_calc_mask(acl_p) < 0)
803fb7
-                return -errno;
803fb7
-        return 1;
803fb7
+        return 0;
803fb7
 }
803fb7
 
803fb7
 int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
803fb7
@@ -159,59 +160,68 @@ int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
-int search_acl_groups(char*** dst, const char* path, bool* belong) {
803fb7
-        acl_t acl;
803fb7
+int acl_search_groups(const char *path, char ***ret_groups) {
803fb7
+        _cleanup_strv_free_ char **g = NULL;
803fb7
+        _cleanup_(acl_free) acl_t acl = NULL;
803fb7
+        bool ret = false;
803fb7
+        acl_entry_t entry;
803fb7
+        int r;
803fb7
 
803fb7
         assert(path);
803fb7
-        assert(belong);
803fb7
 
803fb7
         acl = acl_get_file(path, ACL_TYPE_DEFAULT);
803fb7
-        if (acl) {
803fb7
-                acl_entry_t entry;
803fb7
-                int r;
803fb7
-
803fb7
-                r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
803fb7
-                while (r > 0) {
803fb7
-                        acl_tag_t tag;
803fb7
-                        gid_t *gid;
803fb7
-                        char *name;
803fb7
+        if (!acl)
803fb7
+                return -errno;
803fb7
 
803fb7
-                        r = acl_get_tag_type(entry, &tag;;
803fb7
-                        if (r < 0)
803fb7
-                                break;
803fb7
+        r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
803fb7
+        for (;;) {
803fb7
+                _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
803fb7
+                acl_tag_t tag;
803fb7
+
803fb7
+                if (r < 0)
803fb7
+                        return -errno;
803fb7
+                if (r == 0)
803fb7
+                        break;
803fb7
+
803fb7
+                if (acl_get_tag_type(entry, &tag) < 0)
803fb7
+                        return -errno;
803fb7
 
803fb7
-                        if (tag != ACL_GROUP)
803fb7
-                                goto next;
803fb7
+                if (tag != ACL_GROUP)
803fb7
+                        goto next;
803fb7
 
803fb7
-                        gid = acl_get_qualifier(entry);
803fb7
-                        if (!gid)
803fb7
-                                break;
803fb7
+                gid = acl_get_qualifier(entry);
803fb7
+                if (!gid)
803fb7
+                        return -errno;
803fb7
+
803fb7
+                if (in_gid(*gid) > 0) {
803fb7
+                        if (!ret_groups)
803fb7
+                                return true;
803fb7
 
803fb7
-                        if (in_gid(*gid) > 0) {
803fb7
-                                *belong = true;
803fb7
-                                break;
803fb7
-                        }
803fb7
+                        ret = true;
803fb7
+                }
803fb7
+
803fb7
+                if (ret_groups) {
803fb7
+                        char *name;
803fb7
 
803fb7
                         name = gid_to_name(*gid);
803fb7
-                        if (!name) {
803fb7
-                                acl_free(acl);
803fb7
-                                return log_oom();
803fb7
-                        }
803fb7
-
803fb7
-                        r = strv_consume(dst, name);
803fb7
-                        if (r < 0) {
803fb7
-                                acl_free(acl);
803fb7
-                                return log_oom();
803fb7
-                        }
803fb7
-
803fb7
-                next:
803fb7
-                        r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
803fb7
+                        if (!name)
803fb7
+                                return -ENOMEM;
803fb7
+
803fb7
+                        r = strv_consume(&g, name);
803fb7
+                        if (r < 0)
803fb7
+                                return r;
803fb7
                 }
803fb7
 
803fb7
-                acl_free(acl);
803fb7
+        next:
803fb7
+                r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
803fb7
         }
803fb7
 
803fb7
-        return 0;
803fb7
+        if (ret_groups) {
803fb7
+                *ret_groups = g;
803fb7
+                g = NULL;
803fb7
+        }
803fb7
+
803fb7
+        return ret;
803fb7
 }
803fb7
 
803fb7
 int parse_acl(char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
803fb7
diff --git a/src/shared/acl-util.h b/src/shared/acl-util.h
803fb7
index fdb90063f..c8bcc266d 100644
803fb7
--- a/src/shared/acl-util.h
803fb7
+++ b/src/shared/acl-util.h
803fb7
@@ -32,7 +32,7 @@
803fb7
 int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry);
803fb7
 int calc_acl_mask_if_needed(acl_t *acl_p);
803fb7
 int add_base_acls_if_needed(acl_t *acl_p, const char *path);
803fb7
-int search_acl_groups(char*** dst, const char* path, bool* belong);
803fb7
+int acl_search_groups(const char* path, char ***ret_groups);
803fb7
 int parse_acl(char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask);
803fb7
 int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl);
803fb7