From patchwork Tue Feb 6 10:31:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197308 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1450086dyb; Tue, 6 Feb 2024 02:39:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IHpoBNowmLVcUWVnnLUMH7bYpbFX832kwSVLIRH9VCw2vLstWlUZsNnXdxht0TqfNA3nA6R X-Received: by 2002:a17:903:1251:b0:1d9:c876:b82f with SMTP id u17-20020a170903125100b001d9c876b82fmr1224624plh.30.1707215986184; Tue, 06 Feb 2024 02:39:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707215986; cv=pass; d=google.com; s=arc-20160816; b=swIdk75XQXm3q9ErfDN6TM2u9gucs+wSGBKf8Su5XxoXTzfNnsLIq8St6oVz2cc0LQ /W7wQealvIqflXeJTm2gtNYJ6qTPOmtkoCefM2RlxhR64y1GePCXxdY1lDdxH7lybDXI SSwGXgPKs3x4+I7/bnMrOLwwFXcJOx86kAEvmiVS+Ms8B9YN0Le36/sp4WUeveNUeT/S vJ/LOILd/s9lbK8n/n3qi5gzC3RsBqUB3rUH7vEH9eancNpo4794fIx+jM+PU1FCk6mh FfMpn5OBgO/2/Mco9AXWkNRJJmbVT513+PaSjbuVFFLwmRa6ZUQWZVDmMHwS2pwedypX Vs+w== 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=QphZTJ2YkJ63c2lk2h3jm8PAaMKsJdXhMlN0zVjn/Jc=; fh=/KXVm9lIvY1zR4v19lAj3Cez9Zwl9FZw2Eeu9PtADk0=; b=yCQ/toQsuvaJ02QFd9is298Txy/k4N9cjktlqW1V8d0gkrFfE8BLMx5Fy+4lxlCCfm qPPrWF7xIYWUwAp6pv7P17+lGWFsAxcrloUbnuAylz81DTH4wj2PduA5IZkhf7UeR0DH gDpr7vtNalxR6NCBZiqh/msMCFJqbgAgUZOLaHo6yWnGEA0X4AD+AZ1weCC5XI8qGgmA 7pCXP+NAS74Mc2W7HibX1JUcoij7qC2u4gsxFYdw84mxQrpcO+KaHYG3AKxpLKGJsJjW dpUhLk+cHSayj0y4pzjmw7vCkz8E6G9gG0J2RMNhjTJXSFNMSXpjiaaMKP2T49SKgG6X 3wzQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=e7Fe3LiJ; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-54692-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54692-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=1; AJvYcCU8XTbLEbItVHJ+S/BoZrwhkkGAr4bU4eUw2g9SAzIyZSXatdJPB5gNYJCOERC1R5bEjngb/XvG1isJd0DMuVhTNjP+3g== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id jb3-20020a170903258300b001d71d722818si1408572plb.513.2024.02.06.02.39.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:39:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54692-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=e7Fe3LiJ; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-54692-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54692-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id B54B5B261AB for ; Tue, 6 Feb 2024 10:33:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9209612EBDD; Tue, 6 Feb 2024 10:32:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="e7Fe3LiJ" Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (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 9782C33EE for ; Tue, 6 Feb 2024 10:32:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707215525; cv=none; b=UE49bkU6sxEYD/wfwafTYIGuiPZ5oz2rhpVOrzfvGQFKP1wt7fDdkpBQqif0jO1p2PK0ssgidHhFUAjw4MzbljHgJt9c+oCg9EHT73TfsxO5pHA0/0NSfzdb49+4Wa+LcdvWmwP+GkYKlLklhsUNjRGODw0YMkkh0EMl2mf2ALQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707215525; c=relaxed/simple; bh=6Jgp3vxxK3Vk/XDvje4qMUZMER6LkaJQhb8c5Sf9IQw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ciuA66VJJuw2HIhcIdlWdUiOKtUHY/3mfNYNdHO0j93l5cV823IW4QHd0iEqMN6NMiz+SBbxI1d7v1s6wq9KyZVwDLyO77xEoKHw46opZd46yVWCQuOi8JTDiOXpTYqexTsnfjCD21ZRG3k0HI7Xqr3QxTHNukeA2gOQlSNovEI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=e7Fe3LiJ; arc=none smtp.client-ip=209.85.215.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-517ab9a4a13so5229245a12.1 for ; Tue, 06 Feb 2024 02:32:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707215523; x=1707820323; 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=QphZTJ2YkJ63c2lk2h3jm8PAaMKsJdXhMlN0zVjn/Jc=; b=e7Fe3LiJ+u+HN07QeS+PBl4pdMYJdzrp9SYc5hKp5068XOjOCROeyucoUivJAv/2jg YeaMQ1U+SSzXcWgWs/rL0tMoz3HI6Gs6AQYSgVHS+W0msQlUpELBY+h+Ca8Hr4oUi1LR 9Fo1NwN1+ho/G7+aKRtqUTPeA0f7bfaRfywOo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707215523; x=1707820323; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QphZTJ2YkJ63c2lk2h3jm8PAaMKsJdXhMlN0zVjn/Jc=; b=aMZos4Pva5dBSwz6dhMtsMEZw1wKBxdZ2YwTxpMCHvSbbMi9KUNZCAcHTT6QLzh34e qD0JN+sddyKnQyIgiSqEcjepM+aH6S+ONgXfS3SmMnkhi8J0GEE6ZZQA6W6tBzqP/jYz tLc9xv4dnX4KSAo3qVdBteBQkAncb9ssjVL8whZBMzlhcmIpmDZ2fFE7Hzt1vX0oQYdv egA2Vv+vKOgTCDhd/o+qH76tGaxvPQVCqcMdiPGIn2gjReDsRQFwtLJD9YhzezEleKA5 mH2sjy0rGYPP+cNNmxR/65shS9SeJ5noO6gX6+4mn6YfJqh+wJbAq00LgVRTwCIJGEnb nuhA== X-Gm-Message-State: AOJu0Yx7SP82VTRFVfDQElIoHBKWSWPzGGSYagl689Zyle+SmluDSPfy ADU2EGbbZRM2NdzRRt3GQCFYfdUy/6Bg8In/R6oj2fVlUy8PURsOPuD1r9ik5Q== X-Received: by 2002:a05:6a20:4294:b0:19c:8bc7:c82d with SMTP id o20-20020a056a20429400b0019c8bc7c82dmr1281941pzj.40.1707215522983; Tue, 06 Feb 2024 02:32:02 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVZ08SRVjb0/BRWQtEe0sMgtIsd0pJXgjb1JJOKV/fs8H8l2wXG5WzYVMiQNiW4XOV7ukVKqVdSrnlZSST5gwYsI9tthgnKdzhrOzNjIpQd8GntKZ7g6fKl3GDmAJ1N34U3n05S+DTKk25mEMfnt7Hc1Ll+vBqX0Oqj+WFDlVeb/F1EA97PJU4N9nKU9387uMQPpwefA66sVj7Ah3S2U6/OQLJVUVUlsXhz/I6RtG5+zG+YQAYX6vpv1TsVW8h8DbEMLfsj8+qD9V1ZSsAfT4Z/tBwF+ASbfLaJT4k8 Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id y28-20020aa793dc000000b006e033804fa8sm1497952pff.101.2024.02.06.02.32.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:32:01 -0800 (PST) From: Kees Cook To: Rasmus Villemoes Cc: Kees Cook , "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org, Marco Elver , Eric Biggers , Mark Rutland , Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v4 1/3] overflow: Adjust check_*_overflow() kern-doc to reflect results Date: Tue, 6 Feb 2024 02:31:54 -0800 Message-Id: <20240206103201.2013060-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206102354.make.081-kees@kernel.org> References: <20240206102354.make.081-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2673; i=keescook@chromium.org; h=from:subject; bh=6Jgp3vxxK3Vk/XDvje4qMUZMER6LkaJQhb8c5Sf9IQw=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwgqcMG0B/D0l8I3Zre9ccIWewGWghw7fRDA6+ Ex6geHfWP6JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcIKnAAKCRCJcvTf3G3A Joz+D/0cfyydT7lF4BjFsEbIOJCxDYkkLmOjtUQN7/LCKYtVZm+DLMpe3xQlqQ5642hnqrHlwvW vbBhB4FtiAAmcgYL1tSSVzwAKiYZHRjg9/TGsZzYfHuCx7IZmw+bjwONCr0xGQQ7ECB1/9owrH2 DBesL91WSyxI+bxbU6d1oPB+8xsJ1QG1FPLRJAqAlmD0reEby+mINShrJTMfzvwG7KGUWK9MaCE 5idH7bzvD3u23dl6N5X/Gk83T3JNAcQbSZN/PCmcASTFABWLJeMRzhbnoZrqdEyAnU/GVRrmkH+ 7UQHA75exvR8iRTk699l61Kfvl+iT6ieWXV3zgw7mluq5BzKRlav6YRy9hECMP3DAiD40WkFnsV 1wZB2B2doRYnAN99/20Woo0U+F0qG7L2xEvNXdkk/luDZSF19iGV4C1PvzmXlWakqmTD21WgEBn QBZIINqi7eDUime2W+T7JDAmNlIPRjD+8GrVsGZ0KOJ5oG5SZfwk98JGI08tWyrF2LD62r0Uzh4 kVUzw5clpopb7ORRQ90x5ldF8N8UE2bnVBJF5pFUQOcx1wzds1rm6bR/WWIgKCFK/hZRgBrH7PZ /+FNbKQba2rqh+XHg+My84GC3PYtbU/1raU3g9lx1d2zDVe5EzthoHelVjcU8GhAPTtWTmRQ6yF VUBItL7K5+oHnbQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790145709664098229 X-GMAIL-MSGID: 1790145709664098229 The check_*_overflow() helpers will return results with potentially wrapped-around values. These values have always been checked by the selftests, so avoid the confusing language in the kern-doc. The idea of "safe for use" was relative to the expectation of whether or not the caller wants a wrapped value -- the calculation itself will always follow arithmetic wrapping rules. Cc: "Gustavo A. R. Silva" Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook Reviewed-by: Gustavo A. R. Silva --- include/linux/overflow.h | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 7b5cf4a5cd19..4e741ebb8005 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -57,11 +57,9 @@ static inline bool __must_check __must_check_overflow(bool overflow) * @b: second addend * @d: pointer to store sum * - * Returns 0 on success. + * Returns 0 on success, 1 on wrap-around. * - * *@d holds the results of the attempted addition, but is not considered - * "safe for use" on a non-zero return value, which indicates that the - * sum has overflowed or been truncated. + * *@d holds the results of the attempted addition, which may wrap-around. */ #define check_add_overflow(a, b, d) \ __must_check_overflow(__builtin_add_overflow(a, b, d)) @@ -72,11 +70,9 @@ static inline bool __must_check __must_check_overflow(bool overflow) * @b: subtrahend; value to subtract from @a * @d: pointer to store difference * - * Returns 0 on success. + * Returns 0 on success, 1 on wrap-around. * - * *@d holds the results of the attempted subtraction, but is not considered - * "safe for use" on a non-zero return value, which indicates that the - * difference has underflowed or been truncated. + * *@d holds the results of the attempted subtraction, which may wrap-around. */ #define check_sub_overflow(a, b, d) \ __must_check_overflow(__builtin_sub_overflow(a, b, d)) @@ -87,11 +83,9 @@ static inline bool __must_check __must_check_overflow(bool overflow) * @b: second factor * @d: pointer to store product * - * Returns 0 on success. + * Returns 0 on success, 1 on wrap-around. * - * *@d holds the results of the attempted multiplication, but is not - * considered "safe for use" on a non-zero return value, which indicates - * that the product has overflowed or been truncated. + * *@d holds the results of the attempted multiplication, which may wrap-around. */ #define check_mul_overflow(a, b, d) \ __must_check_overflow(__builtin_mul_overflow(a, b, d)) From patchwork Tue Feb 6 10:31:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197305 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1447302dyb; Tue, 6 Feb 2024 02:33:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IFbwkn3CT6TSfJlLS8/Y1HiMrdklq+CN3X9erERptkgqzndY+GUGVr8M1DyEibinp6OGT5y X-Received: by 2002:a17:907:b9ce:b0:a36:917f:556f with SMTP id xa14-20020a170907b9ce00b00a36917f556fmr1344394ejc.7.1707215591835; Tue, 06 Feb 2024 02:33:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707215591; cv=pass; d=google.com; s=arc-20160816; b=UUQIAv9tM4irIsxymULtvFmncYgcxqCI/9Uwnp76s0H/Ll+itNJ0ZsPox5Z8aXCmNF 6iL0TUn8NY1g6210165TFG5/lT0OM3dUS0z4XATGDNh8R8FWeH3YvLoif0gy1P9mxDSp OcaYnbqu4VXQOgxYzqyvRSh3cweuvbGZKN4AqtirHsr8Zdgq/rgBDtVq9M6PPn6m91G3 Sx4vHJ2M9mRKQ/dEVFbvnkhuXRx+IDeld0QuOKgDYRl7InXz4VnlrCPWvfMfhX9J2Nzw +bz+H4golYgKZhKmW7B/M/AjaLsuf3itgSdTHhJzH8vWMonQbjtZuvyBiTDpjU1unlIB 8Pvw== 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=vhFCws/Ub8aMZOpLyoDbYEiUVdxcrMWspU59qodMGdQ=; fh=tjpLbcIgGmd7zCXT0QHttq+Vfhv+RnEMhgSjTL0v79Q=; b=kWm5AlRASUvim8iUp8tccFX9ImsGkPKX5ZKEhOncGmdbM6+iO8qgKOErr6fDASZJhu C2UUOsENUEMLcziC50TxiTqt/V6MO90k/3P/1TKodUfeu3+cm40POKfpSN887CXU7jgZ +rGmQcps7M3sqGrCUerQU7CQtBV2jJHs+gZZP+Q8KUUyeUboLZXUU8Wr6jpP12uCTRgN NjpJwuqRtvRfR0y5uRbPRJaU1ZSE/O018mrdJfaovUgqiz1cJuAAazm9FMCnLiPAowqb S1iqAnmhn3ks8z2I3cuYwUYBYNQEKewvwweyXucCAKGFkZhQxSlwBgyM+kBUDVoG3TvG qcng==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gbv2Kr3O; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-54691-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54691-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=1; AJvYcCV9zA9qDVpSHmoqjk6RIlvUnn0OkDCRctZTYgRh6sRvPdkCWiQ1MaiDHWOArfz6D50+RdRGqOthEjKuz2+w8rfiIm0koQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id i16-20020a1709061cd000b00a382f17c9fcsi417695ejh.56.2024.02.06.02.33.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:33:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54691-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gbv2Kr3O; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-54691-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54691-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 9A1561F25773 for ; Tue, 6 Feb 2024 10:33:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EDF4912EBE9; Tue, 6 Feb 2024 10:32:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="gbv2Kr3O" Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 3306A1AB7E2 for ; Tue, 6 Feb 2024 10:32:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707215524; cv=none; b=cLQPdqkWlYO0jW8afSxLq9lqk8EoZuFtZRlRrbvd7V0+Y8wn8Oh2XEi4AOufIZLQ9r+M7k7YngxRohJDn297cCYUG7Lws3ZPKqfe4cUx0gPFNddyNT+r3PN3hXdijPbC89WI2VurTHzSfe1DUKWlENxjUF7+Q2L2o7ssKrLD6/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707215524; c=relaxed/simple; bh=DbmgLK5FCaGALvzwgkJxPr5U6aJ/eDPf0sDwp7xRfDM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pHyfyt072Jq8hDmilqfLQV2go+FeLxsd2l/wMuRaS3MS+0TjrTUchlCb2SVRWkDpA2vnb4GAr1k/ZcGERn95aaMyfXQH2Tyzn2eJQsvGlcOFaznr5W0qCTbYfrLxReupgzToQxNSaPHmxZTihnwords3RWH0XNcZSR4oZbbABRg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=gbv2Kr3O; arc=none smtp.client-ip=209.85.216.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-295c8b795e2so4007297a91.0 for ; Tue, 06 Feb 2024 02:32:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707215522; x=1707820322; 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=vhFCws/Ub8aMZOpLyoDbYEiUVdxcrMWspU59qodMGdQ=; b=gbv2Kr3OHPjJzTq4TMso9j5nYJZYOdz4BIhDoijSZkmM7b1GC7Age+OuuAGC6dNlte OBVN0hMZjhys51Nfl2zIULZerufkAjn4Qfr/Mn5woLHJk76rh8zAmPfQX24ohYOhPt3s qn98UQETftRhr4PcwIQYgU/kkvuIzil0E+t8k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707215522; x=1707820322; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vhFCws/Ub8aMZOpLyoDbYEiUVdxcrMWspU59qodMGdQ=; b=laZwMrivpYgrTb8ukcKHtxSfurUCTmho/z8Ys5I2HMPDEty9ijR94dROFELWKAIMeb mf7+N7U7QgjSgEsf+CHaFnF1OZfn5sZt9U+saqRcI98xTfCCARd6OebG244D/enwftMu KT8K0ZOordB0dPLzXp8hEitPiJb/nBqgmDcjL5zGCSWiWH6xY1G1GTeor1VTicizWmzr rCKpe/SRH+jpThNI62WebW8IqypZsmVWhVJK4fXd6r+L8Erv7LGYvmiow6zAg81kjgtf gzIrTX9eAmERYtyAG+Yt/SxIipogAMPQaMCfbnHTio/uLn1LFhYiMw+hxBd2wl1lGbqC XaEw== X-Gm-Message-State: AOJu0YzW8mBE+1ldOaeKPV7tVwu4/5odjIqZg7kkZxwizjPR99SPoGm/ aHUv1+7xvDD5UrS0cpoW7MWYaCBjVAWtAdTaHRtFb4wn1xDF7y/KtsnH2TmJnQ== X-Received: by 2002:a17:90a:f494:b0:295:d782:95df with SMTP id bx20-20020a17090af49400b00295d78295dfmr2109624pjb.34.1707215522544; Tue, 06 Feb 2024 02:32:02 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCW6xKecjlbMLOyR7rsVt6VQVhxc1dg/yMd5hmR9JhAIFhOtIY0O6kh1WCLq5KNQsHnLKfnJgJyUDpchLNTHtKalC7uSYWpZ5oYdp+VO5sLE268Kh8+GtJ6DES7u6soeofZAWViHoL+NNRO5u3JhWNt4blVuWdAoJ8WN6+R6UbHkJ3q4XWMYLa3hlhPIxScIjT2PsjnOzcqsPXb8ag9WuSLE67m0ZiH/UTU1zXptEQw/snC56WwdjNNnKH2r+JitAMNuqKgVUztGDstl2VefltqvBpD4Dd+VG4TBfLGY Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id a5-20020a170902ecc500b001d752c4f36asm1489543plh.78.2024.02.06.02.32.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:32:01 -0800 (PST) From: Kees Cook To: Rasmus Villemoes Cc: Kees Cook , Marco Elver , Eric Biggers , Mark Rutland , linux-hardening@vger.kernel.org, "Gustavo A . R . Silva" , Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v4 2/3] overflow: Introduce wrapping_add(), wrapping_sub(), and wrapping_mul() Date: Tue, 6 Feb 2024 02:31:55 -0800 Message-Id: <20240206103201.2013060-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206102354.make.081-kees@kernel.org> References: <20240206102354.make.081-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5732; i=keescook@chromium.org; h=from:subject; bh=DbmgLK5FCaGALvzwgkJxPr5U6aJ/eDPf0sDwp7xRfDM=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwgqciWHmTjoSuG/dx6zOi/1l/YjqP+laoqdfR ovdssAE7m6JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcIKnAAKCRCJcvTf3G3A JsXxD/wLUYpp78EMl8iOGH6disPMQua5RJtfe5N1B4ytfwBB+lsT7+xb3YG3ihf+QTvdR8acujV LJz2JioIhloCYJxZsh/sdr6RxglRGzJRuZeFeTnPJxgc7tdmzmFvGcSZ0EnqccKRu3llkDcD3qy KEl8wNBJzwigEn9+RtrlT50EdAAN+ONrrGlCbJvQ0OTCWTgdYvnXSre2h7AdvmPCtViKgF7v7MK hIQJJ6ev6Re0ZWoiwVyx5tYKI0mXOybtQLvrzBRqpp/oAuf3OmGt5u1lhQORCYSSAzgnWkBZ1sG W4R7o1Hd8J7X64Qhm/vVrnSjAlvq85wzHnHGRxsuT2KpUGPyKqRS4F9PKahzAO0xGiDnknvPWVr uLo/5R6NQFrQO4dQa+o3/DsX9XlCSZP0AF6Ny3Zwv3UCsY4STZv9Ouy1PyYbBCKrLaWRTHMjESU vMD5kg9YQKl/BSRWPJqFxDnvycSfRZf3isW820kO2dwgm9yjh++HR4i0quEbGRHEczay/XJIAor vPhHn2AV1T6xBHqGfhMmfwNEnP5RSyPKq5grCMeue1tI5HGCEO20qsyqs5HNF0Dpnmp74rU+Mfz S9j0POaTnS0wRZCnt03d7BLdam2qnsPVFeriTl9AVZ6lm90pNeOCge31dNokQIaMGCauu70mLc2 oQeyhZ32FLyJBXA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790145296214385856 X-GMAIL-MSGID: 1790145296214385856 Provide helpers that will perform wrapping addition, subtraction, or multiplication without tripping the arithmetic wrap-around sanitizers. The first argument is the type under which the wrap-around should happen with. In other words, these two calls will get very different results: wrapping_mul(int, 50, 50) == 2500 wrapping_mul(u8, 50, 50) == 196 Add to the selftests to validate behavior and lack of side-effects. Cc: Rasmus Villemoes Cc: Marco Elver Cc: Eric Biggers Cc: Mark Rutland Cc: linux-hardening@vger.kernel.org Reviewed-by: Gustavo A. R. Silva Signed-off-by: Kees Cook --- include/linux/overflow.h | 54 ++++++++++++++++++++++++++++++++++++++++ lib/overflow_kunit.c | 24 +++++++++++++++--- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 4e741ebb8005..429c4d61a940 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -64,6 +64,24 @@ static inline bool __must_check __must_check_overflow(bool overflow) #define check_add_overflow(a, b, d) \ __must_check_overflow(__builtin_add_overflow(a, b, d)) +/** + * wrapping_add() - Intentionally perform a wrapping addition + * @type: type for result of calculation + * @a: first addend + * @b: second addend + * + * Return the potentially wrapped-around addition without + * tripping any wrap-around sanitizers that may be enabled. + */ +#define wrapping_add(type, a, b) \ + ({ \ + type __val; \ + if (__builtin_add_overflow(a, b, &__val)) { \ + /* do nothing */ \ + } \ + __val; \ + }) + /** * check_sub_overflow() - Calculate subtraction with overflow checking * @a: minuend; value to subtract from @@ -77,6 +95,24 @@ static inline bool __must_check __must_check_overflow(bool overflow) #define check_sub_overflow(a, b, d) \ __must_check_overflow(__builtin_sub_overflow(a, b, d)) +/** + * wrapping_sub() - Intentionally perform a wrapping subtraction + * @type: type for result of calculation + * @a: minuend; value to subtract from + * @b: subtrahend; value to subtract from @a + * + * Return the potentially wrapped-around subtraction without + * tripping any wrap-around sanitizers that may be enabled. + */ +#define wrapping_sub(type, a, b) \ + ({ \ + type __val; \ + if (__builtin_sub_overflow(a, b, &__val)) { \ + /* do nothing */ \ + } \ + __val; \ + }) + /** * check_mul_overflow() - Calculate multiplication with overflow checking * @a: first factor @@ -90,6 +126,24 @@ static inline bool __must_check __must_check_overflow(bool overflow) #define check_mul_overflow(a, b, d) \ __must_check_overflow(__builtin_mul_overflow(a, b, d)) +/** + * wrapping_mul() - Intentionally perform a wrapping multiplication + * @type: type for result of calculation + * @a: first factor + * @b: second factor + * + * Return the potentially wrapped-around multiplication without + * tripping any wrap-around sanitizers that may be enabled. + */ +#define wrapping_mul(type, a, b) \ + ({ \ + type __val; \ + if (__builtin_mul_overflow(a, b, &__val)) { \ + /* do nothing */ \ + } \ + __val; \ + }) + /** * check_shl_overflow() - Calculate a left-shifted value and check overflow * @a: Value to be shifted diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c index c527f6b75789..d3fdb906d3fe 100644 --- a/lib/overflow_kunit.c +++ b/lib/overflow_kunit.c @@ -258,20 +258,36 @@ DEFINE_TEST_ARRAY(s64) = { \ _of = check_ ## op ## _overflow(a, b, &_r); \ KUNIT_EXPECT_EQ_MSG(test, _of, of, \ - "expected "fmt" "sym" "fmt" to%s overflow (type %s)\n", \ + "expected check "fmt" "sym" "fmt" to%s overflow (type %s)\n", \ a, b, of ? "" : " not", #t); \ KUNIT_EXPECT_EQ_MSG(test, _r, r, \ - "expected "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \ + "expected check "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \ a, b, r, _r, #t); \ /* Check for internal macro side-effects. */ \ _of = check_ ## op ## _overflow(_a_orig++, _b_orig++, &_r); \ - KUNIT_EXPECT_EQ_MSG(test, _a_orig, _a_bump, "Unexpected " #op " macro side-effect!\n"); \ - KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, "Unexpected " #op " macro side-effect!\n"); \ + KUNIT_EXPECT_EQ_MSG(test, _a_orig, _a_bump, \ + "Unexpected check " #op " macro side-effect!\n"); \ + KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, \ + "Unexpected check " #op " macro side-effect!\n"); \ + \ + _r = wrapping_ ## op(t, a, b); \ + KUNIT_EXPECT_TRUE_MSG(test, _r == r, \ + "expected wrap "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \ + a, b, r, _r, #t); \ + /* Check for internal macro side-effects. */ \ + _a_orig = a; \ + _b_orig = b; \ + _r = wrapping_ ## op(t, _a_orig++, _b_orig++); \ + KUNIT_EXPECT_EQ_MSG(test, _a_orig, _a_bump, \ + "Unexpected wrap " #op " macro side-effect!\n"); \ + KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, \ + "Unexpected wrap " #op " macro side-effect!\n"); \ } while (0) #define DEFINE_TEST_FUNC_TYPED(n, t, fmt) \ static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \ { \ + /* check_{add,sub,mul}_overflow() and wrapping_{add,sub,mul} */ \ check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \ check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \ check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \ From patchwork Tue Feb 6 10:31:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197306 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1447455dyb; Tue, 6 Feb 2024 02:33:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbxLXlhjCnQdJbidehUU+t/V7y/3lchKpmKx7v2gnsJrYlZ+9zzpFSDLS65NZZkGg1OBqo X-Received: by 2002:a05:6214:e6b:b0:686:9de1:7015 with SMTP id jz11-20020a0562140e6b00b006869de17015mr2277903qvb.61.1707215612696; Tue, 06 Feb 2024 02:33:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707215612; cv=pass; d=google.com; s=arc-20160816; b=o9GyKRXwfP+86zry8qEFHZVAHFbNhtAoNt924qSMT7MM5hhb19y4RT7jjuOlol2X1Y qTPRyc/R2HoXzdxO6wBM+T8CpGQOlWZQ30uBTD+DBmy1oQp9zhBuggmEVwHDkQY7gj5m BASA+oox8rdoTktFUUbP7U1ChGmdf+VisLHu/lSd9CgkOzVlApCZUkcAufUg/NZzQQeK WyMrbGxMd4DbJoAhQqWD7+sddSQGYQR3yjbOCTfCD2Hv9vp0RXUDX9+c6/+WP2gLcRPr 2eo5CNpSP3NyQdmCLHFOkzAgnyIEeTuQf+6OP10LI5Z2ir5E/VSnAvReMFFkMNbC6un2 0X9w== 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=GUGh1m4vEJfx4LYw/dq2uJg2ss+SX8Rk3NEV+3Psk0A=; fh=5DWTY7FNanNPTlNmQXNsG6fE3PZxY0LZTYlouz/AjNQ=; b=NNAc8t4Yjz1mxXEkvVkR802KPpyBJLzm/gPM29AXHZwIp51XmR3c400jU1DjCrn40b tGC6txkk0783jT8u+6/iBe2Toi1veFvNwE/JVpp+qRExvmkvobUl/f08L6rtgGUJqRvd bYINeRZQd44youIaT8YaOeBOFzhOztXWk+5VsDaB49pxHFMQhASwGW4lLaW6+zpOi5JS 6CdkcfIwZ1fNevqgFMzErYTW92KcUQ5bmp6O2OPQxOaHaTkFJgwIt8efD+BSMHMdRrMn 8CFW8zapeh8bdtIghF9eUFiLY/NSNnDk/p3eo8ywz8uDotD7Q8R3PN2Ca/n+j3/Nj/0S OyLg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=djXBH0S0; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-54694-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54694-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=1; AJvYcCX+gW9KjlZiw37WgG+maVpMeP6QkYc3jI0EWNEv/SdirCAgEDnSIuGqK44QPUJxIhxtCZ+nLeklv2LsUZRpEG6Q7lQ2NQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id gf9-20020a056214250900b0068cb47d5974si875991qvb.494.2024.02.06.02.33.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:33:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54694-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=djXBH0S0; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-54694-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54694-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 6E5D21C23C0B for ; Tue, 6 Feb 2024 10:33:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7C7DF12F39D; Tue, 6 Feb 2024 10:32:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="djXBH0S0" Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) (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 42EDB1EB34 for ; Tue, 6 Feb 2024 10:32:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707215527; cv=none; b=MGHF4UKliugq8wbMtxllCtNh01TsJ5YPYzSc4+c7gAymjsBN+tiOvH0q84IaqpvnnyBNb1lnobxxUpQE/fkd5XTxl96//+pXrZNEgh1KqPZlc3i8Q75ZUGSkwMpPsafUZs7kYjdE4ufg+7hZgyrz4eJOXA58++GeIimu5HuzRv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707215527; c=relaxed/simple; bh=x+5u1/62GjqPm4J+K/Qoybak4L71WQqcO1bP9T4XrmI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nUk6lb3PqNlCf4rpHdKYXk0L1CEpqrftRVKiPiXYg+OFeo/TA/zFZci56mmSEYM3mcQPN4tf6di2IIa+oMnUYwawXzsJfL4plkFgMta+XsF5gQLsWjT4IRBsH6k1Jg2ok5A3VcZn5X785w4qHQ0ReqP9wmBbZX1BWt/BatnKlKc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=djXBH0S0; arc=none smtp.client-ip=209.85.167.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3bb9d54575cso3956322b6e.2 for ; Tue, 06 Feb 2024 02:32:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707215525; x=1707820325; 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=GUGh1m4vEJfx4LYw/dq2uJg2ss+SX8Rk3NEV+3Psk0A=; b=djXBH0S0pSbWcmvBMLLDo26slhDlN+la84ij+540gktO+u1mKlvmIxLIRq0aRUqcD/ E4zdTe4d3rYnhR73xqEA3P30nYas6dOQqUYWG9hjkoINUHAXUUgaQMJTXJQgjrQ92dXn aUN86AJHqxheFvGA/CBskJMhWG8CLj4UUv1dg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707215525; x=1707820325; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GUGh1m4vEJfx4LYw/dq2uJg2ss+SX8Rk3NEV+3Psk0A=; b=oIEvlarqyfqZGH/2pTBGuMR/zpYfSZiF8SisD69Vb/7cfSwHnTuo4J1RheJ62w8f6G S8H3j0Nx42tu+UAahSksJT5Oyghk+7vM/qxQYBZ0QFRuIhWPr2sjRwbQPBgbuC0VF9Fr O3yDbEmRa7Lwor+j2kXjPO0W/MY0BNNHgEEIOVnsipmO/0is+CM2a+Vz0hQGbNRUhXiA 8aXLFuyJSbdSKMXTEcHZpB8dhmfaN6I3MKWs72dnJWd/d9H320IABJka2A8JfIU0K2H5 8Xkz5MCntAM+WrxWh4D+Axy+3IftvhGQ4LLuHQi5aPCt4/PT0xHrFUQadmxkWISEdGi/ 8v+g== X-Gm-Message-State: AOJu0YzrBg7BpdS4qstEcA/F4lSlfwO3E4Xkk9y9D/mGWtjEqFaggTAh OlitHE2O2BKJ0eY5nEnUlwg+UsXEms5G4PwOdd1qaW6k5xCUo4t2JpMbYDgA6g== X-Received: by 2002:a05:6358:d3a9:b0:178:95b6:f87c with SMTP id mp41-20020a056358d3a900b0017895b6f87cmr2409328rwb.11.1707215525452; Tue, 06 Feb 2024 02:32:05 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCV24em5IS4s7JAE218P07kqL46txEuIxTL1kfA1VPy8qdwJK4jFyTRhVXbBJzHV522IhtNl0eX1u8Vp40IbvitznQty3Vt+niOa12WFcuyyr38sxhXi0fDqs0CQd4LKwXGLJ3pBqGPCSl0oY0TiePi4o6TOCQv9A8qg1VzFxmt5Y7T5oy0KTgq/fIH/XVXusBRnZJ6XYbMs+vk0e+p9PXYJN2+BD0WPfJlO7+qopilM0F8wMVvyDlKPp5CBaPQspg5daaHNN5S4Ylu3SKkf7oTx5hhmlXXDuZw3mDby Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id u18-20020a634552000000b005d34cf68664sm1706606pgk.25.2024.02.06.02.32.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:32:04 -0800 (PST) From: Kees Cook To: Rasmus Villemoes Cc: Kees Cook , Marco Elver , Eric Biggers , Mark Rutland , "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org, Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v4 3/3] overflow: Introduce wrapping_inc() and wrapping_dec() Date: Tue, 6 Feb 2024 02:31:56 -0800 Message-Id: <20240206103201.2013060-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206102354.make.081-kees@kernel.org> References: <20240206102354.make.081-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4594; i=keescook@chromium.org; h=from:subject; bh=x+5u1/62GjqPm4J+K/Qoybak4L71WQqcO1bP9T4XrmI=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwgqcvO4krnAoU+Rr+Gvl32kxOeOhCYatsR4T/ haupH9B8XGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcIKnAAKCRCJcvTf3G3A JuzyEACzIv2MQscJjBejmNkVxm4bFcbzhhhchqsGibBbieQc/rFAhmbNdB5sYv5ORxjv5cMcYkv xX4V8HgwRYFjCugg8O/BdDK8A9YXckGXVFGoWII5El9rC5EBvCq0MaSoOiq7W3O41tgzQqGbMXO LrNQ+cexzghCSKOBoB12ID3s0e0/HmYiZjbfNSnxuB/+D9kz/kKia3bZFAhY0bs9CAvYoyyrkS+ 2ypLoduanCyex9vlEjfY/bs6voxRX71T/4w86Ji/sXZfMv+jqrQ0EvQt4TS7OGM4GL0kUMLkTvM iRiENAdMFViWlB4/gIkqqoHvTlE84wd8rk2AMtDhTzcE8uqkj6JbJyh7YI8vGE7fCuL0bCZnWOy FwP9xJ4vbqXoDEsnEfQass9Wj46X3F7Sgf0ZdJGzdI+93JcSugQYngR+mGhpQy2KAmWJ6Mu63ss eXdgU/m4mHQ/5Jep4finoVlIxZ7AngYMVCKDcwTWTQSwohaB0swTi/zimad6hh4RShHeod7WW+u eiI6tp3yYylhzSsd/HMvtmOoJI3E0bUPP18O3hkPQOXYX7T95xJFfQUsJxgHduTqpBxwfM45hn3 wvjPp9RnN4mcXbRNJqtcsHrctbCdua0Ern6y8PPNN6rzGjrdAyNluVQplQwaw9jKpuDwGI5TCLF BbLS6RazbsEbM7w== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790145318501614422 X-GMAIL-MSGID: 1790145318501614422 This allows replacements of the idioms "var += offset" and "var -= offset" with the wrapping_inc() and wrapping_dec() helpers respectively. They will avoid wrap-around sanitizer instrumentation. Add to the selftests to validate behavior and lack of side-effects. Cc: Rasmus Villemoes Cc: Marco Elver Cc: Eric Biggers Cc: Mark Rutland Cc: "Gustavo A. R. Silva" Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- include/linux/overflow.h | 32 ++++++++++++++++++++++++++++++ lib/overflow_kunit.c | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 429c4d61a940..8ab68350f976 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -82,6 +82,22 @@ static inline bool __must_check __must_check_overflow(bool overflow) __val; \ }) +/** + * wrapping_inc() - Intentionally perform a wrapping increment + * @var: variable to be incremented + * @offset: amount to add + * + * Increments @var by @offset with wrap-around. Returns the resulting + * value of @var. Will not trip any wrap-around sanitizers. + * + * Returns the new value of @var. + */ +#define wrapping_inc(var, offset) \ + ({ \ + typeof(var) *__ptr = &(var); \ + *__ptr = wrapping_add(typeof(var), *__ptr, offset); \ + }) + /** * check_sub_overflow() - Calculate subtraction with overflow checking * @a: minuend; value to subtract from @@ -113,6 +129,22 @@ static inline bool __must_check __must_check_overflow(bool overflow) __val; \ }) +/** + * wrapping_dec() - Intentionally perform a wrapping decrement + * @var: variable to be decremented + * @offset: amount to subtract + * + * Decrements @var by @offset with wrap-around. Returns the resulting + * value of @var. Will not trip any wrap-around sanitizers. + * + * Returns the new value of @var. + */ +#define wrapping_dec(var, offset) \ + ({ \ + typeof(var) *__ptr = &(var); \ + *__ptr = wrapping_sub(typeof(var), *__ptr, offset); \ + }) + /** * check_mul_overflow() - Calculate multiplication with overflow checking * @a: first factor diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c index d3fdb906d3fe..6cadce8f8a47 100644 --- a/lib/overflow_kunit.c +++ b/lib/overflow_kunit.c @@ -284,6 +284,45 @@ DEFINE_TEST_ARRAY(s64) = { "Unexpected wrap " #op " macro side-effect!\n"); \ } while (0) +static int global_counter; +static void bump_counter(void) +{ + global_counter++; +} + +static int get_index(void) +{ + volatile int index = 0; + bump_counter(); + return index; +} + +#define check_self_op(fmt, op, sym, a, b) do { \ + typeof(a + 0) _a = a; \ + typeof(b + 0) _b = b; \ + typeof(a + 0) _a_sym = a; \ + typeof(a + 0) _a_orig[1] = { a }; \ + typeof(b + 0) _b_orig = b; \ + typeof(b + 0) _b_bump = b + 1; \ + typeof(a + 0) _r; \ + \ + _a_sym sym _b; \ + _r = wrapping_ ## op(_a, _b); \ + KUNIT_EXPECT_TRUE_MSG(test, _r == _a_sym, \ + "expected "fmt" "#op" "fmt" == "fmt", got "fmt"\n", \ + a, b, _a_sym, _r); \ + KUNIT_EXPECT_TRUE_MSG(test, _a == _a_sym, \ + "expected "fmt" "#op" "fmt" == "fmt", got "fmt"\n", \ + a, b, _a_sym, _a); \ + /* Check for internal macro side-effects. */ \ + global_counter = 0; \ + wrapping_ ## op(_a_orig[get_index()], _b_orig++); \ + KUNIT_EXPECT_EQ_MSG(test, global_counter, 1, \ + "Unexpected wrapping_" #op " macro side-effect on arg1!\n"); \ + KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, \ + "Unexpected wrapping_" #op " macro side-effect on arg2!\n"); \ +} while (0) + #define DEFINE_TEST_FUNC_TYPED(n, t, fmt) \ static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \ { \ @@ -293,6 +332,10 @@ static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \ check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \ check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \ check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \ + /* wrapping_{inc,dec}() */ \ + check_self_op(fmt, inc, +=, p->a, p->b); \ + check_self_op(fmt, inc, +=, p->b, p->a); \ + check_self_op(fmt, dec, -=, p->a, p->b); \ } \ \ static void n ## _overflow_test(struct kunit *test) { \