1
0
mirror of https://github.com/systemd/systemd synced 2026-03-16 18:14:46 +01:00

Compare commits

...

5 Commits

Author SHA1 Message Date
gvenugo3
4f187dc7c1 test: add test case for masked files in cat_files()
Add a test case to verify that cat_files() correctly handles symlinks
to /dev/null (masked configuration files) and returns success (0)
instead of failing.

This test complements the fix for issue #40313.

Co-authored-by: Yu Watanabe <watanabe.yu+github@gmail.com>
(cherry picked from commit 01efa01dd28b9c47c1f5fe84c684d9d13192ec96)
2026-01-20 11:05:54 +09:00
Yu Watanabe
0d6e3b136d pretty-print: do not fail when cat_files() tries to show a masked file
Before 661b5bfd216e383ac7836261eea9671875e6709b, cat_files() does not
check if a file is regular. If the file is a symlink to /dev/null, then
cat_files() simply shows an empty contents for the file.
With the offending commit, as the CHASE_MUST_BE_REGULAR flag is set,
hence when we found a masked file, the function fails.

Fixes #40313.
Fixes regression caused by 661b5bfd216e383ac7836261eea9671875e6709b.

Co-authored-by: gvenugo3 <gvenugo3@asu.edu>
(cherry picked from commit f2125229234e798272f0c05ec99ec15e48d10fc0)
2026-01-20 11:05:54 +09:00
Yu Watanabe
899f97b9fb conf-files: make conf_file_new() take ConfFilesFlags rather than ChaseFlags
No functional change, preparation for later commit.

(cherry picked from commit 5d0dc9915ca6b1f0b5ed5f37b2d080ae1981a489)
2026-01-20 11:05:54 +09:00
Yu Watanabe
cc5ea34ae1 conf-files: use empty_to_root()
No functional change. For consistency with conf_file_chase_and_verify().

(cherry picked from commit 0c109deda17aeca2468b2a268a46e22ac78e55e7)
2026-01-20 11:05:54 +09:00
Yu Watanabe
397befbb10 conf-files: split out several helper functions from files_add()
No functional change, preparation for later commit.

(cherry picked from commit 4a8e0ea4ec80fc5b9ce93ee8d114b205ceda4759)
2026-01-20 11:05:54 +09:00
7 changed files with 213 additions and 122 deletions

View File

@ -85,6 +85,7 @@ union sockaddr_union;
typedef enum CGroupFlags CGroupFlags;
typedef enum CGroupMask CGroupMask;
typedef enum ChaseFlags ChaseFlags;
typedef enum ConfFilesFlags ConfFilesFlags;
typedef enum ExtractFlags ExtractFlags;
typedef enum ForkFlags ForkFlags;
typedef enum Glyph Glyph;

View File

@ -112,7 +112,147 @@ static int conf_file_prefix_root(ConfFile *c, const char *root) {
return 0;
}
int conf_file_new_at(const char *path, int rfd, ChaseFlags chase_flags, ConfFile **ret) {
static bool conf_files_need_stat(ConfFilesFlags flags) {
return (flags & (CONF_FILES_FILTER_MASKED | CONF_FILES_REGULAR | CONF_FILES_DIRECTORY | CONF_FILES_EXECUTABLE)) != 0;
}
static ChaseFlags conf_files_chase_flags(ConfFilesFlags flags) {
ChaseFlags chase_flags = CHASE_AT_RESOLVE_IN_ROOT;
if (!conf_files_need_stat(flags) || FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK))
/* Even if no verification is requested, let's unconditionally call chaseat(),
* to drop unsafe symlinks. */
chase_flags |= CHASE_NONEXISTENT;
return chase_flags;
}
static int conf_file_chase_and_verify(
int rfd,
const char *root, /* for logging, can be NULL */
const char *original_path, /* for logging */
const char *path,
const char *name,
Set **masked, /* optional */
ConfFilesFlags flags,
char **ret_path,
int *ret_fd,
struct stat *ret_stat) {
_cleanup_free_ char *resolved_path = NULL;
_cleanup_close_ int fd = -EBADF;
struct stat st = {};
int r;
assert(rfd >= 0 || rfd == AT_FDCWD);
assert(original_path);
assert(path);
assert(name);
root = empty_to_root(root);
r = chaseat(rfd, path, conf_files_chase_flags(flags), &resolved_path, &fd);
if (r < 0)
return log_debug_errno(r, "Failed to chase '%s%s': %m",
root, skip_leading_slash(original_path));
if (r == 0) {
if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK)) {
/* If the path points to /dev/null in a image or so, then the device node may not exist. */
if (path_equal(skip_leading_slash(resolved_path), "dev/null")) {
if (masked) {
/* Mark this one as masked */
r = set_put_strdup(masked, name);
if (r < 0)
return log_oom_debug();
}
return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
"File '%s%s' is a mask (symlink to /dev/null).",
root, skip_leading_slash(original_path));
}
}
if (conf_files_need_stat(flags))
/* If we need to have stat, skip the entry. */
return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to chase '%s%s': %m",
root, skip_leading_slash(original_path));
}
/* Even if we do not need stat, let's take stat now. The caller may use the info later. */
if (fd >= 0 && fstat(fd, &st) < 0)
return log_debug_errno(errno, "Failed to stat '%s%s': %m",
root, skip_leading_slash(original_path));
/* Is this a masking entry? */
if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK) && stat_may_be_dev_null(&st)) {
if (masked) {
/* Mark this one as masked */
r = set_put_strdup(masked, name);
if (r < 0)
return log_oom_debug();
}
return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
"File '%s%s' is a mask (symlink to /dev/null).",
root, skip_leading_slash(original_path));
}
if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_EMPTY) && stat_is_empty(&st)) {
if (masked) {
/* Mark this one as masked */
r = set_put_strdup(masked, name);
if (r < 0)
return log_oom_debug();
}
return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL),
"File '%s%s' is a mask (an empty file).",
root, skip_leading_slash(original_path));
}
if (FLAGS_SET(flags, CONF_FILES_REGULAR|CONF_FILES_DIRECTORY)) {
if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
return log_debug_errno(SYNTHETIC_ERRNO(EBADFD),
"File '%s%s' is neither a regular file or directory.",
root, skip_leading_slash(original_path));
} else {
/* Is this node a regular file? */
if (FLAGS_SET(flags, CONF_FILES_REGULAR)) {
r = stat_verify_regular(&st);
if (r < 0)
return log_debug_errno(r, "File '%s%s' is not a regular file: %m",
root, skip_leading_slash(original_path));
}
/* Is this node a directory? */
if (FLAGS_SET(flags, CONF_FILES_DIRECTORY)) {
r = stat_verify_directory(&st);
if (r < 0)
return log_debug_errno(r, "File '%s%s' is not a directory: %m",
root, skip_leading_slash(original_path));
}
}
/* Does this node have the executable bit set?
* As requested: check if the file is marked executable. Note that we don't check access(X_OK) here,
* as we care about whether the file is marked executable at all, and not whether it is executable
* for us, because if so, such errors are stuff we should log about. */
if (FLAGS_SET(flags, CONF_FILES_EXECUTABLE) && (st.st_mode & 0111) == 0)
return log_debug_errno(SYNTHETIC_ERRNO(ENOEXEC),
"File '%s%s' is not marked executable.",
root, skip_leading_slash(original_path));
if (ret_path)
*ret_path = TAKE_PTR(resolved_path);
if (ret_fd)
*ret_fd = TAKE_FD(fd);
if (ret_stat)
*ret_stat = st;
return 0;
}
int conf_file_new_at(const char *path, int rfd, ConfFilesFlags flags, ConfFile **ret) {
int r;
assert(path);
@ -145,9 +285,7 @@ int conf_file_new_at(const char *path, int rfd, ChaseFlags chase_flags, ConfFile
return log_debug_errno(r, "Failed to extract directory from '%s': %m", path);
if (r >= 0) {
r = chaseat(rfd, dirpath,
CHASE_AT_RESOLVE_IN_ROOT |
CHASE_MUST_BE_DIRECTORY |
(FLAGS_SET(chase_flags, CHASE_NONEXISTENT) ? CHASE_NONEXISTENT : 0),
CHASE_MUST_BE_DIRECTORY | conf_files_chase_flags(flags),
&resolved_dirpath, /* ret_fd= */ NULL);
if (r < 0)
return log_debug_errno(r, "Failed to chase '%s%s': %m", empty_to_root(root), skip_leading_slash(dirpath));
@ -157,22 +295,28 @@ int conf_file_new_at(const char *path, int rfd, ChaseFlags chase_flags, ConfFile
if (!c->result)
return log_oom_debug();
r = chaseat(rfd, c->result, CHASE_AT_RESOLVE_IN_ROOT | chase_flags, &c->resolved_path, &c->fd);
r = conf_file_chase_and_verify(
rfd,
root,
c->original_path,
c->result,
c->name,
/* masked= */ NULL,
flags,
&c->resolved_path,
&c->fd,
&c->st);
if (r < 0)
return log_debug_errno(r, "Failed to chase '%s%s': %m", empty_to_root(root), skip_leading_slash(c->original_path));
if (c->fd >= 0 && fstat(c->fd, &c->st) < 0)
return log_debug_errno(r, "Failed to stat '%s%s': %m", empty_to_root(root), skip_leading_slash(c->resolved_path));
return r;
*ret = TAKE_PTR(c);
return 0;
}
int conf_file_new(const char *path, const char *root, ChaseFlags chase_flags, ConfFile **ret) {
int conf_file_new(const char *path, const char *root, ConfFilesFlags flags, ConfFile **ret) {
int r;
assert(path);
assert((chase_flags & (CHASE_PREFIX_ROOT | CHASE_STEP)) == 0);
assert(ret);
_cleanup_free_ char *root_abs = NULL;
@ -191,7 +335,7 @@ int conf_file_new(const char *path, const char *root, ChaseFlags chase_flags, Co
}
_cleanup_(conf_file_freep) ConfFile *c = NULL;
r = conf_file_new_at(path, rfd, chase_flags, &c);
r = conf_file_new_at(path, rfd, flags, &c);
if (r < 0)
return r;
@ -228,9 +372,9 @@ static int files_add(
assert(files);
assert(masked);
root = strempty(root);
root = empty_to_root(root);
FOREACH_DIRENT(de, dir, return log_debug_errno(errno, "Failed to read directory '%s/%s': %m",
FOREACH_DIRENT(de, dir, return log_debug_errno(errno, "Failed to read directory '%s%s': %m",
root, skip_leading_slash(original_dirpath))) {
_cleanup_free_ char *original_path = path_join(original_dirpath, de->d_name);
@ -239,19 +383,19 @@ static int files_add(
/* Does this match the suffix? */
if (suffix && !endswith(de->d_name, suffix)) {
log_debug("Skipping file '%s/%s', suffix is not '%s'.", root, skip_leading_slash(original_path), suffix);
log_debug("Skipping file '%s%s', suffix is not '%s'.", root, skip_leading_slash(original_path), suffix);
continue;
}
/* Has this file already been found in an earlier directory? */
if (hashmap_contains(*files, de->d_name)) {
log_debug("Skipping overridden file '%s/%s'.", root, skip_leading_slash(original_path));
log_debug("Skipping overridden file '%s%s'.", root, skip_leading_slash(original_path));
continue;
}
/* Has this been masked in an earlier directory? */
if ((flags & CONF_FILES_FILTER_MASKED) != 0 && set_contains(*masked, de->d_name)) {
log_debug("File '%s/%s' is masked by previous entry.", root, skip_leading_slash(original_path));
log_debug("File '%s%s' is masked by previous entry.", root, skip_leading_slash(original_path));
continue;
}
@ -261,100 +405,22 @@ static int files_add(
_cleanup_free_ char *resolved_path = NULL;
_cleanup_close_ int fd = -EBADF;
bool need_stat = (flags & (CONF_FILES_FILTER_MASKED | CONF_FILES_REGULAR | CONF_FILES_DIRECTORY | CONF_FILES_EXECUTABLE)) != 0;
ChaseFlags chase_flags = CHASE_AT_RESOLVE_IN_ROOT;
if (!need_stat || FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK))
/* Even if no verification is requested, let's unconditionally call chaseat(),
* to drop unsafe symlinks. */
chase_flags |= CHASE_NONEXISTENT;
r = chaseat(rfd, p, chase_flags, &resolved_path, &fd);
if (r < 0) {
log_debug_errno(r, "Failed to chase '%s/%s', ignoring: %m",
root, skip_leading_slash(original_path));
continue;
}
if (r == 0) {
if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK)) {
/* If the path points to /dev/null in a image or so, then the device node may not exist. */
if (path_equal(skip_leading_slash(resolved_path), "dev/null")) {
/* Mark this one as masked */
r = set_put_strdup(masked, de->d_name);
struct stat st;
r = conf_file_chase_and_verify(
rfd,
root,
original_path,
p,
de->d_name,
masked,
flags,
&resolved_path,
&fd,
&st);
if (r == -ENOMEM)
return r;
if (r < 0)
return log_oom_debug();
log_debug("File '%s/%s' is a mask (symlink to /dev/null).",
root, skip_leading_slash(original_path));
continue;
}
}
if (need_stat) {
/* If we need to have stat, skip the entry. */
log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to chase '%s/%s', ignoring.",
root, skip_leading_slash(original_path));
continue;
}
}
/* Even if we do not need stat, let's take stat now. The caller may use the info later. */
struct stat st = {};
if (fd >= 0 && fstat(fd, &st) < 0) {
log_debug_errno(errno, "Failed to stat '%s/%s', ignoring: %m",
root, skip_leading_slash(original_path));
continue;
}
/* Is this a masking entry? */
if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_SYMLINK) && stat_may_be_dev_null(&st)) {
/* Mark this one as masked */
r = set_put_strdup(masked, de->d_name);
if (r < 0)
return log_oom_debug();
log_debug("File '%s/%s' is a mask (symlink to /dev/null).", root, skip_leading_slash(original_path));
continue;
}
if (FLAGS_SET(flags, CONF_FILES_FILTER_MASKED_BY_EMPTY) && stat_is_empty(&st)) {
/* Mark this one as masked */
r = set_put_strdup(masked, de->d_name);
if (r < 0)
return log_oom_debug();
log_debug("File '%s/%s' is a mask (an empty file).", root, skip_leading_slash(original_path));
continue;
}
if (FLAGS_SET(flags, CONF_FILES_REGULAR|CONF_FILES_DIRECTORY)) {
if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) {
log_debug("Ignoring '%s/%s', as it is neither a regular file or directory.", root, skip_leading_slash(original_path));
continue;
}
} else {
/* Is this node a regular file? */
if (FLAGS_SET(flags, CONF_FILES_REGULAR) && !S_ISREG(st.st_mode)) {
log_debug("Ignoring '%s/%s', as it is not a regular file.", root, skip_leading_slash(original_path));
continue;
}
/* Is this node a directory? */
if (FLAGS_SET(flags, CONF_FILES_DIRECTORY) && !S_ISDIR(st.st_mode)) {
log_debug("Ignoring '%s/%s', as it is not a directory.", root, skip_leading_slash(original_path));
continue;
}
}
/* Does this node have the executable bit set?
* As requested: check if the file is marked executable. Note that we don't check access(X_OK)
* here, as we care about whether the file is marked executable at all, and not whether it is
* executable for us, because if so, such errors are stuff we should log about. */
if (FLAGS_SET(flags, CONF_FILES_EXECUTABLE) && (st.st_mode & 0111) == 0) {
log_debug("Ignoring '%s/%s', as it is not marked executable.", root, skip_leading_slash(original_path));
continue;
}
_cleanup_(conf_file_freep) ConfFile *c = new(ConfFile, 1);
if (!c)
@ -533,8 +599,10 @@ static int conf_files_list_impl(
assert(rfd >= 0 || rfd == AT_FDCWD);
assert(ret);
root = empty_to_root(root);
if (replacement) {
r = conf_file_new_at(replacement, rfd, CHASE_NONEXISTENT, &c);
r = conf_file_new_at(replacement, rfd, /* flags= */ 0, &c);
if (r < 0)
return r;
}
@ -546,7 +614,8 @@ static int conf_files_list_impl(
r = chase_and_opendirat(rfd, *p, CHASE_AT_RESOLVE_IN_ROOT, &path, &dir);
if (r < 0) {
if (r != -ENOENT)
log_debug_errno(r, "Failed to chase and open directory '%s/%s', ignoring: %m", strempty(root), skip_leading_slash(*p));
log_debug_errno(r, "Failed to chase and open directory '%s%s', ignoring: %m",
root, skip_leading_slash(*p));
continue;
}

View File

@ -29,8 +29,8 @@ ConfFile* conf_file_free(ConfFile *c);
DEFINE_TRIVIAL_CLEANUP_FUNC(ConfFile*, conf_file_free);
void conf_file_free_many(ConfFile **array, size_t n);
int conf_file_new_at(const char *path, int rfd, ChaseFlags chase_flags, ConfFile **ret);
int conf_file_new(const char *path, const char *root, ChaseFlags chase_flags, ConfFile **ret);
int conf_file_new_at(const char *path, int rfd, ConfFilesFlags flags, ConfFile **ret);
int conf_file_new(const char *path, const char *root, ConfFilesFlags flags, ConfFile **ret);
int conf_files_list(char ***ret, const char *suffix, const char *root, ConfFilesFlags flags, const char *dir);
int conf_files_list_at(char ***ret, const char *suffix, int rfd, ConfFilesFlags flags, const char *dir);

View File

@ -6,7 +6,6 @@
#include <unistd.h>
#include "alloc-util.h"
#include "chase.h"
#include "color-util.h"
#include "conf-files.h"
#include "constants.h"
@ -325,7 +324,20 @@ static int cat_file_by_path(const char *p, bool *newline, CatFlags flags) {
assert(p);
r = conf_file_new(p, /* root= */ NULL, CHASE_MUST_BE_REGULAR, &c);
r = conf_file_new(p, /* root= */ NULL, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED, &c);
if (r == -ERFKILL) { /* masked */
if (newline) {
if (*newline)
putc('\n', stdout);
*newline = true;
}
printf("%s# %s is a mask.%s\n",
ansi_highlight_magenta(),
p,
ansi_normal());
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to chase '%s': %m", p);
@ -458,7 +470,8 @@ int conf_files_cat(const char *root, const char *name, CatFlags flags) {
if (!p)
return log_oom();
if (conf_file_new(p, root, CHASE_MUST_BE_REGULAR, &c) >= 0)
r = conf_file_new(p, root, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED, &c);
if (r >= 0 || r == -ERFKILL) /* Found a regular file or masked file */
break;
}

View File

@ -4,8 +4,11 @@
#include <unistd.h>
#include "alloc-util.h"
#include "path-util.h"
#include "pretty-print.h"
#include "rm-rf.h"
#include "tests.h"
#include "tmpfile-util.h"
#define CYLON_WIDTH 6
@ -53,6 +56,13 @@ TEST(cat_files) {
if (access("/etc/fstab", R_OK) >= 0)
assert_se(cat_files("/etc/fstab", STRV_MAKE("/etc/fstab", "/etc/fstab"), 0) == 0);
/* Test masked file (symlink to /dev/null) - should succeed with exit code 0 */
_cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
ASSERT_OK(mkdtemp_malloc("/tmp/test-cat-files-XXXXXX", &tmp));
_cleanup_free_ char *masked_file = ASSERT_NOT_NULL(path_join(tmp, "masked.conf"));
ASSERT_OK_ERRNO(symlink("/dev/null", masked_file));
ASSERT_OK(cat_files(masked_file, /* dropins= */ NULL, /* flags= */ 0));
}
TEST(red_green_cross_check_mark) {

View File

@ -2,7 +2,6 @@
#include <stdio.h>
#include "chase.h"
#include "conf-files.h"
#include "fd-util.h"
#include "fuzz.h"
@ -29,7 +28,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
assert_se(rules = udev_rules_new(RESOLVE_NAME_EARLY));
_cleanup_(conf_file_freep) ConfFile *c = NULL;
ASSERT_OK(conf_file_new(filename, /* root= */ NULL, CHASE_MUST_BE_REGULAR, &c));
ASSERT_OK(conf_file_new(filename, /* root= */ NULL, CONF_FILES_REGULAR, &c));
r = udev_rules_parse_file(rules, c, /* extra_checks= */ false, /* ret= */ NULL);
log_info_errno(r, "Parsing %s: %m", filename);

View File

@ -7,7 +7,6 @@
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "chase.h"
#include "conf-files.h"
#include "constants.h"
#include "device-private.h"
@ -249,7 +248,7 @@ static int search_rules_file_in_conf_dirs(const char *s, const char *root, ConfF
return log_oom();
_cleanup_(conf_file_freep) ConfFile *c = NULL;
r = conf_file_new(path, root, CHASE_MUST_BE_REGULAR, &c);
r = conf_file_new(path, root, CONF_FILES_REGULAR, &c);
if (r == -ENOENT)
continue;
if (r < 0)
@ -279,7 +278,7 @@ static int search_rules_file(const char *s, const char *root, ConfFile ***files,
/* If not found, or if it is a path, then chase it. */
_cleanup_(conf_file_freep) ConfFile *c = NULL;
r = conf_file_new(s, root, CHASE_MUST_BE_REGULAR, &c);
r = conf_file_new(s, root, CONF_FILES_REGULAR, &c);
if (r >= 0) {
if (!GREEDY_REALLOC_APPEND(*files, *n_files, &c, 1))
return log_oom();