From nobody Tue Dec 2 01:27:29 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 911D32727ED for ; Fri, 21 Nov 2025 19:40:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763754025; cv=none; b=uKtzPeHhXExB84jzTsattGPmdW8NNYzYEEjylzhiW3FuurAdVZz0lmq+njYMGHC2RmQNPZGLti9APeGENn2UQuYa4HRE3whqjj+Ei+Bj4kfdpW7ifHrmJVQkA/j2xGGGBNVLZMkmhfevxb/b/osVtXwD/QkXscO+W6mV+G0xzmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763754025; c=relaxed/simple; bh=MYCPdC5mx9elsFs5SLOof97idIHJHiNCqO7tF8T3XPE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=reOxrUWuy2CcvBALiKvDEp73wTELnTgJ/f+7Se4W366hTPsfW5jzDfimFHXCXpVrJ/+Cy5PSAuWe5vOGZRUQl5AcZntkocg2ULniXYisjoQv+u7F6K5rIKRzdGF0GdcWrXH2UbpxTBVctGK/n26LSg/pzMfejIeONfxxpyrWCN0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--morbo.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YRB8cUr/; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--morbo.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YRB8cUr/" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7b8ed43cd00so2739596b3a.2 for ; Fri, 21 Nov 2025 11:40:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763754023; x=1764358823; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=oHmgZO6ethqvsbdYO3ab/+ap1rYqr7kszhE/exMXa8o=; b=YRB8cUr/urL0fjbrFd/oH54JDlHYEgM57QSpgVENhrdkqVcJInWbkO5NDMMtnP2JL+ p4btTlqm9f4LkhO33zGIWHrq+QGE+c1NuRFz+8AmQknocnybp+zqgCxOK/Csh63Yx4aO GKKOSZsL4Un19gY9qtpdTgh1Ps0zxyVTaLCIedtXtFOj+OUatiSI+gWfd/qYxGOOUOlP km5zaCn68IQ1mJwmvDufspkQeRBYt+40KSoquXtbYcS8hd120jeShT7DNDTiHIpEGfA2 fkZizmMJzATdJ/8oP+093kajjae9cCjfY4EGVVBGwoZnnq9McI6uDO4Cg55eamQf5i6q Bpgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763754023; x=1764358823; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=oHmgZO6ethqvsbdYO3ab/+ap1rYqr7kszhE/exMXa8o=; b=urbHnm6y9VCpKS0Y3qYIMnjKy4PzQPK/ZZDDTWUvolLDDauyivBIV6t3iEsquuFSSw M/ZxKNGTx4IjYhmSmBTDaXlKy/vALu2e/PbSYNszwM4Sq2BKR+k+/6hHJ81tjQGaPpmN w/FhUfi0KvrKZJrXsm3zoCtC1vOL22Ykz/WBr971u7+6KDvHp0Yy8eNRFr6Fa4sIeE8T TCRK2lmmHa6+euwAruD82MoOigmWGVmvDVpuyaFjTiw0XQ4vz7OxBWq9qWg7RGSdnHAm nWeDaufG3w79fm9B0tgsrXyyqxeQMM+P9JyDgxwNoijT2GNAcPGFJkZoQc0Yx+aBXH40 OJBA== X-Gm-Message-State: AOJu0YxuwxLypkXm1Q6Y9OmeYipuYFUpZ2ID7f2P1XOGsGISwcS8rSe+ moKgSoniHevef2v62hYCo99+2HOrYya6rTo5vr0bSRE2+nAeJ7l+ctN+AS3WWjaGtC7bTEaL5Er a+7V/cm8IkDatyOLTQN5U30aaxtw3CLNeX5uGNP8eU3vO+vXBZ8xUijhvnIDaL1xLZdTylSNYPz FLOSHXoG/Ny3YhDbtbAEFj2O/zpP6fdrwW8ytCkw== X-Google-Smtp-Source: AGHT+IFih8IJ4RxuxQ2Hu+KI5Fa8rvFm0yb8dOgyZXKnABQhio/5PRYjKYWbcph5Yc9RmZLpsyq4X9QrgA== X-Received: from pfbdf7.prod.google.com ([2002:a05:6a00:4707:b0:782:3712:63e4]) (user=morbo job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:918e:b0:350:f9c5:f90c with SMTP id adf61e73a8af0-36150e995admr3760493637.27.1763754022557; Fri, 21 Nov 2025 11:40:22 -0800 (PST) Date: Fri, 21 Nov 2025 19:39:43 +0000 In-Reply-To: <20251121193957.1655580-1-morbo@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251121193957.1655580-1-morbo@google.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog Message-ID: <20251121193957.1655580-2-morbo@google.com> Subject: [PATCH 1/2] Compiler Attributes: Add __counted_by_ptr macro From: Bill Wendling To: linux-kernel@vger.kernel.org Cc: Bill Wendling , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Justin Stitt , Miguel Ojeda , Peter Zijlstra , Andrew Morton , Heiko Carstens , Marc Herbert , Uros Bizjak , Tejun Heo , Jeff Xu , "=?UTF-8?q?Michal=20Koutn=C3=BD?=" , Shakeel Butt , "=?UTF-8?q?Thomas=20Wei=C3=9Fschuh?=" , John Stultz , Christian Brauner , Randy Dunlap , Brian Gerst , Masahiro Yamada , linux-hardening@vger.kernel.org, llvm@lists.linux.dev, Jan Hendrik Farr Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Clang and GCC are expanding the '__counted_by' attribute to support pointers in structs. Clang has support for it since version 21. This requires defining a separate macro, '__counted_by_ptr', because, while the attribute has the same name for both a pointer and a flexible array member, minimal compiler versions need to catch up. The effect of this feature is the same as for __counted_by on flexible array members. It provides hardening the ability to perform run-time bounds checking on otherwise unknown-size pointers. Cc: Kees Cook Cc: "Gustavo A. R. Silva" Cc: Nathan Chancellor Cc: Nick Desaulniers Cc: Justin Stitt Cc: Miguel Ojeda Cc: Peter Zijlstra Cc: Andrew Morton Cc: Heiko Carstens Cc: Marc Herbert Cc: Uros Bizjak Cc: Tejun Heo Cc: Jeff Xu Cc: "Michal Koutn=C3=BD" Cc: Shakeel Butt Cc: "Thomas Wei=C3=9Fschuh" Cc: John Stultz Cc: Christian Brauner Cc: Randy Dunlap Cc: Brian Gerst Cc: Masahiro Yamada Cc: linux-kernel@vger.kernel.org Cc: linux-hardening@vger.kernel.org Cc: llvm@lists.linux.dev Signed-off-by: Bill Wendling --- include/linux/compiler_types.h | 11 +++++++++++ init/Kconfig | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index 0a1b9598940d..2b0251bb951c 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -351,6 +351,17 @@ struct ftrace_likely_data { # define __assume(expr) #endif =20 +/* + * Optional: only supported since clang >=3D 21 + * + * clang: https://github.com/llvm/llvm-project/pull/137250 + */ +#ifdef CONFIG_CC_HAS_COUNTED_BY_FOR_POINTER +#define __counted_by_ptr(member) __attribute__((__counted_by__(member))) +#else +#define __counted_by_ptr(member) +#endif + /* * Optional: only supported since gcc >=3D 15 * Optional: only supported since clang >=3D 18 diff --git a/init/Kconfig b/init/Kconfig index cab3ad28ca49..298c94c4c1b1 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -139,6 +139,11 @@ config CC_HAS_COUNTED_BY # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108896 default y if CC_IS_GCC && GCC_VERSION >=3D 150100 =20 +config CC_HAS_COUNTED_BY_ON_POINTERS + bool + # Needs clang 21.1.0 or higher. + default y if CC_IS_CLANG && CLANG_VERSION >=3D 210100 + config CC_HAS_MULTIDIMENSIONAL_NONSTRING def_bool $(success,echo 'char tag[][4] __attribute__((__nonstring__)) =3D= { };' | $(CC) $(CLANG_FLAGS) -x c - -c -o /dev/null -Werror) =20 --=20 2.52.0.rc2.455.g230fcf2819-goog