Skip to content

container: make ENOENT error clearer#2126

Open
giuseppe wants to merge 2 commits into
containers:mainfrom
giuseppe:make-ENOENT-error-clearer
Open

container: make ENOENT error clearer#2126
giuseppe wants to merge 2 commits 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 added 2 commits July 10, 2026 13:42
a not existing rootfs path was giving a weird error message:

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

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
@giuseppe giuseppe force-pushed the make-ENOENT-error-clearer branch from aefbd68 to 691ba78 Compare July 10, 2026 11:42
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.

2 participants