Repository navigation
Don't fail on EROFS when a mount point already exists - #832
sarankumar1325 wants to merge 1 commit into
Conversation
On NFS, mkdir() of an existing directory that the client hasn't looked up yet can fail with EROFS instead of EEXIST on a read-only mount, so starting a sandbox whose parents or destinations are on such a mount failed with "Can't mkdir parents for ...: Read-only file system" until something walked the tree. ensure_dir() and ensure_file() already stat first for this reason. Do the same in ensure_newroot_parents_at() and ensure_node_at(): a new mkdirat_allow_existing() helper checks with fstatat() before mkdirat(), and the non-writable file case skips the O_CREAT|O_EXCL open when the node exists. Fixes: containers#827 Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
| static int | ||
| mkdirat_allow_existing (int dirfd, | ||
| const char *name, | ||
| mode_t mode) |
There was a problem hiding this comment.
This seems like something that should be in utils.c and not be static.
Could ensure_dir() be implemented as mkdirat_allow_existing (AT_FDCWD, path, mode)? (At the moment I think the answer is no, because its behaviour is different.)
| { | ||
| struct stat buf; | ||
|
|
||
| if (fstatat (dirfd, name, &buf, AT_SYMLINK_NOFOLLOW) == 0) |
There was a problem hiding this comment.
ensure_dir() allows path to be either a directory, a symlink to an existing directory, or nonexistent, but fails with ENOTDIR if path is a dangling symlink, a symlink to a non-directory object, or a non-symlink non-directory (for example a regular file).
This new function allows dirfd/name to be any file type, including a regular file or any symlink, and will return success anyway. Is that an intentional difference? If so, why?
| if (node_type == NODE_TYPE_FILE) | ||
| struct stat buf; | ||
|
|
||
| /* As for directories: creating a node that already exists on a | ||
| * read-only NFS mount can fail with EROFS instead of EEXIST. */ | ||
| if (node_type == NODE_TYPE_FILE && | ||
| (return_writable || fstatat (parent_fd, name, &buf, AT_SYMLINK_NOFOLLOW) != 0)) |
There was a problem hiding this comment.
This feels like it should be a separate commit, because it's dealing with creation of regular files rather than directories.
In that case I'm not sure I understand why you've chosen this particular issue to prioritize? |
Fixes #827.
On NFS,
mkdir()of an existing directory that this client hasn't looked up yet can fail withEROFSinstead ofEEXISTon a read-only mount.ensure_dir()andensure_file()in utils.c already stat first for this reason, but the*at()paths in bubblewrap.c tolerate onlyEEXIST:ensure_newroot_parents_at(): this is where the reporter'sCan't mkdir parents for /app/share/runtime/locale: Read-only file systemcomes from.ensure_node_at(): the directory case (mkdirat) and the non-writable file case (openat (O_CREAT | O_EXCL)).Changes:
mkdirat_allow_existing():fstatat (..., AT_SYMLINK_NOFOLLOW)first, thenmkdirat(), still acceptingEEXIST. It's used in both directory places.ensure_node_at(), file case: whenreturn_writableis false and the node already exists, skip theO_CREAT | O_EXCLopen. That's what theEEXISTpath did anyway. The writable path (O_TRUNC) is unchanged.An existing node of any type is treated exactly as an
EEXISTresult was before, so behaviour on local filesystems doesn't change. It costs one extrafstatat()per component.Testing: I don't have an NFS server here, so I used an
LD_PRELOADshim (not committed) that makesmkdirat()andopenat (O_CREAT | O_EXCL)returnEROFSwhen the name already exists, which is the NFS behaviour from the issue:--ro-bind / / --bind /tmp /usr/share/doc,--ro-bind / / --tmpfs /tmp,--ro-bind / / --ro-bind /etc/hostname /etc/hostsand--ro-bind / / --dev /devall fail withRead-only file system.meson test -C _build: test-utils, test-run.sh, test-specifying-pidns.sh, test-specifying-userns.sh and test-sandbox.py pass. test-seccomp.py is skipped here.I didn't add an automated test, because the existing suite can't make a local filesystem return
EROFSfor an existing name. If you'd like the shim as a test helper, I can add it.I used an AI assistant for this change and reviewed the code and tests myself.