From patchwork Wed May 31 00:49:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13261272 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C35FC77B7A for ; Wed, 31 May 2023 00:49:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232026AbjEaAti (ORCPT ); Tue, 30 May 2023 20:49:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231534AbjEaAth (ORCPT ); Tue, 30 May 2023 20:49:37 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71B8D9C for ; Tue, 30 May 2023 17:49:36 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-64d30ab1ef2so3890658b3a.2 for ; Tue, 30 May 2023 17:49:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1685494176; x=1688086176; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=BPd2wyhAkJ3YpAxbQZ8ec8dh+1Dyphp7NWb1XFVf7wI=; b=WTADF+8ozE5wQEi8qcNO0PrWAPOAPAuNFYF0z/kGb3DV6REuux6tsP26CjIqmR6TTo QElBuAOLQD2AxZa/enpBVgup4LGMFXEe4RN3JUSxG/7YPADZ3ZvYemVWq6A1QduR5mJW wKi+00F/Dnzxv0tmwGb4JV3luyBNbU5TneTpY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685494176; x=1688086176; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=BPd2wyhAkJ3YpAxbQZ8ec8dh+1Dyphp7NWb1XFVf7wI=; b=GbHdfgDNb6MaesXI9VlxNsJmiJGO0g9l8ilX/H7uMgtwaZonz3oI8W4GD54nEMiJx4 PBG9Bk24/KcINKv9+oA6upakcGKFn1WNl38IqoZ1lY/fOuGWQWdtDh+P8bRHnmBZ7eSS iDN7gjb+9nT/dFSRYjGg8pTQo4GJ/I18H253BB9/H5GcLfwG2yRJV8rCVotb5pOQhcjl 1guv56vVcoEtVlzTn97FnUDLFq7EsJ8XnRfxUjkjj6J52HkSG9q7dog58hbKLfyE6s6+ CfrxZeFmYBZGI4BYVNu1Tpc4oPFhTVkgeY9y5vK+6ql/kpdkiLVKEn3Hf7JrszUS7pgy x1cA== X-Gm-Message-State: AC+VfDyq3P1UlH5loLc+BdoEeUTXi+1Y9xQqoA2x2yqXB8yUeOJ6R0qq iRVyZXm5wEkHEE9YQHhFYi3wLQ== X-Google-Smtp-Source: ACHHUZ49GdfBCckWeF+aIJZlU6nfDm5RqPdtw15ZUoPOaiLu9XfgLdDKJvGH0ie0FlxYU9qSUpn+sQ== X-Received: by 2002:a05:6a20:6a25:b0:106:4197:b7ff with SMTP id p37-20020a056a206a2500b001064197b7ffmr4699373pzk.30.1685494175985; Tue, 30 May 2023 17:49:35 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id z5-20020aa791c5000000b0064fabbc047dsm2225131pfa.55.2023.05.30.17.49.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 17:49:35 -0700 (PDT) From: Kees Cook To: Andy Whitcroft Cc: Kees Cook , Joe Perches , Dwaipayan Ray , Lukas Bulwahn , "Gustavo A . R . Silva" , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v4] checkpatch: Check for 0-length and 1-element arrays Date: Tue, 30 May 2023 17:49:32 -0700 Message-Id: <20230531004929.you.436-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1845; h=from:subject:message-id; bh=GEL21VHX48Hc+3Yj+QiYsS2aI6hXB6UbODJ0k2u5hZU=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBkdpmcAHbDJqm8gmIbZ6eiJbOuoOVyek8OiALo7epp v8CgkPiJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZHaZnAAKCRCJcvTf3G3AJsz/D/ 9UfeZNwPnNevZIHxAWOgNFu4kL49PAOf+nK0hZ2LpSWUM2mQQwbPwRGVz8PnWRRuzoN1lnwg7Zvhli DA+iwi8jdhznEBiOk5MWeQsWDuUOCKrBRfsy5JMbAx/8+7m6nGF6GosRv2974fVJ37rDSiWB/vSFm7 YwIaRUjSqXWMiGHovT0yrFbZlSwvvWIGYHtrdw1yQQ8s7Fisp1q7qKJm/LTAuZs8KMO1NLqhvZkNck o3pgcN/94V6g5p13xW99fABw44AlXSZ/mJD+gyQuxzdqO6QNZxT9t2vLLBSOP32IPWdRl+fWxWuycG aW4d5tGX3wGCQIsKcF5pRTq2vakex5WyTZx7/4XwKcNiWB7dGwR3nQ/TbyAMV7+nGcOLYSADMYY3Ti lU899gVrqYiRCki4W/HEzs2magklp8/hzFIEjlycXo0gL5p4Js0kZPAXUvfg9dKZwAWcu6+kxpi5eM 3hQkrl2I3+kzFv8lKvpESHebqYVRULZNUCRfa/VQ2rUeWhcAuj/gtprlSrCZHToWIfNxI8AGWz6ulV L1vNk8tSBFa+ES3UmTtpvu2kuZKjvbO8GmJG+jg8/slxuM3YZzAyML6QTb9zRHgrL6X1hrJeIPOxoZ v9O9MUvSq1pwmkXL3GaczkgNd+D5rEM9TENWvwU5HTF7QG6troisOg3bDf/Q== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org Fake flexible arrays have been deprecated since last millennium. Proper C99 flexible arrays must be used throughout the kernel so CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS can provide proper array bounds checking. Cc: Andy Whitcroft Cc: Joe Perches Cc: Dwaipayan Ray Cc: Lukas Bulwahn Cc: Gustavo A. R. Silva Signed-off-by: Kees Cook Link: https://lore.kernel.org/r/20230517204530.never.151-kees@kernel.org --- v4: - combine errors (joe) - switch to kerndoc url (joe) - add __packed for struct matching (joe) v3: https://lore.kernel.org/r/20230527020929.give.261-kees@kernel.org v2: https://lore.kernel.org/lkml/20230526173921.gonna.349-kees@kernel.org v1: https://lore.kernel.org/lkml/20230517204530.never.151-kees@kernel.org --- scripts/checkpatch.pl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 30b0b4fdb3bf..64d21b6aa6df 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -7430,6 +7430,16 @@ sub process { } } +# check for array definition/declarations that should use flexible arrays instead + if ($sline =~ /^[\+ ]\s*}\s*;\s*$/ && + $prevline =~ /^\+\s*(?:\}(?:\s*__packed\s*)?|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) { + if (ERROR("FLEXIBLE_ARRAY", + "Use C99 flexible arrays - see https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays\n" . $hereprev) && + $1 == '0' && $fix) { + $fixed[$fixlinenr - 1] =~ s/\[\s*0\s*\]/[]/; + } + } + # nested likely/unlikely calls if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { WARN("LIKELY_MISUSE",