Skip to content

Commit 9259e89

Browse files
giuseppeclaude
andcommitted
linux: use new mount API in do_mount when available
Try fsopen_mount()+fs_move_mount_to() for filesystem mounts and get_bind_mount()+fs_move_mount_to() for bind mounts before falling back to mount(). For propagation changes, try mount_setattr() first. This removes the dependency on /proc/self/fd paths for the initial mount and propagation steps when the new mount API is available. Co-Authored-By: Claude Opus 4.6 <[email protected]> Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent a98380b commit 9259e89

1 file changed

Lines changed: 187 additions & 18 deletions

File tree

‎src/libcrun/linux.c‎

Lines changed: 187 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ struct private_data_s
143143
int rootfsfd;
144144

145145
int notify_socket_tree_fd;
146+
int old_root_fd;
146147

147148
struct libcrun_fd_map *mount_fds;
148149
struct libcrun_fd_map *dev_fds;
@@ -176,6 +177,8 @@ cleanup_private_data (void *private_data)
176177
TEMP_FAILURE_RETRY (close (p->procfd));
177178
if (p->maskdir_fd >= 0)
178179
TEMP_FAILURE_RETRY (close (p->maskdir_fd));
180+
if (p->old_root_fd >= 0)
181+
TEMP_FAILURE_RETRY (close (p->old_root_fd));
179182
if (p->mount_fds)
180183
cleanup_close_mapp (&(p->mount_fds));
181184
if (p->dev_fds)
@@ -199,6 +202,7 @@ get_private_data (struct libcrun_container_s *container)
199202
p->rootfsfd = -1;
200203
p->procfd = -1;
201204
p->notify_socket_tree_fd = -1;
205+
p->old_root_fd = -1;
202206
p->maskdir_fd = -1;
203207
container->cleanup_private_data = cleanup_private_data;
204208
}
@@ -750,7 +754,26 @@ get_mount_flags_or_option (const char *name, int current_flags, unsigned long *e
750754
__attribute__ ((unused)) cleanup_free char *prev = NULL;
751755
unsigned long flags = get_mount_flags (name, current_flags, &found, extra_flags, rec_clear, rec_set);
752756
if (found)
753-
return flags;
757+
{
758+
/* MS_SYNCHRONOUS and MS_DIRSYNC cannot be set through fsmount()
759+
attr_flags. Pass them also as data so that fsconfig(SET_FLAG)
760+
can set SB_SYNCHRONOUS / SB_DIRSYNC on the superblock. */
761+
const char *data_flag = NULL;
762+
if ((flags & MS_SYNCHRONOUS) && ! (current_flags & MS_SYNCHRONOUS))
763+
data_flag = "sync";
764+
else if ((flags & MS_DIRSYNC) && ! (current_flags & MS_DIRSYNC))
765+
data_flag = "dirsync";
766+
767+
if (data_flag)
768+
{
769+
prev = *option;
770+
if (*option && **option)
771+
xasprintf (option, "%s,%s", *option, data_flag);
772+
else
773+
*option = xstrdup (data_flag);
774+
}
775+
return flags;
776+
}
754777

755778
prev = *option;
756779
if (*option && **option)
@@ -903,7 +926,7 @@ open_mount_target (libcrun_container_t *container, const char *target_rel, libcr
903926

904927
/* Attempt to open a mount of the specified type. */
905928
static int
906-
fsopen_mount (const char *type, const char *labeltype, const char *label)
929+
fsopen_mount (const char *type, const char *source_name, const char *labeltype, const char *label, const char *data)
907930
{
908931
#ifdef HAVE_NEW_MOUNT_API
909932
cleanup_close int fsfd = -1;
@@ -913,20 +936,49 @@ fsopen_mount (const char *type, const char *labeltype, const char *label)
913936
if (UNLIKELY (fsfd < 0))
914937
return fsfd;
915938

939+
/* Best-effort: pseudo file systems (tmpfs, proc, ...) ignore "source",
940+
so a failure here is expected and harmless. Real failures surface
941+
at FSCONFIG_CMD_CREATE below. */
942+
(void) syscall_fsconfig (fsfd, FSCONFIG_SET_STRING, "source", source_name ? source_name : type, 0);
943+
916944
if (labeltype)
917945
{
918946
ret = syscall_fsconfig (fsfd, FSCONFIG_SET_STRING, labeltype, label, 0);
919947
if (UNLIKELY (ret < 0))
920948
return ret;
921949
}
922950

951+
if (data && data[0])
952+
{
953+
cleanup_free char *data_copy = xstrdup (data);
954+
char *saveptr = NULL;
955+
char *token;
956+
957+
for (token = strtok_r (data_copy, ",", &saveptr); token; token = strtok_r (NULL, ",", &saveptr))
958+
{
959+
char *eq = strchr (token, '=');
960+
if (eq)
961+
{
962+
*eq = '\0';
963+
ret = syscall_fsconfig (fsfd, FSCONFIG_SET_STRING, token, eq + 1, 0);
964+
}
965+
else
966+
ret = syscall_fsconfig (fsfd, FSCONFIG_SET_FLAG, token, NULL, 0);
967+
968+
if (UNLIKELY (ret < 0))
969+
return ret;
970+
}
971+
}
972+
923973
ret = syscall_fsconfig (fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
924974
if (UNLIKELY (ret < 0))
925975
return ret;
926976

927977
return syscall_fsmount (fsfd, FSMOUNT_CLOEXEC, 0);
928978
#else
929979
(void) type;
980+
(void) source_name;
981+
(void) data;
930982
errno = ENOSYS;
931983
return -1;
932984
#endif
@@ -940,7 +992,7 @@ get_procfd (struct private_data_s *data, libcrun_error_t *err)
940992
if (data->procfd >= 0)
941993
return data->procfd;
942994

943-
fd = fsopen_mount ("proc", NULL, NULL);
995+
fd = fsopen_mount ("proc", NULL, NULL, NULL, NULL);
944996
if (fd < 0)
945997
fd = open ("/proc", O_DIRECTORY | O_CLOEXEC);
946998
if (UNLIKELY (fd < 0))
@@ -1268,6 +1320,7 @@ do_mount (libcrun_container_t *container, const char *source, int targetfd,
12681320
int label_how, libcrun_error_t *err)
12691321
{
12701322
cleanup_free char *data_with_label = NULL;
1323+
const char *context_type = NULL;
12711324
cleanup_close int ms_move_fd = -1;
12721325
const char *real_target = target;
12731326
bool single_instance = false;
@@ -1293,7 +1346,7 @@ do_mount (libcrun_container_t *container, const char *source, int targetfd,
12931346

12941347
if (label_how == LABEL_MOUNT)
12951348
{
1296-
const char *context_type = get_selinux_context_type (container, err);
1349+
context_type = get_selinux_context_type (container, err);
12971350
if (UNLIKELY (context_type == NULL))
12981351
return -1;
12991352

@@ -1324,7 +1377,61 @@ do_mount (libcrun_container_t *container, const char *source, int targetfd,
13241377
{
13251378
unsigned long flags = mountflags & ~(ALL_PROPAGATIONS_NO_REC | MS_RDONLY);
13261379

1327-
ret = mount (source, real_target, fstype, flags, data);
1380+
ret = -1;
1381+
1382+
if (targetfd >= 0)
1383+
{
1384+
if (mountflags & MS_BIND)
1385+
{
1386+
cleanup_close int bindfd = -1;
1387+
libcrun_error_t tmp_err = NULL;
1388+
1389+
bindfd = get_bind_mount (AT_FDCWD, source,
1390+
(mountflags & MS_REC) != 0,
1391+
false, false, 0, &tmp_err);
1392+
if (bindfd < 0)
1393+
{
1394+
int old_root_fd = get_private_data (container)->old_root_fd;
1395+
1396+
if (old_root_fd >= 0 && source && source[0] == '/')
1397+
{
1398+
crun_error_release (&tmp_err);
1399+
bindfd = get_bind_mount (old_root_fd, (source[1] == '\0') ? "." : source + 1,
1400+
(mountflags & MS_REC) != 0,
1401+
false, false, 0, &tmp_err);
1402+
}
1403+
}
1404+
if (bindfd >= 0)
1405+
ret = fs_move_mount_to (bindfd, targetfd, NULL);
1406+
if (ret < 0)
1407+
crun_error_release (&tmp_err);
1408+
}
1409+
else
1410+
{
1411+
cleanup_close int newfs = fsopen_mount (fstype, source, context_type, label, data);
1412+
if (newfs >= 0)
1413+
ret = fs_move_mount_to (newfs, targetfd, NULL);
1414+
}
1415+
}
1416+
1417+
if (ret < 0)
1418+
{
1419+
ret = mount (source, real_target, fstype, flags, data);
1420+
if (ret < 0 && (mountflags & MS_BIND) && source && source[0] == '/')
1421+
{
1422+
int old_root_fd = get_private_data (container)->old_root_fd;
1423+
1424+
if (old_root_fd >= 0)
1425+
{
1426+
cleanup_free char *proc_source_buf = NULL;
1427+
proc_fd_path_t old_root_path;
1428+
1429+
get_proc_self_fd_path (old_root_path, old_root_fd);
1430+
xasprintf (&proc_source_buf, "%s/%s", old_root_path, source + 1);
1431+
ret = mount (proc_source_buf, real_target, fstype, flags, data);
1432+
}
1433+
}
1434+
}
13281435
if (UNLIKELY (ret < 0))
13291436
{
13301437
int saved_errno = errno;
@@ -1340,17 +1447,44 @@ do_mount (libcrun_container_t *container, const char *source, int targetfd,
13401447
if (ret > 0)
13411448
{
13421449
cleanup_close int mountfd = -1;
1450+
int sys_old_root_fd = get_private_data (container)->old_root_fd;
13431451

1344-
if (! has_mount_for (container, "/sys/fs/cgroup"))
1452+
if (sys_old_root_fd >= 0)
1453+
mountfd = get_bind_mount (sys_old_root_fd, "sys", true, false, false, MS_PRIVATE, err);
1454+
if (mountfd < 0)
13451455
{
1346-
ret = mount ("/sys", real_target, NULL, MS_BIND | MS_REC, NULL);
1347-
if (UNLIKELY (ret < 0))
1348-
return crun_make_error (err, errno, "bind mount `/sys` from the host");
1456+
crun_error_release (err);
1457+
mountfd = get_bind_mount (AT_FDCWD, "/sys", true, false, false, MS_PRIVATE, err);
1458+
}
13491459

1460+
if (! has_mount_for (container, "/sys/fs/cgroup"))
1461+
{
1462+
if (mountfd >= 0)
1463+
{
1464+
ret = fs_move_mount_to (mountfd, targetfd, NULL);
1465+
if (UNLIKELY (ret < 0))
1466+
return crun_make_error (err, errno, "move mount `/sys` to `%s`", real_target);
1467+
}
1468+
else
1469+
{
1470+
crun_error_release (err);
1471+
if (sys_old_root_fd >= 0)
1472+
{
1473+
cleanup_free char *sys_path = NULL;
1474+
proc_fd_path_t sys_root_path;
1475+
1476+
get_proc_self_fd_path (sys_root_path, sys_old_root_fd);
1477+
xasprintf (&sys_path, "%s/sys", sys_root_path);
1478+
ret = mount (sys_path, real_target, NULL, MS_BIND | MS_REC, NULL);
1479+
}
1480+
else
1481+
ret = mount ("/sys", real_target, NULL, MS_BIND | MS_REC, NULL);
1482+
if (UNLIKELY (ret < 0))
1483+
return crun_make_error (err, errno, "bind mount `/sys` from the host");
1484+
}
13501485
return do_masked_or_readonly_path (container, "/sys/fs/cgroup", false, false, err);
13511486
}
13521487

1353-
mountfd = get_bind_mount (-1, "/sys", true, true, false, MS_PRIVATE, err);
13541488
if (UNLIKELY (mountfd < 0))
13551489
return mountfd;
13561490

@@ -1377,7 +1511,27 @@ do_mount (libcrun_container_t *container, const char *source, int targetfd,
13771511
/* We are replacing the rootfs, reopen it. */
13781512
if (is_empty_string (target))
13791513
{
1380-
int tmp = dup (fd);
1514+
int procfd = get_procfd (get_private_data (container), err);
1515+
int tmp;
1516+
if (UNLIKELY (procfd < 0))
1517+
return procfd;
1518+
1519+
{
1520+
cleanup_close int nsfd = openat (procfd, "self/ns/mnt", O_RDONLY | O_CLOEXEC);
1521+
if (UNLIKELY (nsfd < 0))
1522+
return crun_make_error (err, errno, "open `/proc/self/ns/mnt`");
1523+
1524+
ret = setns (nsfd, CLONE_NEWNS);
1525+
if (UNLIKELY (ret < 0))
1526+
return crun_make_error (err, errno, "setns `CLONE_NEWNS`");
1527+
}
1528+
1529+
close_and_reset (&fd);
1530+
fd = open (get_private_data (container)->rootfs, O_PATH | O_CLOEXEC);
1531+
if (UNLIKELY (fd < 0))
1532+
return crun_make_error (err, errno, "reopen rootfs after mount on /");
1533+
1534+
tmp = dup (fd);
13811535
if (UNLIKELY (tmp < 0))
13821536
return crun_make_error (err, errno, "dup");
13831537

@@ -1410,9 +1564,24 @@ do_mount (libcrun_container_t *container, const char *source, int targetfd,
14101564

14111565
if (mountflags & ALL_PROPAGATIONS_NO_REC)
14121566
{
1413-
ret = mount (NULL, real_target, NULL, mountflags & ALL_PROPAGATIONS, NULL);
1414-
if (UNLIKELY (ret < 0))
1415-
return crun_make_error (err, errno, "set propagation for `%s`", target);
1567+
bool propagation_done = false;
1568+
1569+
if (targetfd >= 0)
1570+
{
1571+
libcrun_error_t tmp_err = NULL;
1572+
ret = do_mount_setattr (false, target, targetfd, 0, mountflags & ALL_PROPAGATIONS, &tmp_err);
1573+
if (LIKELY (ret == 0))
1574+
propagation_done = true;
1575+
else
1576+
crun_error_release (&tmp_err);
1577+
}
1578+
1579+
if (! propagation_done)
1580+
{
1581+
ret = mount (NULL, real_target, NULL, mountflags & ALL_PROPAGATIONS, NULL);
1582+
if (UNLIKELY (ret < 0))
1583+
return crun_make_error (err, errno, "set propagation for `%s`", target);
1584+
}
14161585
}
14171586

14181587
if (mountflags & (MS_BIND | MS_RDONLY))
@@ -1430,7 +1599,7 @@ do_mount (libcrun_container_t *container, const char *source, int targetfd,
14301599

14311600
if ((remount_flags & MS_RDONLY) == 0)
14321601
{
1433-
ret = do_remount (fd, real_target, remount_flags, data, err);
1602+
ret = do_remount (fd >= 0 ? fd : targetfd, real_target, remount_flags, data, err);
14341603
if (UNLIKELY (ret < 0))
14351604
return ret;
14361605
}
@@ -4812,7 +4981,7 @@ prepare_and_send_dev_mounts (libcrun_container_t *container, int sync_socket_hos
48124981
}
48134982
}
48144983

4815-
devs_mountfd = fsopen_mount ("tmpfs", context_type, label);
4984+
devs_mountfd = fsopen_mount ("tmpfs", NULL, context_type, label, NULL);
48164985
if (UNLIKELY (devs_mountfd < 0))
48174986
{
48184987
ret = crun_make_error (err, errno, "fsopen_mount `tmpfs`");
@@ -5053,9 +5222,9 @@ init_container (libcrun_container_t *container, int sync_socket_container, struc
50535222
An error will be generated later if it is not possible to join the namespace.
50545223
*/
50555224
if (init_status->join_pidns && strcmp (def->mounts[i]->type, "proc") == 0)
5056-
fd = fsopen_mount (def->mounts[i]->type, NULL, NULL);
5225+
fd = fsopen_mount (def->mounts[i]->type, def->mounts[i]->source, NULL, NULL, NULL);
50575226
if (init_status->join_ipcns && strcmp (def->mounts[i]->type, "mqueue") == 0)
5058-
fd = fsopen_mount (def->mounts[i]->type, NULL, NULL);
5227+
fd = fsopen_mount (def->mounts[i]->type, def->mounts[i]->source, NULL, NULL, NULL);
50595228

50605229
if (fd >= 0)
50615230
{

0 commit comments

Comments
 (0)