From patchwork Wed May 17 20:45:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13245653 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 2006BC77B7A for ; Wed, 17 May 2023 20:45:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229514AbjEQUpn (ORCPT ); Wed, 17 May 2023 16:45:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbjEQUpm (ORCPT ); Wed, 17 May 2023 16:45:42 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19CB830DE for ; Wed, 17 May 2023 13:45:41 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1ae4f28454bso10041015ad.3 for ; Wed, 17 May 2023 13:45:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684356340; x=1686948340; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=lDECabSrlFrISxM7rzu+fquYe681wwwMcqhoZr9uvCQ=; b=loXJM53z+mT9FjhlIlIAH3qAWpB+9SSZWL1UxRYK1N2oFnx9nBVQ6wtp87rMMM4zUU klGyPgo0kCBZXItxWFmAgeLwJjRZ7m5ijD++n4TG2vfzIKoE196B9DBP9VQG73anx14J ZG5zf/7qp0b98W0vR4fDK5oGmilKXXaC8zM9A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684356340; x=1686948340; 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=lDECabSrlFrISxM7rzu+fquYe681wwwMcqhoZr9uvCQ=; b=HYa03HUIIsnhF2Co92yixBdZpsTRYeb3tB5qJ7vVAYSQeqNzSsXg1sYIZ1zQeRZy/i Lth9Qkk4FMnPfeEijFqJcuMV6yLoqSgDi3VPvyrgbNgv6zqNNy7Yp2mG6PLlW3b56sz+ KlOoZJU7VpKkBOx5vjQySpllLRVGkHq9hec6bXlY3kc3DLnh0/GqZabazpXeVfPQgcVi wtHpma2/JGDnbPTo/qxeWJnPsI93UaS4A+cVPp4ehij1bgHhw1VNijoiuY2YAULfXYhd UgFlZVtPcdhgpHFCo4TjRqTN1FQgJ0eB0UGGcXUF96LC1/IQvHyb7//8KgfmdvTFvGAz KVvQ== X-Gm-Message-State: AC+VfDw17yr5+DFXrru6PnFezR/7vJCZ1EZwbxAI4s1i+u6eN94WBEd/ a4OuEJybs4VJJR9Wdo6fjMVSHQ== X-Google-Smtp-Source: ACHHUZ7umzAmc2jWBWxRqCVn5foFmDBvI8oVj5ZZk5xU9y3TjJ/wMcw9kPfQx2ZzKgAlTTsTdanQlg== X-Received: by 2002:a17:902:f549:b0:1ae:5916:9f12 with SMTP id h9-20020a170902f54900b001ae59169f12mr158021plf.13.1684356340432; Wed, 17 May 2023 13:45:40 -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 x18-20020a170902ec9200b001a1adbe215asm18025692plg.142.2023.05.17.13.45.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 13:45:39 -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] checkpatch: Warn about 0-length and 1-element arrays Date: Wed, 17 May 2023 13:45:37 -0700 Message-Id: <20230517204530.never.151-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1825; h=from:subject:message-id; bh=wDO9y45sse31MEdYIydw4XH9ecDLDywEI1HGqDEAzbA=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBkZTzwka7oUmX4SrSDPpzLH4sMs1bbjbwsMLh7XkFM h5/6rrWJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZGU88AAKCRCJcvTf3G3AJlgXD/ 4pLGAMOWzwS+SRPL9pn4fWTCBfkJaDNQzU78cJLlM7S8iRB4Ao5U8AZOqEbtawN9Nw7pQBmfk1V4k/ RCmBb+mX0fzmvlHuuh8+oLrlG9mO8tyzLKPXjJKVSPTjxDxhk93mdp4Tcep+iyLAcgPz4YUnNdh0hJ Kk/FfYTQQ51FYb/1MkMN0d+rHC13S4OnwsXraM+te0EP3lfUxzqdbxYIzULB4lHZpb2qZjpptY/XOg 2Q4Iwm5VfNWDukJhnoJJbaTJNhzQAY0Mcy0Bza2hwtk+VQkgMRQZKFWrfXpIS/i8pMyYlXyTp6Ysnz k7ji2c01QhNrC6ToNk1Sl6pZcwo64YozFnEmtM4Q8UpuuRZfvZlvE/nHo4Henp9jR5ob1O2SP2y+pl eWmY/G1Ko89W1o+yYeL2EnB+zYvr9sWvSR/KzDq79nUtKsWGJy/T8uPOzrKTLMPumDkPJN7ee3LSUu T9iQnkr/Nb4cl9DhXAE264muZC22Hunk/j37IdFwTqDkr03YNcQDSuXz4KouZbF5mN6UWV+/zZz8bX 7neSV1XMDo01vETxu7XnCLkAjKOHw3JzLgBipzbCoioFx/In8VabdQlfn0iOhTbPIGc5tqFLQwoTHI sD2CORtcVHmEqd3oTHbdviI2JhLsmtvOOhyxpAOvA9V+PBnR7HJYusaMr/xA== 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 --- I couldn't find a better way to figure out if a patch line is part of a structure definition. I'm open to ideas! e.g. this would have caught commit f5823fe6897c. --- scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index a90e0ede53ad..415b3c14589b 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -7430,6 +7430,20 @@ sub process { } } +# check for fake flexible array declarations (i.e. not in function scope) + if (!defined($context_function)) { + if ($line =~ /^\+\s*((struct|union|enum)\s+$Ident|$Type|})\s+$Ident\s*\[0\][^;]*;/) { + if (WARN("ZERO_LENGTH_ARRAY", + "Use C99 flexible arrays instead of zero-length arrays - see https://github.com/KSPP/linux/issues/78\n" . $herecurr) && $fix) { + $fixed[$fixlinenr] =~ s/\[0\]/[]/g; + } + } + if ($line =~ /^\+\s*((struct|union|enum)\s+$Ident|$Type|})\s+$Ident\s*\[1\][^;]*;/) { + WARN("ONE_ELEMENT_ARRAY", + "Use C99 flexible arrays instead of one-element arrays - see https://github.com/KSPP/linux/issues/79\n" . $herecurr); + } + } + # nested likely/unlikely calls if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { WARN("LIKELY_MISUSE",