Received: by 2002:ab2:855:0:b0:1ef:b27b:cc29 with SMTP id 21csp1722454lqy; Fri, 29 Mar 2024 06:50:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVlqpcSJsan6ug5HeK+XTSGhWMr0G7XBKZVPeODttzYMeSlOPwAnRkPwcs3/p1gIIIOiYkHksfbInmcYhcrAkucfZ7eirSbvke7Sp13UA== X-Google-Smtp-Source: AGHT+IG9DSvV+enwjUVSGm7hj08XMsWfQ4UWNPXQurRto14BOf3/dXURifHftFypBvdJNBqn2aMg X-Received: by 2002:a50:8a87:0:b0:56b:ff5d:8bdc with SMTP id j7-20020a508a87000000b0056bff5d8bdcmr1550048edj.32.1711720237140; Fri, 29 Mar 2024 06:50:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711720237; cv=pass; d=google.com; s=arc-20160816; b=O/305EPxd1uBLc/qrgy81W9hhmVY9nXFOa74yi/9vyJKggpxm/8l3FXLQTpLNAShOd GNFXKtrQd9dLOF3VcAgdhDex0sIz2CKnd+A0RZSIKjW33PQgjO/48hTPLD6/dy7rhXHV zkRnmb9D+dh1dcVzPWno0jT2zGDLBQdNAa9EPNZkaoSB7rEIGOu+9YZgl25w9QE4O2kj 8U5MwDng4SELgweZVukoUFTlTmBx3QNY8mvx9rmgrvBwoccwlwOBcwrNniNfg9NuGFRq GIPGA8mwkLiJ6jN9aL9bKJMxI86FdvJqAsGr721TncneetheAiheGT2mxMvic2LXtaJu pwzw== 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=8ytYYx1Awr+SOiLnZMp6wTTaDvGIAJ6Gb8b47KCBigs=; fh=zoMWNTR+0uFqLKn0xjlwYrZgJbxdgrIaGzlI4U3/Y7Y=; b=A9psi5L34cddZeHlDMBP93rT/Ok3GHEPXnWUP1yTMakvvFALO4G8nAG60XDN8rrzj7 6aq+Gknhf2CLX7cpnQfMHy1aqpO0fz51FLtj1sjzHHrt3DcNFqM/73Pv025SU3jAf/1x 6G7j+GmgHx7o2Xl0XFSSnxaAzCIaY7Ppohr3qvl3iwzMXaMeSwNYalyv8UHUx/kbJ+r4 +2hEIXLaoiHl7nsnL31b+oLyAhFsptAT1qgYqNHm/IwCtrU+3wD/RI+OQQgzBAovWtCV R5/LXntxn99CB2HVUBODTjATy0my99++emZpvZR7chg2NGf7fZNVP08juaqa9RkUtGXk HrWw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NL2ppa1M; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-124784-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124784-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 c30-20020a50d65e000000b00568c5c7a3e3si1892375edj.555.2024.03.29.06.50.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 06:50:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124784-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=NL2ppa1M; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-124784-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124784-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 DACBE1F21392 for ; Fri, 29 Mar 2024 13:50:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 30ACB187E90; Fri, 29 Mar 2024 12:42:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NL2ppa1M" 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 46F0D187E76; Fri, 29 Mar 2024 12:42:16 +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=1711716136; cv=none; b=ex5vmLhJXujY+TxWUwaGeUnoDyJj0CyCiS94epfCBQhFgrXtBgixQuDXFFp1iYp0J0z8kIf+bc/YsOkn5yt/45twTsrSy/A06EvLIPVt19vC3sB4rcAeJ71IoS/sf9MncWoLKkyy47cJfod36ErIBCrSADu+BsRlEd55c9QRcms= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711716136; c=relaxed/simple; bh=yFrxUrCxr4nCVz5WlYcMp85OB4SzOnEQNJZ80EpWwjY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=X7Ji0/xx+T5Z3eiEHa5J2TqYAe1SSBcQtQqKZVRuKcXVPE2B1CPOgxNwVHZNeAV0r0jFosixKH/CX5XpaHRaLPNT49eOQsmVbsb3QxGr/nCMVBlR66tnHvniT8t263Y9jNftOW7CGHQBHiOuSaOufb6Wu+9Zxbj7I4+y1UNBGBw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NL2ppa1M; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89165C433C7; Fri, 29 Mar 2024 12:42:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711716136; bh=yFrxUrCxr4nCVz5WlYcMp85OB4SzOnEQNJZ80EpWwjY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NL2ppa1MANHS7QW6IJ8ZhaStVnXIKMEe+0M5QYCpi1ZN0vcAmnPIk4kiQSHVzFsAi iYmZFRjQmnfQRvoQqhnoMo+Al4prpcJ1yberzl5uXELKT1lhbMl3lUeYvrsL2m8Gak 2ByO9H13u/Q7/hfyjs9wb4Kep5ZplAO/5x8RpuI6MUw4dP7lli8+4Lgw3E2cwiEobo umZZ/BkyXN+w+I7sDYKSN/58GAwjwXJ/Zv7w03QrROtwq9Ji5hn0jqXq2tLpK98W9w u4HhSHAuNCAOyBt3BXKpGlsKPJ4SQtVZb8axRgaiO4oXjuv7w3UfpLFB6eVcMdBl7K uw1+kyK4Jna6w== 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.8 74/98] overflow: Allow non-type arg to type_max() and type_min() Date: Fri, 29 Mar 2024 08:37:45 -0400 Message-ID: <20240329123919.3087149-74-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240329123919.3087149-1-sashal@kernel.org> References: <20240329123919.3087149-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.8.2 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 7b5cf4a5cd191..51af56522915e 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