From nobody Thu Oct 2 13:03:49 2025 Received: from fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com [35.158.23.94]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDC5C274FEF; Tue, 16 Sep 2025 21:23:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=35.158.23.94 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758057822; cv=none; b=roHX9va7hea+xmh7WGWgNQ36oH0m+L++kupavBeC5qNUIePNrIpf7LIj/a3gRXa73olha9zJ7yrsZ0Y9mUtYzhqviqZWZKP0/1glVcFk1lcNzERraSmWGyR0eXIO954faHQR6WSZZdWMb8aYzC/1zoi7ePQ0FOuNcPQMhBW8ys8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758057822; c=relaxed/simple; bh=pM0GBqHaGgzTZAGnSnn4Qw9WJ3hdqwctGR3yE354y1A=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hphNKuytjJs6V7S8WwbuGnVrOnIC+23U5I9uJ9vyXoxj2qcRpziHonzZ8KnKRw1UZF2idWhdB3tUHMsgEXMGrimAEiV/HWtwkwg1Boubz2W6p2FdhMrDmQcKTc1sa1W06g5l/w+ihcf798ZQ9f55y5YvFG1CBqsssb3XNRHNM2M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=AoKzvVUR; arc=none smtp.client-ip=35.158.23.94 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="AoKzvVUR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1758057820; x=1789593820; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/D/xQfMG4xISuPuewijkl5zRVX5oHEv0v15y1ZTU64s=; b=AoKzvVURTx++R4HgDf803i1leLyNbHbHiN05PZZ5l8MrAZkKj87WQhS5 7V1dZI7Dolqd+Zm76ehJXvZX8azjMjnRJfdnObUT8LuAC12AsnZQNZtuQ oWk6+v62fQ3U1+po5NGKFuMnPvXFekgcgqTdA8KV7nHBF0NcZ9nUft/wA 72jeLBhVXv+Wv0lXFbMEu5OTIIaM8eDHmsArDhP1VVRe/UPq07imW2JIu 9vGEQkc4zKG0jbh4cuP/rqxbvUv0CQwe7bPYrBwd1nilYlCMiGKwksAj1 vNy7raon4fFm1QNTA1axAjxv+jzct8OAzPp2NBPk/dqVhTfJM/42xpBtu g==; X-CSE-ConnectionGUID: ZOYVgWIKR9uYc+VYcI48uQ== X-CSE-MsgGUID: Dg2cLvD3RQy+dTLvVJcQRw== X-IronPort-AV: E=Sophos;i="6.18,270,1751241600"; d="scan'208";a="2214971" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Sep 2025 21:23:29 +0000 Received: from EX19MTAEUC001.ant.amazon.com [54.240.197.225:10742] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.21.238:2525] with esmtp (Farcaster) id bf89d354-2c59-4d77-bb3e-bd01be6eb971; Tue, 16 Sep 2025 21:23:29 +0000 (UTC) X-Farcaster-Flow-ID: bf89d354-2c59-4d77-bb3e-bd01be6eb971 Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Tue, 16 Sep 2025 21:23:26 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Tue, 16 Sep 2025 21:23:22 +0000 From: Eliav Farber To: , , , , , , , , , , , , CC: , , Subject: [PATCH 2/7 5.10.y] minmax: sanity check constant bounds when clamping Date: Tue, 16 Sep 2025 21:22:54 +0000 Message-ID: <20250916212259.48517-3-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250916212259.48517-1-farbere@amazon.com> References: <20250916212259.48517-1-farbere@amazon.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 X-ClientProxiedBy: EX19D038UWC001.ant.amazon.com (10.13.139.213) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: "Jason A. Donenfeld" commit 5efcecd9a3b18078d3398b359a84c83f549e22cf upstream. The clamp family of functions only makes sense if hi>=3Dlo. If hi and lo are compile-time constants, then raise a build error. Doing so has already caught buggy code. This also introduces the infrastructure to improve the clamping function in subsequent commits. [akpm@linux-foundation.org: coding-style cleanups] [akpm@linux-foundation.org: s@&&\@&& \@] Link: https://lkml.kernel.org/r/20220926133435.1333846-1-Jason@zx2c4.com Signed-off-by: Jason A. Donenfeld Reviewed-by: Andy Shevchenko Cc: Kees Cook Signed-off-by: Andrew Morton (cherry picked from commit 5efcecd9a3b18078d3398b359a84c83f549e22cf) Signed-off-by: SeongJae Park Signed-off-by: Greg Kroah-Hartman Signed-off-by: Eliav Farber --- include/linux/minmax.h | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 1aea34b8f19b..8b092c66c5aa 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -37,6 +37,28 @@ __cmp(x, y, op), \ __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) =20 +#define __clamp(val, lo, hi) \ + __cmp(__cmp(val, lo, >), hi, <) + +#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ + typeof(val) unique_val =3D (val); \ + typeof(lo) unique_lo =3D (lo); \ + typeof(hi) unique_hi =3D (hi); \ + __clamp(unique_val, unique_lo, unique_hi); }) + +#define __clamp_input_check(lo, hi) \ + (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ + __is_constexpr((lo) > (hi)), (lo) > (hi), false))) + +#define __careful_clamp(val, lo, hi) ({ \ + __clamp_input_check(lo, hi) + \ + __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \ + __typecheck(hi, lo) && __is_constexpr(val) && \ + __is_constexpr(lo) && __is_constexpr(hi), \ + __clamp(val, lo, hi), \ + __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ + __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) + /** * min - return minimum of two values of the same or compatible types * @x: first value @@ -103,7 +125,7 @@ * This macro does strict typechecking of @lo/@hi to make sure they are of= the * same type as @val. See the unnecessary pointer comparisons. */ -#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) +#define clamp(val, lo, hi) __careful_clamp(val, lo, hi) =20 /* * ..and if you can't take the strict @@ -138,7 +160,7 @@ * This macro does no typechecking and uses temporary variables of type * @type to make all the comparisons. */ -#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo)= , (type)(hi)) =20 /** * clamp_val - return a value clamped to a given range using val's type --=20 2.47.3