From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 3775 invoked from network); 3 Oct 2021 08:15:17 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 3 Oct 2021 08:15:17 -0000 Received: (qmail 5324 invoked by uid 550); 3 Oct 2021 08:15:13 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 5306 invoked from network); 3 Oct 2021 08:15:12 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hanne.name; s=kas202109251027; t=1633248899; bh=hY/Fcc6O5SpN1HNRHt9upbJJWAAcUmi9NSGv+n2ovWA=; h=Subject:To:References:From:In-Reply-To:Date:From; b=nmjuLZeCKhqOq7N79pTn6ZX2bTkNQdW1/kIr3no1nBvmARj+bOJrhfLNWn9228JHQ j1CQlMLKHbfW/ZfIWmiq+8csXqlTqawjPVN7XnctSPtONMfoLwzAEJqBio3rdnNHtR hBEa6N8a78LREUBz1Ul8Rv+nxHB8w8hm7sH79A7dDVj/Zk8Wx+l3q34XPs73sAxBhR j/v0K4Jv6ioCn57fYPV9KRr/gkt08X5fqA+U7BvDN6slGZhZKSl6UkCg47xm7ovVdM W4vS1QFTIa4kfm4vZyHR/pV9np+UWjo1UAXH0tfb+BPpcBQusGJhbPTuVFEwF+/63m 1Tmw293Js231A== To: musl@lists.openwall.com References: <20210903101352.379FC2FC06B2@dd11108.kasserver.com> <20210903131212.GE13220@brightrain.aerifal.cx><20210905172745.GD3090@voyager> From: "J. Hanne" User-Agent: ALL-INKL Webmail 2.11 X-SenderIP: 79.254.118.10 MIME-Version: 1.0 In-Reply-To: <20210905172745.GD3090@voyager> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Message-Id: <20211003081459.34C1C2FC23EF@dd11108.kasserver.com> Date: Sun, 3 Oct 2021 10:14:59 +0200 (CEST) Subject: Re: [musl] CMSG_LEN macro Hi, thanks for your thoughts. The NLMSG macros also already gave me similar headache some time ago. I personally find both APIs counter-intuitive because they use the term "ALIGN", when they mean "PAD", so that the *following* item is aligned. I would suggest the following patch now: Do not use CMSG_ALIGN on struct cmsghdr, because: - This has no effect on any architecture anyway, because sizeof(struct cmsghdr) == 16 on all archs - Using it contradicts with CMSG_DATA, which does NOT apply any padding after struct cmsghdr - This is consistent with the NLMSG_* macros --- diff -uNr a/include/sys/socket.h b/include/sys/socket.h --- a/include/sys/socket.h 2021-01-15 03:26:00.000000000 +0100 +++ b/include/sys/socket.h 2021-10-03 09:49:35.000000000 +0200 @@ -358,8 +358,8 @@ #define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) #define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) -#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) -#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) +#define CMSG_SPACE(len) (CMSG_ALIGN (len) + sizeof (struct cmsghdr)) +#define CMSG_LEN(len) (sizeof (struct cmsghdr) + (len)) #define SCM_RIGHTS 0x01 #define SCM_CREDENTIALS 0x02 -- By the way, the question which led me to all this stuff is: How do I get the payload length of a received cmsg. Neither the man page nor an Internet search gave me any satisfactory answer. So my best guess was "do some arithmetic with CMSG_LEN": payloadlen = cmsghdr->cmsg_len - CMSG_LEN(0); However, when double-checking with musl source code, the CMSG_ALIGN on struct cmsghdr made me doubt my approach. Now, I will stick to it - as long as nobody else has a better idea? Regards, Johann Markus Wichmann schrieb am 05.09.2021 19:27 (GMT +02:00): > On Fri, Sep 03, 2021 at 09:12:13AM -0400, Rich Felker wrote: > > Anyone else have thoughts on this? > > > > Rich > > I noticed something similar about the NLMSG_* macros that allow for > padding where there can be none (in the interface). struct nlmsghdr has > alignment of 4, and the netlink message alignment is also 4, and that > can never be changed on any existing arch since it would break binary > compatibility. And for netlink, it is unlikely they would add > architecture specific alignment in future, given that today it is > arch-independent. > > I guess those are symptoms of overly general software design. The macros > must exist, but I concur with your conclusion that they can be > implemented without reference to CMSG_ALIGN. > > BTW, I just checked the implementation of the NLMSG_* macros in musl, > and they do assume the alignment of struct nlmsghdr. So for consistency, > we should probably do the same for the CMSG_* macros. > > Ciao, > Markus > > >