From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Subject: [PATCH] Fix CVE-2026-42394 Upstream commits: lib: i_close_fd*() - Document that it preserves errno https://github.com/dovecot/core/commit/40a60ac7163180098415644506c62200bbb908a6 lib: path-util - Add t_openat_safe() for jailed openat() resolution https://github.com/dovecot/core/commit/3ad0946aacdb6b18927570d1fb29c53e5d157a01 lib: Add t_openat_safe_dir() https://github.com/dovecot/core/commit/22379bb0f01a46fb2c0d15e226744b49394fdf78 acl: dovecot-acl-list is never supposed to be a symlink - use O_NOFOLLOW flag https://github.com/dovecot/core/commit/0375f6f9851ce0ad6a20bd85449384284728118b lib: Add comments about memory allocations and string functions preserving errno https://github.com/dovecot/core/commit/54a9bdf057cf7574bdd6d6ab3e1a6c60eb6584fc Taken from the Debian bookworm security update (dovecot 1:2.3.19.1+dfsg1-2.1+deb12u7), which carries these fixes for 2.3, and applied to 2.3.21.1. The Pigeonhole part of this fix is in mail/pigeonhole/patches. --- a/src/lib/data-stack.h +++ b/src/lib/data-stack.h @@ -31,6 +31,11 @@ overflows. */ +/* All data stack allocations - t_malloc(), t_malloc0(), t_buffer_get(), + t_try_realloc() etc. - preserve errno. It is therefore safe to allocate + temporary memory between a failing syscall and reading errno (or + formatting "%m"). */ + #ifndef STATIC_CHECKER typedef unsigned int data_stack_frame_t; #else --- a/src/lib/fd-util.h +++ b/src/lib/fd-util.h @@ -17,7 +17,7 @@ void fd_close_maybe_stdio(int *fd_in, in /* Close the fd and set it to -1. This assert-crashes if fd == 0, and is a no-op if fd == -1. Normally fd == 0 would happen only if an uninitialized - fd is attempted to be closed, which is a bug. */ + fd is attempted to be closed, which is a bug. Preserves original errno. */ void i_close_fd_path(int *fd, const char *path, const char *arg, const char *func, const char *file, int line); #define i_close_fd_path(fd, path) i_close_fd_path((fd), (path), #fd, __func__, __FILE__, __LINE__) --- a/src/lib/imem.h +++ b/src/lib/imem.h @@ -3,6 +3,9 @@ /* For easy allocation of memory from default memory pool. */ +/* Like all pool allocations, i_malloc()/i_realloc()/i_free() and the + i_strdup*() helpers preserve errno (see mempool.h). */ + extern pool_t default_pool; #define i_new(type, count) p_new(default_pool, type, count) --- a/src/lib/mempool.h +++ b/src/lib/mempool.h @@ -19,6 +19,12 @@ zeroed, it will cost only a few CPU cycles and may well save some debug time. */ +/* All pool memory operations - p_malloc(), p_realloc(), p_free() and the + p_new()/p_strdup*() helpers built on them - preserve errno. This means an + allocation between a failing syscall and reading errno (e.g. when building + an error string with "%m") will not clobber errno. The same guarantee holds + for the t_* (data stack) and i_* (default pool) allocators. */ + typedef struct pool *pool_t; struct pool_vfuncs { --- a/src/lib/path-util.c +++ b/src/lib/path-util.c @@ -4,12 +4,17 @@ #include "str.h" #include "path-util.h" +#include #include #include #include #define PATH_UTIL_MAX_PATH 8*1024 #define PATH_UTIL_MAX_SYMLINKS 80 +/* Symlink-hop cap for t_openat_safe(). Lower than the realpath cap: + t_openat_safe() refuses absolute and escaping targets outright, so a + long chain inside the base dir is the only legitimate use. */ +#define PATH_UTIL_OPENAT_SAFE_MAX_SYMLINKS 16 static int t_getcwd_noalloc(char **dir_r, size_t *asize_r, const char **error_r) ATTR_NULL(2) @@ -358,6 +363,190 @@ int t_readlink(const char *path, const c return 0; } +/* Iteratively resolve `path` beneath `cur_fd` using openat() with + O_NOFOLLOW per component. Symlinks are resolved manually: their targets + are validated (non-absolute, no '..' past the base) and the loop + restarts with the target spliced into the remaining path. dir_depth + tracks how many directory levels we have descended below the original + base; '..' is refused when the depth would go negative. + */ +static int +path_openat_safe_walk(int cur_fd, const char *path, int flags, + const char **error_r) +{ + bool cur_owned = FALSE; + unsigned int symlink_count = 0; + unsigned int dir_depth = 0; + + /* Absolute paths cannot stay beneath the base directory. */ + if (*path == '/') { + errno = ELOOP; + *error_r = "Path is absolute (escapes base directory)"; + return -1; + } + + for (;;) { + while (*path == '/') + path++; + + if (*path == '\0') { + /* Empty path or trailing slash: open the current + directory itself with the requested flags. */ + int fd = openat(cur_fd, ".", + flags | O_NOFOLLOW | O_CLOEXEC); + if (cur_owned) + i_close_fd(&cur_fd); + if (fd < 0) { + *error_r = t_strdup_printf( + "openat() failed: %m"); + } + return fd; + } + + const char *slash = strchr(path, '/'); + size_t comp_len = (slash != NULL ? + (size_t)(slash - path) : strlen(path)); + bool is_last = (slash == NULL); + + if (comp_len == 1 && path[0] == '.') { + path += comp_len; + continue; + } + + if (comp_len == 2 && path[0] == '.' && path[1] == '.') { + if (dir_depth == 0) { + if (cur_owned) + i_close_fd(&cur_fd); + errno = ELOOP; + *error_r = "Path escapes base directory via '..'"; + return -1; + } + int parent_fd = openat(cur_fd, "..", + O_RDONLY | O_DIRECTORY | + O_NOFOLLOW | O_CLOEXEC); + if (cur_owned) + i_close_fd(&cur_fd); + if (parent_fd < 0) { + *error_r = t_strdup_printf( + "openat(..) failed: %m"); + return -1; + } + cur_fd = parent_fd; + cur_owned = TRUE; + dir_depth--; + path += comp_len; + continue; + } + + const char *comp = t_strdup_until(path, path + comp_len); + int next_fd; + if (is_last) { + next_fd = openat(cur_fd, comp, + flags | O_NOFOLLOW | O_CLOEXEC); + } else { + next_fd = openat(cur_fd, comp, + O_RDONLY | O_DIRECTORY | + O_NOFOLLOW | O_CLOEXEC); + } + + if (next_fd < 0 && (errno == ELOOP || errno == EMLINK)) { + /* Component is a symlink. Resolve it ourselves and + restart processing with its target spliced into + `path`; that way the target is itself walked + component-by-component and the boundary check + continues to apply. (O_NOFOLLOW on a symlink reports + ELOOP on Linux/macOS but EMLINK on the BSDs, so both + must be treated as "this is a symlink".) */ + if (++symlink_count > + PATH_UTIL_OPENAT_SAFE_MAX_SYMLINKS) { + if (cur_owned) + i_close_fd(&cur_fd); + errno = ELOOP; + *error_r = "Too many symlink dereferences"; + return -1; + } + + char buf[PATH_UTIL_MAX_PATH]; + ssize_t llen = readlinkat(cur_fd, comp, + buf, sizeof(buf) - 1); + if (llen < 0) { + if (cur_owned) + i_close_fd(&cur_fd); + *error_r = t_strdup_printf( + "readlinkat(%s) failed: %m", comp); + return -1; + } + buf[llen] = '\0'; + + if (buf[0] == '/') { + if (cur_owned) + i_close_fd(&cur_fd); + errno = ELOOP; + *error_r = t_strdup_printf( + "Symlink '%s' has absolute target " + "'%s' (escapes base directory)", + comp, buf); + return -1; + } + + path = is_last ? t_strdup(buf) : + t_strconcat(buf, "/", + slash + 1, NULL); + continue; + } + + if (next_fd < 0) { + if (cur_owned) + i_close_fd(&cur_fd); + *error_r = t_strdup_printf( + "openat(%s) failed: %m", comp); + return -1; + } + + if (is_last) { + if (cur_owned) + i_close_fd(&cur_fd); + return next_fd; + } + + if (cur_owned) + i_close_fd(&cur_fd); + cur_fd = next_fd; + cur_owned = TRUE; + dir_depth++; + path = slash + 1; + } +} + +int t_openat_safe(int base_fd, const char *path, int flags, + const char **error_r) +{ + i_assert(base_fd >= 0); + i_assert(path != NULL); + i_assert(error_r != NULL); + + return path_openat_safe_walk(base_fd, path, flags, error_r); +} + +int t_openat_safe_dir(const char *base_dir, const char *path, int flags, + const char **error_r) +{ + i_assert(base_dir != NULL); + i_assert(path != NULL); + i_assert(error_r != NULL); + + int dir_fd = open(base_dir, + O_DIRECTORY | O_RDONLY | O_CLOEXEC | + (flags & O_NOFOLLOW)); + if (dir_fd < 0) { + *error_r = t_strdup_printf("open(%s) failed: %m", base_dir); + return -1; + } + int fd = t_openat_safe(dir_fd, path, flags, error_r); + i_close_fd(&dir_fd); + return fd; +} + bool t_binary_abspath(const char **binpath, const char **error_r) { const char *path_env, *const *paths; --- a/src/lib/path-util.h +++ b/src/lib/path-util.h @@ -57,6 +57,42 @@ int t_get_working_dir(const char **dir_r * -1 on failure. error_r is set on failure and cannot be NULL. */ int t_readlink(const char *path, const char **dest_r, const char **error_r); +/* Open `path` relative to `base_fd`, refusing any resolution that escapes + the directory referenced by `base_fd`. Each path component is opened with + O_NOFOLLOW so symlinks are detected explicitly rather than transparently + followed; symlinks are followed only when their (recursively resolved) + target also stays beneath `base_fd`. Absolute symlink targets, leading + '/', and '..' past the base are rejected. + + `base_fd` must reference a directory (e.g. obtained via + open(dir, O_DIRECTORY|O_RDONLY|O_CLOEXEC)) and remains owned by the + caller; this function does not close it. + + `flags` is OR-ed into the openat() call for the final component (e.g. + O_RDONLY). O_NOFOLLOW and O_CLOEXEC are added implicitly. + + Anchoring resolution at `base_fd` makes the lookup TOCTOU-safe even when + intermediate path components are mutated on disk concurrently: the kernel + resolves all component lookups relative to the original directory inode + `base_fd` references. + + Returns the new fd (>= 0) on success. On failure returns -1 with errno + set and *error_r set to a descriptive message: + errno=ELOOP target escapes base, or symlink chain too deep + errno=ENOENT file not found + errno=EACCES permission denied + other errno as set by openat()/readlinkat() + */ +int t_openat_safe(int base_fd, const char *path, int flags, + const char **error_r); + +/* Convenience wrapper: opens base_dir as a directory fd, then delegates to + t_openat_safe(). If O_NOFOLLOW is present in flags it is also applied to + the open() of base_dir itself, refusing a symlink as its final component. + Returns the new fd on success, -1 on failure. */ +int t_openat_safe_dir(const char *base_dir, const char *path, int flags, + const char **error_r); + /* Update binpath to be absolute: * a) begins with '/' -> no change * b) contains '/' -> assume relative to working directory --- a/src/lib/strfuncs.h +++ b/src/lib/strfuncs.h @@ -12,6 +12,13 @@ extern const unsigned char *uchar_empty_ int i_snprintf(char *dest, size_t max_chars, const char *format, ...) ATTR_FORMAT(3, 4); +/* The p_/t_/i_ strdup, strconcat and *printf helpers below preserve errno. + So this pattern is safe - errno still refers to the failed syscall when + "%m" is expanded: + + if (syscall(...) < 0) + error = t_strdup_printf("syscall() failed: %m"); */ + char *p_strdup(pool_t pool, const char *str) ATTR_MALLOC; void *p_memdup(pool_t pool, const void *data, size_t size) ATTR_MALLOC; /* return NULL if str = "" */ --- a/src/lib/test-path-util.c +++ b/src/lib/test-path-util.c @@ -233,6 +233,180 @@ static void test_link_alloc2(void) tmpdir = o_tmpdir; } +static void test_openat_safe(void) +{ + const char *error; + int base_fd, fd; + + base_fd = open(tmpdir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (base_fd < 0) + i_fatal("open(%s) failed: %m", tmpdir); + + /* Plain regular file inside base. */ + const char *plain = t_strconcat(tmpdir, "/plain", NULL); + int wfd = creat(plain, 0600); + if (wfd < 0) + i_fatal("creat(%s) failed: %m", plain); + i_close_fd(&wfd); + + fd = t_openat_safe(base_fd, "plain", O_RDONLY, &error); + test_assert(fd >= 0); + i_close_fd(&fd); + + /* "./plain" and redundant slashes. */ + fd = t_openat_safe(base_fd, "./plain", O_RDONLY, &error); + test_assert(fd >= 0); + i_close_fd(&fd); + fd = t_openat_safe(base_fd, ".//plain", O_RDONLY, &error); + test_assert(fd >= 0); + i_close_fd(&fd); + + /* Symlink whose target is inside the base: allowed. */ + const char *in_link = t_strconcat(tmpdir, "/in-link", NULL); + if (symlink("plain", in_link) < 0) + i_fatal("symlink(plain, %s) failed: %m", in_link); + fd = t_openat_safe(base_fd, "in-link", O_RDONLY, &error); + test_assert(fd >= 0); + i_close_fd(&fd); + + /* Absolute symlink target: refused. */ + const char *abs_link = t_strconcat(tmpdir, "/abs-link", NULL); + if (symlink("/etc/hostname", abs_link) < 0) + i_fatal("symlink failed: %m"); + errno = 0; + fd = t_openat_safe(base_fd, "abs-link", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ELOOP); + test_assert(error != NULL); + + /* Relative symlink with '..' that escapes the base: refused. */ + const char *escape_link = t_strconcat(tmpdir, "/escape-link", NULL); + if (symlink("../../etc/hostname", escape_link) < 0) + i_fatal("symlink failed: %m"); + errno = 0; + fd = t_openat_safe(base_fd, "escape-link", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ELOOP); + test_assert(error != NULL); + + /* Direct '..' escape attempt: refused. */ + errno = 0; + fd = t_openat_safe(base_fd, "../plain", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ELOOP); + + /* Absolute literal path: refused. */ + errno = 0; + fd = t_openat_safe(base_fd, "/etc/hostname", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ELOOP); + + /* link2's target is an absolute path (tmpdir/link3): refused as an + absolute target before the loop is ever followed. */ + errno = 0; + fd = t_openat_safe(base_fd, "link2", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ELOOP); + + /* Relative symlink loop staying inside the base: nothing rejects it + except the hop counter, so this exercises + PATH_UTIL_OPENAT_SAFE_MAX_SYMLINKS. */ + const char *loopa = t_strconcat(tmpdir, "/loopa", NULL); + const char *loopb = t_strconcat(tmpdir, "/loopb", NULL); + if (symlink("loopb", loopa) < 0) + i_fatal("symlink(loopb, %s) failed: %m", loopa); + if (symlink("loopa", loopb) < 0) + i_fatal("symlink(loopa, %s) failed: %m", loopb); + errno = 0; + fd = t_openat_safe(base_fd, "loopa", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ELOOP); + test_assert(error != NULL); + + /* '..' that stays within the base: allowed. Create sub/under and + reach back to plain via sub/../plain. */ + const char *subdir = t_strconcat(tmpdir, "/sub", NULL); + if (mkdir(subdir, 0700) < 0 && errno != EEXIST) + i_fatal("mkdir(%s) failed: %m", subdir); + fd = t_openat_safe(base_fd, "sub/../plain", O_RDONLY, &error); + test_assert(fd >= 0); + i_close_fd(&fd); + + /* Breaking out of the base via subdirectory. */ + errno = 0; + fd = t_openat_safe(base_fd, "sub/../../plain", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ELOOP); + + /* Nonexistent file inside base: ENOENT, not ELOOP. */ + errno = 0; + fd = t_openat_safe(base_fd, "no-such-file", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ENOENT); + + i_close_fd(&base_fd); + + i_unlink(plain); + i_unlink(in_link); + i_unlink(abs_link); + i_unlink(escape_link); + i_unlink(loopa); + i_unlink(loopb); + if (rmdir(subdir) < 0) + i_error("rmdir(%s) failed: %m", subdir); +} + +static void test_openat_safe_dir(void) +{ + const char *error; + int fd; + + /* Plain file: success. */ + const char *plain = t_strconcat(tmpdir, "/plain2", NULL); + int wfd = creat(plain, 0600); + if (wfd < 0) + i_fatal("creat(%s) failed: %m", plain); + i_close_fd(&wfd); + + fd = t_openat_safe_dir(tmpdir, "plain2", O_RDONLY, &error); + test_assert(fd >= 0); + i_close_fd(&fd); + + /* Nonexistent base_dir. */ + errno = 0; + fd = t_openat_safe_dir(t_strconcat(tmpdir, "/no-such-dir", NULL), + "plain2", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ENOENT); + test_assert(error != NULL); + + /* Absolute symlink in relative part: refused. */ + const char *abs_link2 = t_strconcat(tmpdir, "/abs-link2", NULL); + if (symlink("/etc/hostname", abs_link2) < 0) + i_fatal("symlink failed: %m"); + errno = 0; + fd = t_openat_safe_dir(tmpdir, "abs-link2", O_RDONLY, &error); + test_assert(fd == -1); + test_assert(errno == ELOOP); + + /* O_NOFOLLOW: symlink as base_dir final component refused. */ + const char *dir_link = t_strconcat(tmpdir, "/dir-link", NULL); + if (symlink(tmpdir, dir_link) < 0) + i_fatal("symlink failed: %m"); + errno = 0; + fd = t_openat_safe_dir(dir_link, "plain2", + O_RDONLY | O_NOFOLLOW, &error); + test_assert(fd == -1); + /* Without O_NOFOLLOW the same symlinked base_dir succeeds. */ + fd = t_openat_safe_dir(dir_link, "plain2", O_RDONLY, &error); + test_assert(fd >= 0); + i_close_fd(&fd); + + i_unlink(plain); + i_unlink(abs_link2); + i_unlink(dir_link); +} + static void test_cleanup(void) { const char *error; @@ -272,6 +446,8 @@ void test_path_util(void) test_abspath_vs_normpath(); test_link_alloc(); test_link_alloc2(); + test_openat_safe(); + test_openat_safe_dir(); test_cleanup(); alarm(0); test_end(); --- a/src/plugins/acl/acl-backend-vfile-acllist.c +++ b/src/plugins/acl/acl-backend-vfile-acllist.c @@ -114,7 +114,7 @@ static int acl_backend_vfile_acllist_rea return 0; } - fd = open(path, O_RDONLY); + fd = open(path, O_RDONLY | O_NOFOLLOW); if (fd == -1) { if (errno == ENOENT) { backend->acllist_mtime = 0;