Skip to content

container: make ENOENT error clearer - #2126

Merged
giuseppe merged 1 commit into
containers:mainfrom
giuseppe:make-ENOENT-error-clearer
Jul 31, 2026
Merged

container: make ENOENT error clearer#2126
giuseppe merged 1 commit into
containers:mainfrom
giuseppe:make-ENOENT-error-clearer

Conversation

@giuseppe

Copy link
Copy Markdown
Member

a not existing rootfs path was giving a weird error message:

2026-07-10T11:02:00.086289Z: readlink ``: No such file or directory

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request modifies resolve_rootfs_path in src/libcrun/container.c to immediately return an error if realpath fails with ENOENT. However, the reviewer points out that this change breaks the fallback mechanism designed for cases where the absolute path is not visible in the current mount namespace but is accessible relatively. The reviewer suggests fixing the fallback mechanism itself and only returning the ENOENT error after the fallback has been attempted and failed.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/libcrun/container.c Outdated
Comment on lines +1120 to +1121
if (errno == ENOENT)
return crun_make_error (err, errno, "the rootfs path `%s` does not exist", def->root->path);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Issue

Immediately returning an error when errno == ENOENT completely bypasses and breaks the fallback mechanism designed for cases where the absolute path is not visible/accessible in the current mount namespace but is accessible relatively (e.g., when running in restricted namespaces or chroots).

Root Cause of the Weird Error

The reason the weird error message readlink : No such file or directory` happens is because the fallback mechanism itself has a bug:

  1. It opens /proc/self/cwd with O_RDONLY (which follows the symlink and opens the directory).
  2. It then calls safe_readlinkat with "" and flags 0, which always fails with ENOENT on directories (especially on kernels < 5.14 where AT_EMPTY_PATH is not supported or when flags is 0).
  3. Because the fallback fails, it immediately returns the error from safe_readlinkat instead of falling back to the next option or returning the original ENOENT error.

Recommended Solution

To fix both issues properly, we should:

  1. Fix the fallback to use safe_readlinkat (AT_FDCWD, "/proc/self/cwd", ...) which is clean, standard, and works on all kernels.
  2. Use a temporary error variable so we don't pollute err if the fallback fails.
  3. Only return the clear ENOENT error after the fallback has been attempted and failed to resolve the path.

Here is the fully refactored resolve_rootfs_path function:

static int
resolve_rootfs_path (libcrun_container_t *container, char **rootfs, libcrun_error_t *err)
{
  runtime_spec_schema_config_schema *def = container->container_def;
  if (def->root && def->root->path)
    {
      *rootfs = realpath (def->root->path, NULL);
      if (UNLIKELY (*rootfs == NULL))
        {
          int saved_errno = errno;

          /* If realpath failed for any reason, try the relative directory.  */
          if (def->root->path[0] == '/')
            {
              cleanup_free char *cwd = NULL;
              ssize_t len;
              libcrun_error_t tmp_err = NULL;

              len = safe_readlinkat (AT_FDCWD, "/proc/self/cwd", &cwd, 0, &tmp_err);
              if (len >= 0)
                {
                  /* If the rootfs is under the current working directory, just use its relative path.  */
                  if (has_prefix (def->root->path, cwd) && def->root->path[len] == '/')
                    {
                      const char *it = consume_slashes (def->root->path + len);
                      if (*it)
                        *rootfs = xstrdup (it);
                    }
                }
              else
                {
                  crun_error_release (&tmp_err);
                }
            }

          /* If nothing else worked, return a clear error if it was ENOENT.  */
          if (*rootfs == NULL)
            {
              if (saved_errno == ENOENT)
                return crun_make_error (err, saved_errno, "the rootfs path `%s` does not exist", def->root->path);

              *rootfs = xstrdup (def->root->path);
            }
        }
    }
  return 0;
}

@giuseppe
giuseppe force-pushed the make-ENOENT-error-clearer branch from fea9fbb to aefbd68 Compare July 10, 2026 11:31
Comment thread src/libcrun/container.c Outdated
@giuseppe
giuseppe force-pushed the make-ENOENT-error-clearer branch from aefbd68 to 691ba78 Compare July 10, 2026 11:42
@giuseppe

Copy link
Copy Markdown
Member Author

@kolyshkin PTAL

@giuseppe

Copy link
Copy Markdown
Member Author

@jnovy PTAL

Comment thread src/libcrun/container.c Outdated
if (crun_error_get_errno (err) == ENOENT)
{
crun_error_release (err);
return crun_make_error (err, errno, "the rootfs path `%s` does not exist", def->root->path);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

errno can be clobbered by free() - use ENOENT directly instead of errno?

Comment thread src/libcrun/container.c Outdated

len = safe_readlinkat (ret, "", &cwd, 0, err);
close (ret);
len = safe_readlinkat (fd, "", &cwd, 0, err);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe I'm missing somethign but the entire fallback mechanism (readlinkat(fd, "") with an O_RDONLY fd) is broken and has been dead code since it was introduced? The PR masks this by intercepting the ENOENT that readlinkat always produces, rather than fixing the root cause. The cleaner approach is to fix the readlinkat call to actually work (use O_PATH|O_NOFOLLOW or readlinkat(proc_fd, "self/cwd", ...)) and then add the improved ENOENT error message as a final fallback after the working retry logic fails.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

thanks, I've messed this up in the latest refactoring, but didn't hit a release yet.

I've reworked the implementation

readlinkat(fd, "") with an O_RDONLY fd always fails, making the
relative path fallback dead code since it was introduced.

Use safe_readlinkat(proc_fd, "self/cwd") to read the symlink by
name through the cached proc fd.

Also use a temporary error to avoid polluting err if the fallback
fails, verify the relative path exists with faccessat before using
it, and use ENOENT directly instead of errno which can be clobbered
by free().

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
@giuseppe
giuseppe force-pushed the make-ENOENT-error-clearer branch from 60d355e to d5b5950 Compare July 31, 2026 09:33
@packit-as-a-service

Copy link
Copy Markdown

Ephemeral COPR build failed. @containers/packit-build please check.

@jnovy

jnovy commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

LGTM

@giuseppe
giuseppe merged commit 5bf35d4 into containers:main Jul 31, 2026
46 checks passed
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.

3 participants