Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp2148901rdb; Mon, 9 Oct 2023 14:31:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8Q7k7PHdFfXFJGcFcfkV/k0yaK4bPVvLS0zE7RTKRzQOs4d0zGXMjTLyam5LEreNeilr4 X-Received: by 2002:a05:6a21:81a4:b0:16b:beb5:da5d with SMTP id pd36-20020a056a2181a400b0016bbeb5da5dmr7351090pzb.43.1696887086679; Mon, 09 Oct 2023 14:31:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696887086; cv=none; d=google.com; s=arc-20160816; b=tD9hchl+QEbEkrwTRUQB7DidsbAHBdFdt5N/0N7LXxAS1MNrDPb2cta4SVu5jhghr7 CAvWofWInyCsDstkT18Gdv6qhq+TsVGifrbs0AYkQdOQs4q/J2hIqbjfRiRgmd1b2ahy sF7rY9pB+XpFucM6iu/0G8t3R2HnOKrbaNRmMdJPry/zyHIwBsS2jyNKLwf6u67k3PWF SaxYkn7iVCx9IM6wMtp/y1xKm4ZfskWhL9jAp+tUCHM3q2z35VVoXIHeTxRNv24mplfg 6naXrstth4s7KEh45PuCJCAvueZAcjnHxXLcksw0rOHplnKUWBpZ2Cl6TtyEZxGF5XtN Bwfw== 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=bZgIpFs1cvlSs8wxxuT2gYnYizO0+6nV0Fzg3pchGbc=; fh=H/sfbYbtM1nqnHS5dmMIKiZH+o+RAIa2eS1MzqoDL3E=; b=kpCW36nEZzWkqZjNh8NOW0gqyD4bDMIxX6Cxn3fpVPa+6teSEoB7naZMfdXJ0KPjBP 2b1b39+m06dn9O1gFIJkxcJigkT1erKEg8rN+ltffm4yA1MeyCRacLYyujE9orW/Hb5j 1CaTcfj/6SoPsO5bNX/HkH4KKoHaGxfD7HqbyJqrh4rqDZa0vb8ZX4bWjMOPPTaVxP42 kx363PeDmGfot7HD5FDBFiD343gr1+NZz1/vW9a6fr1XCI5GOh/XNxjZP/vUAAophXbE psXt26pIcfxaTyni7M1P4MHIcoNsUwf3jg3bo0BaofyKE9XLXNHxibZJ4z/vj1eYHfMq lOQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rT1qhpFp; 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=kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id q7-20020a635047000000b0056a1ed17deasi8279430pgl.675.2023.10.09.14.31.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 14:31:26 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=rT1qhpFp; 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=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 6798A8087270; Mon, 9 Oct 2023 14:30:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378699AbjJIVaY (ORCPT + 99 others); Mon, 9 Oct 2023 17:30:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378542AbjJIVaW (ORCPT ); Mon, 9 Oct 2023 17:30:22 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92D4592; Mon, 9 Oct 2023 14:30:21 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2D10C433C7; Mon, 9 Oct 2023 21:30:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696887021; bh=hKpw9LZwyKaVS0FWXHmJymdA4SjptOQOm2bWj75OgPs=; h=Date:From:To:Cc:Subject:From; b=rT1qhpFpqRiVqgpTZwUGexp2A+nUsN2ViOQjRqnwpV0V0OEDPncJ7UNSgaYVBGHsH qKxM3kOH+f9qJrEyWZPcguD95u3rmDjaCIF5AVOLgk5fP2yAOEmMMJtYd77KoXZrFw Wq6qTfZatu8Mlf81/v9INrgEXKTDHDPi5aCI+OO2PEAo9gx3ZRj1LmHA3FEQ4cIP+N YwNrvNLakxwSDssvtvd2pvR/7MhoZLyFiiR95n8xwK6qH/kqt1oL05VW5cl6ROxs9x fIomWW8LKuLgLPRQpFao87ba4zFOX2cCBX/2F8Q35XWeJEhC05ayqb8/jR9GpH/LE2 BSFrwwIN8Vd5g== Date: Mon, 9 Oct 2023 15:30:17 -0600 From: "Gustavo A. R. Silva" To: Srinivas Kandagatla , Banajit Goswami , Andy Gross , Bjorn Andersson , Konrad Dybcio Cc: alsa-devel@alsa-project.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] soc: qcom: apr: Add __counted_by for struct apr_rx_buf and use struct_size() Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no 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]); Mon, 09 Oct 2023 14:30:39 -0700 (PDT) X-Spam-Level: ** Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). While there, use struct_size() helper, instead of the open-coded version, to calculate the size for the allocation of the whole flexible structure, including of course, the flexible-array member. This code was found with the help of Coccinelle, and audited and fixed manually. Signed-off-by: Gustavo A. R. Silva --- drivers/soc/qcom/apr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/soc/qcom/apr.c b/drivers/soc/qcom/apr.c index 30f81d6d9d9d..1f8b315576a4 100644 --- a/drivers/soc/qcom/apr.c +++ b/drivers/soc/qcom/apr.c @@ -41,7 +41,7 @@ struct packet_router { struct apr_rx_buf { struct list_head node; int len; - uint8_t buf[]; + uint8_t buf[] __counted_by(len); }; /** @@ -171,7 +171,7 @@ static int apr_callback(struct rpmsg_device *rpdev, void *buf, return -EINVAL; } - abuf = kzalloc(sizeof(*abuf) + len, GFP_ATOMIC); + abuf = kzalloc(struct_size(abuf, buf, len), GFP_ATOMIC); if (!abuf) return -ENOMEM; -- 2.34.1