From patchwork Sun Feb 5 15:28:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1737730 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=oC2IfSrp; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P8tdG4vJBz23hn for ; Mon, 6 Feb 2023 02:30:42 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13A8E3858291 for ; Sun, 5 Feb 2023 15:30:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 13A8E3858291 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675611040; bh=VU2zRmdDezhcg1tV1N9BKQByb+mc/HyDCQKGJKdaU84=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=oC2IfSrpXeUHhdH2V/vxRWpoI30yvk3YyvqGYV8/pDg1nvbun4VMiYWvdfbKLkji/ R4oek2voym+opjBbpc5Mx7O3J/KceKVVXxH3M0zeozHdKOSKrSSD8AuQjKeAQ6zCWu /6PT2fLJak7TvqHJWBtTv/6ZxVFFRe8OxAVmwvHk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 09CAA3858D32 for ; Sun, 5 Feb 2023 15:30:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 09CAA3858D32 Received: by mail-wr1-x436.google.com with SMTP id m14so8324948wrg.13 for ; Sun, 05 Feb 2023 07:30:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=VU2zRmdDezhcg1tV1N9BKQByb+mc/HyDCQKGJKdaU84=; b=ptayfj1rSMXYvRrGjbe4dErQMwM1NeZ1BSCpPuOt4/9SeTCXnfkKkQWBOAdOtszWbm r5jYjbDzd/dtS0EF2GbWp/C9qLz65Sayu4i2lSZbttmvaL8flmm9Z8+eQiFrCPNnELiY RpJJfMQdVW/Z231C3oJziaELMindCBTjiClok6Y0/nHe03B0hwsvxeW64B9PRrzuYHA5 dacEc3ADyomUIP/AE/C38MQDL3+D9gya7TVrD1mq2VphVhNbzIHqN9VwfD+suyD/akdg lA3g8YZf2W0r1Y5cwlfFDbrfDo0gmJrDsD6LaRp6ptxvkTk9D7gzhDyB8RzwL34Eg0jz Jv7w== X-Gm-Message-State: AO0yUKU3/jUUA5opHwwXQL0yRgQB9W/smNipUgBxlYlz10B9UcV/IJOh Ke9qj7soQkpXBY5EZ+DYZNY= X-Google-Smtp-Source: AK7set+rgr7+PGBq2xt/PZzkQly23B8lUXXcYEW7+VW3rRHwzwwVr32JNpl6/Rb5sIPaXvXpCZrpIQ== X-Received: by 2002:adf:dc03:0:b0:2bf:f2f2:7d64 with SMTP id t3-20020adfdc03000000b002bff2f27d64mr14041504wri.33.1675611022881; Sun, 05 Feb 2023 07:30:22 -0800 (PST) Received: from asus5775.alejandro-colomar.es ([170.253.36.171]) by smtp.googlemail.com with ESMTPSA id b12-20020adff90c000000b002c3e89039casm914150wrr.12.2023.02.05.07.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Feb 2023 07:30:22 -0800 (PST) X-Google-Original-From: Alejandro Colomar To: linux-man@vger.kernel.org Cc: Alejandro Colomar , GCC , glibc , =?utf-8?q?Bastien_Roucari=C3=A8s?= , Stefan Puiu , Igor Sysoev , Rich Felker , Andrew Clayton , Richard Biener , Zack Weinberg , Florian Weimer , Joseph Myers , Jakub Jelinek , Eric Blake Subject: [PATCH] sockaddr.3type: BUGS: Document that libc should be fixed using a union Date: Sun, 5 Feb 2023 16:28:36 +0100 Message-Id: <20230205152835.17413-1-alx@kernel.org> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 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 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: Alejandro Colomar via Libc-alpha From: Alejandro Colomar Reply-To: Alejandro Colomar Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" As discussed before, and Bastien and I seem to agree, ideally we should define the following types: struct sockaddr_storage { union { struct { sa_family_t ss_family; }; struct sockaddr_in sin; struct sockaddr_in6 sin6; struct sockaddr_un sun; // ... }; }; struct [[deprecated]] sockaddr { sa_family_t sa_family; }; union [[gnu::transparent_union]] sockaddr_ptr { struct sockaddr_storage *ss; struct sockaddr *sa; }; And then we could define APIs like: int bind(int sockfd, const union sockaddr_ptr *addr, socklen_t len); Link: Link: Cc: GCC Cc: glibc Cc: Bastien Roucariès Cc: Stefan Puiu Cc: Igor Sysoev Cc: Rich Felker Cc: Andrew Clayton Cc: Richard Biener Cc: Zack Weinberg Cc: Florian Weimer Cc: Joseph Myers Cc: Jakub Jelinek Cc: Eric Blake Signed-off-by: Alejandro Colomar --- man3type/sockaddr.3type | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/man3type/sockaddr.3type b/man3type/sockaddr.3type index 319a5e552..239e836fc 100644 --- a/man3type/sockaddr.3type +++ b/man3type/sockaddr.3type @@ -120,6 +120,26 @@ .SH NOTES .I and .IR . +.SH BUGS +.I sockaddr_storage +was designed back when strict aliasing wasn't a problem. +Back then, +one would define a variable of that type, +and then access it as any of the other +.IR sockaddr_ * +types, +depending on the value of the first member. +This is Undefined Behavior. +However, there is no way to use these APIs without invoking Unedfined Behavior, +either in the user program or in libc, +so it is still recommended to use this method. +The only correct way to use different types in an API is through a union. +However, +that union must be implemented in the library, +since the type must be shared between the library and user code, +so libc should be fixed by implementing +.I sockaddr_storage +as a union. .SH SEE ALSO .BR accept (2), .BR bind (2),