container: make ENOENT error clearer - #2126
Conversation
There was a problem hiding this comment.
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.
| if (errno == ENOENT) | ||
| return crun_make_error (err, errno, "the rootfs path `%s` does not exist", def->root->path); |
There was a problem hiding this comment.
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:
- It opens
/proc/self/cwdwithO_RDONLY(which follows the symlink and opens the directory). - It then calls
safe_readlinkatwith""and flags0, which always fails withENOENTon directories (especially on kernels < 5.14 whereAT_EMPTY_PATHis not supported or when flags is0). - Because the fallback fails, it immediately returns the error from
safe_readlinkatinstead of falling back to the next option or returning the originalENOENTerror.
Recommended Solution
To fix both issues properly, we should:
- Fix the fallback to use
safe_readlinkat (AT_FDCWD, "/proc/self/cwd", ...)which is clean, standard, and works on all kernels. - Use a temporary error variable so we don't pollute
errif the fallback fails. - Only return the clear
ENOENTerror 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;
}fea9fbb to
aefbd68
Compare
aefbd68 to
691ba78
Compare
|
@kolyshkin PTAL |
|
@jnovy PTAL |
| 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); |
There was a problem hiding this comment.
errno can be clobbered by free() - use ENOENT directly instead of errno?
|
|
||
| len = safe_readlinkat (ret, "", &cwd, 0, err); | ||
| close (ret); | ||
| len = safe_readlinkat (fd, "", &cwd, 0, err); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
thanks, I've messed this up in the latest refactoring, but didn't hit a release yet.
I've reworked the implementation
691ba78 to
60d355e
Compare
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>
60d355e to
d5b5950
Compare
|
Ephemeral COPR build failed. @containers/packit-build please check. |
|
LGTM |
a not existing rootfs path was giving a weird error message:
2026-07-10T11:02:00.086289Z: readlink ``: No such file or directory