Skip to content

Commit 0b680e8

Browse files
Added an option to use private propagation for bind mounts.
1 parent 7d54d05 commit 0b680e8

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

bind-mount.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ parse_mountinfo (int proc_fd,
241241
int max_id;
242242
unsigned int n_lines;
243243
int root;
244-
245244
mountinfo = load_file_at (proc_fd, "self/mountinfo");
246245
if (mountinfo == NULL)
247246
die_with_error ("Can't open /proc/self/mountinfo");
@@ -376,6 +375,7 @@ parse_mountinfo (int proc_fd,
376375

377376
bind_mount_result
378377
bind_mount (int proc_fd,
378+
int p_priv,
379379
const char *src,
380380
const char *dest,
381381
bind_option_t options)
@@ -391,10 +391,12 @@ bind_mount (int proc_fd,
391391
cleanup_free char *kernel_case_combination = NULL;
392392
cleanup_fd int dest_fd = -1;
393393
int i;
394-
394+
int current_propagation = 0;
395+
if (p_priv == 1)
396+
current_propagation = MS_PRIVATE;
395397
if (src)
396398
{
397-
if (mount (src, dest, NULL, MS_SILENT | MS_BIND | (recursive ? MS_REC : 0), NULL) != 0)
399+
if (mount (src, dest, NULL, MS_SILENT | MS_BIND | current_propagation | (recursive ? MS_REC : 0), NULL) != 0)
398400
return BIND_MOUNT_ERROR_MOUNT;
399401
}
400402

@@ -436,7 +438,7 @@ bind_mount (int proc_fd,
436438
new_flags = current_flags | (devices ? 0 : MS_NODEV) | MS_NOSUID | (readonly ? MS_RDONLY : 0);
437439
if (new_flags != current_flags &&
438440
mount ("none", resolved_dest,
439-
NULL, MS_SILENT | MS_BIND | MS_REMOUNT | new_flags, NULL) != 0)
441+
NULL, MS_SILENT | MS_BIND | MS_REMOUNT | new_flags | current_propagation, NULL) != 0)
440442
return BIND_MOUNT_ERROR_REMOUNT_DEST;
441443

442444
/* We need to work around the fact that a bind mount does not apply the flags, so we need to manually
@@ -451,7 +453,7 @@ bind_mount (int proc_fd,
451453
new_flags = current_flags | (devices ? 0 : MS_NODEV) | MS_NOSUID | (readonly ? MS_RDONLY : 0);
452454
if (new_flags != current_flags &&
453455
mount ("none", mount_tab[i].mountpoint,
454-
NULL, MS_SILENT | MS_BIND | MS_REMOUNT | new_flags, NULL) != 0)
456+
NULL, MS_SILENT | MS_BIND | MS_REMOUNT | new_flags | current_propagation, NULL) != 0)
455457
{
456458
/* If we can't read the mountpoint we can't remount it, but that should
457459
be safe to ignore because its not something the user can access. */

bind-mount.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef enum
4040
} bind_mount_result;
4141

4242
bind_mount_result bind_mount (int proc_fd,
43+
int p_priv,
4344
const char *src,
4445
const char *dest,
4546
bind_option_t options);

bubblewrap.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ int opt_userns_block_fd = -1;
8585
int opt_info_fd = -1;
8686
int opt_json_status_fd = -1;
8787
int opt_seccomp_fd = -1;
88+
int opt_propagation = 0;
8889
const char *opt_sandbox_hostname = NULL;
8990
char *opt_args_data = NULL; /* owned */
9091
int opt_userns_fd = -1;
@@ -331,6 +332,7 @@ usage (int ecode, FILE *out)
331332
" --symlink SRC DEST Create symlink at DEST with target SRC\n"
332333
" --seccomp FD Load and use seccomp rules from FD (not repeatable)\n"
333334
" --add-seccomp-fd FD Load and use seccomp rules from FD (repeatable)\n"
335+
" --private Set mount propagation to private\n"
334336
" --block-fd FD Block on FD until some data to read is available\n"
335337
" --userns-block-fd FD Block on FD until the user namespace is ready\n"
336338
" --info-fd FD Write information about the running container to FD\n"
@@ -1070,7 +1072,7 @@ privileged_op (int privileged_op_socket,
10701072
break;
10711073

10721074
case PRIV_SEP_OP_REMOUNT_RO_NO_RECURSIVE:
1073-
bind_result = bind_mount (proc_fd, NULL, arg2, BIND_READONLY);
1075+
bind_result = bind_mount (proc_fd, opt_propagation, NULL, arg2, BIND_READONLY);
10741076

10751077
if (bind_result != BIND_MOUNT_SUCCESS)
10761078
die_with_bind_result (bind_result, errno,
@@ -1081,7 +1083,7 @@ privileged_op (int privileged_op_socket,
10811083
case PRIV_SEP_OP_BIND_MOUNT:
10821084
/* We always bind directories recursively, otherwise this would let us
10831085
access files that are otherwise covered on the host */
1084-
bind_result = bind_mount (proc_fd, arg1, arg2, BIND_RECURSIVE | flags);
1086+
bind_result = bind_mount (proc_fd, opt_propagation, arg1, arg2, BIND_RECURSIVE | flags);
10851087

10861088
if (bind_result != BIND_MOUNT_SUCCESS)
10871089
die_with_bind_result (bind_result, errno,
@@ -2149,6 +2151,8 @@ parse_args_recurse (int *argcp,
21492151
argv += 1;
21502152
argc -= 1;
21512153
}
2154+
else if (strcmp (arg, "--private") == 0)
2155+
opt_propagation = 1;
21522156
else if (strcmp (arg, "--add-seccomp-fd") == 0)
21532157
{
21542158
int the_fd;
@@ -2956,7 +2960,12 @@ main (int argc,
29562960
/* Mark everything as slave, so that we still
29572961
* receive mounts from the real root, but don't
29582962
* propagate mounts to the real root. */
2959-
if (mount (NULL, "/", NULL, MS_SILENT | MS_SLAVE | MS_REC, NULL) < 0)
2963+
int current_propagation;
2964+
if (opt_propagation == 0)
2965+
current_propagation = MS_SLAVE;
2966+
else
2967+
current_propagation = MS_PRIVATE;
2968+
if (mount (NULL, "/", NULL, MS_SILENT | current_propagation | MS_REC, NULL) < 0)
29602969
die_with_error ("Failed to make / slave");
29612970

29622971
/* Create a tmpfs which we will use as / in the namespace */

0 commit comments

Comments
 (0)