Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1644589rwb; Wed, 14 Dec 2022 12:54:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf6lv3/9WDX03lsx3sV5NvXIN4c+KXoUnQrrzDI8WMBLLz1IL6fCDayJsPY6u1OAoMoUtYlf X-Received: by 2002:a17:906:3b8a:b0:7c0:eb33:892a with SMTP id u10-20020a1709063b8a00b007c0eb33892amr21608232ejf.77.1671051273622; Wed, 14 Dec 2022 12:54:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671051273; cv=none; d=google.com; s=arc-20160816; b=FP04zyotio4lVFix8yvZrfJAPv25i00i+ztG0o13T4ZR/yN9n6x1X5MfhsSn/l+gdc oUnAwuCA8O4yAsS0qKDY+SR7ghMA+e7wIOzlKcOH+s4o+fhCZx2+8E89SXM9TFJteBAn kNnBjbbC7yNAumvKhUffqGjQapFL7RCo0R/3ZoT+oUl13usR78/dSLym7e0MTZKOGqxM LK7pNBEVefOVhaWCoJWuz8K5OCaEUXyEJpD+sT1gAysptIu540lAR6/+HkrVZ8gpuM+6 bUjUebqrgSwU/3Vc3xYr5SpPkI5tnIVO+iX3NFkF+/xuL2N9PeBriDO+VMhIB2WrXIGS FtRg== 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 :references:message-id:subject:cc:to:from:date:dkim-signature; bh=TCMuwp6GHgx/gJjEvxb/mxRR0z43kVs2zMyGQJ7ZUao=; b=zpdoNqAoJ0fbss3V3fCtGXwor2KVeJloERLSao9P74cVIsAxq6BFL2EK8tNsfPxVFo zF0ToQG4IwLHDC6FpQ7pAug/IzZUI8b0df25OkuVYaO0PUXrCBymP2XyUcuW/e723s0C S3RW1YYpvH+ot7smZSQ2ywoPBtoHLMy+HYkLUFVPRtS8RxfwaMK9H1zZgT/KDga4+Sdk cGSlVAqibQOar0znclx8cx78gC63MNDNb8EN7ObHBNmxhm12OJg6ijllZ+1Na91IDQ7n Y63ujkPAkMNmJvHvzinmdmF9INanwrl3jGiq8jJ1i6xSh69NTgPuPuC8iddIyIc2h4bf 9Kbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AQLztXK1; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa41-20020a17090786a900b007c19a5b7697si3902988ejc.233.2022.12.14.12.54.14; Wed, 14 Dec 2022 12:54:33 -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=AQLztXK1; 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 S230209AbiLNUb7 (ORCPT + 70 others); Wed, 14 Dec 2022 15:31:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230211AbiLNUb0 (ORCPT ); Wed, 14 Dec 2022 15:31:26 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 164FD53ED6; Wed, 14 Dec 2022 12:20:05 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id js9so8188101pjb.2; Wed, 14 Dec 2022 12:20:05 -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:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=TCMuwp6GHgx/gJjEvxb/mxRR0z43kVs2zMyGQJ7ZUao=; b=AQLztXK1M1qpPDgvPdbon4vR2HPGNB1ixFvO2/yz7mYIcNQUkP4VB8nDMKJ1MDUHny KRfaA1kWY1m9vCttGMDGCvr987BlUkF7z5vsZVOEhUNE/ipFMbjCWOeJevCM8BRSlvnF /uoCeMieK2qUfkQe/cf9pKw7B7dbtCIscRX3KfW8tpIwJi+j+9Ua3j24IGoWrbNbeZyp lsGbD4P7Mi3Gzw7ng0QCHRFM0FaV2eTk6B1Zgp+pn8Z/F9wcUkh6+Rhh3vhtlOhDYSe4 WJHV9AueDIql8WMy/ete+n9+jA9JhTm20u6JY1Ms5KBbbSLsNAtlmX4Er3ignpT7+EO8 e/oA== 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:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=TCMuwp6GHgx/gJjEvxb/mxRR0z43kVs2zMyGQJ7ZUao=; b=oU3NNl/pXAQMeaEq0+8aO7p0yN+UjloZAwLpnUg4LYUlmP6AOnlfx/+GOhSPbhDL7t aQgsp2fQ9/dZkFz0E1gpDv5oBZkrFqDEF5tEtZ5i5E/rjXodL1eFkLuRf01qUV25If7z pIP2CHaVJDFK8ikPYwTTitcECL4HLTg6iWCtJ1/XYJx5PSCnlHRFtZch4hM7v2KjecQR GvFcf0UYWQEIB98KhZVE4uX/Rmi13HEJ3QDmKEUsbAZyvZ/GckD1FFdxH8R0k2VQlBOf NLBTbiN9GKfrK3PDlZWqf+RRMCELn2JkxZzwnyOxUmzm/bzL0BqQkv8vlH84km2pFUXd Zxiw== X-Gm-Message-State: ANoB5pmQgl7PIWhDfSIgHv/mIWI7VXendu2zSaH6eWIQEUidoQs3Q1UC 0FQ86XCb2gz888Zn1AdHpbE= X-Received: by 2002:a17:902:bf47:b0:189:e904:c831 with SMTP id u7-20020a170902bf4700b00189e904c831mr6529639pls.3.1671049204468; Wed, 14 Dec 2022 12:20:04 -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 x22-20020a170902821600b00189a50d2a38sm2283486pln.38.2022.12.14.12.20.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 12:20:04 -0800 (PST) Date: Thu, 15 Dec 2022 09:19:57 +1300 From: Paulo Miguel Almeida To: Andy Shevchenko Cc: Arnd Bergmann , Greg Kroah-Hartman , Ilpo =?utf-8?B?SsOkcnZpbmVu?= , Jiri Slaby , Haowen Bai , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, paulo.miguel.almeida.rodenas@gmail.com Subject: Re: [PATCH v2] [next] pcmcia: synclink_cs: replace 1-element array with flex-array member Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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: X-Mailing-List: linux-kernel@vger.kernel.org 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 > 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 > > --- > > 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.