diff --git a/pkg/sentry/fsimpl/kernfs/filesystem.go b/pkg/sentry/fsimpl/kernfs/filesystem.go index 5eaa9a5c5..6eb7bbeba 100644 --- a/pkg/sentry/fsimpl/kernfs/filesystem.go +++ b/pkg/sentry/fsimpl/kernfs/filesystem.go @@ -362,8 +362,9 @@ func (fs *Filesystem) LinkAt(ctx context.Context, rp *vfs.ResolvingPath, vd vfs. return err } - parent.dirMu.Lock() - defer parent.dirMu.Unlock() + if rp.Mount() != vd.Mount() { + return linuxerr.EXDEV + } inode := vd.Dentry().Impl().(*Dentry).Inode() if inode.Mode().IsDir() { return linuxerr.EPERM @@ -371,6 +372,8 @@ func (fs *Filesystem) LinkAt(ctx context.Context, rp *vfs.ResolvingPath, vd vfs. if err := vfs.MayLink(rp.Credentials(), inode.Mode(), inode.UID(), inode.GID()); err != nil { return err } + parent.dirMu.Lock() + defer parent.dirMu.Unlock() pc := rp.Component() if err := checkCreateLocked(ctx, rp.Credentials(), pc, parent); err != nil { return err @@ -378,9 +381,6 @@ func (fs *Filesystem) LinkAt(ctx context.Context, rp *vfs.ResolvingPath, vd vfs. if rp.MustBeDir() { return linuxerr.ENOENT } - if rp.Mount() != vd.Mount() { - return linuxerr.EXDEV - } if err := rp.Mount().CheckBeginWrite(); err != nil { return err } diff --git a/test/syscalls/linux/link.cc b/test/syscalls/linux/link.cc index 8299b50d2..43e51667f 100644 --- a/test/syscalls/linux/link.cc +++ b/test/syscalls/linux/link.cc @@ -349,6 +349,12 @@ TEST(LinkTest, LinkatWithSymlinkFollow) { EXPECT_THAT(unlink(newname.c_str()), SyscallSucceeds()); } +TEST(LinkTest, KernfsAcrossFilesystem) { + auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); + EXPECT_THAT(link(file.path().c_str(), "/sys/newfile"), + SyscallFailsWithErrno(::testing::AnyOf(EROFS, EXDEV))); +} + } // namespace } // namespace testing