X Tutup
Skip to content

Commit 25cd496

Browse files
committed
mount: forbid mount on path with symlinks
It was forbidden to create mount units for a symlink. But the reason is that the mount unit needs to know the real path that will appear in /proc/self/mountinfo. The kernel dereferences *all* the symlinks in the path at mount time (I checked this with `mount -c` running under `strace`). This will have no effect on most systems. As recommended by docs, most systems use /etc/fstab, as opposed to native mount unit files. fstab-generator dereferences symlinks for backwards compatibility. A relatively minor issue regarding Time Of Check / Time Of Use also exists here. I can't see how to get rid of it entirely. If we pass an absolute path to mount, the racing process can replace it with a symlink. If we chdir() to the mount point and pass ".", the racing process can move the directory. The latter might potentially be nicer, except that it breaks WorkingDirectory=. I'm not saying the race is relevant to security - I just want to consider how bad the effect is. Currently, it can make the mount unit active (and hence the job return success), despite there never being a matching entry in /proc/self/mountinfo. This wart will be removed in the next commit; i.e. it will make the mount unit fail instead.
1 parent 22bc57c commit 25cd496

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

src/core/automount.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ static void automount_enter_waiting(Automount *a) {
578578

579579
set_clear(a->tokens);
580580

581-
r = unit_fail_if_symlink(UNIT(a), a->where);
581+
r = unit_fail_if_noncanonical(UNIT(a), a->where);
582582
if (r < 0)
583583
goto fail;
584584

src/core/mount.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ static void mount_enter_mounting(Mount *m) {
942942

943943
assert(m);
944944

945-
r = unit_fail_if_symlink(UNIT(m), m->where);
945+
r = unit_fail_if_noncanonical(UNIT(m), m->where);
946946
if (r < 0)
947947
goto fail;
948948

src/core/unit.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4705,25 +4705,29 @@ void unit_warn_if_dir_nonempty(Unit *u, const char* where) {
47054705
NULL);
47064706
}
47074707

4708-
int unit_fail_if_symlink(Unit *u, const char* where) {
4708+
int unit_fail_if_noncanonical(Unit *u, const char* where) {
4709+
_cleanup_free_ char *canonical_where;
47094710
int r;
47104711

47114712
assert(u);
47124713
assert(where);
47134714

4714-
r = is_symlink(where);
4715+
r = chase_symlinks(where, NULL, CHASE_NONEXISTENT, &canonical_where);
47154716
if (r < 0) {
4716-
log_unit_debug_errno(u, r, "Failed to check symlink %s, ignoring: %m", where);
4717+
log_unit_debug_errno(u, r, "Failed to check %s for symlinks, ignoring: %m", where);
47174718
return 0;
47184719
}
4719-
if (r == 0)
4720+
4721+
/* We will happily ignore a trailing slash (or any redundant slashes) */
4722+
if (path_equal(where, canonical_where))
47204723
return 0;
47214724

4725+
/* No need to mention "." or "..", they would already have been rejected by unit_name_from_path() */
47224726
log_struct(LOG_ERR,
47234727
"MESSAGE_ID=" SD_MESSAGE_OVERMOUNTING_STR,
47244728
LOG_UNIT_ID(u),
47254729
LOG_UNIT_INVOCATION_ID(u),
4726-
LOG_UNIT_MESSAGE(u, "Mount on symlink %s not allowed.", where),
4730+
LOG_UNIT_MESSAGE(u, "Mount path %s is not canonical (contains a symlink).", where),
47274731
"WHERE=%s", where,
47284732
NULL);
47294733

src/core/unit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ static inline bool unit_supported(Unit *u) {
760760
}
761761

762762
void unit_warn_if_dir_nonempty(Unit *u, const char* where);
763-
int unit_fail_if_symlink(Unit *u, const char* where);
763+
int unit_fail_if_noncanonical(Unit *u, const char* where);
764764

765765
int unit_start_limit_test(Unit *u);
766766

0 commit comments

Comments
 (0)
X Tutup