From patchwork Mon Oct 9 21:46:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 150367 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp2136529vqo; Mon, 9 Oct 2023 14:46:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEJ1WHJhbly8i36AdPOSm+t4fmlsZ9W9v+cRm9oT3iec5wHGEhf+4fGfL0s6i7dgk1zM4jr X-Received: by 2002:a05:6a00:2e90:b0:690:3b59:cc7a with SMTP id fd16-20020a056a002e9000b006903b59cc7amr21682743pfb.23.1696887993122; Mon, 09 Oct 2023 14:46:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696887993; cv=none; d=google.com; s=arc-20160816; b=G5qQXr9hxGE1SavUR0Yh+8/NQ/4PP+YSBwPHoKOSSJ8fAilYwFtnKOb19zLfYX5QkI TImwDZ0D3lvHpJwBHHON6HTDN5tcA9hn+H5vil9DMGSNKuh0BgTOEOhTd4Uj1aRKiWbP iT7v4gsaGhbvxnakGRi3SCDQPmyAwBSeiUdVFFEY3W/1taeV/GVmUYky1Is6MBOAgYv/ jBfttH4ZU7xroLBZN+IckCS0dzewdkLpCPenC/8w2FkxnIxxGWcxPBs6VRQGSU+K7TnD M2yBvqehih5uozYuONc0s9xHeVm/QRZtY+CgW5tW1+J6iumfgmEvtRewSiq/VxoKsodk sj4A== 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=V4xCoaw+zCIC3a25L/+BAaGY2jxiAYvITagSsotDuMs=; fh=nCIH3Jt4mhyupZrl1x3RSkYksERubiuLMOScuVAo1BM=; b=NVXEv1ZOjEeNpG7FAby3bs/6qgYUJfUTbcqk79e9Uml6xvbqnByZSUAdpz9iezD5wH qgpqC05mWRYkWU7d89w7frNEKHQUNbceRvzNO0MjKNKK7GxMQc4mNg0eLxMtg2zAsr// 3wGSyhmLolGCdch62AmVe0yvqP4q7n2FMUPMgtLz53hEaVCHY8Oe64xBp4e+RFyausF0 ZwXvLYTM8uwSGttR1EGnPjBlSN4sfRLf/gI4k4Evg5FUdu9EgdZWNOvpE2cb6qLV1Cfc 4WosB+cS3X0XE83ghk2Ihvk49HlwDbh1+WABJJr7nFYnJ1RvirSogveuJDJ1ueew4NCY wV2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DbzY3biD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id s22-20020a63af56000000b00585a45417b3si10149888pgo.129.2023.10.09.14.46.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 14:46:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DbzY3biD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id CE63880A22DC; Mon, 9 Oct 2023 14:46:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378792AbjJIVq1 (ORCPT + 19 others); Mon, 9 Oct 2023 17:46:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378682AbjJIVqZ (ORCPT ); Mon, 9 Oct 2023 17:46:25 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A737EA9 for ; Mon, 9 Oct 2023 14:46:23 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 416FBC433C8; Mon, 9 Oct 2023 21:46:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696887983; bh=hOV1TPaxRK6lKBeU8FkupnPDNPfBklnFK2iqPujS67E=; h=Date:From:To:Cc:Subject:From; b=DbzY3biDgO4mfCIA4qPzupLl+uX9GQuhb11X3aVuywb+Bl5IiANsNQ87Dq6LSmK2G N+NCZxZ6Io0Elk4NgT8Tu4MX/Wbc/Zlyh+3yaAk2picUd7NwkzG2epTTBf92SeO7Zn H+iQ5RERpBZ9vTb3eOW5ELre2i0mPtM7mA2AkKXShWbogN5G9ftpllRSnRVZDjiX+u Z6gHzuzsir9EofnMs4F0iGxxpYt+rRTCLF4k2fA0rHNG8r99DhPaFICqWsqKebAfxR EltoEEpdN8DY7vzaPZL+x8Im429T6LSYjnYmhGiioMo6fjMsYsbjcQft4CvhkJ7dap wytsDX0E0V9tg== Date: Mon, 9 Oct 2023 15:46:18 -0600 From: "Gustavo A. R. Silva" To: Chandrashekar Devegowda , Intel Corporation , Chiranjeevi Rapolu , Liu Haijun , M Chetan Kumar , Ricardo Martinez , Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] net: wwan: t7xx: Add __counted_by for struct t7xx_fsm_event and use struct_size() Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 09 Oct 2023 14:46:31 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779316024268522443 X-GMAIL-MSGID: 1779316024268522443 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 Reviewed-by: Kees Cook --- drivers/net/wwan/t7xx/t7xx_state_monitor.c | 3 ++- drivers/net/wwan/t7xx/t7xx_state_monitor.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/net/wwan/t7xx/t7xx_state_monitor.c b/drivers/net/wwan/t7xx/t7xx_state_monitor.c index 80edb8e75a6a..0bc97430211b 100644 --- a/drivers/net/wwan/t7xx/t7xx_state_monitor.c +++ b/drivers/net/wwan/t7xx/t7xx_state_monitor.c @@ -445,7 +445,8 @@ int t7xx_fsm_append_event(struct t7xx_fsm_ctl *ctl, enum t7xx_fsm_event_state ev return -EINVAL; } - event = kmalloc(sizeof(*event) + length, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); + event = kmalloc(struct_size(event, data, length), + in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); if (!event) return -ENOMEM; diff --git a/drivers/net/wwan/t7xx/t7xx_state_monitor.h b/drivers/net/wwan/t7xx/t7xx_state_monitor.h index b6e76f3903c8..b0b3662ae6d7 100644 --- a/drivers/net/wwan/t7xx/t7xx_state_monitor.h +++ b/drivers/net/wwan/t7xx/t7xx_state_monitor.h @@ -102,7 +102,7 @@ struct t7xx_fsm_event { struct list_head entry; enum t7xx_fsm_event_state event_id; unsigned int length; - unsigned char data[]; + unsigned char data[] __counted_by(length); }; struct t7xx_fsm_command {