Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6112436rdb; Thu, 14 Dec 2023 08:35:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IH5r2BBu2okxW1DeWG+POFHpijBomQb36/XzE/vuZHnGj5ePR2PE3ARq6yfjcTeLoFhl3SO X-Received: by 2002:a17:90a:e017:b0:286:b91b:effb with SMTP id u23-20020a17090ae01700b00286b91beffbmr12310350pjy.30.1702571748787; Thu, 14 Dec 2023 08:35:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702571748; cv=none; d=google.com; s=arc-20160816; b=Oz9/wSjh/UyalxeTj70P7lIWbE3GeEGyy35f9we8AX573zG8sZwv8Lf0aUliSdB+ET 9qAzdd0D/cqc5dxd3xccQg2cGfE/Yp9S3+rz0mkzc0lPaUCObLnbWiQmu2j5wPzO6EX4 WMRVCPOASZWLDMgGn9s5T2gcNtyr13U3fqZ4lQ5WCIGndX9hwvHkXH3b2B6I+7phG49S GVvmd0q3CbGBvrVlYfJUK16RUjisuSQ3QpDj1XUcKvTKVMdcX816WyfJZSHPfmdr7Enb nKjFsF3Av5ktSFD0MkViixukgu/9Amz8cXI74wP99X3tHIL7YUW3RjXAuA915S5BcWvf 96sg== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=luawVzpNb+QxL668ocEaZVPjlwFQcIxmSlMsAYgwWS4=; fh=zAC+IambVEfbj7QuDB3+T+j51eLmztGLyvWTW4QPtyc=; b=I8BwsZOZ07m26TGqNCtndMLKtZ2QkWAs5ml9bkVm7sAe6/CjVdA6MsW6h8l6wKtI5c clNG/wMRKRW+ccVABDG2CBuc9F60dnkFSbRzm8iybeOwlU+MwyG9qiJUzdHiXa1NP/bd ugFtrNJwqNMEmGXaw/jEfWITg1alAimbZ0VIvqMvEm76I8eOVakIZ+4NZ0dSHxGbhNMc I5+IEBhNdYimP6yXT9imWxBt+Bkt/2XfO1oSgpO21Vo7HTOElqzHpss1GARexJvW4svm bYZlORt7w4fTOhm94Ts0lpNDS5GTsKKp8cbgDiWPm5SMg/nHS3Q8iRW9XlicHyZzOqGP g5EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RUWcVQ1Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id x19-20020a1709027c1300b001cff7628fd4si11331708pll.595.2023.12.14.08.35.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 08:35:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RUWcVQ1Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id A8FA58280838; Thu, 14 Dec 2023 08:35:30 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230212AbjLNQfP (ORCPT + 99 others); Thu, 14 Dec 2023 11:35:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229743AbjLNQfN (ORCPT ); Thu, 14 Dec 2023 11:35:13 -0500 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26AFD11D for ; Thu, 14 Dec 2023 08:35:19 -0800 (PST) Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1d075392ff6so6555755ad.1 for ; Thu, 14 Dec 2023 08:35:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1702571718; x=1703176518; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=luawVzpNb+QxL668ocEaZVPjlwFQcIxmSlMsAYgwWS4=; b=RUWcVQ1YHbWs+RsKvjl8kD81OtgOmQQEWEY8Ku88pfAnPQpxkdAGcGz5HjlACheAQD GtcmqQUJpqJWMRNAJacLGcTqsBx4PEUKIsiqspW9WJZSc/o9WCKxT6IS5P/n9M/qgezj FBh/AHGLiKM4OPwAcI81S6Nx6Gru5pdkTUdQA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702571718; x=1703176518; h=in-reply-to:content-transfer-encoding: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=luawVzpNb+QxL668ocEaZVPjlwFQcIxmSlMsAYgwWS4=; b=CixKYW+ODNjAjeWJyP+D2tFIUOAx+efdBHvilZ20dbujB52lvyi/zLymh25CJu8cG4 7zOpInssH2rfrffRgrJWZZeNe5XRBmL8p3a/XzMGCSA5J4f0rUVK+LNDY4Q2vOLwUlmd kibbK2nVrY1ggv262DByRff4Bhmd7ninfZddl4FZOCLflNj4rnxcehGYZe5gl1QXku+e 3AR0pSWtM++3kTKNBCT1G+s1DCXuzk0BnYpy9LL0opvrEcG1xnCJ5vrxw1/YuMglWQD2 pWH4uimMb8RGpUtf/8dAxyw+zjmqWfv4zLTDvg/CcS25fcGhLDEwhfoOV9V2I2c2Rt9H UrIg== X-Gm-Message-State: AOJu0YyTBha7fK4H/3Hka9B7Mo78gMWV5sDsg65VSq7MJt/wskU4YFKm xBbIWjjGYoQSkQtdO52kgkcXiQ== X-Received: by 2002:a17:902:f283:b0:1d3:3676:c9d with SMTP id k3-20020a170902f28300b001d336760c9dmr4808777plc.19.1702571718563; Thu, 14 Dec 2023 08:35:18 -0800 (PST) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id c2-20020a170902d48200b001cfb4d36eb1sm12611665plg.215.2023.12.14.08.35.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 08:35:17 -0800 (PST) Date: Thu, 14 Dec 2023 08:35:17 -0800 From: Kees Cook To: "Gustavo A. R. Silva" Cc: Benson Leung , Guenter Roeck , chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH][next] platform/chrome: Fix -Warray-bounds warnings Message-ID: <202312140830.953C55AC9@keescook> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 14 Dec 2023 08:35:31 -0800 (PST) On Wed, Mar 29, 2023 at 07:54:02PM -0600, Gustavo A. R. Silva wrote: > GCC-13 (and Clang) does not like having a partially allocated object, > since it cannot reason about it for bounds checking. > > Notice that the compiler is legitimately complaining about accessing > an object (params, in this case) for which not enough memory was > allocated. > > The object is of size 20 bytes: > > struct ec_params_vbnvcontext { > uint32_t op; /* 0 4 */ > uint8_t block[16]; /* 4 16 */ > > /* size: 20, cachelines: 1, members: 2 */ > /* last cacheline: 20 bytes */ > }; > > but only 16 bytes are allocated: > > sizeof(struct ec_response_vbnvcontext) == 16 > > In this case, as only enough space for the op field is allocated, > we can use an object of type uint32_t instead of a whole > struct ec_params_vbnvcontext (for which not enough memory is > allocated). > > Fix the following warning seen under GCC 13: > drivers/platform/chrome/cros_ec_vbc.c: In function ‘vboot_context_read’: > drivers/platform/chrome/cros_ec_vbc.c:36:15: warning: array subscript ‘struct ec_params_vbnvcontext[1]’ is partly outside array bounds of ‘unsigned char[36]’ [-Warray-bounds=] > 36 | params->op = EC_VBNV_CONTEXT_OP_READ; > | ^~ > In file included from drivers/platform/chrome/cros_ec_vbc.c:12: > In function ‘kmalloc’, > inlined from ‘vboot_context_read’ at drivers/platform/chrome/cros_ec_vbc.c:30:8: > ./include/linux/slab.h:580:24: note: at offset 20 into object of size 36 allocated by ‘kmalloc_trace’ > 580 | return kmalloc_trace( > | ^~~~~~~~~~~~~~ > 581 | kmalloc_caches[kmalloc_type(flags)][index], > | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > 582 | flags, size); > | ~~~~~~~~~~~~ > > Link: https://github.com/KSPP/linux/issues/278 > Signed-off-by: Gustavo A. R. Silva This patch seems to have gotten lost? Looking at the conversation, I think it should land as-is rather than changing the allocation size. I can pick this up via my tree if that helps... -Kees > --- > drivers/platform/chrome/cros_ec_vbc.c | 12 ++++++++---- > 1 file changed, 8 insertions(+), 4 deletions(-) > > diff --git a/drivers/platform/chrome/cros_ec_vbc.c b/drivers/platform/chrome/cros_ec_vbc.c > index c859c862d7ac..b5a584f5469a 100644 > --- a/drivers/platform/chrome/cros_ec_vbc.c > +++ b/drivers/platform/chrome/cros_ec_vbc.c > @@ -20,10 +20,14 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj, > struct device *dev = kobj_to_dev(kobj); > struct cros_ec_dev *ec = to_cros_ec_dev(dev); > struct cros_ec_device *ecdev = ec->ec_dev; > - struct ec_params_vbnvcontext *params; > struct cros_ec_command *msg; > + /* > + * This should be a pointer to the same type as op field in > + * struct ec_params_vbnvcontext. > + */ > + uint32_t *params_op; > int err; > - const size_t para_sz = sizeof(params->op); > + const size_t para_sz = sizeof(*params_op); > const size_t resp_sz = sizeof(struct ec_response_vbnvcontext); > const size_t payload = max(para_sz, resp_sz); > > @@ -32,8 +36,8 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj, > return -ENOMEM; > > /* NB: we only kmalloc()ated enough space for the op field */ > - params = (struct ec_params_vbnvcontext *)msg->data; > - params->op = EC_VBNV_CONTEXT_OP_READ; > + params_op = (uint32_t *)msg->data; > + *params_op = EC_VBNV_CONTEXT_OP_READ; > > msg->version = EC_VER_VBNV_CONTEXT; > msg->command = EC_CMD_VBNV_CONTEXT; > -- > 2.34.1 > -- Kees Cook