tree-wide: use path_implies_directory() instead of checking for a trailing slash

A path that ends in a slash must refer to a directory, but so must one
whose last component is "." or "..". We have path_implies_directory()
covering all these cases, hence use it where we so far just checked for
a trailing slash, to determine whether a path can only refer to a
directory.

This affects valid_shell() and valid_device_node_path() (no change in
behavior there, since the paths are refused anyway if not normalized),
and the check in the boot loader spec parser that refuses paths to
directories (where "/foo/." so far slipped through).
This commit is contained in:
Lennart Poettering authored and Lennart Poettering committed 2026-11-05 22:00:49 +01:00
1 parent 67ffd130ea
commit 7c6c0851f1
3 files changed
+4 -4

No files matched your search

+1 -1
View File
@@ -1308,7 +1308,7 @@ bool valid_device_node_path(const char *path) {
if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
return false;
if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
if (path_implies_directory(path)) /* can't be a device node if it must be a directory */
return false;
return path_is_normalized(path);
+1 -1
View File
@@ -915,7 +915,7 @@ bool valid_shell(const char *p) {
if (!valid_home(p))
return false;
return !endswith(p, "/"); /* one additional restriction: shells may not be dirs */
return !path_implies_directory(p); /* one additional restriction: shells may not be dirs */
}
int maybe_setgroups(size_t size, const gid_t *list) {
+2 -2
View File
@@ -172,8 +172,8 @@ static int mangle_path(
return -ENOMEM;
/* We only reference files, never directories */
if (endswith(c, "/")) {
log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' has trailing slash, ignoring: %s", field, c);
if (path_implies_directory(c)) {
log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' refers to a directory, ignoring: %s", field, c);
*ret = NULL;
return 0;
}