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 6E19720FAA4 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=OcvtR5BeH8aa+tskEN12TikXpAnUG8s5HuoX+xrKYuphxNzNOQl7pqWct2FcKXFi5WbCa4JudEBgN4BaLlI5NpW1oUFruUNpsvYdroWLAdlAUPg41k8FcIxvT9tfy03LHIT9tHy0b1Y32xDiK+CM5cMr3cOKB/tSpAcpGLiNTmQ= 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=KLf0d6lY; 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="KLf0d6lY" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7bb2303fe94so2627868b3a.3 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=lists.linux.dev; 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=KLf0d6lYKEPpl++h3mNG4XyI8mbXWsrd183Ot8wHTbEiclSFdlHBbhYRt61Zy7+6o8 oxi79czopyV6l7WP21a6eXnm3csaflwsBLyyuTlLb3FzfLX/ChD+ARAqLC9+QZlhlm1C D3JtJJfDQaMcWn5YMg363TCY3wSQ1WAvANEwjCwlWy8JLBvWSdTMR6Uq3FN2sRwEHoI1 WVoU4ld3w156iL4CMdiMDC5++PAU9aaEfQvcdEo3gUgmkUGS6Vd4eUf5Y2DTdrpWyLmw 9/l4b0zZjaABF5EA5IQkJFLHLYe8LjGOStjb+jTeJX/y7LUtodZPRgTcXBd5a3WPuRXt 9VnA== 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=MVQlJkiJThp6rPDLTojP+tKDeaV/mJ+FZI/kpe8vxx4Ht/yoR+IBdoCOsGuo5oqO2c p6GhJhTyjtuTQrNDxjktgk4H6Ra3x4Vf8BaeqCXpyTtcdRV47yMNipqGuskU8nq/EjsJ gNXRpNILNWmwhJYVb5Y7DjnfdZAzorbT6z+27fzSnzcUVwYZKIz8j8vnUc/KgXiW6rhl WFV1LApIr5VvsOlJMslSCTaKckG9bXPB9KegBhiM+KdrQn7e9ltanwHWuCCMZh0qPg+9 PzSClcelf9leDK4e5YCdJXiZgWVT6Td2sCCFBv1nyUIwWhRgSWrtiU/GCz0wm9RjqsZi gLvA== X-Forwarded-Encrypted: i=1; AJvYcCWMgLE80M9kXbijcA1NFI4fbab8JbZlKQ5E/YO9M5RkH0YpMGLtJQr/3iDoG1Ninm4kGcDa@lists.linux.dev X-Gm-Message-State: AOJu0Yzc/mt3BLQD0s0Lm1LnhNaYA505cT2dPEeDTOOd5fVhyd+w4IPx saTQzPQOzQM/vbCZRjNrhjgFnfZPHgq7+FMuk1MDwN8FnkIAzhi1hfx9nJSiPt61LBStteoWN31 6 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: llvm@lists.linux.dev 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-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: "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