From patchwork Sun Apr 23 16:05:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 68186 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 93E28385700A for ; Sun, 23 Apr 2023 16:06:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 93E28385700A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682265976; bh=X++uT09e24oWUMFRffYiiNtQGqg3Ld1n4OnlJK/kB8w=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=oHtDUEhO6SWk+CoQ9R1MN4dhCTxIcS0YfHASGdGFud2IRF+2ag3SPNxaDOtrAnn/2 8OuGr7PsG6n7SUE6+zEJPQx13TiGe95xXuDLVPscwboUn13b3jw27t7e2/GbwiEino dKw9NbKbUjDmjeDjuIs0O+coGOb/ZaSAx1VLw2e0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id 577ED3858C50 for ; Sun, 23 Apr 2023 16:05:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 577ED3858C50 Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-4edc63c82d1so3572609e87.0 for ; Sun, 23 Apr 2023 09:05:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682265950; x=1684857950; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=X++uT09e24oWUMFRffYiiNtQGqg3Ld1n4OnlJK/kB8w=; b=kv+eq3XPbqmtNwnNgdsHcuVKAGJtgOqnZrkB6TSNLRQvuvXa2kF+u1y6YDV/cpjlM/ rteI+vjcFQDzHzCC4FiLDX525M6nvRmdVwVjwQwjo/QsziBM/scD43M2jH/XxyOrWN/j Zs6/BNEazimYk48gbRFHy+SWD6M/RDyIxQ7l82GzEp39N3/QZxi6yRhgt3iRgRfElG1P 9qsbaDEF84KZeEtNDbCcG6GX/IFQuaftwTSq3538FubksVYybxgXlAFR8c5I/tEZTcDB BeFfUK8jhUUHOLVcTk9/4byE9gYwbCxA1XN7HGpzLx0aKS0RskEQRGcA9qH8BPwWeuOy 9OBA== X-Gm-Message-State: AAQBX9ehKtR8vTPtn+2Cx2HvVCYcfITv6lZXF6jU5rYkVfJfUyyUlPKt c3XZpf2JGpqSchNHgJ6HTi++z7dLLMmlOQ== X-Google-Smtp-Source: AKy350bCRSxxzdvKXWXHb7MLoQdMGM/u081AIWdIjUJYp1+cBU4LAY2984yqbuknkLgoDZ+I7457eg== X-Received: by 2002:a19:c51a:0:b0:4e8:3f35:6844 with SMTP id w26-20020a19c51a000000b004e83f356844mr2625238lfe.19.1682265950184; Sun, 23 Apr 2023 09:05:50 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:e26d:6595:78ee:6d64]) by smtp.gmail.com with ESMTPSA id y20-20020a2e7d14000000b002a9ed7f6f90sm1411277ljc.73.2023.04.23.09.05.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Apr 2023 09:05:49 -0700 (PDT) To: libc-alpha@sourceware.org Cc: bug-hurd@gnu.org, Samuel Thibault Subject: [PATCH v2 1/4] hurd: Don't pass FD_CLOEXEC in CMSG_DATA Date: Sun, 23 Apr 2023 19:05:45 +0300 Message-Id: <20230423160548.126576-1-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It is of no concern to the receiving process whether or not the sender process wants to close its copy of sent file descriptor upon exec, and it should not influence whether or not the received file descriptor gets the FD_CLOEXEC flag set in the receiving process. The latter should in fact be dependent on the MSG_CMSG_CLOEXEC flag being passed to the recvmsg () call, which is going to be implemented in the following commit. Fixes 344e755248ce02c0f8d095d11cc49e340703d926 "hurd: Support sending file descriptors over Unix sockets" Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/recvmsg.c | 5 +++-- sysdeps/mach/hurd/sendmsg.c | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/sysdeps/mach/hurd/recvmsg.c b/sysdeps/mach/hurd/recvmsg.c index 39de86f6..c08eb499 100644 --- a/sysdeps/mach/hurd/recvmsg.c +++ b/sysdeps/mach/hurd/recvmsg.c @@ -189,7 +189,8 @@ __libc_recvmsg (int fd, struct msghdr *message, int flags) if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { /* SCM_RIGHTS support. */ - /* The fd's flags are passed in the control data. */ + /* The fd's flags are passed in the control data, but there currently + aren't any defined other than FD_CLOEXEC which we don't respect. */ int *fds = (int *) CMSG_DATA (cmsg); nfds = (cmsg->cmsg_len - CMSG_ALIGN (sizeof (struct cmsghdr))) / sizeof (int); @@ -200,7 +201,7 @@ __libc_recvmsg (int fd, struct msghdr *message, int flags) if (err) goto cleanup; fds[j] = opened_fds[newfds] = _hurd_intern_fd (newports[newfds], - fds[j], 0); + 0, 0); if (fds[j] == -1) { err = errno; diff --git a/sysdeps/mach/hurd/sendmsg.c b/sysdeps/mach/hurd/sendmsg.c index 5871d1d8..2f19797b 100644 --- a/sysdeps/mach/hurd/sendmsg.c +++ b/sysdeps/mach/hurd/sendmsg.c @@ -138,8 +138,11 @@ __libc_sendmsg (int fd, const struct msghdr *message, int flags) 0, 0, 0, 0); if (! err) nports++; - /* We pass the flags in the control data. */ - fds[i] = descriptor->flags; + /* We pass the file descriptor flags in the control data. + The only currently defined flag is FD_CLOEXEC, which we + don't want to pass and so we mask it out, so this will + always be 0 currently. */ + fds[i] = descriptor->flags & ~FD_CLOEXEC; err; })); From patchwork Sun Apr 23 16:05:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 68187 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 76975385772C for ; Sun, 23 Apr 2023 16:06:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 76975385772C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682265977; bh=T+9W8flSu9pnM8XKPgyKM+1hQ27FChDAsQNawyYn+H8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=NkJsy0m8zR9eaUvNc2nqZf1/nnUBj/RBCFpapCV3ORwRh8wHEY+YxIdUyl5CstUJq 35GrLYN5rCO1polDSpd3cBfa/JhbQXxAyle/izIKBNCpyH/F3u0Gqpo5ZWQoryXlLY l3iOTtdv3eLyVsoSCDiofUFgt4fTFdY6/jWOOqZw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id 56F173858C83 for ; Sun, 23 Apr 2023 16:05:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 56F173858C83 Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2a8b1b51dbdso31871661fa.0 for ; Sun, 23 Apr 2023 09:05:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682265951; x=1684857951; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T+9W8flSu9pnM8XKPgyKM+1hQ27FChDAsQNawyYn+H8=; b=EOMC25dKYRhXPqMB8xLU5W54o9iyl888xr4Jigrqug4642gpBZTtkdDyf8Ui3O9K5G /73oE5fwCet4bvMnCdp5A9lTQPRRvsk3osKm4ntYSs3ZZB4zi5vulN8SYKUuM7t5c8hD 1bj3BcSheFmYCN0w3WC3zL9C4TWRrexiV9R5Ofkxclho6QFZv4R6jGEC4p980lR04MTh NNx3HjoZHuddSe5gaHqtSzdKXkonBLKG0V86J2n+AV9D7OKGkN8NqH5nrBNIbYt4/EYe gXhLZnISo56sRCcyFAOkqFJqgfeGD29MOVcIRJiuOv9GnDqszDrz2ARVugFSfpFMWJoZ +czw== X-Gm-Message-State: AAQBX9fPZ0qvUAh59dWsyxfGyjMkDjk1qDmocTQtJrvXoJz2TpnBqiLj wYOQWTQzu3V+V0iaq0jq0IXmn3VFWbrkuQ== X-Google-Smtp-Source: AKy350YS44yy8X4TqusY1ANcfhn8YhdDObi3FJo8q6Gq6peX/mi/YEceHbrUDWr5rOQHsqPFjM1UBw== X-Received: by 2002:a2e:8095:0:b0:2a0:69e0:9f7a with SMTP id i21-20020a2e8095000000b002a069e09f7amr1937437ljg.41.1682265951083; Sun, 23 Apr 2023 09:05:51 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:e26d:6595:78ee:6d64]) by smtp.gmail.com with ESMTPSA id y20-20020a2e7d14000000b002a9ed7f6f90sm1411277ljc.73.2023.04.23.09.05.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Apr 2023 09:05:50 -0700 (PDT) To: libc-alpha@sourceware.org Cc: bug-hurd@gnu.org, Samuel Thibault Subject: [PATCH v2 2/4] hurd: Implement MSG_CMSG_CLOEXEC Date: Sun, 23 Apr 2023 19:05:46 +0300 Message-Id: <20230423160548.126576-2-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230423160548.126576-1-bugaevc@gmail.com> References: <20230423160548.126576-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This is a new flag that can be passed to recvmsg () to make it atomically set the CLOEXEC flag on all the file descriptors received using the SCM_RIGHTS mechanism. This is useful for all the same reasons that the other XXX_CLOEXEC flags are useful: namely, it provides atomicity with respect to another thread of the same process calling (fork and then) exec at the same time. This flag is already supported on Linux and FreeBSD. The flag's value, 0x40000, is choosen to match FreeBSD's. Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/bits/socket.h | 5 ++++- sysdeps/mach/hurd/recvmsg.c | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/sysdeps/mach/hurd/bits/socket.h b/sysdeps/mach/hurd/bits/socket.h index 2d78a916..c2392bed 100644 --- a/sysdeps/mach/hurd/bits/socket.h +++ b/sysdeps/mach/hurd/bits/socket.h @@ -197,8 +197,11 @@ enum #define MSG_WAITALL MSG_WAITALL MSG_DONTWAIT = 0x80, /* This message should be nonblocking. */ #define MSG_DONTWAIT MSG_DONTWAIT - MSG_NOSIGNAL = 0x0400 /* Do not generate SIGPIPE on EPIPE. */ + MSG_NOSIGNAL = 0x0400, /* Do not generate SIGPIPE on EPIPE. */ #define MSG_NOSIGNAL MSG_NOSIGNAL + MSG_CMSG_CLOEXEC = 0x40000 /* Atomically set close-on-exec flag + for file descriptors in SCM_RIGHTS. */ +#define MSG_CMSG_CLOEXEC MSG_CMSG_CLOEXEC }; diff --git a/sysdeps/mach/hurd/recvmsg.c b/sysdeps/mach/hurd/recvmsg.c index c08eb499..9a37a053 100644 --- a/sysdeps/mach/hurd/recvmsg.c +++ b/sysdeps/mach/hurd/recvmsg.c @@ -197,11 +197,12 @@ __libc_recvmsg (int fd, struct msghdr *message, int flags) for (j = 0; j < nfds; j++) { + int fd_flags = (flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0; err = reauthenticate (ports[i], &newports[newfds]); if (err) goto cleanup; fds[j] = opened_fds[newfds] = _hurd_intern_fd (newports[newfds], - 0, 0); + fd_flags, 0); if (fds[j] == -1) { err = errno; From patchwork Sun Apr 23 16:05:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 68188 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B27EA3856DED for ; Sun, 23 Apr 2023 16:06:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B27EA3856DED DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682266018; bh=F9E2/SmI+5kO6abVEu5l/6cZWbiF6D6J8pPHD1YYAao=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=mqwj6dsNzlDxnD9zjBCam0RKMxELpfMfoIuI7KFxH94Wbl4V2T0IiJYS4A5D38Hdb v0HacjK+5JuoChYLboNFbmTngZj5tlT95qDuU4p/ocdNMgRxJmGjqM0zi412FmnueB K9dK2Tmi24RKOGV1goTrIk5fxfFTVSmPtsCR+U7s= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by sourceware.org (Postfix) with ESMTPS id 0D1983858C53 for ; Sun, 23 Apr 2023 16:05:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0D1983858C53 Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2a7ac8a2c8bso33159351fa.3 for ; Sun, 23 Apr 2023 09:05:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682265952; x=1684857952; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F9E2/SmI+5kO6abVEu5l/6cZWbiF6D6J8pPHD1YYAao=; b=aw1rTQBmH+qR+Y8wILRDfZMOdIm5ZApQPiyX/AZnaxPpbq/B913K6wdH9aJ1B27MPg NajqMR+ys1AyC9oK3pDvdxVHAKXvOzg+EbArAIo2WSrywZl0+c4Y+pOxejK+b9esIjNf a0oRnf/c05ZBaIRe4oqOSjCUDZ/PUIgFLrfIctygT63juq2q14NPj+pCmdYKpsTrMZvX Aemg5BEAF/j+GAqNLjheI7tXTTrjAW1nM41LZ/F5e8F7dR0AS6UAumRdUGNrwC1fzrov sQlINlTn/wpQ4kK0Kd+/XKirUzJn+FdgPRLA9PEbr0NBa83v9/2Qyndr56xagfCjmICR YJ9w== X-Gm-Message-State: AAQBX9fBBHBXNpcFq55a8KrLioJmViuQUuY4KJ2e2qmELJyG6kCTOXke gqbC4iGcJA1K2d+O/yiZSkHtXRtn8pvSUA== X-Google-Smtp-Source: AKy350a1he3c+MCMy5SUv4g0+37h3lzTtTAAdxMAhGT3NNks4aBmHFYPkyeAI3S/1X5I8kkJUa9rFQ== X-Received: by 2002:a2e:9f09:0:b0:2a8:b95a:c10d with SMTP id u9-20020a2e9f09000000b002a8b95ac10dmr2030679ljk.8.1682265951920; Sun, 23 Apr 2023 09:05:51 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:e26d:6595:78ee:6d64]) by smtp.gmail.com with ESMTPSA id y20-20020a2e7d14000000b002a9ed7f6f90sm1411277ljc.73.2023.04.23.09.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Apr 2023 09:05:51 -0700 (PDT) To: libc-alpha@sourceware.org Cc: bug-hurd@gnu.org, Samuel Thibault Subject: [PATCH v2 3/4] hurd: Only deallocate addrport when it's valid Date: Sun, 23 Apr 2023 19:05:47 +0300 Message-Id: <20230423160548.126576-3-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230423160548.126576-1-bugaevc@gmail.com> References: <20230423160548.126576-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/recv.c | 3 ++- sysdeps/mach/hurd/recvfrom.c | 3 ++- sysdeps/mach/hurd/recvmsg.c | 3 ++- sysdeps/mach/hurd/sendmsg.c | 5 +++-- sysdeps/mach/hurd/sendto.c | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/sysdeps/mach/hurd/recv.c b/sysdeps/mach/hurd/recv.c index 3bd5c16f..1783e38d 100644 --- a/sysdeps/mach/hurd/recv.c +++ b/sysdeps/mach/hurd/recv.c @@ -54,7 +54,8 @@ __recv (int fd, void *buf, size_t n, int flags) if (err) return __hurd_sockfail (fd, flags, err); - __mach_port_deallocate (__mach_task_self (), addrport); + if (MACH_PORT_VALID (addrport)) + __mach_port_deallocate (__mach_task_self (), addrport); __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen); if (bufp != buf) diff --git a/sysdeps/mach/hurd/recvfrom.c b/sysdeps/mach/hurd/recvfrom.c index 1cd5f917..6f2c927a 100644 --- a/sysdeps/mach/hurd/recvfrom.c +++ b/sysdeps/mach/hurd/recvfrom.c @@ -94,7 +94,8 @@ __recvfrom (int fd, void *buf, size_t n, int flags, __SOCKADDR_ARG addrarg, else if (addr_len != NULL) *addr_len = 0; - __mach_port_deallocate (__mach_task_self (), addrport); + if (MACH_PORT_VALID (addrport)) + __mach_port_deallocate (__mach_task_self (), addrport); /* Toss control data; we don't care. */ __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen); diff --git a/sysdeps/mach/hurd/recvmsg.c b/sysdeps/mach/hurd/recvmsg.c index 9a37a053..9cf3de48 100644 --- a/sysdeps/mach/hurd/recvmsg.c +++ b/sysdeps/mach/hurd/recvmsg.c @@ -135,7 +135,8 @@ __libc_recvmsg (int fd, struct msghdr *message, int flags) else if (message->msg_name != NULL) message->msg_namelen = 0; - __mach_port_deallocate (__mach_task_self (), aport); + if (MACH_PORT_VALID (aport)) + __mach_port_deallocate (__mach_task_self (), aport); if (buf == data) buf += len; diff --git a/sysdeps/mach/hurd/sendmsg.c b/sysdeps/mach/hurd/sendmsg.c index 2f19797b..f9ad7699 100644 --- a/sysdeps/mach/hurd/sendmsg.c +++ b/sysdeps/mach/hurd/sendmsg.c @@ -198,8 +198,9 @@ __libc_sendmsg (int fd, const struct msghdr *message, int flags) message->msg_controllen, &amount); LIBC_CANCEL_RESET (cancel_oldtype); - __mach_port_deallocate (__mach_task_self (), - aport); + if (MACH_PORT_VALID (aport)) + __mach_port_deallocate (__mach_task_self (), + aport); } err; })); diff --git a/sysdeps/mach/hurd/sendto.c b/sysdeps/mach/hurd/sendto.c index 5a960de8..777af1c4 100644 --- a/sysdeps/mach/hurd/sendto.c +++ b/sysdeps/mach/hurd/sendto.c @@ -94,7 +94,7 @@ __sendto (int fd, err; })); - if (aport != MACH_PORT_NULL) + if (MACH_PORT_VALID (aport)) __mach_port_deallocate (__mach_task_self (), aport); return err ? __hurd_sockfail (fd, flags, err) : wrote; From patchwork Sun Apr 23 16:05:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 68189 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 158D3385771C for ; Sun, 23 Apr 2023 16:07:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 158D3385771C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682266020; bh=/QmrlRTNXwNpu0kxljKAutrM+WqKHynNJFHePuj0cbI=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=gSYrLJ/AKf3pnSI61WWPV9rSyzm1oTCOHodcYfvSnwyepNffLIoYUqWTBY3RNiKei pe2BwgFd2RvKeFEHIcVYNGLtW7YzHvJTtLwqiEEjLHaG2xW/O7OOnw/rMS8+QXfJQX xNBk1F1FiA66M4hEEUN9SKLmaOwk9qOQKG9fv36k= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by sourceware.org (Postfix) with ESMTPS id 812C43858C5E for ; Sun, 23 Apr 2023 16:05:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 812C43858C5E Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2a8a5f6771fso33261031fa.3 for ; Sun, 23 Apr 2023 09:05:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682265953; x=1684857953; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/QmrlRTNXwNpu0kxljKAutrM+WqKHynNJFHePuj0cbI=; b=WoArZ5CMwTk55h7YAuJsmZ/Y31PX67Pqa+DTaPpOVMn+j9DKdOwvmGqNe9jR2TMQ1G oZjE18ZetjZwnVwngQ1Q2b784oerj6dUIwJWK1CWLTMwUn6+u5Bxbf7zhPvobqpiNG4M Lq/IWbodH541fZlw0DbCeUSna0DGjvZKYWtud4K7cZiMejtmqtjtMr0xAUlJtptsTE8R koU73HApRd22YCU4cdLutZvbNYAhA4ch4yyinL/CllRcjO1jIWNRfcSaOjwqT4F8PqPl hQvbh47fZU5ddVHum5S25oiVBksaTS7PwgLvrmfDel12h5XK/gXLbQipNtqK9HmN5gy1 YCBw== X-Gm-Message-State: AAQBX9cuc8i4SXAarwiIZQzxF84BwiKjO5b08kkZCT5zN9elK0zJoP9p QvNudZazhxmb3Mt7xuTOkIBPp4Du5Yp2Bw== X-Google-Smtp-Source: AKy350YfreeJhTXSIZDbQDy7mPPA6ZQk0+4D1BuNt+ePbn3UG7XeINNKntUOtWu7ySVM2jpAA8XMOw== X-Received: by 2002:a2e:9b51:0:b0:2a9:ebd4:1c45 with SMTP id o17-20020a2e9b51000000b002a9ebd41c45mr2223959ljj.48.1682265952804; Sun, 23 Apr 2023 09:05:52 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:e26d:6595:78ee:6d64]) by smtp.gmail.com with ESMTPSA id y20-20020a2e7d14000000b002a9ed7f6f90sm1411277ljc.73.2023.04.23.09.05.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Apr 2023 09:05:52 -0700 (PDT) To: libc-alpha@sourceware.org Cc: bug-hurd@gnu.org, Samuel Thibault , Adhemerval Zanella Subject: [PATCH v2 4/4] socket: Add a test for MSG_CMSG_CLOEXEC Date: Sun, 23 Apr 2023 19:05:48 +0300 Message-Id: <20230423160548.126576-4-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230423160548.126576-1-bugaevc@gmail.com> References: <20230423160548.126576-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This checks that: * We can send and receive fds over Unix domain sockets using SCM_RIGHTS; * msg_controllen, cmsg_level, cmsg_type, cmsg_len are all filled in correctly on receive; * Most importantly, the received fd has or has not the close-on-exec flag set depending on whether we pass MSG_CMSG_CLOEXEC to recvmsg (). Checked on i686-gnu and x86_64-linux-gnu. Reviewed-by: Adhemerval Zanella Signed-off-by: Sergey Bugaev --- socket/Makefile | 1 + socket/tst-cmsg_cloexec.c | 126 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 socket/tst-cmsg_cloexec.c diff --git a/socket/Makefile b/socket/Makefile index fffed7dd..94951ae3 100644 --- a/socket/Makefile +++ b/socket/Makefile @@ -35,6 +35,7 @@ tests := \ tst-accept4 \ tst-sockopt \ tst-cmsghdr \ + tst-cmsg_cloexec \ # tests tests-internal := \ diff --git a/socket/tst-cmsg_cloexec.c b/socket/tst-cmsg_cloexec.c new file mode 100644 index 00000000..a18f4912 --- /dev/null +++ b/socket/tst-cmsg_cloexec.c @@ -0,0 +1,126 @@ +/* Smoke test for MSG_CMSG_CLOEXEC. + Copyright (C) 2021-2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +static void +send_fd (int sockfd, int fd) +{ + char data[] = "hello"; + struct iovec iov = { .iov_base = data, .iov_len = sizeof (data) }; + + union + { + struct cmsghdr header; + char bytes[CMSG_SPACE (sizeof (fd))]; + } cmsg_storage; + + struct msghdr msg = + { + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = cmsg_storage.bytes, + .msg_controllen = sizeof (cmsg_storage) + }; + + memset (&cmsg_storage, 0, sizeof (cmsg_storage)); + + struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN (sizeof (fd)); + memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd)); + + ssize_t nsent = sendmsg (sockfd, &msg, 0); + if (nsent < 0) + FAIL_EXIT1 ("sendmsg (%d): %m", sockfd); + TEST_COMPARE (nsent, sizeof (data)); +} + +static int +recv_fd (int sockfd, int flags) +{ + char buffer[100]; + struct iovec iov = { .iov_base = buffer, .iov_len = sizeof (buffer) }; + + union + { + struct cmsghdr header; + char bytes[100]; + } cmsg_storage; + + struct msghdr msg = + { + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = cmsg_storage.bytes, + .msg_controllen = sizeof (cmsg_storage) + }; + + ssize_t nrecv = recvmsg (sockfd, &msg, flags); + if (nrecv < 0) + FAIL_EXIT1 ("recvmsg (%d): %m", sockfd); + + TEST_COMPARE (msg.msg_controllen, CMSG_SPACE (sizeof (int))); + struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); + TEST_COMPARE (cmsg->cmsg_level, SOL_SOCKET); + TEST_COMPARE (cmsg->cmsg_type, SCM_RIGHTS); + TEST_COMPARE (cmsg->cmsg_len, CMSG_LEN (sizeof (int))); + + int fd; + memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd)); + return fd; +} + +static int +do_test (void) +{ + int sockfd[2]; + int newfd; + int flags; + int rc = socketpair (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockfd); + if (rc < 0) + FAIL_EXIT1 ("socketpair: %m"); + + send_fd (sockfd[1], STDIN_FILENO); + newfd = recv_fd (sockfd[0], 0); + TEST_VERIFY_EXIT (newfd > 0); + flags = fcntl (newfd, F_GETFD, 0); + TEST_VERIFY_EXIT (flags != -1); + TEST_VERIFY (!(flags & FD_CLOEXEC)); + xclose (newfd); + + send_fd (sockfd[1], STDIN_FILENO); + newfd = recv_fd (sockfd[0], MSG_CMSG_CLOEXEC); + TEST_VERIFY_EXIT (newfd > 0); + flags = fcntl (newfd, F_GETFD, 0); + TEST_VERIFY_EXIT (flags != -1); + TEST_VERIFY (flags & FD_CLOEXEC); + xclose (newfd); + + xclose (sockfd[0]); + xclose (sockfd[1]); + return 0; +} + +#include