From nobody Thu Apr 2 05:49:25 2026 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 AAB8A3D3CE3 for ; Mon, 30 Mar 2026 13:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774876814; cv=none; b=ubog7cFKqdm6GgZ3En8NZwfXWJUxmFbRAc6Fes19KFKftdJMlV+Yvf9ImUb+wi8tUvre2Q/nP4jO0tFK4Tf+jyVVqW/8T2faModNWY2EYzufGDLox95tyyzkE0qWvIWClU78x9nXeEKzuqA8uWKHCAPRipyexicMne5c4F55Gl8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774876814; c=relaxed/simple; bh=Apu7QMg5t9X4XqrrbiaHMr1fwOGa5UsX6I+AkCFq82s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rFfgtx73LWB1ArgaWgjKy9u14UhZzLCiQSvnqb0s7udRp37oL7sl1NlVCFUeKyj5jydatJgrfxk6V5ioyCzIpauqqxObWUNcGfykIcYBSMpfUKMzjZTm8qjCD2HOeOKjwfWdzibAy1NfWdxZcSvViWNfDqB/MvyahgHNkBU7QCw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iu08MTWV; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iu08MTWV" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-48538c5956bso44543405e9.0 for ; Mon, 30 Mar 2026 06:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774876811; x=1775481611; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WAjBJkYCqeFogUSVOqhybn+Qkp+h53Wf7afwElb+C9U=; b=iu08MTWVyQBXil+stpw+Uc+GBUJA0R0ERNFTJaaTpCyKaQlmZMq092fYT58Jzp8e2p Q18/BSrMjxx2mfOlwc52tuJSJ5vp3MNJo5OwKfQCjKA6CTXkuOqQKxCn7chURrIKn1Qv 1EjiDWlaG+WvYocIdUZio+Y+5+of/9NwSKrnUbdUD0ZNhlTfCqw+fkKSNW/NHw7p94Eb I8TzqP3g+Hox/iDuIE2EmnYGqn1ABpnp2uvuROc6mzh+9cQl36OW4mMkZlg7r9XA0wd2 6qKUGzY0sbKIYFgpzjYJj2BvKki1qrdxcEAbxUFuh12jtKwkIsowrYemNY+m1CB9TqPu ZRag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774876811; x=1775481611; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=WAjBJkYCqeFogUSVOqhybn+Qkp+h53Wf7afwElb+C9U=; b=bPTn06euJwjOGeEgk2ihiMuwdAyyKSAxfYQiGSq6aBmiAu3MKKc8kJTvwKkhLJWGKO QQl31c8TlFk82Ds8wXq9jbcZ4tgsbiC29hExsKCO6NNl7vKHx8exKbdP8otKAt/gyaF+ 6am47osxxqIh7x4zfgZM/QlOrSwMmnPN0TO5O+Xhu/DnyXN8nQpn9IpWsaPxk1xbG4/d efwCdduUAhwoSIhLgfPlG8BTtYKekYMYdz5z29aa6IbKnifdZCdHtJq7ZeXvpjpT0bYw Fiq+ymxrcqEWCdvLItfY4IF/64CGU4rYsP/5D6il+MXkl8FoObWs/vlLd/zDTGBU351q kQwQ== X-Forwarded-Encrypted: i=1; AJvYcCWDtZHODMUs6O8peksZI1XYr65YCNHpxA3iUFW/S76lX+AN3I365xXL/PkWgOfU5QKswYaNfvFC0fyU6dE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5uUhMpL9MlPlW7P795KQkahKw0zpwHj5vw8/OMORU7Hj5QhJ4 hbx614PeuV1kZg2XPzAh+/RdN//BdAO6JF5potFxqLS9Q5OgfDhaEp26 X-Gm-Gg: ATEYQzxQru2nErq+FteFkbLDUazPQskUrfo5dsRjNcDH55D91DoimDTO4j64MkCNnXp lvg2QViOv5P7onGZzPp7zxomXDfYVJ42Rsgs0CdGyhPjaHaR80b52vD36XzvicDPVwYlIPI3Md/ VC2hxtZzAbpeh6g+hWIYcReIkqFYvBHvVmyvf4NeLIZJJ2iLbu6lf2HWFz+33ohtZ5NqF//t8/X 1NyNUnHXKi+gj6qix22QdvpZj2IG3Rc/DL4DW6jfmDXqvgG5NQEjn8z6U2NyNaEMI6EXJqb7HzT rr2VdFZLJ6uOTwKVPUDJG7Mpyk0bhy5EF6Fc+gqNUXYIFVwkgF90rd2JHdOts7OnwzRHK7gaPrE PbCTn9ytcwpWV0+c4KdngCX1HUleobgv7/T5U3Jan+99vz1GbsYo8FQa/rh+E+zdHv3eqX1+C/p rnMqnEfwmbUacD7tzvNm0qgm1f5ypBvpItHGJLVIjI1Z/RTw4SmF3nD2dMZciOxnvXtOCkidcBx HT2QvzB X-Received: by 2002:a05:600c:4e0b:b0:485:fbd2:f72 with SMTP id 5b1f17b1804b1-487290a9254mr185295365e9.1.1774876810909; Mon, 30 Mar 2026 06:20:10 -0700 (PDT) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48725eb52e5sm81322315e9.7.2026.03.30.06.20.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Mar 2026 06:20:10 -0700 (PDT) From: david.laight.linux@gmail.com To: Kees Cook , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Cc: David Laight Subject: [PATCH next 3/3] fortify: Simplify strlen() logic Date: Mon, 30 Mar 2026 14:20:03 +0100 Message-Id: <20260330132003.3379-4-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260330132003.3379-1-david.laight.linux@gmail.com> References: <20260330132003.3379-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight The __builtin_choose_expr() doesn't gain you anything, replace with a simple ?: operator. Then __is_constexpr() can then be replaced with __builtin_constant_p(). This still works for static initialisers - the expression can contain a function call - provided it isn't actually called. Calling the strnlen() wrapper just add a lot more logic to read through. Replace with a call to __real_strnlen(). However the compiler can decide that __builtin_constant_p(__builtin_strlen(= p)) is false, but split as ret =3D __builtin_strlen(p); __builtin_constant_p(re= t) and it suddenly becomes true. So an additional check is needed before calling __real_strnlen(). Signed-off-by: David Laight --- include/linux/fortify-string.h | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 758afd7c5f8a..6cd670492270 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -230,9 +230,8 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * c= onst POS p, __kernel_size } =20 /* - * Defined after fortified strnlen to reuse it. However, it must still be - * possible for strlen() to be used on compile-time strings for use in - * static initializers (i.e. as a constant expression). + * strlen() of a compile-time string needs to be a constant expression + * so it can be used, for example, as a static initializer. */ /** * strlen - Return count of characters in a NUL-terminated string @@ -247,9 +246,9 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * c= onst POS p, __kernel_size * Returns number of characters in @p (NOT including the final NUL). * */ -#define strlen(p) \ - __builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \ - __builtin_strlen(p), __fortify_strlen(p)) +#define strlen(p) \ + (__builtin_constant_p(__builtin_strlen(p)) ? \ + __builtin_strlen(p) : __fortify_strlen(p)) __FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1) __kernel_size_t __fortify_strlen(const char * const POS p) { @@ -259,7 +258,14 @@ __kernel_size_t __fortify_strlen(const char * const PO= S p) /* Give up if we don't know how large p is. */ if (p_size =3D=3D SIZE_MAX) return __underlying_strlen(p); - ret =3D strnlen(p, p_size); + /* + * 'ret' can be constant here even though the __builtin_constant_p(__buil= tin_strlen(p)) + * in the #define wrapper is false. + */ + ret =3D __builtin_strlen(p); + if (__builtin_constant_p(ret)) + return ret; + ret =3D __real_strnlen(p, p_size); if (p_size <=3D ret) fortify_panic(FORTIFY_FUNC_strlen, FORTIFY_READ, p_size, ret + 1, ret); return ret; --=20 2.39.5