]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
notify-recv: disable SO_PASSRIGHTS by default in notify_socket_prepare()
authorMike Yuan <me@yhndnzj.com>
Thu, 5 Jun 2025 21:01:09 +0000 (23:01 +0200)
committerMike Yuan <me@yhndnzj.com>
Tue, 17 Jun 2025 11:16:43 +0000 (13:16 +0200)
src/home/homed-manager.c
src/import/importd.c
src/notify/notify.c
src/shared/fork-journal.c
src/shared/notify-recv.c
src/shared/notify-recv.h
src/sysupdate/sysupdate-transfer.c
src/sysupdate/sysupdated.c
src/test/test-notify-recv.c
src/udev/udev-manager.c

index 5c0ad7625f49b5371fdcb46124de48e1dfe806d5..072cb12360ab7ffe343794fefc09552cc844f5bc 100644 (file)
@@ -1154,15 +1154,16 @@ static int manager_listen_notify(Manager *m) {
         assert(m);
         assert(!m->notify_socket_path);
 
-        r = notify_socket_prepare(
+        r = notify_socket_prepare_full(
                         m->event,
                         SD_EVENT_PRIORITY_NORMAL - 5, /* Make sure we process sd_notify() before SIGCHLD for
                                                        * any worker, so that we always know the error number
                                                        * of a client before it exits. */
                         on_notify_socket,
                         m,
+                        /* accept_fds = */ true,
                         &m->notify_socket_path,
-                        /* ret_event_source= */ NULL);
+                        /* ret_event_source = */ NULL);
         if (r < 0)
                 return log_error_errno(r, "Failed to prepare notify socket: %m");
 
index 2b59c965f75f496ac7eaf7624e98298776ff25f4..b28e64453417a128c00b850af6906f628cc8ee76 100644 (file)
@@ -723,8 +723,7 @@ static int manager_new(Manager **ret) {
                         SD_EVENT_PRIORITY_NORMAL,
                         manager_on_notify,
                         m,
-                        &m->notify_socket_path,
-                        /* ret_event_source= */ NULL);
+                        &m->notify_socket_path);
         if (r < 0)
                 return r;
 
index facd1eb9eecbd1abf3240d098ddb705afb75ac2b..3035c9f6cfe176eac873d044741b953d2888570e 100644 (file)
@@ -511,8 +511,7 @@ static int action_fork(char *const *_command) {
                                                         * more interesting, "positive" information. */
                         on_notify_socket,
                         &child,
-                        &addr_string,
-                        /* ret_event_source= */ NULL);
+                        &addr_string);
         if (r < 0)
                 return log_error_errno(r, "Failed to prepare notify socket: %m");
 
index 6e4a0300fff44916afd064f0c3f8f61c1dc9b65a..4fac8dead172347bc463d1b81f25ca54dd746400 100644 (file)
@@ -108,11 +108,12 @@ int journal_fork(RuntimeScope scope, char * const *units, PidRef *ret_pidref) {
         _cleanup_(sd_event_source_disable_unrefp) sd_event_source *notify_event_source = NULL;
         _cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
         _cleanup_free_ char *addr_string = NULL;
-        r = notify_socket_prepare(
+        r = notify_socket_prepare_full(
                         event,
                         SD_EVENT_PRIORITY_NORMAL-10, /* We want the notification message from the child before the SIGCHLD */
                         on_child_notify,
                         &child,
+                        /* accept_fds = */ false,
                         &addr_string,
                         &notify_event_source);
         if (r < 0)
index 1bc320af41b0d0e2756694443f47bb10f42b577d..ceee6587fa3f97d30c1635af8fd0c27b24728bb2 100644 (file)
 #include "socket-util.h"
 #include "strv.h"
 
-int notify_socket_prepare(
+int notify_socket_prepare_full(
                 sd_event *event,
                 int64_t priority,
                 sd_event_io_handler_t handler,
                 void *userdata,
+                bool accept_fds,
                 char **ret_path,
                 sd_event_source **ret_event_source) {
 
@@ -48,6 +49,13 @@ int notify_socket_prepare(
         if (r < 0)
                 log_debug_errno(r, "Failed to enable SO_PASSPIDFD on notification socket, ignoring: %m");
 
+        if (!accept_fds) {
+                /* since kernel v6.16 */
+                r = setsockopt_int(fd, SOL_SOCKET, SO_PASSRIGHTS, false);
+                if (r < 0)
+                        log_debug_errno(r, "Failed to disable SO_PASSRIGHTS on notification socket, ignoring: %m");
+        }
+
         _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
         r = sd_event_add_io(event, &s, fd, EPOLLIN, handler, userdata);
         if (r < 0)
index 4306439b6f752aa80372a93f64954e8c16cfcdca..e260e7ef5e770dd9249cb1f5b92d28e9e91fd261 100644 (file)
@@ -3,14 +3,25 @@
 
 #include "forward.h"
 
-int notify_socket_prepare(
+int notify_socket_prepare_full(
                 sd_event *event,
                 int64_t priority,
                 sd_event_io_handler_t handler,
                 void *userdata,
+                bool accept_fds,
                 char **ret_path,
                 sd_event_source **ret_event_source);
 
+static inline int notify_socket_prepare(
+                sd_event *event,
+                int64_t priority,
+                sd_event_io_handler_t handler,
+                void *userdata,
+                char **ret_path) {
+
+        return notify_socket_prepare_full(event, priority, handler, userdata, false, ret_path, NULL);
+}
+
 int notify_recv_with_fds(
                 int fd,
                 char **ret_text,
index f9c87ffad7e7d47718c453a6d8f5488848c75d43..0916aacab281695940849109817529e905334951 100644 (file)
@@ -1066,8 +1066,7 @@ static int run_callout(
                         SD_EVENT_PRIORITY_NORMAL - 5,
                         helper_on_notify,
                         ctx,
-                        &bind_name,
-                        /* ret_event_source= */ NULL);
+                        &bind_name);
         if (r < 0)
                 return log_error_errno(r, "Failed to prepare notify socket: %m");
 
index 3d65d99389bb240c5a0a7fb4884c033cb900e984..c46b2804b5b75b2b5e474f3d4707f131bf2b78ee 100644 (file)
@@ -1748,8 +1748,7 @@ static int manager_new(Manager **ret) {
                         SD_EVENT_PRIORITY_NORMAL,
                         manager_on_notify,
                         m,
-                        &m->notify_socket_path,
-                        /* ret_event_source= */ NULL);
+                        &m->notify_socket_path);
         if (r < 0)
                 return r;
 
index 5820048c92c214f16d84fbaf9d48d7c9af11fb01..bca56df13b9e9b813dabf846d5eade60b592c860 100644 (file)
@@ -88,7 +88,7 @@ TEST(notify_socket_prepare) {
                 .pidref = PIDREF_NULL,
         };
         _cleanup_free_ char *path = NULL;
-        ASSERT_OK(notify_socket_prepare(e, SD_EVENT_PRIORITY_NORMAL - 10, on_recv, &c, &path, /* ret_event_source= */ NULL));
+        ASSERT_OK(notify_socket_prepare_full(e, SD_EVENT_PRIORITY_NORMAL - 10, on_recv, &c, true, &path, NULL));
 
         ASSERT_OK(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD));
 
index aaaabe0d3186fd4d48a08c6e3493caf3f87fad3e..09ab8997e10b2ae3b5f5dbfaadd22d4d285bb2f4 100644 (file)
@@ -1232,8 +1232,7 @@ static int manager_start_worker_notify(Manager *manager) {
                         EVENT_PRIORITY_WORKER_NOTIFY,
                         on_worker_notify,
                         manager,
-                        &manager->worker_notify_socket_path,
-                        /* ret_event_source= */ NULL);
+                        &manager->worker_notify_socket_path);
         if (r < 0)
                 return log_error_errno(r, "Failed to prepare worker notification socket: %m");