Skip to content

mount does not bind mount over a symlink #2370

Open

Description

@nl6720

mount will not bind mount files if the "target" is a symlink. See https://serverfault.com/q/322906.

For example:

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.

Activity

  1. klausenbusk commented on Jul 12, 2023

    @klausenbusk

    The ideal solution would be supporting AT_SYMLINK_NOFOLLOW as mentioned in https://serverfault.com/a/1126837, this way mount would not follow the symlink.

  2. jwilk commented on Jul 31, 2023

    @jwilk
    Contributor

    You could open the symlink with O_PATH | O_NOFOLLOW, then use /proc/self/fd/N as 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.

  3. karelzak commented on Aug 2, 2023

    @karelzak
    Collaborator

    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
    

    there is also strace:

    # strace -e open_tree,fsopen,fsconfig,fsmount,mount_setattr,move_mount,mount ./mount --bind -c   /mnt/test/fileA /mnt/test/ln
    ...
    open_tree(AT_FDCWD, "/mnt/test/fileA", OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC) = 3
    move_mount(3, "", AT_FDCWD, "/mnt/test/ln", MOVE_MOUNT_F_EMPTY_PATH) = 0
    

    It's pretty simple with the new API.

    It seems we need more information about this on the mount(8) man page ;-)

  4. karelzak commented on Aug 2, 2023

    @karelzak
    Collaborator

    Maybe for --bind, the --no-canonicalize should be enabled by default if the target is a symlink.

  5. nl6720 commented on Aug 2, 2023

    @nl6720
    ContributorAuthor

    @karelzak thanks! -c/--no-canonicalize is just what I needed. 😊

  6. karelzak commented on Aug 2, 2023

    @karelzak
    Collaborator

    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.

  7. cgwalters commented on Feb 7, 2024

    @cgwalters
    Contributor

    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-target argument for example.

  8. karelzak commented on Feb 13, 2024

    @karelzak
    Collaborator

    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.

  9. added
    TODO
    on Feb 13, 2024
  10. karelzak commented on Sep 26, 2024

    @karelzak
    Collaborator

    It seems in v2.41 mount(8) will be able to bind-mount symlink over a file, directory, or another symlink, and vice-versa.

  11. MNLierman commented on Jan 6, 2025

    @MNLierman

    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.

  12. xaverdh commented on Aug 21, 2025

    @xaverdh

    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?

  13. karelzak commented on Sep 15, 2025

    @karelzak
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    TODO

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions