Skip to content

Commit 7de03e6

Browse files
committed
linux: safe_openat reopens root
If an empty path is used, reopens directly the rootfs so that it can grab a reference to the topmost mount, not the previously open file descriptor. Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 2e210bd commit 7de03e6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/libcrun/utils.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,25 @@ crun_ensure_directory_at (int dirfd, const char *path, int mode, bool nofollow,
298298
return 0;
299299
}
300300

301+
static int
302+
check_fd_is_path (const char *path, int fd, const char *fdname, libcrun_error_t *err)
303+
{
304+
proc_fd_path_t fdpath;
305+
size_t path_len = strlen (path);
306+
char link[PATH_MAX];
307+
int ret;
308+
309+
get_proc_self_fd_path (fdpath, fd);
310+
ret = TEMP_FAILURE_RETRY (readlink (fdpath, link, sizeof (link)));
311+
if (UNLIKELY (ret < 0))
312+
return crun_make_error (err, errno, "readlink `%s`", fdname);
313+
314+
if (((size_t) ret) != path_len || memcmp (link, path, path_len))
315+
return crun_make_error (err, 0, "target `%s` does not point to the directory `%s`", fdname, path);
316+
317+
return 0;
318+
}
319+
301320
static int
302321
check_fd_under_path (const char *rootfs, size_t rootfslen, int fd, const char *fdname, libcrun_error_t *err)
303322
{
@@ -377,6 +396,23 @@ safe_openat (int dirfd, const char *rootfs, const char *path, int flags, int mod
377396
static bool openat2_supported = true;
378397
int ret;
379398

399+
if (is_empty_string (path))
400+
{
401+
cleanup_close int fd = -1;
402+
403+
fd = open (rootfs, flags, mode);
404+
if (UNLIKELY (fd < 0))
405+
return crun_make_error (err, errno, "open `%s`", rootfs);
406+
407+
ret = check_fd_is_path (rootfs, fd, path, err);
408+
if (UNLIKELY (ret < 0))
409+
return ret;
410+
411+
ret = fd;
412+
fd = -1;
413+
return ret;
414+
}
415+
380416
if (openat2_supported)
381417
{
382418
repeat:

0 commit comments

Comments
 (0)