Skip to content

Commit 2e210bd

Browse files
committed
linux: use rootfsfd directly from container data
This change ensures that the file descriptor for the rootfs is always sourced directly from the container's private data. This avoids potential stale file descriptor issues that could happen if a local variable were used and the descriptor in the private data was updated elsewhere. Should not introduce any behavior change. Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent e9d159f commit 2e210bd

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

src/libcrun/linux.c

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,6 @@ do_masked_or_readonly_path (libcrun_container_t *container, const char *rel_path
10301030
{
10311031
unsigned long mount_flags = 0;
10321032
const char *rootfs = get_private_data (container)->rootfs;
1033-
int rootfsfd = get_private_data (container)->rootfsfd;
10341033
cleanup_close int pathfd = -1;
10351034
struct statfs sfs;
10361035
int ret;
@@ -1039,7 +1038,7 @@ do_masked_or_readonly_path (libcrun_container_t *container, const char *rel_path
10391038
if (rel_path[0] == '/')
10401039
rel_path++;
10411040

1042-
pathfd = safe_openat (rootfsfd, rootfs, rel_path, O_PATH | O_CLOEXEC, 0, err);
1041+
pathfd = safe_openat (get_private_data (container)->rootfsfd, rootfs, rel_path, O_PATH | O_CLOEXEC, 0, err);
10431042
if (UNLIKELY (pathfd < 0))
10441043
{
10451044
if (errno != ENOENT && errno != EACCES)
@@ -1588,7 +1587,6 @@ libcrun_create_dev (libcrun_container_t *container, int devfd, int srcfd,
15881587
mode_t type = (device->type[0] == 'b') ? S_IFBLK : ((device->type[0] == 'p') ? S_IFIFO : S_IFCHR);
15891588
const char *fullname = device->path;
15901589
cleanup_close int fd = -1;
1591-
int rootfsfd = get_private_data (container)->rootfsfd;
15921590
const char *rootfs = get_private_data (container)->rootfs;
15931591
if (is_empty_string (fullname))
15941592
return crun_make_error (err, EINVAL, "device path is empty");
@@ -1619,7 +1617,7 @@ libcrun_create_dev (libcrun_container_t *container, int devfd, int srcfd,
16191617
{
16201618
const char *rel_path = consume_slashes (normalized_path);
16211619

1622-
fd = crun_safe_create_and_open_ref_at (false, rootfsfd, rootfs, rel_path, 0755, err);
1620+
fd = crun_safe_create_and_open_ref_at (false, get_private_data (container)->rootfsfd, rootfs, rel_path, 0755, err);
16231621
if (UNLIKELY (fd < 0))
16241622
return fd;
16251623
}
@@ -1684,18 +1682,18 @@ libcrun_create_dev (libcrun_container_t *container, int devfd, int srcfd,
16841682

16851683
if (dirname[0] == '\0')
16861684
{
1687-
dirfd = dup (rootfsfd);
1685+
dirfd = dup (get_private_data (container)->rootfsfd);
16881686
if (UNLIKELY (dirfd < 0))
16891687
return crun_make_error (err, errno, "dup fd for `%s`", rootfs);
16901688
}
16911689
else
16921690
{
1693-
dirfd = safe_openat (rootfsfd, rootfs, dirname, O_DIRECTORY | O_PATH | O_CLOEXEC, 0, err);
1691+
dirfd = safe_openat (get_private_data (container)->rootfsfd, rootfs, dirname, O_DIRECTORY | O_PATH | O_CLOEXEC, 0, err);
16941692
if (dirfd < 0 && ensure_parent_dir)
16951693
{
16961694
crun_error_release (err);
16971695

1698-
dirfd = crun_safe_create_and_open_ref_at (true, rootfsfd, rootfs, dirname, 0755, err);
1696+
dirfd = crun_safe_create_and_open_ref_at (true, get_private_data (container)->rootfsfd, rootfs, dirname, 0755, err);
16991697
}
17001698
if (UNLIKELY (dirfd < 0))
17011699
return dirfd;
@@ -1751,13 +1749,12 @@ create_missing_devs (libcrun_container_t *container, bool binds, libcrun_error_t
17511749
cleanup_close int devfd = -1;
17521750
runtime_spec_schema_config_schema *def = container->container_def;
17531751
const char *rootfs = get_private_data (container)->rootfs;
1754-
int rootfsfd = get_private_data (container)->rootfsfd;
17551752
cleanup_close_map struct libcrun_fd_map *dev_fds = NULL;
17561753

17571754
dev_fds = get_private_data (container)->dev_fds;
17581755
get_private_data (container)->dev_fds = NULL;
17591756

1760-
devfd = openat (rootfsfd, "dev", O_CLOEXEC | O_PATH | O_DIRECTORY);
1757+
devfd = openat (get_private_data (container)->rootfsfd, "dev", O_CLOEXEC | O_PATH | O_DIRECTORY);
17611758
if (UNLIKELY (devfd < 0))
17621759
return crun_make_error (err, errno, "open `/dev` directory in `%s`", rootfs);
17631760

@@ -1912,7 +1909,6 @@ static int
19121909
append_tmpfs_mode_if_missing (libcrun_container_t *container, runtime_spec_schema_defs_mount *mount, char **data, libcrun_error_t *err)
19131910
{
19141911
const char *rootfs = get_private_data (container)->rootfs;
1915-
int rootfsfd = get_private_data (container)->rootfsfd;
19161912
bool empty_data = is_empty_string (*data);
19171913
cleanup_close int fd = -1;
19181914
struct stat st;
@@ -1921,7 +1917,7 @@ append_tmpfs_mode_if_missing (libcrun_container_t *container, runtime_spec_schem
19211917
if (*data != NULL && strstr (*data, "mode="))
19221918
return 0;
19231919

1924-
fd = safe_openat (rootfsfd, rootfs, mount->destination, O_CLOEXEC | O_RDONLY, 0, err);
1920+
fd = safe_openat (get_private_data (container)->rootfsfd, rootfs, mount->destination, O_CLOEXEC | O_RDONLY, 0, err);
19251921
if (fd < 0)
19261922
{
19271923
if (crun_error_get_errno (err) != ENOENT)
@@ -2050,12 +2046,11 @@ get_force_cgroup_v1_annotation (libcrun_container_t *container)
20502046
static int
20512047
do_mounts (libcrun_container_t *container, const char *rootfs, libcrun_error_t *err)
20522048
{
2053-
size_t i;
2054-
int ret;
20552049
runtime_spec_schema_config_schema *def = container->container_def;
20562050
const char *systemd_cgroup_v1 = get_force_cgroup_v1_annotation (container);
20572051
cleanup_close_map struct libcrun_fd_map *mount_fds = NULL;
2058-
int rootfsfd = get_private_data (container)->rootfsfd;
2052+
size_t i;
2053+
int ret;
20592054

20602055
mount_fds = get_private_data (container)->mount_fds;
20612056
get_private_data (container)->mount_fds = NULL;
@@ -2137,7 +2132,7 @@ do_mounts (libcrun_container_t *container, const char *rootfs, libcrun_error_t *
21372132
if (UNLIKELY (len < 0))
21382133
return len;
21392134

2140-
ret = safe_create_symlink (rootfsfd, rootfs, target, def->mounts[i]->destination, err);
2135+
ret = safe_create_symlink (get_private_data (container)->rootfsfd, rootfs, target, def->mounts[i]->destination, err);
21412136
if (UNLIKELY (ret < 0))
21422137
return ret;
21432138

@@ -2146,20 +2141,20 @@ do_mounts (libcrun_container_t *container, const char *rootfs, libcrun_error_t *
21462141
else if (is_sysfs_or_proc)
21472142
{
21482143
/* Enforce sysfs and proc to be mounted on a regular directory. */
2149-
ret = openat (rootfsfd, target, O_CLOEXEC | O_NOFOLLOW | O_DIRECTORY);
2144+
ret = openat (get_private_data (container)->rootfsfd, target, O_CLOEXEC | O_NOFOLLOW | O_DIRECTORY);
21502145
if (UNLIKELY (ret < 0))
21512146
{
21522147
if (errno == ENOENT)
21532148
{
21542149
if (strchr (target, '/'))
21552150
return crun_make_error (err, 0, "invalid target `%s`: it must be mounted at the root", target);
21562151

2157-
ret = mkdirat (rootfsfd, target, 0755);
2152+
ret = mkdirat (get_private_data (container)->rootfsfd, target, 0755);
21582153
if (UNLIKELY (ret < 0))
21592154
return crun_make_error (err, errno, "mkdirat `%s`", target);
21602155

21612156
/* Try opening it again. */
2162-
ret = openat (rootfsfd, target, O_CLOEXEC | O_NOFOLLOW | O_DIRECTORY);
2157+
ret = openat (get_private_data (container)->rootfsfd, target, O_CLOEXEC | O_NOFOLLOW | O_DIRECTORY);
21632158
}
21642159
else if (errno == ENOTDIR)
21652160
return crun_make_error (err, errno, "the target `/%s` is invalid", target);
@@ -2175,7 +2170,7 @@ do_mounts (libcrun_container_t *container, const char *rootfs, libcrun_error_t *
21752170
bool is_dir = S_ISDIR (src_mode);
21762171

21772172
/* Make sure any other directory/file is created and take a O_PATH reference to it. */
2178-
ret = crun_safe_create_and_open_ref_at (is_dir, rootfsfd, rootfs, target, is_dir ? 01755 : 0755, err);
2173+
ret = crun_safe_create_and_open_ref_at (is_dir, get_private_data (container)->rootfsfd, rootfs, target, is_dir ? 01755 : 0755, err);
21792174
if (UNLIKELY (ret < 0))
21802175
return ret;
21812176

@@ -2248,7 +2243,7 @@ do_mounts (libcrun_container_t *container, const char *rootfs, libcrun_error_t *
22482243
{
22492244
int destfd, tmpfd;
22502245

2251-
destfd = safe_openat (rootfsfd, rootfs, target, O_CLOEXEC | O_DIRECTORY, 0, err);
2246+
destfd = safe_openat (get_private_data (container)->rootfsfd, rootfs, target, O_CLOEXEC | O_DIRECTORY, 0, err);
22522247
if (UNLIKELY (destfd < 0))
22532248
return crun_error_wrap (err, "open `%s` to write for tmpcopyup", target);
22542249

@@ -2265,7 +2260,7 @@ do_mounts (libcrun_container_t *container, const char *rootfs, libcrun_error_t *
22652260
const bool is_dir = S_ISDIR (src_mode);
22662261
cleanup_close int dfd = -1;
22672262

2268-
dfd = safe_openat (rootfsfd, rootfs, target, O_RDONLY | O_PATH | O_CLOEXEC | (is_dir ? O_DIRECTORY : 0), 0, err);
2263+
dfd = safe_openat (get_private_data (container)->rootfsfd, rootfs, target, O_RDONLY | O_PATH | O_CLOEXEC | (is_dir ? O_DIRECTORY : 0), 0, err);
22692264
if (UNLIKELY (dfd < 0))
22702265
return crun_make_error (err, errno, "open mount target `/%s`", target);
22712266

@@ -2286,7 +2281,6 @@ do_mounts (libcrun_container_t *container, const char *rootfs, libcrun_error_t *
22862281
int
22872282
libcrun_container_do_bind_mount (libcrun_container_t *container, char *mount_source, char *mount_destination, char **mount_options, size_t mount_options_len, libcrun_error_t *err)
22882283
{
2289-
int ret, rootfsfd;
22902284
const char *target = consume_slashes (mount_destination);
22912285
cleanup_free char *data = NULL;
22922286
unsigned long flags = 0;
@@ -2296,9 +2290,9 @@ libcrun_container_do_bind_mount (libcrun_container_t *container, char *mount_sou
22962290
uint64_t rec_clear = 0;
22972291
uint64_t rec_set = 0;
22982292
const char *rootfs = get_private_data (container)->rootfs;
2299-
rootfsfd = get_private_data (container)->rootfsfd;
2293+
int ret;
23002294

2301-
if ((rootfsfd < 0) || (rootfs == NULL))
2295+
if ((get_private_data (container)->rootfsfd < 0) || (rootfs == NULL))
23022296
return crun_make_error (err, 0, "invalid rootfs state while performing bind mount from external plugin or handler");
23032297

23042298
if (mount_options == NULL)
@@ -2324,7 +2318,7 @@ libcrun_container_do_bind_mount (libcrun_container_t *container, char *mount_sou
23242318
}
23252319

23262320
/* Make sure any other directory/file is created and take a O_PATH reference to it. */
2327-
ret = crun_safe_create_and_open_ref_at (is_dir, rootfsfd, rootfs, target, is_dir ? 01755 : 0755, err);
2321+
ret = crun_safe_create_and_open_ref_at (is_dir, get_private_data (container)->rootfsfd, rootfs, target, is_dir ? 01755 : 0755, err);
23282322
if (UNLIKELY (ret < 0))
23292323
return ret;
23302324

0 commit comments

Comments
 (0)