]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mount tunnel: use PidRef
authorLuca Boccassi <bluca@debian.org>
Thu, 19 Oct 2023 15:00:00 +0000 (16:00 +0100)
committerLuca Boccassi <bluca@debian.org>
Thu, 19 Oct 2023 15:31:05 +0000 (16:31 +0100)
src/core/dbus-service.c
src/machine/machine-dbus.c
src/shared/mount-util.c
src/shared/mount-util.h

index 5bc487bc39925068f315caac6cd3ca089be11b6c..41f4ee399ef748d29d74017f42c12a7abe026a04 100644 (file)
@@ -198,7 +198,7 @@ static int bus_service_method_mount(sd_bus_message *message, void *userdata, sd_
         propagate_directory = strjoina("/run/systemd/propagate/", u->id);
         if (is_image)
                 r = mount_image_in_namespace(
-                                unit_pid->pid,
+                                unit_pid,
                                 propagate_directory,
                                 "/run/systemd/incoming/",
                                 src, dest,
@@ -208,7 +208,7 @@ static int bus_service_method_mount(sd_bus_message *message, void *userdata, sd_
                                 c->mount_image_policy ?: &image_policy_service);
         else
                 r = bind_mount_in_namespace(
-                                unit_pid->pid,
+                                unit_pid,
                                 propagate_directory,
                                 "/run/systemd/incoming/",
                                 src, dest,
index 6341335c4dd46789b7a9a0e4806fa51a8e8ff86d..347cc9b0c0bc52a4b9fb978f8712f8d64116a81b 100644 (file)
@@ -881,7 +881,7 @@ int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bu
 
         propagate_directory = strjoina("/run/systemd/nspawn/propagate/", m->name);
         r = bind_mount_in_namespace(
-                        m->leader.pid,
+                        &m->leader,
                         propagate_directory,
                         "/run/host/incoming/",
                         src, dest,
index b6d2b6b61592e1438fb696a5b298d1fb0bafb469..e385f2177736f8718dbb05c6f162aee1d9c9e3d6 100644 (file)
@@ -1067,7 +1067,7 @@ finish:
 }
 
 static int mount_in_namespace(
-                pid_t target,
+                PidRef *target,
                 const char *propagate_path,
                 const char *incoming_path,
                 const char *src,
@@ -1087,24 +1087,29 @@ static int mount_in_namespace(
         pid_t child;
         int r;
 
-        assert(target > 0);
         assert(propagate_path);
         assert(incoming_path);
         assert(src);
         assert(dest);
         assert(!options || is_image);
 
-        r = namespace_open(target, &pidns_fd, &mntns_fd, NULL, NULL, &root_fd);
+        if (!pidref_is_set(target))
+                return -ESRCH;
+
+        r = namespace_open(target->pid, &pidns_fd, &mntns_fd, NULL, NULL, &root_fd);
         if (r < 0)
                 return log_debug_errno(r, "Failed to retrieve FDs of the target process' namespace: %m");
 
-        r = in_same_namespace(target, 0, NAMESPACE_MOUNT);
+        r = in_same_namespace(target->pid, 0, NAMESPACE_MOUNT);
         if (r < 0)
                 return log_debug_errno(r, "Failed to determine if mount namespaces are equal: %m");
         /* We can't add new mounts at runtime if the process wasn't started in a namespace */
         if (r > 0)
                 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to activate bind mount in target, not running in a mount namespace");
 
+        if (pidref_verify(target) < 0)
+                return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "Failed to verify target process '" PID_FMT "': %m", target->pid);
+
         r = chase(src, NULL, 0, &chased_src_path, &chased_src_fd);
         if (r < 0)
                 return log_debug_errno(r, "Failed to resolve source path of %s: %m", src);
@@ -1241,7 +1246,7 @@ static int mount_in_namespace(
 }
 
 int bind_mount_in_namespace(
-                pid_t target,
+                PidRef * target,
                 const char *propagate_path,
                 const char *incoming_path,
                 const char *src,
@@ -1253,7 +1258,7 @@ int bind_mount_in_namespace(
 }
 
 int mount_image_in_namespace(
-                pid_t target,
+                PidRef * target,
                 const char *propagate_path,
                 const char *incoming_path,
                 const char *src,
index 7c0189480e314ff854efc08270e675381109de27..f06fd6de8c68ed1492b47602e93c11bc31ed3b48 100644 (file)
@@ -10,6 +10,7 @@
 #include "dissect-image.h"
 #include "errno-util.h"
 #include "macro.h"
+#include "pidref.h"
 
 int repeat_unmount(const char *path, int flags);
 
@@ -98,8 +99,8 @@ static inline char *umount_and_free(char *p) {
 }
 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, umount_and_free);
 
-int bind_mount_in_namespace(pid_t target, const char *propagate_path, const char *incoming_path, const char *src, const char *dest, bool read_only, bool make_file_or_directory);
-int mount_image_in_namespace(pid_t target, const char *propagate_path, const char *incoming_path, const char *src, const char *dest, bool read_only, bool make_file_or_directory, const MountOptions *options, const ImagePolicy *image_policy);
+int bind_mount_in_namespace(PidRef *target, const char *propagate_path, const char *incoming_path, const char *src, const char *dest, bool read_only, bool make_file_or_directory);
+int mount_image_in_namespace(PidRef *target, const char *propagate_path, const char *incoming_path, const char *src, const char *dest, bool read_only, bool make_file_or_directory, const MountOptions *options, const ImagePolicy *image_policy);
 
 int make_mount_point(const char *path);
 int fd_make_mount_point(int fd);