Message ID | Y5mQTOqFOGbJMerV@mail.google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp100435wrn; Wed, 14 Dec 2022 01:13:51 -0800 (PST) X-Google-Smtp-Source: AA0mqf54WE4P6cfsLe5ww3qKamt3DOwFte+iki2V2ZcHxzPKYEy8rw9EfKL50eYSbTGoA/ArYLD8 X-Received: by 2002:a05:6a20:102a:b0:a4:7077:5a01 with SMTP id a42-20020a056a20102a00b000a470775a01mr38129565pzd.13.1671009230759; Wed, 14 Dec 2022 01:13:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671009230; cv=none; d=google.com; s=arc-20160816; b=RgZch3JmpLgI3ixBqQ6VMSiBiZbEdHKq9sjQidC7bGsSHKg5ux3awhNTYhp8w4LYkB FjIJvK+7Qwzsja/4KHvvmNIyn556jAoGOHwM3kYeghfK4vmnSNuFb9sUkU1Yg9sxGtAJ 8KYf2CY/kEq6rq1EqG1/9gOe2/MYqLhRwWY1kW62uCZnL7hpAue5p7c+guUAVsSlD4Y8 JzK/6vr5wZuPgeCT1nqOdxrJfdZDs6cnoMFgtKNUvDewaTrhVRzZXqmiImUZvMrIRsVG OLugjkUgyhCzShsz8TPLEBGFUr8v350vZdHn7/d0ikOuKC8dZJ+ixilefiCsJiTweB9x F52A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature; bh=QJpPmTe/XBClfxdnG0INFkwd4DEdd16k8DnX9kH12Oo=; b=rJJQljF9Sx+C5391CKcukXHUdB6A/TAIqiY3VuyWgUwAd2GAkjYDBZ7wnYlugYWPIj KzV1fubdgnBB2N+cyK6h9XPb0PHGNImqRbyJxfXIOiMDGBb3Ykcu6GGBP9kcaPBzjbdy HZbJtN62ajpopa/yVBDwKaknYXs3DxmH+VRqwrh4hGXtIc9QgvooBRktxPLT8mCg6XNE iYuTR7HSSmijmBjzZIJwt2x2s//QmPQohlqooiTlcsLpAWfrAL6Vr4Ksdw28p7Qiv4WX adqWdf/a8YOiOaoYGbihCq9oDquiM5NLlxcVajAcVtbBs9QG12MEXdnR5nqWudWW3DHU ZI8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=nDsigzCB; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v23-20020a63b957000000b00473c5330abbsi15364431pgo.158.2022.12.14.01.13.36; Wed, 14 Dec 2022 01:13:50 -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=@gmail.com header.s=20210112 header.b=nDsigzCB; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237860AbiLNI7E (ORCPT <rfc822;jeantsuru.cumc.mandola@gmail.com> + 99 others); Wed, 14 Dec 2022 03:59:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236910AbiLNI7B (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 14 Dec 2022 03:59:01 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EDED1FCC4; Wed, 14 Dec 2022 00:58:59 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id fa4-20020a17090af0c400b002198d1328a0so3541316pjb.0; Wed, 14 Dec 2022 00:58:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=QJpPmTe/XBClfxdnG0INFkwd4DEdd16k8DnX9kH12Oo=; b=nDsigzCB1W7unRbCm/62QVdANiKye41IPr668xqAA/mAmbC5qg+yVVhU3Rq9BCSfr8 vZ1Kljwnw7W0Saf6246IeTK3MFXLQtrk67qxycMVU1luzfjhPkC39F6FRskX2IxhdfMJ 4QYcjiQ91bNvBbafQwqk2dsjV0CVjZd3HR9JYjz0YdrUe7hEvTAyiujPZxJSlHKscixR tpD5uRf1c7/GidwqXdFBUx5IS7HRpe6IVxSD+2qg4k7Bd0n2I+CLOwzbIOA+Qn+b07Kd DXTa/HFQtMAb0wDq9cUclcQOSzTPxAlj6tcZSEj5IhTO6gNZngH0Vszu64SfpxesjDSm TsOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QJpPmTe/XBClfxdnG0INFkwd4DEdd16k8DnX9kH12Oo=; b=4T2pId9gMb1sdUmeWrHLGokL9524SalcGyBqJXCr+pckmhWpgbEJhcQdXoV2DvKeKX rFW3eg8JYJBhXFCTMcaoqSoj/Hx9DPFHvplwyH1SRDyS8VhCv8hFxQF50ZIPuURJcGDs lNM2r3hqIqe+GiI9nNS/ogBfsc6vbiIHY7Z7KneVKI+6uY267JFespddfRz189HrhhQM kT1Bvr9LALsZrub+FTli4kI2EjvIWepMvPHIOD3RvIf7tHhirXu5OPIiJq7zK0MT1rk9 sfQLTd7B0/vhdFZF0CJeqICrmUN9vqyN2sU/fhUXj2fdIM/swkcjq5jngNIrW39smAZh Aj+g== X-Gm-Message-State: ANoB5pkzxdcUYhSgWHJllD6WSOKoMI8iAmOAutJyl/e8nk/2oLVp4lLa 6lDIoEHbL8T/Xv1qNVoBcZ0= X-Received: by 2002:a17:902:6b08:b0:186:639f:6338 with SMTP id o8-20020a1709026b0800b00186639f6338mr5984814plk.6.1671008338906; Wed, 14 Dec 2022 00:58:58 -0800 (PST) Received: from mail.google.com (125-237-37-88-fibre.sparkbb.co.nz. [125.237.37.88]) by smtp.gmail.com with ESMTPSA id s15-20020a170902b18f00b00189393ab02csm1327487plr.99.2022.12.14.00.58.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 00:58:58 -0800 (PST) Date: Wed, 14 Dec 2022 21:58:52 +1300 From: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> To: Arnd Bergmann <arnd@arndb.de>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Ilpo =?utf-8?b?SsOkcnZp?= =?utf-8?b?bmVu?= <ilpo.jarvinen@linux.intel.com>, Jiri Slaby <jirislaby@kernel.org>, Haowen Bai <baihaowen@meizu.com>, Andy Shevchenko <andy.shevchenko@gmail.com> Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, paulo.miguel.almeida.rodenas@gmail.com Subject: [PATCH v2] [next] pcmcia: synclink_cs: replace 1-element array with flex-array member Message-ID: <Y5mQTOqFOGbJMerV@mail.google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <Y5mMWEtHWKOiPVU+@mail.google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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?1752178628281291331?= X-GMAIL-MSGID: =?utf-8?q?1752180175099132615?= |
Series |
[v2,next] pcmcia: synclink_cs: replace 1-element array with flex-array member
|
|
Commit Message
Paulo Miguel Almeida
Dec. 14, 2022, 8:58 a.m. UTC
One-element arrays are deprecated, and we are replacing them with
flexible array members instead. So, replace one-element array with
flexible-array member in struct RXBUF. No changes were required
within the source code because of the existing padding in RXBUF struct
It's worth mentioning that doing a build before/after this patch
results in no binary output differences.
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://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 [1]
Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com>
---
Changelog:
- v2: removed changes to how the size of RXBUF was calculated. I
changed my mind after thinking about the existing padding in the
struct. Happy to discuss it if anyone sees it differently.
- v1: https://lore.kernel.org/lkml/Y5mMWEtHWKOiPVU+@mail.google.com/
---
drivers/char/pcmcia/synclink_cs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Wed, Dec 14, 2022 at 10:58 AM Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> wrote: > > One-element arrays are deprecated, and we are replacing them with > flexible array members instead. So, replace one-element array with > flexible-array member in struct RXBUF. No changes were required > within the source code because of the existing padding in RXBUF struct You shouldn't rely on padding. Make you change robust independently on the padding. See also below. > It's worth mentioning that doing a build before/after this patch > results in no binary output differences. This is interesting... > 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://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 [1] > The blank lines are not allowed in the tag block (in case you want to have Link: to be recognized as a tag). > Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> > --- > Changelog: > > - v2: removed changes to how the size of RXBUF was calculated. I > changed my mind after thinking about the existing padding in the > struct. Happy to discuss it if anyone sees it differently. I feel worried about in particular this code: /* each buffer has header and data */ info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; which means that entire rx_alloc_buffers() should be revisited. Also take into account the use of one or more macros from overflow.h for memory allocation.
On Wed, Dec 14, 2022 at 12:43:48PM +0200, Andy Shevchenko wrote: > On Wed, Dec 14, 2022 at 10:58 AM Paulo Miguel Almeida > <paulo.miguel.almeida.rodenas@gmail.com> wrote: > > > > One-element arrays are deprecated, and we are replacing them with > > flexible array members instead. So, replace one-element array with > > flexible-array member in struct RXBUF. No changes were required > > within the source code because of the existing padding in RXBUF struct > > You shouldn't rely on padding. Make you change robust independently on > the padding. See also below. > > > It's worth mentioning that doing a build before/after this patch > > results in no binary output differences. > > This is interesting... > > > 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://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 [1] > > > > > The blank lines are not allowed in the tag block (in case you want to > have Link: to be recognized as a tag). > > > Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> > > --- > > Changelog: > > > > - v2: removed changes to how the size of RXBUF was calculated. I > > changed my mind after thinking about the existing padding in the > > struct. Happy to discuss it if anyone sees it differently. > > I feel worried about in particular this code: > > /* each buffer has header and data */ > info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > which means that entire rx_alloc_buffers() should be revisited. Also > take into account the use of one or more macros from overflow.h for > memory allocation. > > -- > With Best Regards, > Andy Shevchenko Hi Kees, Hi Andy, Thanks for taking the time to review this patch. As both of you had similar points, I will reply them here. The reasons why it had no binary changes was because of the combination of this 2 things: 1) Existing padding - so sizeof(RXBUF) returned 8 bytes in both cases. pahole -C RXBUF gcc/before/drivers/char/pcmcia/synclink_cs.ko typedef struct { int count; /* 0 4 */ unsigned char status; /* 4 1 */ char data[1]; /* 5 1 */ /* size: 8, cachelines: 1, members: 3 */ /* padding: 2 */ /* last cacheline: 8 bytes */ } RXBUF; pahole -C RXBUF gcc/after/drivers/char/pcmcia/synclink_cs.ko typedef struct { int count; /* 0 4 */ unsigned char status; /* 4 1 */ char data[]; /* 5 0 */ /* size: 8, cachelines: 1, members: 3 */ /* padding: 3 */ /* last cacheline: 8 bytes */ } RXBUF; 2) RXBUF (as implemented now) is just like a pair of lenses from which a developer can have access to one of the circular buffers in MGSLPC_INFO struct called 'rx_buf'. 2611 static int rx_alloc_buffers(MGSLPC_INFO *info) 2612 { 2613 /* each buffer has header and data */ 2614 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; 2615 2616 /* calculate total allocation size for 8 buffers */ 2617 info->rx_buf_total_size = info->rx_buf_size * 8; 2618 2619 /* limit total allocated memory */ 2620 if (info->rx_buf_total_size > 0x10000) 2621 info->rx_buf_total_size = 0x10000; 2622 2623 /* calculate number of buffers */ 2624 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size; 2625 2626 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL); To be honest, char data[_1_] in RXBUF was never required to be there. The code base seems to make sure that it doesn't run past its limits by keeping track of size buffer on MGSLPC_INFO->rx_buf_size (and sometimes RXBUF->count) (Addressing one point made by Andy about using of of the macros in overflow.h) struct_size(buf, data, 1) would return 9 bytes which could potentially break the existing driver as it produces binary changes. Let me know your thoughts thanks! - Paulo A.
diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c index b2735be81ab2..0b03c6d13d59 100644 --- a/drivers/char/pcmcia/synclink_cs.c +++ b/drivers/char/pcmcia/synclink_cs.c @@ -105,7 +105,7 @@ static MGSL_PARAMS default_params = { typedef struct { int count; unsigned char status; - char data[1]; + char data[]; } RXBUF; /* The queue of BH actions to be performed */