https://github.com/linux-audit/audit-userspace/commit/798b4a16bcd592c8822aa88122b0e5aaef504292 From 798b4a16bcd592c8822aa88122b0e5aaef504292 Mon Sep 17 00:00:00 2001 From: Steve Grubb Date: Thu, 3 Dec 2026 15:51:55 -0500 Subject: [PATCH] Avoid directory read checks while traversing audit log paths The 4.2.2 log-path hardening opened every directory with O_RDONLY. Fedora SELinux permits auditd_t to search and inspect /var but denies directory read access to var_t, so startup failed with an AVC and an unsafe-log-directory error on an otherwise valid installation. Pin and validate directory components with O_PATH, including the system root. Reopen only the pinned final log directory with O_RDONLY so permission repair can still use fchmod and fchown. Keep the no-symlink, ownership, and writable-ancestor checks. Add a searchable-but-unreadable ancestor regression that drops effective root privileges when necessary. It fails before the fix and passes afterward. --- src/auditd-log.c | 19 ++++++++++------ src/test/auditd_log_test.c | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/auditd-log.c b/src/auditd-log.c index 55539961e..762d992b6 100644 --- a/src/auditd-log.c +++ b/src/auditd-log.c @@ -53,15 +53,16 @@ static int validate_directory(int dir_fd, * @name: component name to open * @policy: ownership policy for the audit log hierarchy * - * Returns an owned directory descriptor on success and -1 on failure. + * Returns an owned O_PATH directory descriptor on success and -1 on failure. */ static int open_directory(int dir_fd, const char *name, const struct auditd_log_policy *policy) { int fd; + /* Ancestors need search/getattr access, not SELinux directory read. */ fd = openat(dir_fd, name, - O_RDONLY|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC); + O_PATH|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC); if (fd < 0) return -1; if (validate_directory(fd, policy, NULL) < 0) { @@ -86,7 +87,7 @@ static int open_trusted_directory(int root_fd, const char *directory, const struct auditd_log_policy *policy) { char *component, *copy, *saveptr; - int dir_fd; + int dir_fd, fd; dir_fd = fcntl(root_fd, F_DUPFD_CLOEXEC, 0); if (dir_fd < 0) @@ -99,8 +100,6 @@ static int open_trusted_directory(int root_fd, const char *directory, goto bad_dir; component = strtok_r(copy, "/", &saveptr); while (component) { - int fd; - /* Dot components make the configured path needlessly ambiguous. */ if (strcmp(component, ".") == 0 || strcmp(component, "..") == 0) { @@ -114,8 +113,14 @@ static int open_trusted_directory(int root_fd, const char *directory, dir_fd = fd; component = strtok_r(NULL, "/", &saveptr); } + /* Permission repair needs a normal descriptor for fchmod/fchown. */ + fd = openat(dir_fd, ".", + O_RDONLY|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC); + if (fd < 0) + goto bad_copy; + close(dir_fd); free(copy); - return dir_fd; + return fd; bad_copy: { @@ -202,7 +207,7 @@ int auditd_log_path_open(struct auditd_log_path *path, const char *file, { int root_fd, rc, saved_errno; - root_fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC); + root_fd = open("/", O_PATH|O_DIRECTORY|O_CLOEXEC); if (root_fd < 0) return -1; rc = auditd_log_path_openat(path, root_fd, file, policy); diff --git a/src/test/auditd_log_test.c b/src/test/auditd_log_test.c index 3871deb69..7046ecc54 100644 --- a/src/test/auditd_log_test.c +++ b/src/test/auditd_log_test.c @@ -237,6 +237,51 @@ static int test_root_group_preserves_directory(const char *root, int root_fd, return rc == 0 && mode == 0755 && object_group(directory) == group; } +/* + * test_search_only_ancestor - traverse a parent without directory read access + * @root: disposable test-root pathname + * @root_fd: descriptor treated as the test resolution root + * @policy: ownership policy for the test + * + * Returns: None. The test aborts on failure. + */ +static void test_search_only_ancestor(const char *root, int root_fd, + const struct auditd_log_policy *policy) +{ + struct auditd_log_policy root_policy = *policy; + struct auditd_log_path log_path; + char ancestor[PATH_MAX], directory[PATH_MAX]; + uid_t uid = geteuid(); + + root_policy.group = 0; + root_policy.num_logs = 1; + make_path(ancestor, sizeof(ancestor), root, "search-only"); + make_directory(ancestor, 0700); + make_path(directory, sizeof(directory), root, "search-only/log-dir"); + make_directory(directory, 0755); + assert(chmod(ancestor, 0111) == 0); + if (uid == 0) { + /* Root-run CI must not bypass the missing read permission. */ + assert(chmod(root, 0711) == 0); + assert(seteuid(65534) == 0); + } + errno = 0; + assert(openat(root_fd, "search-only", + O_RDONLY|O_DIRECTORY|O_CLOEXEC) == -1); + assert(errno == EACCES); + assert(auditd_log_path_openat(&log_path, root_fd, + "/search-only/log-dir/audit.log", &root_policy) == 0); + assert(auditd_log_repair_permissions(&log_path, &root_policy) == 0); + auditd_log_path_close(&log_path); + if (uid == 0) { + assert(seteuid(0) == 0); + assert(chmod(root, 0700) == 0); + } + assert(chmod(ancestor, 0700) == 0); + assert(rmdir(directory) == 0); + assert(rmdir(ancestor) == 0); +} + /* * cleanup_tree - remove every disposable test fixture * @root: disposable test-root pathname @@ -296,6 +341,7 @@ int main(void) root_fd = open(root, O_RDONLY|O_DIRECTORY|O_CLOEXEC); assert(root_fd >= 0); setup_tree(root); + test_search_only_ancestor(root, root_fd, &policy); test_untrusted_components_are_rejected(root, root_fd, &policy); root_group_preserved = test_root_group_preserves_directory(root, root_fd, &policy);