Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp14270lqz; Fri, 29 Mar 2024 07:12:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVYjs2X9vHbbAbcDB0p50iW5emTDQMaPRDi8s9fSVp5AHyyIRBLbT6fKiHoz8PMlrxMxqX19JG+um/LPGBEvkNLfU50NMrJI3sr/TJCng== X-Google-Smtp-Source: AGHT+IGBylGLFAp9wf3FztfrK1Nzgi46vFYruCHy3uDkk75T4bptkalVxSvC/BSMjmaqX8n6cDWY X-Received: by 2002:a50:bb47:0:b0:56b:dd0e:9efc with SMTP id y65-20020a50bb47000000b0056bdd0e9efcmr1371760ede.38.1711721549047; Fri, 29 Mar 2024 07:12:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711721549; cv=pass; d=google.com; s=arc-20160816; b=XWUYgKqcBNWw3veIPKPfJk2p+xcWAr3Zd3Qaa57ZtMXWukM/EHHWqhPrInJWcmFdSr 0Ej+30NqxzumIDhwOHvByUR19/jvF40QPmBXr6h3SlY36FVctPlfenGV6SfboZxVVad9 Y9axzSXO95OF8bMs4RUgD4qBQ6iY2W3TmR8pJsG02Nd9zVvWGeJoKD7J8zSKDOsWL3Ua NUc5gmqOUnisLihfvr2V0erzggrLENIBNJwSWmhnKKdMAWvRiyJwmVTwnmtC+TvL8uEz IaWyN6ujJ1ss6y5AS8FhPCa/HseOT+niSGXKbadlGrdqGWoiHOWFyBI78mGUL/c1qcEn /SeQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=mlUJh7gyU2/gXfaziIbCczTk2tmrQxSsnw1GuXtj0fE=; fh=zoMWNTR+0uFqLKn0xjlwYrZgJbxdgrIaGzlI4U3/Y7Y=; b=uSSnfZrmVAompKOrxVTD4pYkqKxU0gIgnpMJl9d5B2kvyUpSyMzbavHdqpBiRItQpB onsqUfsa2AeTQGPsE+GCxNk1uZmtmxMspvpIxps0+E1tSFIIFA6elyWE7yCeGgbCNOst 50nEYFnx/6EgyiSlTBng2+rzD7eWEdZEuGwPg4lcQPZ5jMlFldJs29wO8BgBYCMYArLz 6A5HoCgYFMkFQQTxmFjzrg2kN7ICM5KlxkYNLemqEo6EWFtljr6BOqu2YmFjySjI4DDX vrHGgIs8F4WMTbRhbqW4oSa0t7iOg7yhm5BSTi7XvZxjpE/BoUht+MuKwA86Pa9W2xke 53LA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BsABKrKw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-124863-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124863-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ig3-20020a056402458300b0056bf2890a58si1920123edb.691.2024.03.29.07.12.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 07:12:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124863-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BsABKrKw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-124863-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124863-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C4F201F21ACC for ; Fri, 29 Mar 2024 14:12:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 15B5314A601; Fri, 29 Mar 2024 12:45:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BsABKrKw" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 35BA31C8FC8; Fri, 29 Mar 2024 12:45:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711716311; cv=none; b=o3Vg003OikW3ExxqcNSA2zGqvENUvigZYghqU/OAinmqNCJY7UVO8PqXgJe01OvXkOCav61RbOhPSorRajfUZqjX7UXhpAfN6dx43jylM1YFRhLTrxxdygaNYVdLUX2dG3Mys0xYO4IFrYMPcYSnX2e2UmkZkwINQOj2hCeUgiM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711716311; c=relaxed/simple; bh=2XkywHGTNla9wNUnN56Y6gr1IeKydyjwYr37lT38rYY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JoJgBMtpt0J4y/khxrMIJarGgV9wpRCQY56VAR8f5Uole0TPuR8MWDWTeRCBJ/cBPnp2TfWmyezeTXSZxxxoyQtm3+KoaC0+w9TEhXFbDkkxr8pgq6J0YMMqgpW6fiPn2cp9Bh7Zpla8NydtUar9zoRI4f3amWUqMVTKz+tJZKw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BsABKrKw; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A293C43390; Fri, 29 Mar 2024 12:45:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711716310; bh=2XkywHGTNla9wNUnN56Y6gr1IeKydyjwYr37lT38rYY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BsABKrKwIL69/48G7dR+/H8tXygP/livDxOAUkBYS1ELEP3KkFxi67QozZyEYmyu7 RJ85gigVq7ag/UF2o0qzzVHr73lCLx9PF8/tLpltm0eGTiw3jqwfgX3Zeeeng9rCm/ n23+CdahUueiZzGY7RzMaaXgp5qT0o81XMBSmezgRnxwbBE6Bae1PmzX90kj5AY3rr Ea9PQ0DL5sUqjsxvljON15tiM8G7hIzSl/VNvl3PSeGcEJaEA9uhmdWm9FM5l8A1SW 51gTI2OhA0+GCFVK5ywhFdLX/fdRXhXE1I61wXYkyDcIoDKGFa6g/rXmODI1T7Nand VNIPACSCbTpEQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kees Cook , Sasha Levin , linux-hardening@vger.kernel.org Subject: [PATCH AUTOSEL 6.6 54/75] overflow: Allow non-type arg to type_max() and type_min() Date: Fri, 29 Mar 2024 08:42:35 -0400 Message-ID: <20240329124330.3089520-54-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240329124330.3089520-1-sashal@kernel.org> References: <20240329124330.3089520-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.23 Content-Transfer-Encoding: 8bit From: Kees Cook [ Upstream commit bd1ebf2467f9c5d157bec7b025e83f8ffdae1318 ] A common use of type_max() is to find the max for the type of a variable. Using the pattern type_max(typeof(var)) is needlessly verbose. Instead, since typeof(type) == type we can just explicitly call typeof() on the argument to type_max() and type_min(). Add wrappers for readability. We can do some replacements right away: $ git grep '\btype_\(min\|max\)(typeof' | wc -l 11 Link: https://lore.kernel.org/r/20240301062221.work.840-kees@kernel.org Signed-off-by: Kees Cook Signed-off-by: Sasha Levin --- include/linux/overflow.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index f9b60313eaea2..e04f679476473 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -31,8 +31,10 @@ * credit to Christian Biere. */ #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type))) -#define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T))) -#define type_min(T) ((T)((T)-type_max(T)-(T)1)) +#define __type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T))) +#define type_max(t) __type_max(typeof(t)) +#define __type_min(T) ((T)((T)-type_max(T)-(T)1)) +#define type_min(t) __type_min(typeof(t)) /* * Avoids triggering -Wtype-limits compilation warning, @@ -130,10 +132,10 @@ static inline bool __must_check __must_check_overflow(bool overflow) #define __overflows_type_constexpr(x, T) ( \ is_unsigned_type(typeof(x)) ? \ - (x) > type_max(typeof(T)) : \ + (x) > type_max(T) : \ is_unsigned_type(typeof(T)) ? \ - (x) < 0 || (x) > type_max(typeof(T)) : \ - (x) < type_min(typeof(T)) || (x) > type_max(typeof(T))) + (x) < 0 || (x) > type_max(T) : \ + (x) < type_min(T) || (x) > type_max(T)) #define __overflows_type(x, T) ({ \ typeof(T) v = 0; \ -- 2.43.0