From patchwork Mon Apr 17 13:38:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 67814 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 7C35B3857012 for ; Mon, 17 Apr 2023 13:39:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C35B3857012 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681738773; bh=YUaESShH7fmAVvky45xc+/wx6FM9UK4KaYsqzQT9d24=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=tzeMJKP6XKa/Hlixs9PdlxSOp1pOTTrp8iD2h1NTR3BvBiZSS4KdXL71vPcubu41U JqF3KEEHczYmz0pZHY8d4dJP41vgX3Zf2Ks5K1A2Lcn+c6Dc6NESOGCj1TreSbHSIh r807lqEZhk06KHZsH9zfNcT+214wD8hF0exS+nM4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id DC6B33858C2F for ; Mon, 17 Apr 2023 13:39:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC6B33858C2F Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-4eca0563b31so1394648e87.1 for ; Mon, 17 Apr 2023 06:39:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681738747; x=1684330747; 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=YUaESShH7fmAVvky45xc+/wx6FM9UK4KaYsqzQT9d24=; b=Z/TeQToCQEkYnlE4WjXnj9pR30s5DPW2r9WDA7qmGNv83tXpm6VweZIsYYq8opP8/U YZYovXpExbwvxpy48uOjcKddye10Z7k2dpTnVhb/HUJM0FcdQPAFPazCebCU8JXCxAK5 rW2itBKmnO4OkhD9Zqcl/Awu93uTJBc4z+qyP8X6Osn5AEot9DtBpKH9f2LuNQANpmNq 7rSbPPlKT+XKB1jGor2APu6HmQx+HGWO1pc/nzZxiOx99adbzIJvy7e0iC95Qjb2BZCO GNumuNl4spAu4cbEwUBNSdNLOtqtKT23P1X3Dnat/WRBDybs1pAL+7VKHP0MLV+7jRgE i84g== X-Gm-Message-State: AAQBX9feSvUksrxMmeh7I/lEwRFisFa0oXMc0fC9TE5zY0ADC8kVOF20 vkFJDWpRJ5qFmNPQjjAHUJXciaP9C0I= X-Google-Smtp-Source: AKy350buHVhAE2D/5hv/xiii4rLXg+R0MLbbxEW3qH8fK+ibyR/+WOvfpw2xwiFO34FRCboYbC8zUQ== X-Received: by 2002:ac2:5e83:0:b0:4eb:2643:c6c6 with SMTP id b3-20020ac25e83000000b004eb2643c6c6mr1918507lfq.53.1681738747508; Mon, 17 Apr 2023 06:39:07 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id u27-20020ac24c3b000000b004db3e445f1fsm2067105lfq.97.2023.04.17.06.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 06:39:06 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Emilio Pozuelo Monfort , Sergey Bugaev Subject: [PATCH 1/4] hurd: Don't pass fd flags in CMSG_DATA Date: Mon, 17 Apr 2023 16:38:59 +0300 Message-Id: <20230417133902.99040-1-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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" The only valid flag defined here is FD_CLOEXEC. 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 | 3 +-- sysdeps/mach/hurd/sendmsg.c | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/sysdeps/mach/hurd/recvmsg.c b/sysdeps/mach/hurd/recvmsg.c index 39de86f6..5e8b18c6 100644 --- a/sysdeps/mach/hurd/recvmsg.c +++ b/sysdeps/mach/hurd/recvmsg.c @@ -189,7 +189,6 @@ __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. */ int *fds = (int *) CMSG_DATA (cmsg); nfds = (cmsg->cmsg_len - CMSG_ALIGN (sizeof (struct cmsghdr))) / sizeof (int); @@ -200,7 +199,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..77a720fb 100644 --- a/sysdeps/mach/hurd/sendmsg.c +++ b/sysdeps/mach/hurd/sendmsg.c @@ -138,8 +138,8 @@ __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 just pass 0 in the control data. */ + fds[i] = 0; err; })); From patchwork Mon Apr 17 13:39:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 67817 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 B24853853820 for ; Mon, 17 Apr 2023 13:40:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B24853853820 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681738816; bh=q7P7Tt2QvVtTsE3zhEl54gk0BOXUpqw14RTeJq4282s=; 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=o9Zc6e3Xr933BTnGmUedY3nTd0BiqfaSakx0RhOh31mpHs2XVa7vZX7Zl+4odmDOX KQuE10AlBpB2cUWIDx6uVLPeidj5oo3bi6wzU7X1RuU/Tx4Cyg7kbUcJft1/DBI6G9 SpFgl1sE1IfKMvZT/LFRnDWUdeSeIkl8KZPjXw+Q= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id 4A555385840F for ; Mon, 17 Apr 2023 13:39:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4A555385840F Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-4ec816d64afso6641807e87.1 for ; Mon, 17 Apr 2023 06:39:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681738748; x=1684330748; 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=q7P7Tt2QvVtTsE3zhEl54gk0BOXUpqw14RTeJq4282s=; b=eEAWO3luThMOKYpqVd8x791uwz/U5HIUnE6v5X0f9yLIFmQ/exlfl3DwWVtuAS8lOl Ri9dJXEdg821N2yUyjKO7gv1LYp52Nls1BeI6OuY7DdFSazgh2sEyroCXCB8NUDU4BK9 wBR9TR34TGo6RvV67+kvthv9bH6By00MuiFk0twCMMOD70I2A5uO6aaxNmO1ODt2TyFO IeCWmalMeXA7ZtE9Cj40pF/U4ygqkics9lijFZEXgoCEZjmNbi9Y3cHAk5hpjBddXVzU W04NpTH2nc/pWOemxQIR8d3vqW2YkX51Byyt8cSt02oXHHE6d99FuWa471CtL0NtCnsN p69Q== X-Gm-Message-State: AAQBX9d7pBWMn8n7UXDJXV3qjvXPx85DZYenbi9ys8wb51IUwIMICqOR /o2Knh2zAc0SHMsiOBcAK9IHnFF1YBo= X-Google-Smtp-Source: AKy350aDSBpit7xBtjBE6Lw43cpCtdBCPoOeYs4aTbZHVWIYSogy9UI+lLjSOvyk1ej39BmPrNk5MA== X-Received: by 2002:a05:6512:3088:b0:4e8:3d0:84b4 with SMTP id z8-20020a056512308800b004e803d084b4mr1555775lfd.24.1681738748464; Mon, 17 Apr 2023 06:39:08 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id u27-20020ac24c3b000000b004db3e445f1fsm2067105lfq.97.2023.04.17.06.39.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 06:39:08 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Emilio Pozuelo Monfort , Sergey Bugaev Subject: [PATCH 2/4] hurd: Implement MSG_CMSG_CLOEXEC Date: Mon, 17 Apr 2023 16:39:00 +0300 Message-Id: <20230417133902.99040-2-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230417133902.99040-1-bugaevc@gmail.com> References: <20230417133902.99040-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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 5e8b18c6..56c163fe 100644 --- a/sysdeps/mach/hurd/recvmsg.c +++ b/sysdeps/mach/hurd/recvmsg.c @@ -195,11 +195,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 Mon Apr 17 13:39:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 67818 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 7DE9C3853571 for ; Mon, 17 Apr 2023 13:40:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7DE9C3853571 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681738858; bh=ez5SaXVbRhBhwwZqZixV1oixY0IObYYfZvdEnvlh+bs=; 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=qu0LAOm05NGVP7NzN21yAZzjpfQvYlUTJgbjbySZ4pD21DFxZ+ddVFD5tcIBQqabp uMd4G+p2Ir6fbOcAqUnXp8HX4Mn6eHT+4piWX1vscyUT1xcqXuz8roowbtKA1ql6f3 sNjiIWEN+R/BJwRqnwYq74wpl2ggl99kImFJ1DMs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id 5895B3858418 for ; Mon, 17 Apr 2023 13:39:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5895B3858418 Received: by mail-lj1-x22f.google.com with SMTP id 38308e7fff4ca-2a7b08e84d4so13378891fa.3 for ; Mon, 17 Apr 2023 06:39:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681738749; x=1684330749; 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=ez5SaXVbRhBhwwZqZixV1oixY0IObYYfZvdEnvlh+bs=; b=NTKodUYjYkcB6Yg0IeNjidXGQ2MgppBmLY5gkfyWMKGQSlnCLsVAPf4WJzt133xSyr kK4TvjWLfT6SAPJP0mFLpX1UNJZC4bIKRZ/uQmtpHrgE/fhsyjUi62h1VaDndI+FpOPY rEV7oioj+K/+mBy/R/osAEXhwZMM4ia8uHL9XmQO0VPKqVUJ395AXXkuAQ+XuzzSY+pk uemac1NXjens8s+z7puLi6rFpmT6wjw1M89G060CgK/MmbdBeaCTbNJ2njIcrlyWsH18 yF663HrzU0Ji0/Daj26fJu9W2iTP1wJ7Omo4oftqCK8u33EEF3fuj5HfMojQ55omctIz Atdg== X-Gm-Message-State: AAQBX9cX3U1kg995ZyhdSYIiWsY896qy5qQyQ1KEwqlbbbjidKHXNlm9 q84tbYtsmToO9FXkoxT19AaQDGMMCso= X-Google-Smtp-Source: AKy350Z2KI36efI5Q49zq4u5fDW/6iUV4bgmlwOP/XCDR5a7Ol7dBKwqZr34qt9S7u8r9SJkpKnK3w== X-Received: by 2002:a19:f80a:0:b0:4ec:9e01:71e with SMTP id a10-20020a19f80a000000b004ec9e01071emr1927595lff.4.1681738749410; Mon, 17 Apr 2023 06:39:09 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id u27-20020ac24c3b000000b004db3e445f1fsm2067105lfq.97.2023.04.17.06.39.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 06:39:09 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Emilio Pozuelo Monfort , Sergey Bugaev Subject: [PATCH 3/4] hurd: Only deallocate addrport when it's valid Date: Mon, 17 Apr 2023 16:39:01 +0300 Message-Id: <20230417133902.99040-3-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230417133902.99040-1-bugaevc@gmail.com> References: <20230417133902.99040-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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 56c163fe..2b898c72 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 77a720fb..80eee6b1 100644 --- a/sysdeps/mach/hurd/sendmsg.c +++ b/sysdeps/mach/hurd/sendmsg.c @@ -195,8 +195,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 Mon Apr 17 13:39:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 67815 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 ACD9C3856948 for ; Mon, 17 Apr 2023 13:39:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ACD9C3856948 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681738781; bh=9mZ53YntpJdzEoL0u7Kca3+NKxpCegyLbzlusnZ4EqA=; 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=ILQy/TacJOaRPsWevIskbU+Ip/PCyufUYHBs7ZLP8wmXojoTVVfKWhVBXizKuCOJF 2L3k+tbRt6wfuM8Pj2Y2prvPIVddfetEfEX3AWEY+bqqE0rIKIY36/T+tgq+4LkOuE 5DzrHwCucaSxd52fqSyku3oEkgaEtpkGrfxx/tbg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by sourceware.org (Postfix) with ESMTPS id 2809C3858C2C for ; Mon, 17 Apr 2023 13:39:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2809C3858C2C Received: by mail-lj1-x236.google.com with SMTP id e8so8308794ljn.2 for ; Mon, 17 Apr 2023 06:39:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681738750; x=1684330750; 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=9mZ53YntpJdzEoL0u7Kca3+NKxpCegyLbzlusnZ4EqA=; b=FC9iMhAEwPQ8eWKcYNMk1MN4wxAyWUY0vhXcmwdmVki888Nw3OKG6kqjW+qoKOv4XF DQXxVFK3hfahy/BcvpGq8F3Wr/OZO+CZHQJFlKsuqZPx/rlueartkIZ7f6+hqWMw1xSu 9QeHzc66Kc1cFquK0RZX22R4gL33zTOW/YsR8bHOl5uqNTj74TBlYf14685C8KHXXc34 lYDVbRnLJ7oVidVJejP25Hgq4jirvh8AnbMGCN30OpGPhwPtShAiNbxjXItaAFuUIELa IUNh2+EEbw+PMoS7ilo30kVrb+sUOyFqp/F5BKnFDh6yBtLsmuW2J67N4WLwSJK/r1S+ pvmA== X-Gm-Message-State: AAQBX9dq5s5hKT6fg4/4kqR+83zg3OAU/xOLe/RsJAOIVUpoDUzoxlDV bzPD3O4j/h4S9Nd/WHKmG7vE77dJXes= X-Google-Smtp-Source: AKy350aRZlnS16W1CGIWdq17IMSD6xhf/11T4mae0Ga2tQ8HLDOH5A/l2rp9EQ85IuKoxw5OzvsaaQ== X-Received: by 2002:a2e:9793:0:b0:2a8:b8c3:37f7 with SMTP id y19-20020a2e9793000000b002a8b8c337f7mr1811414lji.23.1681738750353; Mon, 17 Apr 2023 06:39:10 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id u27-20020ac24c3b000000b004db3e445f1fsm2067105lfq.97.2023.04.17.06.39.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 06:39:09 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Emilio Pozuelo Monfort , Sergey Bugaev Subject: [RFC PATCH 4/4] socket: Add a test for MSG_CMSG_CLOEXEC Date: Mon, 17 Apr 2023 16:39:02 +0300 Message-Id: <20230417133902.99040-4-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230417133902.99040-1-bugaevc@gmail.com> References: <20230417133902.99040-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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" Signed-off-by: Sergey Bugaev Signed-off-by: Sergey Bugaev Reviewed-by: Adhemerval Zanella --- This is an attempt to write a test, roughly based on sysdeps/unix/sysv/linux/tst-scm_rights.c Please tell me if this needs any changes. For instance, I opted to do my own error checking for sendmsg/recvmsg/socketpair, but I could've alternatively added xsendmsg/xrecvmsg/xsocketpair. This is also looser on the coding style side, e.g. variables are defined mid-function instead of upfront, since that's what I saw in tst-scm_rights.c; I can get rid of that if that's not intended. I placed this into the generic socket/ since it should also work on Linux (though I only tested it on i686-gnu myself). Please tell me if this would be better suited for sysdeps/mach/hurd/ instead. socket/Makefile | 1 + socket/tst-cmsg_cloexec.c | 124 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 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..237c3db2 --- /dev/null +++ b/socket/tst-cmsg_cloexec.c @@ -0,0 +1,124 @@ +/* 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) +{ + /* const */ 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 = CMSG_LEN (sizeof (fd)) + }; + + 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_LEN (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