You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
echo test1 >test1
echo test2 >test2
ln -s test2 test3
mount --bind test1 test2
mount --bind test1 test3
file test*
test1: ASCII text
test2: ASCII text
test3: symbolic link to test2
This causes issues when setting up chroots using arch-chroot from arch-install-scripts. It bind mounts the host's /etc/resolv.conf to the chroot directory's /etc/resolv.conf. If the file in the chroot directory is a symlink, then it will not be bind mounted over which will cause issues if the symlink target does not exist after chrooting.
The latest mount(8) from v2.39.1 supports a new mount API and has no problem using symlinks as a mount target. The syscall move_mount() that is used to create the target node does not follow symlinks by default (you need MOVE_MOUNT_T_SYMLINKS to follow).
The problem is that mount(8)/libmount itself canonicalizes paths by default. It's because it needs absolute paths to search in fstab and mountinfo files. It has used this behavior for the last 20+ years, and I have doubts we can change this default. Fortunately, there is -c, --no-canonicalize to avoid this feature.
Note that the kernel itself also canonicalizes paths used in /proc/self/mountinfo.
# file /mnt/test/ln
/mnt/test/ln: symbolic link to /mnt/test/A
# mount --bind -c /mnt/test/fileA /mnt/test/ln
# file /mnt/test/ln
/mnt/test/ln: ASCII text
# findmnt /mnt/test/ln
TARGET SOURCE FSTYPE OPTIONS
/mnt/test/ln /dev/sda4[/mnt/test/fileA] ext4 rw,relatime
The problem is that -c is a pretty heavy hammer, as it completely disables canonicalization in the library. I'd try to implement for "bind" something automatic and less invasive.
I can't imagine that's the only regression from this. I would argue for a revert, and the introduction of a new --canonicalize-target argument for example.
This feature introduces a regression. We need to enable it on demand rather than by default.
For v2.41, we will need to implement something smarter, probably a mount option, something like x-mount.symlink={target,source,...} to make it possible to use symlinks also in fstab.
Re: Unexpected Behavior of mount --bind with Symlinks This is actually dangerous behavior.
I would like to clarify the current discussion and confirm whether this is still considered a bug. Specifically, when attempting to bind over a static link file, the mount --bind command follows the symlink/static link to the source file. This default behavior can lead to unintended and potentially harmful consequences, if the user/admin does not consider that bind will follow symlinks or static links. If the file they are binding to was not expected to be a static link, one can inadvertently bind over a binary that handles 20-50+ other binaries. This is completely unexpected behavior and immediately crashed a server I was working on, as I did not understand this default behavior.
As of right now, I don't see any option to not follow symlinks / static links using mount --bind and I still see the default behavior of following links.
I just tried mount --bind --no-canonicalize and it still followed the staticlink/symlink.
@karelzak, can you clarify what you mean by new API? If one is using mount, how do they use this “new API?” Is the default behavior changed in the newest version of mount and all I would need to do is compile it, or is there something else one needs to do? It would be great to have this spelled out.
so while mount from util-linux 2.41 does allow bind mounting over symlinks with --no-canonicalize it still follows symlinks in the target (the file to be bind mounted) I think?
The default behavior should be backwardly compatible from the era when there was no extra support for the symlinks. I do not see another way to avoid regressions (and note that source symlinks are pretty standard for block devices). The default was always to canonicalize everything (or nothing, if the --no-canonicalize command line option was specified), and canonicalization was only about the paths itself.
Since v2.41 (with some fixes in v2.41.1), there is a new mount option X-mount.nocanonicalize[=source|target] to control what should be canonicalized. It also controls how the kernel opens the mount source. If source canonicalization is disabled, then it uses AT_SYMLINK_NOFOLLOW (it opens the symlink itself). This differs from --no-canonicalize, which only considers the paths (and was designed initially for /sbin/mount.type helpers like fuse, etc.).
For example, you can "redirect" a symlink (mount a symlink over a symlink):
# file /mnt/test/symlinkA /mnt/test/symlinkB
/mnt/test/symlinkA: symbolic link to /mnt/test/fileA
/mnt/test/symlinkB: symbolic link to /mnt/test/fileB
# mount --bind -o X-mount.nocanonicalize /mnt/test/symlinkA /mnt/test/symlinkB# ls -la /mnt/test/symlinkB
lrwxrwxrwx 1 root root 15 Sep 26 13:41 /mnt/test/symlinkB -> /mnt/test/fileA
The new mount API means kernel replacement for the classic mount(2) syscall; it's supported since libmount v2.39, and it's used by default. The usable symlinks support has been there since v2.41.1.
Activity
klausenbusk commentedon Jul 12, 2023
The ideal solution would be supporting
AT_SYMLINK_NOFOLLOWas mentioned in https://serverfault.com/a/1126837, this waymountwould not follow the symlink.jwilk commentedon Jul 31, 2023
You could open the symlink with
O_PATH | O_NOFOLLOW, then use/proc/self/fd/Nas the mount target.See my implementation: https://github.com/jwilk/symount
This is the approach systemd uses (systemd/systemd@28126409b20bca9a), so I guess it's battle-tested.
karelzak commentedon Aug 2, 2023
The latest mount(8) from v2.39.1 supports a new mount API and has no problem using symlinks as a mount target. The syscall move_mount() that is used to create the target node does not follow symlinks by default (you need MOVE_MOUNT_T_SYMLINKS to follow).
The problem is that mount(8)/libmount itself canonicalizes paths by default. It's because it needs absolute paths to search in fstab and mountinfo files. It has used this behavior for the last 20+ years, and I have doubts we can change this default. Fortunately, there is -c, --no-canonicalize to avoid this feature.
Note that the kernel itself also canonicalizes paths used in /proc/self/mountinfo.
there is also strace:
It's pretty simple with the new API.
It seems we need more information about this on the mount(8) man page ;-)
karelzak commentedon Aug 2, 2023
Maybe for --bind, the --no-canonicalize should be enabled by default if the target is a symlink.
nl6720 commentedon Aug 2, 2023
@karelzak thanks!
-c/--no-canonicalizeis just what I needed. 😊karelzak commentedon Aug 2, 2023
The problem is that -c is a pretty heavy hammer, as it completely disables canonicalization in the library. I'd try to implement for "bind" something automatic and less invasive.
libmount: don't canonicalize symlinks for bind operation
cgwalters commentedon Feb 7, 2024
Fallout over here https://bugzilla.redhat.com/show_bug.cgi?id=2262892
I can't imagine that's the only regression from this. I would argue for a revert, and the introduction of a new
--canonicalize-targetargument for example.Revert "libmount: don't canonicalize symlinks for bind operation"
Revert "libmount: don't canonicalize symlinks for bind operation"
karelzak commentedon Feb 13, 2024
This feature introduces a regression. We need to enable it on demand rather than by default.
For v2.41, we will need to implement something smarter, probably a mount option, something like x-mount.symlink={target,source,...} to make it possible to use symlinks also in fstab.
Revert "libmount: don't canonicalize symlinks for bind operation"
libmount: add X-mount.nocanonicalize[=source|target]
libmount: add X-mount.nocanonicalize[=source|target]
karelzak commentedon Sep 26, 2024
It seems in v2.41 mount(8) will be able to bind-mount symlink over a file, directory, or another symlink, and vice-versa.
MNLierman commentedon Jan 6, 2025
Re: Unexpected Behavior of
mount --bindwith SymlinksThis is actually dangerous behavior.
I would like to clarify the current discussion and confirm whether this is still considered a bug. Specifically, when attempting to bind over a static link file, the
mount --bindcommand follows the symlink/static link to the source file. This default behavior can lead to unintended and potentially harmful consequences, if the user/admin does not consider that bind will follow symlinks or static links. If the file they are binding to was not expected to be a static link, one can inadvertently bind over a binary that handles 20-50+ other binaries. This is completely unexpected behavior and immediately crashed a server I was working on, as I did not understand this default behavior.As of right now, I don't see any option to not follow symlinks / static links using
mount --bindand I still see the default behavior of following links.I just tried
mount --bind --no-canonicalizeand it still followed the staticlink/symlink.@karelzak, can you clarify what you mean by new API? If one is using
mount, how do they use this “new API?” Is the default behavior changed in the newest version ofmountand all I would need to do is compile it, or is there something else one needs to do? It would be great to have this spelled out.xaverdh commentedon Aug 21, 2025
so while mount from
util-linux 2.41does allow bind mounting over symlinks with--no-canonicalizeit still follows symlinks in the target (the file to be bind mounted) I think?karelzak commentedon Sep 15, 2025
The default behavior should be backwardly compatible from the era when there was no extra support for the symlinks. I do not see another way to avoid regressions (and note that source symlinks are pretty standard for block devices). The default was always to canonicalize everything (or nothing, if the --no-canonicalize command line option was specified), and canonicalization was only about the paths itself.
Since v2.41 (with some fixes in v2.41.1), there is a new mount option
X-mount.nocanonicalize[=source|target]to control what should be canonicalized. It also controls how the kernel opens the mount source. If source canonicalization is disabled, then it uses AT_SYMLINK_NOFOLLOW (it opens the symlink itself). This differs from--no-canonicalize, which only considers the paths (and was designed initially for /sbin/mount.type helpers like fuse, etc.).For example, you can "redirect" a symlink (mount a symlink over a symlink):
The new mount API means kernel replacement for the classic mount(2) syscall; it's supported since libmount v2.39, and it's used by default. The usable symlinks support has been there since v2.41.1.