From patchwork Thu Mar 30 17:13:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 67127 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 A71BA385B524 for ; Thu, 30 Mar 2023 17:21:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A71BA385B524 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680196905; bh=fPmrkxP8lA6mm8xNGo07bPYOfkrPR6g3BToZ+bsCS3c=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=vKMR/3+a8nr0y8qJJWgLLOGx7wIjewf+pIlYKuIgon/J4W9R3imikwy5UHiWZZDGp h7+iKvrO0lTWGztVsBEzSYdpAbnblv/oL4KwjUh89kgTFmntvFLQxCvs65HtknFTUp WtOQ1neucKcDNbGpsV1cHtiTYuUWJChRmxwq3irQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id E4B553858CDB for ; Thu, 30 Mar 2023 17:21:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E4B553858CDB Received: by mail-wm1-x32b.google.com with SMTP id v6-20020a05600c470600b003f034269c96so2043814wmo.4 for ; Thu, 30 Mar 2023 10:21:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680196880; x=1682788880; 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=fPmrkxP8lA6mm8xNGo07bPYOfkrPR6g3BToZ+bsCS3c=; b=Rciyx5YxvGkTW3i9kj9d9JzOcsSFFCPtBQ+mJ5HIUTU6baOAKZ6S6ZxFVuUESNkkVi rwEN0cUOFt7zHnkguKIQQYgnJ4UnBpCNzSne7b0iylooscsolmw9qtWbjbllafeqFtD9 fjQrdZSfvSHySKlR/cqbW20rVOenkI9dMguxBLF+g/hEIkHL1Ha5LyOw/tJiymA2aZ42 Pk7fL5B7XXi1K1OVzHrX34T6AFq7EZo2lNVS9EOQwDwemQM7jz+AmTd1eHMQcttsf6M9 8pc/Wc66SbKKJn/jWPQfemxxHHtteYh0FhPvWpIMo4mGIQ6B67cUMCsFpPo4CwIlw4ah wwzQ== X-Gm-Message-State: AO0yUKUqS0YynUHdBnPP4e+2bSQ8U0e7tYTDVYubedVD5llOkQDou7E3 q9PDp09LFNZU7tGBvpJWKbc= X-Google-Smtp-Source: AK7set/LoFCPP6hxq7r0EarZskBt24yWXwiE2OSQy9Pm50xlhelnA6s2EadV7BvR8tUsQhYHtccBEQ== X-Received: by 2002:a7b:ca4a:0:b0:3ee:555:ea60 with SMTP id m10-20020a7bca4a000000b003ee0555ea60mr20698202wml.8.1680196880497; Thu, 30 Mar 2023 10:21:20 -0700 (PDT) Received: from asus5775.alejandro-colomar.es ([170.253.51.134]) by smtp.googlemail.com with ESMTPSA id j19-20020a05600c191300b003ee1acdaf95sm6976067wmq.36.2023.03.30.10.21.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 10:21:20 -0700 (PDT) X-Google-Original-From: Alejandro Colomar To: linux-man@vger.kernel.org Cc: Alejandro Colomar , =?utf-8?q?Bastien_Roucari=C3=A8s?= , glibc , GCC , Eric Blake , Stefan Puiu , Igor Sysoev , Rich Felker , Andrew Clayton , Richard Biener , Zack Weinberg , Florian Weimer , Joseph Myers , Jakub Jelinek , Sam James Subject: [PATCH] sockaddr.3type: Document that sockaddr_storage is the API to be used Date: Thu, 30 Mar 2023 19:13:11 +0200 Message-Id: <20230330171310.12330-1-alx@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 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=unavailable 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" POSIX.1 Issue 8 will fix the long-standing issue with sockaddr APIs, which inevitably caused UB either on user code, libc, or more likely, both. sockaddr_storage has been clarified to be implemented in a manner that aliasing it is safe (suggesting a unnamed union, or other compiler magic). Link: Reported-by: Bastien Roucariès Reported-by: Alejandro Colomar Cc: glibc Cc: GCC Cc: Eric Blake 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: Sam James Signed-off-by: Alejandro Colomar --- Hi all, This is my proposal for documenting the POSIX decission of fixing the definition of sockaddr_storage. Bastien, I believe you had something similar in mind; please review. Eric, thanks again for the fix! Could you please also have a look at this? Cheers, Alex man3type/sockaddr.3type | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/man3type/sockaddr.3type b/man3type/sockaddr.3type index 32c3c5bd0..d1db87d5d 100644 --- a/man3type/sockaddr.3type +++ b/man3type/sockaddr.3type @@ -23,6 +23,14 @@ .SH SYNOPSIS .PP .B struct sockaddr_storage { .BR " sa_family_t ss_family;" " /* Address family */" +.PP +.RS 4 +/* This structure is not really implemented this way. It may be +\& implemented with an unnamed union or some compiler magic to +\& avoid breaking aliasing rules when accessed as any other of the +\& sockaddr_* structures documented in this page. See CAVEATS. +\& */ +.RE .B }; .PP .BR typedef " /* ... */ " socklen_t; @@ -122,6 +130,20 @@ .SH NOTES .I and .IR . +.SH CAVEATS +To avoid breaking aliasing rules, +programs that use functions that receive pointers to +.I sockaddr +structures should declare objects of type +.IR sockaddr_storage , +which is defined in a way that it +can be accessed as any of the different structures defined in this page. +Failure to do so may result in Undefined Behavior. +.PP +New functions should be written to accept pointers to +.I sockaddr_storage +instead of the traditional +.IR sockaddr . .SH SEE ALSO .BR accept (2), .BR bind (2),