From mboxrd@z Thu Jan 1 00:00:00 1970 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 71CFE2E8DF3 for ; Fri, 21 Nov 2025 19:55:12 +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=1763754914; cv=none; b=shP8B6nb6pNVo5QAI9LFl+uxl9Tt95uHc5owtHvU4pWBTZGUua+LtFuQpVeuEKin4JUJSei/DhFFNjJ4zjVcZTrAcUOSuRRJXtPtkk0RuXpAuGcHQ5g+yK4rNQNJxOUAV0HjhSvwi86m4vsU6788igwq7ztBdpq/cJXvvHYCnMQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763754914; c=relaxed/simple; bh=FeBc3mz9++WhInS5DCc3/yvwNzCoBHeGSj4Nx5UNqAM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BdXuuQr5Wti1xdTCV/PByDmYA+5AkrxpVIPibuxaS+/gTDb37MKfxsbke0aAwvhpoYMhySFw9YZXg+t77Eir2dW9GyFDy+Fl4fLE02h1So6JsCNqOdZY9OYcoQIKzPnyTQfJRE5YTehVeT2TvJHhORSS8yqb3NrZm4hq+AnBGjU= 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=eamxpM8i; 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="eamxpM8i" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7b89c1ce9cfso2656171b3a.2 for ; Fri, 21 Nov 2025 11:55:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763754912; x=1764359712; 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=OsLQEMyKEn/UE/LwUY0j1tuqWL3xiSmMEKB7wO01+Co=; b=eamxpM8inZHcLa53/iuBaT/pmYojJClcAxUQFk8Q/mFKQj1ikrgyGG9tt9D9pUS1X5 iJEEfsnlp8I8zJw/Uodsk1h/leiWr9QZK1N/b1UQBkCqNSaVd2JKxLjohnzlc6KdY0FS IyqZ7iGUCsqngVjOIQLvn4To+bdq0f/3kcgfuhiioKtXT/0feb+ehbc+qFqDZFtZtm5/ yGV77Tks/KmsGKG9Wpk30Q3dOaj+zftsNb4UvWEkGxS7XjGDhhx/KMRZrRb32CulM1VG vtcuWSPgDsDuQcLS7J6jlCBsJ22suQnX/ErdJWn7E5SVdcldOK40Nb572mtPRxxyxPAJ 2roA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763754912; x=1764359712; 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=OsLQEMyKEn/UE/LwUY0j1tuqWL3xiSmMEKB7wO01+Co=; b=p5Ol/MWvGBLJoVPGpiCjAyn5sh1bSVtD/g4mPjsclXVDgSEQL78iikDVDkXM0Xly53 AOXOuG5KjEZJa5PfmFJRvZwPQ+JR37LxmmAfM+Lug91p1LYeURERN7QiyUzfUCM/1e/o J8g5/z28aTzWJKgR2hxVcr/68/WmgkVfh+SrQL/Ler68DPGG6zy3CCWzpolmO0M9MzAI 3nWAOE9funidmRf8jr71bk8fSEUDW9Bnw9QerxoTZdpaM5XSbaenCyDoMO9gXq0njNaH 3KOtnlBDCC1sfzHTAlNZwfzMb8pMUfbwveb/v3qLx0cEoCPcqRuu2C+A92GlSlA4oKYc h4Zw== X-Gm-Message-State: AOJu0Ywgjr45T9MLQmkWhSmdohdPOM3jzCg8xCe5+HDVHtUf+hcBEOhA vLkISHyatIRBnDQLrWuNfvnjvqeMrE91S+1ZZyDkzDWkN9Sr4HPZtyy1DbOiRIFgrzZZ65gcDdi ZPDUq5TD9agAXkCZJYXyMteB6nFBN32ttZe3EuBwNnAPmgm+9XSxMrBjbOV+ker3QE2x7gf4XhM dOE1EuOaNybubuGAj0J+WNzHVeEcnqSx8+zzL8Rg== X-Google-Smtp-Source: AGHT+IGxGbZXuYRUV+1DBM1iC3d8Dgs2MaafPC2v4F8uQUYDIrUiNXJIpBlI3Tv92mLuGvfDyHJ2cMMikw== X-Received: from pgn1.prod.google.com ([2002:a63:d41:0:b0:b6c:b8e0:6988]) (user=morbo job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:9184:b0:34e:cc0a:40b2 with SMTP id adf61e73a8af0-3614eddf63fmr4914029637.30.1763754911092; Fri, 21 Nov 2025 11:55:11 -0800 (PST) Date: Fri, 21 Nov 2025 19:54:33 +0000 In-Reply-To: <20251121193957.1655580-2-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-2-morbo@google.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog Message-ID: <20251121195504.1661783-1-morbo@google.com> Subject: [PATCH v2 1/2] Compiler Attributes: Add __counted_by_ptr macro From: Bill Wendling To: linux-kernel@vger.kernel.org Cc: Bill Wendling , Kees Cook , Qing Zhao , "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-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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: Qing Zhao 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 --- v2 - Add support for GCC. --- include/linux/compiler_types.h | 11 +++++++++++ init/Kconfig | 7 +++++++ 2 files changed, 18 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..f947f242bca8 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -139,6 +139,13 @@ 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 + # supported since clang 21.1.0 + default y if CC_IS_CLANG && CLANG_VERSION >=3D 210100 + # supported since gcc 16.0.0 + default y if CC_IS_GCC && GCC_VERSION >=3D 160000 + 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