Skip to content

Don't fail on EROFS when a mount point already exists - #832

Open
sarankumar1325 wants to merge 1 commit into
containers:mainfrom
sarankumar1325:mkdirat-erofs-nfs
Open

sarankumar1325 wants to merge 1 commit into
containers:mainfrom
sarankumar1325:mkdirat-erofs-nfs

Conversation

@sarankumar1325

Copy link
Copy Markdown

Fixes #827.

On NFS, mkdir() of an existing directory that this client hasn't looked up yet can fail with EROFS instead of EEXIST on a read-only mount. ensure_dir() and ensure_file() in utils.c already stat first for this reason, but the *at() paths in bubblewrap.c tolerate only EEXIST:

  • ensure_newroot_parents_at(): this is where the reporter's Can't mkdir parents for /app/share/runtime/locale: Read-only file system comes from.
  • ensure_node_at(): the directory case (mkdirat) and the non-writable file case (openat (O_CREAT | O_EXCL)).

Changes:

  • New mkdirat_allow_existing(): fstatat (..., AT_SYMLINK_NOFOLLOW) first, then mkdirat(), still accepting EEXIST. It's used in both directory places.
  • ensure_node_at(), file case: when return_writable is false and the node already exists, skip the O_CREAT | O_EXCL open. That's what the EEXIST path did anyway. The writable path (O_TRUNC) is unchanged.

An existing node of any type is treated exactly as an EEXIST result was before, so behaviour on local filesystems doesn't change. It costs one extra fstatat() per component.

Testing: I don't have an NFS server here, so I used an LD_PRELOAD shim (not committed) that makes mkdirat() and openat (O_CREAT | O_EXCL) return EROFS when the name already exists, which is the NFS behaviour from the issue:

  • main: --ro-bind / / --bind /tmp /usr/share/doc, --ro-bind / / --tmpfs /tmp, --ro-bind / / --ro-bind /etc/hostname /etc/hosts and --ro-bind / / --dev /dev all fail with Read-only file system.
  • this branch: all four succeed.
  • 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 EROFS for 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.

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>
Comment thread bubblewrap.c
Comment on lines +988 to +991
static int
mkdirat_allow_existing (int dirfd,
const char *name,
mode_t mode)

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.

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.)

Comment thread bubblewrap.c
{
struct stat buf;

if (fstatat (dirfd, name, &buf, AT_SYMLINK_NOFOLLOW) == 0)

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.

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?

Comment thread bubblewrap.c
Comment on lines -1005 to +1030
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))

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.

This feels like it should be a separate commit, because it's dealing with creation of regular files rather than directories.

@smcv

smcv commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

I don't have an NFS server here

In that case I'm not sure I understand why you've chosen this particular issue to prioritize?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mount point creation fails on NFS with EROFS

2 participants