Message ID | Y5mMWEtHWKOiPVU+@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 c7csp90634wrn; Wed, 14 Dec 2022 00:49:15 -0800 (PST) X-Google-Smtp-Source: AA0mqf5jMsQwnEOq/rVKQF/L3Nx5lvevlnBb0fQH3lsF/ZBJPC5zBke6zLktTuGmrPcJPmiJEt7S X-Received: by 2002:a05:6a00:a93:b0:56b:6c05:3da6 with SMTP id b19-20020a056a000a9300b0056b6c053da6mr30439374pfl.8.1671007755441; Wed, 14 Dec 2022 00:49:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671007755; cv=none; d=google.com; s=arc-20160816; b=puaL49v47v0S5WPoZM4KYUiM6N9y3RkVRqtzDA8XtYVLSDd0PG5Y9BCGnzMx6GI5TF 5I77oOqumi6qVuejaNecHRo9Pyx3aStjJJa0+pxHPpyHQjLdzKY4TC7tGWwClyYlHgon XVvvZY9xs79pXQN1o4crp9HZQLBLC8pzFW19LJgtP6wUgDp0yw/SOOBl7zn39pMabOXU FndE5s8fm8CTOUKt+AALpNcaBafPskRBhR4syCBwszfSDVEHHH+M+argyZwx/IeOGvCr /2XkqOHc5S0VxcOd8JS7Kh03Kg1WqOSxCxYGQoZdAttGSqk4CuUGBd8fzIh+HQVbBfX6 tbIA== 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=mKIXxnGp5Qeh1H1kOjUA7WrQEgWAzUUtfWKKqj9aC5A=; b=XfErqkBxBCZCijCF+5C5Sjoc8LZ3BefosG2DDN2LVOjNCwSTHze9dLFcCRx9PsNLnA BkNeVEMGYHxcKrFdI9VH2wDbieXi1Wlxx1moQwtp7jpDn8JexCIpARTdKCXD52+yDLJV OYWIZHsDRE2sgBd2AzhewAD/3IUkNHFFEFVJOSq98dam/8s0Ttv0Rxb/Xa1xKPMwpvR4 4E1rWsn7OECLI88+safKHLWScFZrrRP5tKrhuAJy/s6H47sSoPnRuv1iLT8mMOfDAars f1Y3d2y04iEX4vVi24URWZajhwyZs+Ka39kxfcusF3+ii7akI9dURQMCl07z6YlfdFl4 AwrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="RUKS8+/p"; 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 190-20020a6217c7000000b00535fa14ffaasi14619710pfx.116.2022.12.14.00.49.01; Wed, 14 Dec 2022 00:49:15 -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="RUKS8+/p"; 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 S237760AbiLNImL (ORCPT <rfc822;jeantsuru.cumc.mandola@gmail.com> + 99 others); Wed, 14 Dec 2022 03:42:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237356AbiLNImI (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 14 Dec 2022 03:42:08 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A58116580; Wed, 14 Dec 2022 00:42:08 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id u15-20020a17090a3fcf00b002191825cf02so6339794pjm.2; Wed, 14 Dec 2022 00:42:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=mKIXxnGp5Qeh1H1kOjUA7WrQEgWAzUUtfWKKqj9aC5A=; b=RUKS8+/pkEoTD8k0JJKHv8nza/xwr2KJsRoJ713qLCRtM5TldRtS0CrAdQWHN15/T5 7ccfexgetApOumRV8EN13507YFLVeL2s8O9PjsVi2TS8qI8exUMro2LR/A2QzL4n8oii XEsXw+zgiiX54cwxIn7/n9O9xOs3pWxw4G38lW2bV4H3w/ymJ1MdIWUdS2IVvObxEjjy RLbqgR0Uj30QoSQH0A8y75S29JO6e1cXEpjsFy8Jh3rhsqqC2dp34FcaWoMMrxLTKAUz +qjdg3exntCjFNlOAIWjZQr40KeOkJ6I6RNgEtcmkcyPxzouYG8ydU1G8gz0wb9Zox+b kOzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=mKIXxnGp5Qeh1H1kOjUA7WrQEgWAzUUtfWKKqj9aC5A=; b=lDn9mgjptMrHFIRf9xsLH5iU+KsTPdFoJ+6UkrPmasY1qW1JDQVjsVMjjy0lZkjD6F K/np1i7CUIYmLpJv2YQpcF5bQ2jYIWftKDfLhQdAlSYaRW4WgN1PfncD86SXpgr04bNz m79T1XfiNDn13c7GhmPuUP6gO7YKnn99JXer+MPndZxo/CzQhdjK+6jQBluREiojtQyX TEq+yZCWiD00xGIT5wC+XHokwWArsGG4XamHhjsrutQ/xR+JvcbeJtP28rov1tDczdf2 HQf5vhKc7M0OdbxB/4KZDE4qr9ITcOfGQN4YbmdllpPDdDxbsqjWneTcIvNOYBekT/65 aMwg== X-Gm-Message-State: ANoB5pl8qr5EHeyniwib+nDjMJtMwrW3W1MZEy2GIt0bj4heUG0C7XJp j6yIQ7y9WmDCTzIPyCd8Xug= X-Received: by 2002:a17:902:7fcb:b0:18b:cea3:645 with SMTP id t11-20020a1709027fcb00b0018bcea30645mr5353251plb.0.1671007327631; Wed, 14 Dec 2022 00:42:07 -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 d9-20020a170903230900b00189988a1a9esm1278650plh.135.2022.12.14.00.42.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 00:42:07 -0800 (PST) Date: Wed, 14 Dec 2022 21:42:00 +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] [next] pcmcia: synclink_cs: replace 1-element array with flex-array member Message-ID: <Y5mMWEtHWKOiPVU+@mail.google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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?1752178628281291331?= |
Series |
[next] pcmcia: synclink_cs: replace 1-element array with flex-array member
|
|
Commit Message
Paulo Miguel Almeida
Dec. 14, 2022, 8:42 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 and refactor the rest of the code
accordingly.
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>
---
drivers/char/pcmcia/synclink_cs.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
Comments
On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida 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 and refactor the rest of the code > accordingly. > > 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> > --- > drivers/char/pcmcia/synclink_cs.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c > index b2735be81ab2..1ab2d552f498 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 */ > @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) > static int rx_alloc_buffers(MGSLPC_INFO *info) > { > /* each buffer has header and data */ > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) > + + info->max_frame_size; It seems like there is an existing size bug here, and likely should be fixed separately? i.e. this was already allocating 1 byte "too much". I'd expect this first: - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; and then the next patch: - char data[1]; + char data[]; ... - info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; + info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; The above would induce a binary output change, and the second would not. Though this results in what you had for the v2 patch (but I can't believe it had no binary changes...)
On Wed, Dec 14, 2022 at 11:29:37AM -0800, Kees Cook wrote: > On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida 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 and refactor the rest of the code > > accordingly. > > > > 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> > > --- > > drivers/char/pcmcia/synclink_cs.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c > > index b2735be81ab2..1ab2d552f498 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 */ > > @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) > > static int rx_alloc_buffers(MGSLPC_INFO *info) > > { > > /* each buffer has header and data */ > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) > > + + info->max_frame_size; > > It seems like there is an existing size bug here, and likely should be > fixed separately? > > i.e. this was already allocating 1 byte "too much". I'd expect this > first: > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > and then the next patch: > > - char data[1]; > + char data[]; > ... > - info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > The above would induce a binary output change, and the second would not. > > Though this results in what you had for the v2 patch (but I can't > believe it had no binary changes...) > > -- > Kees Cook 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.
On Wed, Dec 14, 2022 at 11:29:37AM -0800, Kees Cook wrote: > On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida 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 and refactor the rest of the code > > accordingly. > > > > 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> > > --- > > drivers/char/pcmcia/synclink_cs.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c > > index b2735be81ab2..1ab2d552f498 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 */ > > @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) > > static int rx_alloc_buffers(MGSLPC_INFO *info) > > { > > /* each buffer has header and data */ > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) > > + + info->max_frame_size; > > It seems like there is an existing size bug here, and likely should be > fixed separately? > > i.e. this was already allocating 1 byte "too much". I'd expect this > first: > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > and then the next patch: > > - char data[1]; > + char data[]; > ... > - info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > The above would induce a binary output change, and the second would not. > > Though this results in what you had for the v2 patch (but I can't > believe it had no binary changes...) > > -- > Kees Cook Just realised that you made a comment on PATCH v1 and Andy made a comment on PATCH v2. Please conside my answer for PATCH v2 as I have abandoned the v1. Apologies for the confusion. thanks! - Paulo A.
On Thu, Dec 15, 2022 at 09:09:46AM +1300, Paulo Miguel Almeida wrote: > On Wed, Dec 14, 2022 at 11:29:37AM -0800, Kees Cook wrote: > > On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida 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 and refactor the rest of the code > > > accordingly. > > > > > > 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> > > > --- > > > drivers/char/pcmcia/synclink_cs.c | 5 +++-- > > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c > > > index b2735be81ab2..1ab2d552f498 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 */ > > > @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) > > > static int rx_alloc_buffers(MGSLPC_INFO *info) > > > { > > > /* each buffer has header and data */ > > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > > + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) > > > + + info->max_frame_size; > > > > It seems like there is an existing size bug here, and likely should be > > fixed separately? > > > > i.e. this was already allocating 1 byte "too much". I'd expect this > > first: > > > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > > > and then the next patch: > > > > - char data[1]; > > + char data[]; > > ... > > - info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > + info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > > > The above would induce a binary output change, and the second would not. > > > > Though this results in what you had for the v2 patch (but I can't > > believe it had no binary changes...) > > > > -- > > Kees Cook > > 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; Ah-ha, now I see. > > 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. Yeah, I think your v2 is fine. Perhaps explicitly repeat the notes about struct size padding in a v3 commit log?
On Wed, Dec 14, 2022 at 10:09 PM Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> wrote: > On Wed, Dec 14, 2022 at 11:29:37AM -0800, Kees Cook wrote: > > On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida 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 and refactor the rest of the code > > > accordingly. > > > > > > 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]. ... > > > typedef struct { > > > int count; > > > unsigned char status; > > > - char data[1]; > > > + char data[]; > > > } RXBUF; ... > 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; Yes, and Try to make it work with __packed. As I said, the problem is that the code is relying on something which is architecture dependent strictly speaking. And hence I disagree with Kees that v2 is okay to go. > 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. You got it incorrectly. I believe you should use something different than 1. In previous lines in the function it multiplies sizeof + max_frame_size by 8. The full change should be something like check_add(sizeof(), max_frame_size) kcalloc(8, size) Think about it. > Let me know your thoughts
On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > Yes, and Try to make it work with __packed. As I said, the problem is > that the code is relying on something which is architecture dependent > strictly speaking. And hence I disagree with Kees that v2 is okay to > go. I meant that v2 is functionally identical to the existing code. > The full change should be something like > > check_add(sizeof(), max_frame_size) > kcalloc(8, size) Right -- this would fix the existing mistakes in size calculation (and is certainly better).
On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > > Yes, and Try to make it work with __packed. As I said, the problem is > > that the code is relying on something which is architecture dependent > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > go. > > I meant that v2 is functionally identical to the existing code. Ah, sorry for misunderstanding. > > The full change should be something like > > > > check_add(sizeof(), max_frame_size) > > kcalloc(8, size) > > Right -- this would fix the existing mistakes in size calculation (and > is certainly better). Glad to hear that we are on the same page.
On Thu, Dec 15, 2022 at 12:06:46AM +0200, Andy Shevchenko wrote: > On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > > > Yes, and Try to make it work with __packed. As I said, the problem is > > > that the code is relying on something which is architecture dependent > > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > > go. > > > > I meant that v2 is functionally identical to the existing code. > > Ah, sorry for misunderstanding. > I agree with using __packed attribute to remove the extra padding (and for the reasons you mentioned before). That would reduce the sizeof(RXBUF) from 8 to 5 (which is good) but that is still 1 byte "too much". Piggying back on a suggestion Kees gave before: - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; That way RXBUF->data will point to the first byte of the frame_size (MGSLPC_INFO->max_frame_size) which is what is actually needed. > > > The full change should be something like > > > > > > check_add(sizeof(), max_frame_size) > > > kcalloc(8, size) > > > > Right -- this would fix the existing mistakes in size calculation (and > > is certainly better). > > Glad to hear that we are on the same page. > That makes sense to me. thanks! - Paulo A.
On Thu, Dec 15, 2022 at 05:29:15PM +1300, Paulo Miguel Almeida wrote: > On Thu, Dec 15, 2022 at 12:06:46AM +0200, Andy Shevchenko wrote: > > On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > > > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > > > > Yes, and Try to make it work with __packed. As I said, the problem is > > > > that the code is relying on something which is architecture dependent > > > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > > > go. > > > > > > I meant that v2 is functionally identical to the existing code. > > > > Ah, sorry for misunderstanding. > > > > I agree with using __packed attribute to remove the extra padding (and > for the reasons you mentioned before). That would reduce the sizeof(RXBUF) > from 8 to 5 (which is good) but that is still 1 byte "too much". > > Piggying back on a suggestion Kees gave before: > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > That way RXBUF->data will point to the first byte of the frame_size > (MGSLPC_INFO->max_frame_size) which is what is actually needed. > I chose my words poorly here... sorry my brain is a bit fried today. Let me rephrase that last sentence. After that change (or similar change), RXBUF->data will point to the first byte of the buffer allocated during the initialisation process. (which is limited/controlled by the size of MGSLPC_INFO->max_frame_size)... so no 'extra byte/padding' will be there. - Paulo A. > > > > The full change should be something like > > > > > > > > check_add(sizeof(), max_frame_size) > > > > kcalloc(8, size) > > > > > > Right -- this would fix the existing mistakes in size calculation (and > > > is certainly better). > > > > Glad to hear that we are on the same page. > > > > That makes sense to me. > > thanks! > > - Paulo A.
On Thu, Dec 15, 2022 at 6:29 AM Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> wrote: > On Thu, Dec 15, 2022 at 12:06:46AM +0200, Andy Shevchenko wrote: > > On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > > > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: ... > > > > Yes, and Try to make it work with __packed. As I said, the problem is > > > > that the code is relying on something which is architecture dependent > > > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > > > go. > > > > > > I meant that v2 is functionally identical to the existing code. > > > > Ah, sorry for misunderstanding. > > I agree with using __packed attribute to remove the extra padding (and > for the reasons you mentioned before). That would reduce the sizeof(RXBUF) > from 8 to 5 (which is good) but that is still 1 byte "too much". What I meant with the above is that the code has to work properly with or without __packed. It's just to show you that this code has flaws if it relies on the padding.
On Thu, Dec 15, 2022 at 10:57:57AM +0200, Andy Shevchenko wrote: > On Thu, Dec 15, 2022 at 6:29 AM Paulo Miguel Almeida > <paulo.miguel.almeida.rodenas@gmail.com> wrote: > > On Thu, Dec 15, 2022 at 12:06:46AM +0200, Andy Shevchenko wrote: > > > On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > > > > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > > ... > > > > > > Yes, and Try to make it work with __packed. As I said, the problem is > > > > > that the code is relying on something which is architecture dependent > > > > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > > > > go. > > > > > > > > I meant that v2 is functionally identical to the existing code. > > > > > > Ah, sorry for misunderstanding. > > > > I agree with using __packed attribute to remove the extra padding (and > > for the reasons you mentioned before). That would reduce the sizeof(RXBUF) > > from 8 to 5 (which is good) but that is still 1 byte "too much". > > What I meant with the above is that the code has to work properly with > or without __packed. It's just to show you that this code has flaws if > it relies on the padding. > Right - that would work just as well. I will work on v3 with the suggestions given by you (sizing calculation amendments using overflow.h macros) and kees (adding the notes regarding the padding) then. - Paulo A.
diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c index b2735be81ab2..1ab2d552f498 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 */ @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) static int rx_alloc_buffers(MGSLPC_INFO *info) { /* each buffer has header and data */ - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) + + info->max_frame_size; /* calculate total allocation size for 8 buffers */ info->rx_buf_total_size = info->rx_buf_size * 8;