Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: use mount API to self-clone #1606

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/libcrun/cloned_binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include <sys/syscall.h>

#include "utils.h"
#include "linux.h"

/* Use our own wrapper for memfd_create. */
#if !defined(SYS_memfd_create) && defined(__NR_memfd_create)
Expand Down Expand Up @@ -366,6 +367,17 @@ static int seal_execfd(int *fd, int fdtype)
return -1;
}

static int try_bindfd_mount_api(void)
{
libcrun_error_t err;
int mountfd = get_bind_mount (-1, "/proc/self/exe", false, true, &err);
if (mountfd < 0) {
crun_error_release (&err);
return -1;
}
return mountfd;
}

static int try_bindfd(void)
{
mode_t mask;
Expand Down Expand Up @@ -464,6 +476,13 @@ static int clone_binary(void)
* Before we resort to copying, let's try creating an ro-binfd in one shot
* by getting a handle for a read-only bind-mount of the execfd.
*/
execfd = try_bindfd_mount_api();
if (execfd >= 0) {
/* Transfer ownership to caller */
int ret_execfd = execfd;
execfd = -1;
return ret_execfd;
}
execfd = try_bindfd();
if (execfd >= 0) {
/* Transfer ownership to caller */
Expand Down
2 changes: 1 addition & 1 deletion src/libcrun/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ do_mount_setattr (const char *target, int targetfd, uint64_t clear, uint64_t set
return 0;
}

static int
int
get_bind_mount (int dirfd, const char *src, bool recursive, bool rdonly, libcrun_error_t *err)
{
cleanup_close int open_tree_fd = -1;
Expand Down
2 changes: 2 additions & 0 deletions src/libcrun/linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,6 @@ int libcrun_update_intel_rdt (const char *ctr_name, libcrun_container_t *contain

int libcrun_safe_chdir (const char *path, libcrun_error_t *err);

int get_bind_mount (int dirfd, const char *src, bool recursive, bool rdonly, libcrun_error_t *err);

#endif
Loading