]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logind: fix potential fd leak in deliver_session_leader_fd_consume()
authorMike Yuan <me@yhndnzj.com>
Thu, 6 Nov 2025 18:31:18 +0000 (19:31 +0100)
committerMike Yuan <me@yhndnzj.com>
Thu, 6 Nov 2025 18:31:56 +0000 (19:31 +0100)
Follow-up for 45eea629e3b3a640bf6a5cd13f4c73c86b426b11

src/login/logind.c

index 847ebbdde9aa47c0b80f492edcfcd9b776b0f94f..e643189338282481089b941f1dfbe4ba252ee489 100644 (file)
@@ -441,13 +441,13 @@ static int deliver_session_device_fd(Session *s, const char *fdname, int fd, dev
         return 0;
 }
 
-static int deliver_session_leader_fd_consume(Session *s, const char *fdname, int fd) {
+static int deliver_session_leader_fd_consume(Session *s, const char *fdname, int fd_consume) {
+        _cleanup_close_ int fd = ASSERT_FD(fd_consume);
         _cleanup_(pidref_done) PidRef leader_fdstore = PIDREF_NULL;
         int r;
 
         assert(s);
         assert(fdname);
-        assert(fd >= 0);
 
         /* Already deserialized via pidfd id? */
         if (pidref_is_set(&s->leader)) {
@@ -479,6 +479,7 @@ static int deliver_session_leader_fd_consume(Session *s, const char *fdname, int
                         log_warning_errno(r, "Failed to create reference to leader of session '%s': %m", s->id);
                 goto fail_close;
         }
+        TAKE_FD(fd);
 
         if (leader_fdstore.pid != s->deserialized_pid)
                 log_warning("Leader from pidfd (" PID_FMT ") doesn't match with LEADER=" PID_FMT " for session '%s', proceeding anyway.",
@@ -491,7 +492,7 @@ static int deliver_session_leader_fd_consume(Session *s, const char *fdname, int
         return 0;
 
 fail_close:
-        close_and_notify_warn(fd, fdname);
+        close_and_notify_warn(TAKE_FD(fd), fdname);
         return r;
 }