Message ID | Y9xiYmVLRIKdpJcC@work |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp580867wrn; Thu, 2 Feb 2023 17:34:36 -0800 (PST) X-Google-Smtp-Source: AK7set9PLJq7ZGBOwyEvGHniARD9KHLshIcZdzpoRHZy8PNFOjFSBDGCIGMBWX3Y+nFsBat1J9jm X-Received: by 2002:a50:ee92:0:b0:4a0:e234:5351 with SMTP id f18-20020a50ee92000000b004a0e2345351mr8733155edr.15.1675388075801; Thu, 02 Feb 2023 17:34:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675388075; cv=none; d=google.com; s=arc-20160816; b=XPXBbukvQj1cz61nvrj05hJHkW4zt3RYlMyC3BXzoJu161WXKITuVTHFTK83OcX34l Ft03C47ACFZXSdePvnAkYOJQLP3NDm4Xtn5annr6RXLFD13ihCJrjL+/CaQ647DTZ9Qg tObUtJTg6bFKMgAlqJQJwkjqkpYBv82MDEci5wnVJ4ximcj1mX7kP/EZYSht9uVrk3zr +RPjLZMs3P9u7rVuxB5bGZp68Ms1hZINd4VjQmc4w+/jLhbJS9rLWeycl4IcoHMyNRNu 7tnCSNQIx6rqIlU9VqRJNZoCMXaJnM60VhSx8kFxpnpV3CfZMXdHnIbq101hxbUnFtC8 HBaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=1iqy6EkBfiAeGCyna3kVgnFwG2ACYGaHL5L3KMYTMiI=; b=Q0c+e2ytdSVB6z0tcRWsnmGDcCIjT3tMlL+Ec4NYpfwYCECmgnSxUrkMksq6Ne5CY1 y6jeQ7K+gCWwqG3hnxLEXnXfqLdZx4pRwGM6WFPrb64M0Us4Gjzte8GCU8f3iSs8kWi1 hS0WTq2Bwcz3vB1O2oKMrblhrgjuNmJ5NF3wR5zKQCM5Qg82jlOkou+LsZhhYE0VTuqy fn2NhE6xRdLbaioLZme7MWz7bSDXxp+DK9dAuNLj+CqVfOrsNzdTPwRetqKZPYPq+v+j xNxH7hLdCA/GCmsyvgaIpMUanQEtksIxnBb4kWkf32ClW5IAeJ1s1F84pwm6/9zH6FSN KWhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QHKJhVUi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kb2-20020a170907924200b0087bd3430c13si1274918ejb.359.2023.02.02.17.34.11; Thu, 02 Feb 2023 17:34:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QHKJhVUi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231660AbjBCBYp (ORCPT <rfc822;il.mystafa@gmail.com> + 99 others); Thu, 2 Feb 2023 20:24:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231598AbjBCBYm (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 2 Feb 2023 20:24:42 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BED0B84B64; Thu, 2 Feb 2023 17:24:36 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 6FBA3B810EE; Fri, 3 Feb 2023 01:24:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D815C433EF; Fri, 3 Feb 2023 01:24:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675387474; bh=IscsXZpj+h2AU0j+vLsryYQTUgOMPPGd5Jv1285ZrJk=; h=Date:From:To:Cc:Subject:From; b=QHKJhVUiPYk/zx3KeJ5KOmbtERStOiApOlVI2w8X9mB474wJY93F25VmERJ1N2mjC IzzAx4B80n6Joo+E9878meSE+QVA3miCcbWn9baxFmZQyXEOLwtB8MCmgKcm7JOHmT ZkjIuQ2cuM9hMe+6ZNZu1zAUV/jdwHi8/2ZtzJkccYEc8LHpseYYo2BcwGQxGK6SHF OBxC8TwD1aQgYpLmDx07lyS9jSZJFYnvW06gYJs8AJLh9PQMxbiEoibGRwgIxFJEtn UIn8n2KUQiBfNqadGk62a9bWHOV9ZeydUEA2VYnEdCBr0PKCV0fhnKHyAURdnez9xy mZ/TyOnl8yEYA== Date: Thu, 2 Feb 2023 19:24:50 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: "Darrick J. Wong" <djwong@kernel.org> Cc: linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH][next] xfs: Replace one-element arrays with flexible-array members Message-ID: <Y9xiYmVLRIKdpJcC@work> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756771727191794958?= X-GMAIL-MSGID: =?utf-8?q?1756771727191794958?= |
Series |
[next] xfs: Replace one-element arrays with flexible-array members
|
|
Commit Message
Gustavo A. R. Silva
Feb. 3, 2023, 1:24 a.m. UTC
One-element arrays are deprecated, and we are replacing them with flexible
array members instead. So, replace one-element arrays with flexible-array
members in structures xfs_attr_leaf_name_local and
xfs_attr_leaf_name_remote.
The only binary differences reported after the changes are all like
these:
fs/xfs/libxfs/xfs_attr_leaf.o
_@@ -435,7 +435,7 @@
3b8: movzbl 0x2(%rbx),%eax
3bc: rol $0x8,%bp
3c0: movzwl %bp,%ebp
- 3c3: lea 0x2(%rax,%rbp,1),%ebx
+ 3c3: lea 0x3(%rax,%rbp,1),%ebx
3c7: call 3cc <xfs_attr_leaf_entsize+0x8c>
3c8: R_X86_64_PLT32 __tsan_func_exit-0x4
3cc: or $0x3,%ebx
_@@ -454,7 +454,7 @@
3ea: movzbl 0x8(%rbx),%ebx
3ee: call 3f3 <xfs_attr_leaf_entsize+0xb3>
3ef: R_X86_64_PLT32 __tsan_func_exit-0x4
- 3f3: add $0xa,%ebx
+ 3f3: add $0xb,%ebx
3f6: or $0x3,%ebx
3f9: add $0x1,%ebx
3fc: mov %ebx,%eax
similar changes in fs/xfs/scrub/attr.o and fs/xfs/xfs.o object files.
And the reason for this is because of the round_up() macro called in
functions xfs_attr_leaf_entsize_remote() and xfs_attr_leaf_entsize_local(),
which is compensanting for the one-byte reduction in size (due to the
flex-array transformation) of structures xfs_attr_leaf_name_remote and
xfs_attr_leaf_name_local. So, sizes remain the same before and after
changes.
This helps with the ongoing efforts to tighten the FORTIFY_SOURCE
routines on memcpy() and help us make progress towards globally
enabling -fstrict-flex-arrays=3 [1].
Link: https://github.com/KSPP/linux/issues/79
Link: https://github.com/KSPP/linux/issues/251
Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
fs/xfs/libxfs/xfs_da_format.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
Comments
On Thu, Feb 02, 2023 at 07:24:50PM -0600, Gustavo A. R. Silva wrote: > One-element arrays are deprecated, and we are replacing them with flexible > array members instead. So, replace one-element arrays with flexible-array > members in structures xfs_attr_leaf_name_local and > xfs_attr_leaf_name_remote. > > The only binary differences reported after the changes are all like > these: > > fs/xfs/libxfs/xfs_attr_leaf.o > _@@ -435,7 +435,7 @@ > 3b8: movzbl 0x2(%rbx),%eax > 3bc: rol $0x8,%bp > 3c0: movzwl %bp,%ebp > - 3c3: lea 0x2(%rax,%rbp,1),%ebx > + 3c3: lea 0x3(%rax,%rbp,1),%ebx > 3c7: call 3cc <xfs_attr_leaf_entsize+0x8c> > 3c8: R_X86_64_PLT32 __tsan_func_exit-0x4 > 3cc: or $0x3,%ebx > _@@ -454,7 +454,7 @@ > 3ea: movzbl 0x8(%rbx),%ebx > 3ee: call 3f3 <xfs_attr_leaf_entsize+0xb3> > 3ef: R_X86_64_PLT32 __tsan_func_exit-0x4 > - 3f3: add $0xa,%ebx > + 3f3: add $0xb,%ebx > 3f6: or $0x3,%ebx > 3f9: add $0x1,%ebx > 3fc: mov %ebx,%eax > > similar changes in fs/xfs/scrub/attr.o and fs/xfs/xfs.o object files. I usually turn off the sanitizers for the A/B build comparisons to make it easier to read the results. It looks like it _grew_ in size here, though? > And the reason for this is because of the round_up() macro called in > functions xfs_attr_leaf_entsize_remote() and xfs_attr_leaf_entsize_local(), > which is compensanting for the one-byte reduction in size (due to the > flex-array transformation) of structures xfs_attr_leaf_name_remote and > xfs_attr_leaf_name_local. So, sizes remain the same before and after > changes. > > This helps with the ongoing efforts to tighten the FORTIFY_SOURCE > routines on memcpy() and help us make progress towards globally > enabling -fstrict-flex-arrays=3 [1]. > > Link: https://github.com/KSPP/linux/issues/79 > Link: https://github.com/KSPP/linux/issues/251 > Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1] > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> If xfstests pass, this seems good to me. Thanks! Reviewed-by: Kees Cook <keescook@chromium.org>
On Thu, Feb 02, 2023 at 07:24:50PM -0600, Gustavo A. R. Silva wrote: > One-element arrays are deprecated, and we are replacing them with flexible > array members instead. So, replace one-element arrays with flexible-array > members in structures xfs_attr_leaf_name_local and > xfs_attr_leaf_name_remote. > > The only binary differences reported after the changes are all like > these: > > fs/xfs/libxfs/xfs_attr_leaf.o > _@@ -435,7 +435,7 @@ > 3b8: movzbl 0x2(%rbx),%eax > 3bc: rol $0x8,%bp > 3c0: movzwl %bp,%ebp > - 3c3: lea 0x2(%rax,%rbp,1),%ebx > + 3c3: lea 0x3(%rax,%rbp,1),%ebx > 3c7: call 3cc <xfs_attr_leaf_entsize+0x8c> > 3c8: R_X86_64_PLT32 __tsan_func_exit-0x4 > 3cc: or $0x3,%ebx > _@@ -454,7 +454,7 @@ > 3ea: movzbl 0x8(%rbx),%ebx > 3ee: call 3f3 <xfs_attr_leaf_entsize+0xb3> > 3ef: R_X86_64_PLT32 __tsan_func_exit-0x4 > - 3f3: add $0xa,%ebx > + 3f3: add $0xb,%ebx > 3f6: or $0x3,%ebx > 3f9: add $0x1,%ebx > 3fc: mov %ebx,%eax > > similar changes in fs/xfs/scrub/attr.o and fs/xfs/xfs.o object files. > > And the reason for this is because of the round_up() macro called in > functions xfs_attr_leaf_entsize_remote() and xfs_attr_leaf_entsize_local(), > which is compensanting for the one-byte reduction in size (due to the > flex-array transformation) of structures xfs_attr_leaf_name_remote and > xfs_attr_leaf_name_local. So, sizes remain the same before and after > changes. > > This helps with the ongoing efforts to tighten the FORTIFY_SOURCE > routines on memcpy() and help us make progress towards globally > enabling -fstrict-flex-arrays=3 [1]. > > Link: https://github.com/KSPP/linux/issues/79 > Link: https://github.com/KSPP/linux/issues/251 > Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1] > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > --- > fs/xfs/libxfs/xfs_da_format.h | 8 ++++---- > 1 file changed, 4 insertions(+), 4 deletions(-) > > diff --git a/fs/xfs/libxfs/xfs_da_format.h b/fs/xfs/libxfs/xfs_da_format.h > index 25e2841084e1..e1e62ebb0c44 100644 > --- a/fs/xfs/libxfs/xfs_da_format.h > +++ b/fs/xfs/libxfs/xfs_da_format.h > @@ -620,14 +620,14 @@ typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */ > typedef struct xfs_attr_leaf_name_local { > __be16 valuelen; /* number of bytes in value */ > __u8 namelen; /* length of name bytes */ > - __u8 nameval[1]; /* name/value bytes */ > + __u8 nameval[]; /* name/value bytes */ > } xfs_attr_leaf_name_local_t; > > typedef struct xfs_attr_leaf_name_remote { > __be32 valueblk; /* block number of value bytes */ > __be32 valuelen; /* number of bytes in value */ > __u8 namelen; /* length of name bytes */ > - __u8 name[1]; /* name bytes */ > + __u8 name[]; /* name bytes */ Does the large comment about m68k problems in xfs_ondisk.h need updating here? --D > } xfs_attr_leaf_name_remote_t; > > typedef struct xfs_attr_leafblock { > @@ -747,13 +747,13 @@ xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx) > */ > static inline int xfs_attr_leaf_entsize_remote(int nlen) > { > - return round_up(sizeof(struct xfs_attr_leaf_name_remote) - 1 + > + return round_up(sizeof(struct xfs_attr_leaf_name_remote) + > nlen, XFS_ATTR_LEAF_NAME_ALIGN); > } > > static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen) > { > - return round_up(sizeof(struct xfs_attr_leaf_name_local) - 1 + > + return round_up(sizeof(struct xfs_attr_leaf_name_local) + > nlen + vlen, XFS_ATTR_LEAF_NAME_ALIGN); > } > > -- > 2.34.1 >
On Thu, Feb 02, 2023 at 07:24:50PM -0600, Gustavo A. R. Silva wrote: > One-element arrays are deprecated, and we are replacing them with flexible > array members instead. So, replace one-element arrays with flexible-array > members in structures xfs_attr_leaf_name_local and > xfs_attr_leaf_name_remote. > > The only binary differences reported after the changes are all like > these: > > fs/xfs/libxfs/xfs_attr_leaf.o > _@@ -435,7 +435,7 @@ > 3b8: movzbl 0x2(%rbx),%eax > 3bc: rol $0x8,%bp > 3c0: movzwl %bp,%ebp > - 3c3: lea 0x2(%rax,%rbp,1),%ebx > + 3c3: lea 0x3(%rax,%rbp,1),%ebx > 3c7: call 3cc <xfs_attr_leaf_entsize+0x8c> > 3c8: R_X86_64_PLT32 __tsan_func_exit-0x4 > 3cc: or $0x3,%ebx > _@@ -454,7 +454,7 @@ > 3ea: movzbl 0x8(%rbx),%ebx > 3ee: call 3f3 <xfs_attr_leaf_entsize+0xb3> > 3ef: R_X86_64_PLT32 __tsan_func_exit-0x4 > - 3f3: add $0xa,%ebx > + 3f3: add $0xb,%ebx > 3f6: or $0x3,%ebx > 3f9: add $0x1,%ebx > 3fc: mov %ebx,%eax > > similar changes in fs/xfs/scrub/attr.o and fs/xfs/xfs.o object files. That seems like a red flag to me - an off-by-one change in the compiled code that calculates of the on-disk size of a structure as a result of an in-memory structure change just smells like a bug. How did you test this change? > And the reason for this is because of the round_up() macro called in > functions xfs_attr_leaf_entsize_remote() and xfs_attr_leaf_entsize_local(), > which is compensanting for the one-byte reduction in size (due to the > flex-array transformation) of structures xfs_attr_leaf_name_remote and > xfs_attr_leaf_name_local. So, sizes remain the same before and after > changes. I'm not sure that is true. Before this change: sizeof(xfs_attr_leaf_name_local_t) = 4 sizeof(xfs_attr_leaf_name_remote_t) = 12 After this change: sizeof(xfs_attr_leaf_name_local_t) = 4 sizeof(xfs_attr_leaf_name_remote_t) = 12 i.e. no change because the structures aren't defined as packed structures. Hence the compiler pads them to out to 4 byte alignment naturally regardless of the flex array definition. pahole on x86-64 also confirms that the (padded) size of the structure is not changed. However, the on-disk structure it is being used to decode is packed, and we're only using pointer arithmetic to pull the location of the name/value pairs out of the buffer to copy them - it's the structure size calculations that actually define the size of the structures for a given name length, not the sizeof() value or the flex array definitions... > This helps with the ongoing efforts to tighten the FORTIFY_SOURCE > routines on memcpy() and help us make progress towards globally > enabling -fstrict-flex-arrays=3 [1]. > > Link: https://github.com/KSPP/linux/issues/79 > Link: https://github.com/KSPP/linux/issues/251 > Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1] > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > --- > fs/xfs/libxfs/xfs_da_format.h | 8 ++++---- > 1 file changed, 4 insertions(+), 4 deletions(-) > > diff --git a/fs/xfs/libxfs/xfs_da_format.h b/fs/xfs/libxfs/xfs_da_format.h > index 25e2841084e1..e1e62ebb0c44 100644 > --- a/fs/xfs/libxfs/xfs_da_format.h > +++ b/fs/xfs/libxfs/xfs_da_format.h > @@ -620,14 +620,14 @@ typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */ > typedef struct xfs_attr_leaf_name_local { > __be16 valuelen; /* number of bytes in value */ > __u8 namelen; /* length of name bytes */ > - __u8 nameval[1]; /* name/value bytes */ > + __u8 nameval[]; /* name/value bytes */ > } xfs_attr_leaf_name_local_t; > > typedef struct xfs_attr_leaf_name_remote { > __be32 valueblk; /* block number of value bytes */ > __be32 valuelen; /* number of bytes in value */ > __u8 namelen; /* length of name bytes */ > - __u8 name[1]; /* name bytes */ > + __u8 name[]; /* name bytes */ > } xfs_attr_leaf_name_remote_t; > > typedef struct xfs_attr_leafblock { > @@ -747,13 +747,13 @@ xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx) > */ > static inline int xfs_attr_leaf_entsize_remote(int nlen) > { > - return round_up(sizeof(struct xfs_attr_leaf_name_remote) - 1 + > + return round_up(sizeof(struct xfs_attr_leaf_name_remote) + > nlen, XFS_ATTR_LEAF_NAME_ALIGN); > } To be honest, the actual padding and alignment calculations are kinda whacky because that's the way they were defined back in 1995. And, well, once set in the on-disk format, it can't easily be changed. FYI, here's the original definition from 1995: #define XFS_ATTR_LEAF_ENTSIZE_REMOTE(nlen) /* space for remote struct */ \ (((sizeof(xfs_attr_leaf_name_remote_t)-1 + (nlen)) +3)&~0x3) So apart using round_up and defines instead of magic numbers, the current calculation is unchanged from the original definition. AFAICT, the modification you are proposing above breaks this because the sizeof(xfs_attr_leaf_name_remote) result has not changed with the change of the structure definition. e.g. if namelen = 17, before we had: size = round_up(12 - 1 + 17, 4) = round_up(28, 4) = 28 Which is correct because the on-disk format is packed: 0 4 89 12 20 26 28 +---+---++--+-------+-----+-+-----.... |---------------| 17 bytes of name. |-| 2 bytes of padding |-----.... Next attr record. We end up with 2 bytes of padded between the end of the name and the start of the next attribute record in the block. But after this patch, now we calculate the size as: size = round_up(12 + 17, 4) = round_up(29, 4) = 32 Which is a different result, and would result in incorrect parsing of the attribute records in the buffer. Hence I don't think it is valid to be changing the entsize calculations like this if sizeof() is not changing results. Which comes back to my original question: how did you test this? Cheers, Dave.
On 2/5/23 16:51, Dave Chinner wrote: > On Thu, Feb 02, 2023 at 07:24:50PM -0600, Gustavo A. R. Silva wrote: >> One-element arrays are deprecated, and we are replacing them with flexible >> array members instead. So, replace one-element arrays with flexible-array >> members in structures xfs_attr_leaf_name_local and >> xfs_attr_leaf_name_remote. >> >> The only binary differences reported after the changes are all like >> these: >> >> fs/xfs/libxfs/xfs_attr_leaf.o >> _@@ -435,7 +435,7 @@ >> 3b8: movzbl 0x2(%rbx),%eax >> 3bc: rol $0x8,%bp >> 3c0: movzwl %bp,%ebp >> - 3c3: lea 0x2(%rax,%rbp,1),%ebx >> + 3c3: lea 0x3(%rax,%rbp,1),%ebx >> 3c7: call 3cc <xfs_attr_leaf_entsize+0x8c> >> 3c8: R_X86_64_PLT32 __tsan_func_exit-0x4 >> 3cc: or $0x3,%ebx >> _@@ -454,7 +454,7 @@ >> 3ea: movzbl 0x8(%rbx),%ebx >> 3ee: call 3f3 <xfs_attr_leaf_entsize+0xb3> >> 3ef: R_X86_64_PLT32 __tsan_func_exit-0x4 >> - 3f3: add $0xa,%ebx >> + 3f3: add $0xb,%ebx >> 3f6: or $0x3,%ebx >> 3f9: add $0x1,%ebx >> 3fc: mov %ebx,%eax >> >> similar changes in fs/xfs/scrub/attr.o and fs/xfs/xfs.o object files. > > That seems like a red flag to me - an off-by-one change in the > compiled code that calculates of the on-disk size of a structure as > a result of an in-memory structure change just smells like a bug. Ughh.. You're right. I somehow got confused between the moment I first build-tested this in my build machine and after a final last-minute review I did on the machine from which I ultimately send the patches out. More comments below... > > How did you test this change? > >> And the reason for this is because of the round_up() macro called in >> functions xfs_attr_leaf_entsize_remote() and xfs_attr_leaf_entsize_local(), >> which is compensanting for the one-byte reduction in size (due to the >> flex-array transformation) of structures xfs_attr_leaf_name_remote and >> xfs_attr_leaf_name_local. So, sizes remain the same before and after >> changes. > > I'm not sure that is true. Before this change: Yeah; this in fact was a final last-minute review I did before sending out the patch, and it was when I noticed the round_up() macro was doing something quite idiomatic when it comes to calculating the sizes of structures containing one-element arrays. People usually subtract the sizeof(type-of-one-element) from the sizeof(struct-with-one-element-array) when they perform other calculations. And in this case as the sizeof(type-of-one-element) is one byte, at the moment I thought that subtraction was because of that, and then when I build-tested that final change, I totally forgot about the padding (I had actually noticed it when I modified the structure definitions :/) and now I see I got all confused. > > sizeof(xfs_attr_leaf_name_local_t) = 4 > sizeof(xfs_attr_leaf_name_remote_t) = 12 > > After this change: > > sizeof(xfs_attr_leaf_name_local_t) = 4 > sizeof(xfs_attr_leaf_name_remote_t) = 12 Yes; in fact I noticed that. :/ > > i.e. no change because the structures aren't defined as packed > structures. Hence the compiler pads them to out to 4 byte alignment > naturally regardless of the flex array definition. pahole on x86-64 > also confirms that the (padded) size of the structure is not > changed. Yep; I actually was going to include the pahole output for both structures in the changelog text, but I decided not to do it at the last minute as I didn't see it necessary because, as you pointed out, the sizes before and after the flex-array transformations are the same. > > However, the on-disk structure it is being used to decode is packed, > and we're only using pointer arithmetic to pull the location of the > name/value pairs out of the buffer to copy them - it's the structure > size calculations that actually define the size of the structures > for a given name length, not the sizeof() value or the flex array > definitions... > >> This helps with the ongoing efforts to tighten the FORTIFY_SOURCE >> routines on memcpy() and help us make progress towards globally >> enabling -fstrict-flex-arrays=3 [1]. >> >> Link: https://github.com/KSPP/linux/issues/79 >> Link: https://github.com/KSPP/linux/issues/251 >> Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1] >> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> >> --- >> fs/xfs/libxfs/xfs_da_format.h | 8 ++++---- >> 1 file changed, 4 insertions(+), 4 deletions(-) >> >> diff --git a/fs/xfs/libxfs/xfs_da_format.h b/fs/xfs/libxfs/xfs_da_format.h >> index 25e2841084e1..e1e62ebb0c44 100644 >> --- a/fs/xfs/libxfs/xfs_da_format.h >> +++ b/fs/xfs/libxfs/xfs_da_format.h >> @@ -620,14 +620,14 @@ typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */ >> typedef struct xfs_attr_leaf_name_local { >> __be16 valuelen; /* number of bytes in value */ >> __u8 namelen; /* length of name bytes */ >> - __u8 nameval[1]; /* name/value bytes */ >> + __u8 nameval[]; /* name/value bytes */ >> } xfs_attr_leaf_name_local_t; >> >> typedef struct xfs_attr_leaf_name_remote { >> __be32 valueblk; /* block number of value bytes */ >> __be32 valuelen; /* number of bytes in value */ >> __u8 namelen; /* length of name bytes */ >> - __u8 name[1]; /* name bytes */ >> + __u8 name[]; /* name bytes */ >> } xfs_attr_leaf_name_remote_t; >> >> typedef struct xfs_attr_leafblock { >> @@ -747,13 +747,13 @@ xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx) >> */ >> static inline int xfs_attr_leaf_entsize_remote(int nlen) >> { >> - return round_up(sizeof(struct xfs_attr_leaf_name_remote) - 1 + >> + return round_up(sizeof(struct xfs_attr_leaf_name_remote) + >> nlen, XFS_ATTR_LEAF_NAME_ALIGN); >> } > > To be honest, the actual padding and alignment calculations are > kinda whacky because that's the way they were defined back in 1995. > And, well, once set in the on-disk format, it can't easily be > changed. FYI, here's the original definition from 1995: > > #define XFS_ATTR_LEAF_ENTSIZE_REMOTE(nlen) /* space for remote struct */ \ > (((sizeof(xfs_attr_leaf_name_remote_t)-1 + (nlen)) +3)&~0x3) > > So apart using round_up and defines instead of magic numbers, the > current calculation is unchanged from the original definition. > > AFAICT, the modification you are proposing above breaks this because the > sizeof(xfs_attr_leaf_name_remote) result has not changed with the > change of the structure definition. > > e.g. if namelen = 17, before we had: > > size = round_up(12 - 1 + 17, 4) > = round_up(28, 4) > = 28 > > Which is correct because the on-disk format is packed: > > 0 4 89 12 20 26 28 > +---+---++--+-------+-----+-+-----.... > |---------------| 17 bytes of name. > |-| 2 bytes of padding > |-----.... Next attr record. > > We end up with 2 bytes of padded between the end of the name and the > start of the next attribute record in the block. > > But after this patch, now we calculate the size as: > > size = round_up(12 + 17, 4) > = round_up(29, 4) > = 32 > > Which is a different result, and would result in incorrect parsing > of the attribute records in the buffer. Hence I don't think it is > valid to be changing the entsize calculations like this if sizeof() > is not changing results. Yep; you're right. > > Which comes back to my original question: how did you test this? I compared the generated object files in fs/xfs/, fs/xfs/scrub/ and fs/xfs/libxfs/ before and after the changes with something like these[1]: ARGS=--disassemble --demangle --reloc --no-show-raw-insn --section=.text for i in $(cd $OUT/xfs/before && echo *.o); do echo $i; diff -u <(objdump $ARGS $OUT/xfs/before/$i | sed "0,/^Disassembly/d") <(objdump $ARGS $OUT/xfs/after/$i | sed "0,/^Disassembly/d"); done where of course the generated object files before the changes are located in OUT/xfs/before/ and the ones after changes in $OUT/xfs/after/ I just double-checked and, indeed, the changes I mentioned in the changelog text only show up when I modify the entsize functions. So, because of the padding, the flex-array transformations don't actually affect the sizes of the involved structures. So, it seems that change is enough and is the correct one. I really appreciate your comments and feedback, Dave. And I'm sorry for the confusion. Thank you! -- Gustavo [1] https://outflux.net/blog/archives/2022/06/24/finding-binary-differences/
On 2/3/23 11:53, Kees Cook wrote: > On Thu, Feb 02, 2023 at 07:24:50PM -0600, Gustavo A. R. Silva wrote: >> One-element arrays are deprecated, and we are replacing them with flexible >> array members instead. So, replace one-element arrays with flexible-array >> members in structures xfs_attr_leaf_name_local and >> xfs_attr_leaf_name_remote. >> >> The only binary differences reported after the changes are all like >> these: >> >> fs/xfs/libxfs/xfs_attr_leaf.o >> _@@ -435,7 +435,7 @@ >> 3b8: movzbl 0x2(%rbx),%eax >> 3bc: rol $0x8,%bp >> 3c0: movzwl %bp,%ebp >> - 3c3: lea 0x2(%rax,%rbp,1),%ebx >> + 3c3: lea 0x3(%rax,%rbp,1),%ebx >> 3c7: call 3cc <xfs_attr_leaf_entsize+0x8c> >> 3c8: R_X86_64_PLT32 __tsan_func_exit-0x4 >> 3cc: or $0x3,%ebx >> _@@ -454,7 +454,7 @@ >> 3ea: movzbl 0x8(%rbx),%ebx >> 3ee: call 3f3 <xfs_attr_leaf_entsize+0xb3> >> 3ef: R_X86_64_PLT32 __tsan_func_exit-0x4 >> - 3f3: add $0xa,%ebx >> + 3f3: add $0xb,%ebx >> 3f6: or $0x3,%ebx >> 3f9: add $0x1,%ebx >> 3fc: mov %ebx,%eax >> >> similar changes in fs/xfs/scrub/attr.o and fs/xfs/xfs.o object files. > > I usually turn off the sanitizers for the A/B build comparisons to make Oh yes! that's a good point. I'll see that they are turned off next time. :) > it easier to read the results. It looks like it _grew_ in size here, > though? Yep; I'm sorry I got it wrong. :/ I had it right in the beginning, then after reading the code once again just before sending out a version of this patch with only the flex-array transformations, I noticed the entsize functions and the "sizeof(struct-with-one-element-array) - 1" and I forgot about the padding, removed the "- 1" and got a bit confused with my build-tests. I'll send v2 with my original changes... the flex-array transformations, only. -- Gustavo
diff --git a/fs/xfs/libxfs/xfs_da_format.h b/fs/xfs/libxfs/xfs_da_format.h index 25e2841084e1..e1e62ebb0c44 100644 --- a/fs/xfs/libxfs/xfs_da_format.h +++ b/fs/xfs/libxfs/xfs_da_format.h @@ -620,14 +620,14 @@ typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */ typedef struct xfs_attr_leaf_name_local { __be16 valuelen; /* number of bytes in value */ __u8 namelen; /* length of name bytes */ - __u8 nameval[1]; /* name/value bytes */ + __u8 nameval[]; /* name/value bytes */ } xfs_attr_leaf_name_local_t; typedef struct xfs_attr_leaf_name_remote { __be32 valueblk; /* block number of value bytes */ __be32 valuelen; /* number of bytes in value */ __u8 namelen; /* length of name bytes */ - __u8 name[1]; /* name bytes */ + __u8 name[]; /* name bytes */ } xfs_attr_leaf_name_remote_t; typedef struct xfs_attr_leafblock { @@ -747,13 +747,13 @@ xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx) */ static inline int xfs_attr_leaf_entsize_remote(int nlen) { - return round_up(sizeof(struct xfs_attr_leaf_name_remote) - 1 + + return round_up(sizeof(struct xfs_attr_leaf_name_remote) + nlen, XFS_ATTR_LEAF_NAME_ALIGN); } static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen) { - return round_up(sizeof(struct xfs_attr_leaf_name_local) - 1 + + return round_up(sizeof(struct xfs_attr_leaf_name_local) + nlen + vlen, XFS_ATTR_LEAF_NAME_ALIGN); }