Skip to content

Commit aefbb33

Browse files
committed
linux: fix copy_from_fd ownership
Closes: #2002 Signed-off-by: Erik Sjölund <erik.sjolund@gmail.com>
1 parent 96136b3 commit aefbb33

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/libcrun/linux.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,19 +2124,16 @@ static int
21242124
handle_tmpcopyup (libcrun_container_t *container, const char *rootfs, const char *target,
21252125
int copy_from_fd, libcrun_error_t *err)
21262126
{
2127-
int destfd, tmpfd, ret;
2128-
2127+
int destfd, ret;
2128+
cleanup_close int tmpfd = copy_from_fd;
21292129
destfd = safe_openat (get_private_data (container)->rootfsfd, rootfs, target,
21302130
O_CLOEXEC | O_DIRECTORY, 0, err);
21312131
if (UNLIKELY (destfd < 0))
21322132
return crun_error_wrap (err, "open `%s` to write for tmpcopyup", target);
21332133

2134-
/* take ownership for the fd. */
2135-
tmpfd = get_and_reset (&copy_from_fd);
2136-
2134+
// copy_recursive_fd_to_fd closes tmpfd and destfd
21372135
ret = copy_recursive_fd_to_fd (tmpfd, destfd, target, target, err);
2138-
close (destfd);
2139-
close (tmpfd);
2136+
tmpfd = -1;
21402137

21412138
return ret;
21422139
}
@@ -2339,7 +2336,9 @@ process_single_mount (libcrun_container_t *container, const char *rootfs,
23392336

23402337
if (copy_from_fd >= 0)
23412338
{
2339+
// handle_tmpcopyup closes copy_from_fd
23422340
ret = handle_tmpcopyup (container, rootfs, target, copy_from_fd, err);
2341+
copy_from_fd = -1;
23432342
if (UNLIKELY (ret < 0))
23442343
return ret;
23452344
}

src/libcrun/utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2249,7 +2249,7 @@ copy_recursive_fd_to_fd (int srcdirfd, int dfd, const char *srcname, const char
22492249
if (UNLIKELY (ret < 0))
22502250
return ret;
22512251
#endif
2252-
2252+
// copy_recursive_fd_to_fd closes srcfd and destfd
22532253
ret = copy_recursive_fd_to_fd (srcfd, destfd, de->d_name, de->d_name, err);
22542254
srcfd = destfd = -1;
22552255
if (UNLIKELY (ret < 0))

0 commit comments

Comments
 (0)