Skip to content

vfs: skip write tracking for special files on open - #13149

Merged
copybara-service[bot] merged 1 commit into
google:masterfrom
shayonj:fix-special-file-rofs
Aug 5, 2026
Merged

vfs: skip write tracking for special files on open#13149
copybara-service[bot] merged 1 commit into
google:masterfrom
shayonj:fix-special-file-rofs

Conversation

@shayonj

@shayonj shayonj commented May 12, 2026

Copy link
Copy Markdown
Contributor

Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an IsSpecialFile option on FileDescription, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148

@zkoopmans
zkoopmans requested a review from nixprime May 14, 2026 22:53
@shayonj
shayonj force-pushed the fix-special-file-rofs branch from 1b1760c to 7d4179f Compare July 16, 2026 11:22
@shayonj

shayonj commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

👋🏾 @nixprime - curious if you had any feedback 🙏🏾

@EtiennePerot
EtiennePerot requested review from ayushr2 and removed request for nixprime July 16, 2026 23:17

@ayushr2 ayushr2 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@ayushr2

ayushr2 commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Need a Googler to pull this in.

cobrien-tines

This comment was marked as resolved.

copybara-service Bot pushed a commit that referenced this pull request Jul 23, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 7d4179f
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 23, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 7d4179f
PiperOrigin-RevId: 952841897
@milantracy

milantracy commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

there are some test failures at https://buildkite.com/gvisor/pipeline/builds/44757

do you want to take a look?

Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds.

Mark the remaining FileDescriptions backed by special-file inodes. Also skip overlay copy-up for writable opens of special files; Linux overlayfs applies the same special_file() exception before deciding whether copy-up is required.

Add regression coverage for character devices and FIFOs through read-only bind mounts.
@shayonj
shayonj force-pushed the fix-special-file-rofs branch from 7d4179f to 87673b0 Compare July 28, 2026 10:48
@shayonj

shayonj commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

thank you! just pushed the fix

copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
copybara-service Bot pushed a commit that referenced this pull request Jul 28, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 952841897
@milantracy

Copy link
Copy Markdown
Collaborator

@shayonj

I made a tiny patch to your PR that fixes some test failure i see internally
i will merge the change at #13996

you can see the diff as follows,

  • pkg/sentry/fsimpl/overlay/filesystem.go
@@ -896,7 +896,13 @@ func (d *dentry) ensureOpenableLocked(ct
                return nil
        }
 
-       if !ats.MayWrite() || linux.FileMode(d.mode.Load()).IsSpecialFile() {
+       if !ats.MayWrite() {
+               return nil
+       }
+       if !d.upperVD.Ok() && !d.canBeCopiedUp() {
+               return linuxerr.EPERM
+       }
+       if linux.FileMode(d.mode.Load()).IsSpecialFile() {
                return nil
        }
  • test/syscalls/linux/mount.cc
 TEST(MountTest, ReadOnlyBindMountAllowsWriteOpenOfCharDevice) {
   SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN)));

-  auto const bindDir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
-  auto const mnt = ASSERT_NO_ERRNO_AND_VALUE(
-      Mount("/dev", bindDir.path(), "", MS_BIND, "", 0));
-  ASSERT_THAT(mount("/dev", bindDir.path().c_str(), nullptr,
-                    MS_BIND | MS_REMOUNT | MS_RDONLY, nullptr),
-              SyscallSucceeds());
-
-  std::string const nullPath = JoinPath(bindDir.path(), "null");
+  auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
+  std::string const nullPath = JoinPath(dir.path(), "null");
+  FileDescriptor create_fd =
+      ASSERT_NO_ERRNO_AND_VALUE(Open(nullPath, O_CREAT | O_RDWR, 0666));
+  create_fd.reset();
+
+  auto const mnt = ASSERT_NO_ERRNO_AND_VALUE(
+      Mount("/dev/null", nullPath, "", MS_BIND, "", 0));
+  ASSERT_THAT(mount("/dev/null", nullPath.c_str(), nullptr,
+                    MS_BIND | MS_REMOUNT | MS_RDONLY, nullptr),
+              SyscallSucceeds());

   FileDescriptor wfd = ASSERT_NO_ERRNO_AND_VALUE(Open(nullPath, O_WRONLY));
   char msg[] = "hello";
   EXPECT_THAT(write(wfd.get(), msg, sizeof(msg)),
               SyscallSucceedsWithValue(sizeof(msg)));

@shayonj

shayonj commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for catching this. I missed that my special file shortcut bypassed the existing check for lower layer files that aren't be copied up. For future - do you know why it didn't fail in the CI on this branch?

@milantracy

Copy link
Copy Markdown
Collaborator

the CI on buildkite doens't have any test coverage for the edge cases.

I created #13998, PRs are welcome if you are interesetd

copybara-service Bot pushed a commit that referenced this pull request Aug 5, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 959292441
copybara-service Bot pushed a commit that referenced this pull request Aug 5, 2026
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds. gVisor was running that check unconditionally and returning EROFS, which breaks buildah's mount-bind-then-RO-remount hardening pattern when used under runsc.

This adds an `IsSpecialFile` option on `FileDescription`, paired across the open and close paths so the writer counter stays balanced, and set it on every open path that backs a special-file inode. The socket and host-imported FDs live on internal disconnected mounts that are never remounted RO, so flagging them is a no-op today and exists for parity with the kernel's predicate.

Fixes: #13148
FUTURE_COPYBARA_INTEGRATE_REVIEW=#13149 from shayonj:fix-special-file-rofs 87673b0
PiperOrigin-RevId: 959292441
@copybara-service
copybara-service Bot merged commit 2f841d0 into google:master Aug 5, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Writing to /dev/null fails with EROFS on a read-only bind mount

5 participants