From patchwork Wed Feb 7 15:24:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197975 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2305997dyb; Wed, 7 Feb 2024 07:26:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IEVZrxZkR7AfbrZq7kXGZ4oCPoXW0Z9XhLfrd7VyFwUeoag+3Ku21PBMLqpGgOm7/H7rRbv X-Received: by 2002:a17:90a:f013:b0:290:6b49:e6b6 with SMTP id bt19-20020a17090af01300b002906b49e6b6mr3096095pjb.25.1707319587660; Wed, 07 Feb 2024 07:26:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707319587; cv=pass; d=google.com; s=arc-20160816; b=DZxnshTFIDckA9zddLQAxlA+Ol8qT5tMc9v09CaX7bpL+Uklpp9fiHZaPixU2eROEU KUiyJYwO+7cDjRf0yxt4yxXLOOWY8UZ2OjqV0hLg/PAwfjCG/OkcCYXz5tiIRs2D+99G t65OdIV1I1RXugCRQmVl44ogVgHADd8J15bpkWMNQsKP2CEjp6/wY5fQIv3kRRMmth+G 2NhRz7nI0zFyOfnEz3cB0lfNnToPqPSwzv/uLFthoJabrfyAo8kRwGspDlC1x71Kr9KL zmldQo8wIJJtABhexZBeSwtjA5xMF2Orf8YZ76EqOHQX5L7d/NmK2k/8ysLo3mbZKbbX Pu1g== 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=RerG7d2KkWMxwjKi3Bk1dJMaOiX+RZ04/h8EQNw22Ro=; fh=qHSBmLsv7c4l5IGk95vXt1B7frUSVMkGpvLOz8ZqWT4=; b=p4XwWd+UAWYmJ8nI9dVlLNnLgJjo6MDVxzrb2ZjeUmNKH6Np1ssqO4vsb+gBfvOULF 4NgvW1IU80WRMzSkStWM6vdsQ6iCF3t2zV59Uc+yh5aEsOK3HMzLi6tE6BbmHCT4upjg N52GxCsl4Ny0ySvHkbkz8kMv7hOGsVvqd/J/sARJ3H1uZWCFSJ1HWKzlejzB/Fi1TVc/ zBOdFz7c6D2Kf9CE6wwKnhIhCRqlGtPu2AH+I7edi7EdIkMLMiffSckUtdC4oDlGL15w 9IatDSXJLxZYhr93Z+/vi33E9wY4PzlwLMbqWMnJs9bThGB5xAlBuVIPlLOmHM0Y8c4J SlMg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XWnYOmVx; 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-56713-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56713-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCUqNZrctMjVSTg3B1l0ZrYEpqxqzyj/WMbsTQLbiNiECHJECyeK5RK5BapvpVYGEgl3NK4i3c7myBu7fktqaD4DeSZhTg== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id x18-20020a17090ab01200b00290cdc04acbsi3448339pjq.187.2024.02.07.07.26.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:26:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56713-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XWnYOmVx; 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-56713-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56713-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 9687F28C93D for ; Wed, 7 Feb 2024 15:25:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2F8A68005E; Wed, 7 Feb 2024 15:24:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="XWnYOmVx" Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (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 76FA17F7EB for ; Wed, 7 Feb 2024 15:24:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319467; cv=none; b=EylfAxXMx4l4GhbRkJm9H3Wifi9Gr8zHDZlESy+Qrdv1aLmkWdNgk2cHVXVFLt7lZZlSXSWIUP9L6FAZnMkXryh51Eevwl6NCrNFkcFTuXpsSGq+lb+49XwBLDxgerx7Gs9SbTKbVPCIc9hhZ5zp7P4ECjFwunuEYUEj6+TlRIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319467; c=relaxed/simple; bh=ixNr6X1gB7VeIiW3pk9Za3U1z37zW3zrQNIbl69tCT0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hBjSUFxfRMdXs9W3dg5nSqLyjE/e32xQzTXkr4cuqBnKUEyixahDIFaepwWQ/JykiEGEZ3Cpof34xCPYPY1cyb8SH1K5nS/Uzvb0XFSAHziBIrluXCF6bIMYIpfDhInsCuZJdCr0HTYVP9IZgs3sZ66s7+axdEiGitxDRwGo1cE= 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=XWnYOmVx; arc=none smtp.client-ip=209.85.210.169 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-pf1-f169.google.com with SMTP id d2e1a72fcca58-6e05456a010so542198b3a.0 for ; Wed, 07 Feb 2024 07:24:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707319466; x=1707924266; 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=RerG7d2KkWMxwjKi3Bk1dJMaOiX+RZ04/h8EQNw22Ro=; b=XWnYOmVxSytj9wsDLNNyNKglcn1Qk9EaUcOsivQti0aOQHUDFgXUTFfq3fxd4bXRrK gvZKWFXAlKllDbOkdE6+qPGA2mLTE+G0semPUEEfYb/F2xm54wHoeLuWzaYxr7gktKyv qZ1wRuAyWg0A3iiJc0Azxof7W5czSL27fspGg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707319466; x=1707924266; 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=RerG7d2KkWMxwjKi3Bk1dJMaOiX+RZ04/h8EQNw22Ro=; b=XHnDy/v2TDrs9aB/5d9TelJ8nAuebI1VbFUfNylDl7hdqacUKLM0WsaN+frE/lutAj nPOnVqC+FCzTwh0lZxsrP3nr01LmYDAIUztX5PiQv9Ta+aFd8enW+Y1scYqjM++eLG28 l4n67mCH3raudBC/zAXX1tvphoJnmcFoj0e3LAywhy0chTIEldTWZJgUN7zaNBMB9Pxv WOfkmnijajDipeav/uUKHgFUHqFzuWmxHZNBveum7ct/9wjQUuMt8B9yB9KY3NsXeeoU edtgPpCi5cHANPR0H14/JnaXTYr8a52Airpl5vqfbap+A2SkfZTVjB58g+DyqFDPfYQS FUNQ== X-Gm-Message-State: AOJu0YwYdtd3SN1vY5P+V2eRAcFk77aMMCG9qKluQy9DjY2EZsU5kabg 7FINEknoOnysSLBvd8QQZTCg8sY8EVNmaK1zx2HY9vJ+rJSdyi/+zVcQMk9CmA== X-Received: by 2002:a05:6a21:3418:b0:19e:abbf:a47e with SMTP id yn24-20020a056a21341800b0019eabbfa47emr311728pzb.27.1707319465882; Wed, 07 Feb 2024 07:24:25 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUIN68fbzFNvUo+nMQeBWXzeHDrwMJ1PoHDu3NkMZuzw5PqnVe9v4dYwJC8bFBr5MTSOXyzrZd3c/2XQ31kUNulOf0IXiCl5wj8HrZt7pgdhVZOtuDMj9T0SzzrRQe2w/jr/81Gh6PwcbaUl3xLm3GA0i9V+OoA3LQyvvn9ePzkX2OYgrcISp3ji/pIfakipoGJiRr5ce2COrOWZpF13G17/XkZHKUy0c2KLW73Ez5j4QHQqxkQOIZPOcrOUQr5YPGI2RzSvS8X5NlMaS+909Pk+Yt87DpfZszS7Kb/r/9cnvfrSA8wKGk1HyegC+tG1FiwUJrKDM+FRg== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id fa27-20020a056a002d1b00b006dbdb5946d7sm1715455pfb.6.2024.02.07.07.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:24:24 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , "Gustavo A . R . Silva" , linux-hardening@vger.kernel.org, Rasmus Villemoes , Marco Elver , Eric Biggers , Mark Rutland , Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v5 1/3] overflow: Adjust check_*_overflow() kern-doc to reflect results Date: Wed, 7 Feb 2024 07:24:18 -0800 Message-Id: <20240207152422.3855315-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240207152317.do.560-kees@kernel.org> References: <20240207152317.do.560-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=2680; i=keescook@chromium.org; h=from:subject; bh=ixNr6X1gB7VeIiW3pk9Za3U1z37zW3zrQNIbl69tCT0=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlw6Cj2+8A/TrJjsBiIAx2op9/6wn1k5bn1wnZL w9hYjTQS5WJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcOgowAKCRCJcvTf3G3A JrVrEACqhj3zhpOpirlHTgyqw2dv77Afdp4NYgHxwiKF2RhojJQXRmWssRiy/b+PrmQwg8y4Oo+ VVOBsTYIav/X9fxfiqyn7YYrDEibUC0l/4veXMRu0fxl1urJ2J2teom17HOCbGYk17Jnw2lp5VQ 8MYH0cLCa2s4jLHyuxr/hjmpdj2HFIxa5pJZA1aEhK1T2n6kHC4FieG5j/dO26APViY1DRpi5UD /YCsSn++Ad84Z/hCdqVT2mwggO+veFL06gp0NWOH8akV63+sK0OpG027YsZCM31avb6vY+xkOab l6a0Oowld4rRVYya4KKZE0zlL6RcvUwqlc++CEnV7bvUGcU+pUEWUUtnh/i+kml3n+II7Mrjiy8 N9aPF+VMMHp6Ush0kqVm114qsRP/f5ZbrQewIpmkjS5H/YR0PxOjXA1643SF5NQQ/Ngf9UydMth gq/MJhPXhugN/iR4my4tjhJeiL+5CPw0t3jucp+Ur6Tv4dLH08hA7Q3+unglr0v7hF6QdViKD61 cc62FrhC1LGh/lDH+/JMoCWl5KLlGyRBNCLdR+KifoBrc6Rh04AAMBz4HjtUH83D47X5GobBCyu 62sTrx6BawTTwuONlAPvuSU2nA5UyfOGXc0S1IRAuSl0iGiF1OhPqplky0t0VO4D3QMBxeGb45l HKXRtpdvtCn5Yvw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790254343813284560 X-GMAIL-MSGID: 1790254343813284560 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. Reviewed-by: Gustavo A. R. Silva Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- 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 Wed Feb 7 15:24:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197976 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2312887dyb; Wed, 7 Feb 2024 07:36:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IF5/SxsSrmOtceW9hEbju1Z9rYlA7t2Q+VA57FdXzf42UnHfELozUnGPY5YjotgpWP8+QZ9 X-Received: by 2002:a17:90a:f681:b0:295:f5da:a8d8 with SMTP id cl1-20020a17090af68100b00295f5daa8d8mr3228139pjb.11.1707320194350; Wed, 07 Feb 2024 07:36:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707320194; cv=pass; d=google.com; s=arc-20160816; b=wpPLNCfWUYnHRnO1ZucZZMyjirHLR6WYy35dMD0C9jBsOzmSmjIYEXCmqBYOpQG/ar 120Lijmb1tJY7H5EmCronwXGptLlVUdTv+nWCPQa7f2ZCC5eElCx0kEAt0YDIPUUwC8P HfZWyamUQg9p87o5gUqK63EhR43fDr4fj4lCVxo5xjB6ruIaVPLrADbudVGfTUsNOvXn ASzmbtaHudIDeZAixTg9rxxneZOt1s++PKg3Re9LnrFFdkDq0dOWaORMnGBocEsnOAs0 ohFY7BSDTF9+iqFnvWEvgAqKE4G9ym8mOM9DrUhgh5EEGwWgnAY4c0ltlBRB/er1yXmL UH7w== 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=KS67DoS9siTadX6XoYgXOlZJ6JIdy0dS80ufVgX7tK4=; fh=9dNH/gzCZX2x2psP8T6lG9YEvRcSmQbkqB5IckzPC/U=; b=guzQ72lJcXMOfzd6KNQPp/0DSZ25oe7IhO3hjYlvzPY40Y6SMxD3U66d2Rc7mxmtqS V1n2ixFTCZ6SF2yVOu9dylJgXrmxRItXgboW0YG4mpowCsdY9t4IipO6y8J0OPxWOLXn 0BgAaQEf3ooq8lANnq4Fp0DJU3v0VAyHKFViPNFrSkxMlWTTO7kvv9EKphchxmSEXVKl 7XvW+xED9KjHHQMAiRZuSW0v2Gu2YxfU2vPx2bFp9RsiCdJjoizheeCvvAOYw5pQqr0u G0x7LahoPn/o5bA1sZko8nN1B1sdJ3Go+Z0WqJFOqs5D+6c+dmfmW0XhJq0b+jHhwQzP jSTw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=JBIVdMvP; 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-56712-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56712-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCX1bcxnqEZq3Fi+31aZeMkLCE2/EsJrd8Z88nSzQ/Dmvw7jqKnC9g+QBGyiaUjKzOe0FBde2WkN/6Ig5xo0ejfvFcdqmA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id j3-20020a17090a318300b00296c5f9c2a7si1800324pjb.70.2024.02.07.07.36.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:36:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56712-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=JBIVdMvP; 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-56712-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56712-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 4CC01B2849A for ; Wed, 7 Feb 2024 15:25:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 39ECC80601; Wed, 7 Feb 2024 15:24:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="JBIVdMvP" Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (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 03AA87F49D for ; Wed, 7 Feb 2024 15:24:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319467; cv=none; b=c/r3kdNJtv8bNE4sZESTLSTMrjJEIFQNh3Cq8tXpqbR/lXc7RyH5ycL9gxDTkCScKgoUnApQdlSzO5OTE0mIloJvgnVlik+6cBscMu7P+gdcIjFvR1Sq6sHB2DzqezyMj3IZiZWoW9qiKvtRfkr1XIxAFFU9krvotwo2MtFcmXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319467; c=relaxed/simple; bh=b+f38qUkFmR/TObhuk4Sc9oAIjIVix3f9cue4x2E+Fo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Num5XV6DIG/M3NRumzl2cB7G6G/WaN+ggzMSnEUFxusVrBYd9iNN340+0oxOslhbnQQrI5c7HyL2CGYpeKnZTbmcIXhzPJ95K7LbdEDlyXPBRmeueilhmLlfjZOCRAgP3aSaYA86RQRFulIl8fWpF+5BZVNHDRFFZk6kezhMPPs= 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=JBIVdMvP; arc=none smtp.client-ip=209.85.210.181 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-pf1-f181.google.com with SMTP id d2e1a72fcca58-6daf694b439so628127b3a.1 for ; Wed, 07 Feb 2024 07:24:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707319464; x=1707924264; 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=KS67DoS9siTadX6XoYgXOlZJ6JIdy0dS80ufVgX7tK4=; b=JBIVdMvPNnF6RPdgl2iHwXIbNLUqX4G7NT3Z1lxSja6MxJ0WB/vOyTPXo1Z3BybPcn euhxJWI55qkUCiy8dRVsNqsk2HGkPNJSPlAYwVV2yXkccPV8+uczQpApNtmCOGOIVXqP wgtWanZYVTpfRlH9zKTr5yHvyD5xprBFphUHU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707319464; x=1707924264; 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=KS67DoS9siTadX6XoYgXOlZJ6JIdy0dS80ufVgX7tK4=; b=JKutFDdUH5tuDgwZ3SpRPqmCciJSNH9kIptFklpfcvCcYm3sB7qa1ra89RsateyKYG jNwu795EvqjbhT6YtK32pjqG7Ca6vtndW9VUr5k+fcjCg571P6pQfSg5SNE8/ryDx+9I sUzqmUL3KLXCfsNx7JrsWOItE9o9NN3u9U0/dcuRyN8M+XO4mW4l9650TSqVUx6Q68Hi 6yU2P51LKVPi98cOVIRDju/I/4FrBAgX1i6iPtl1S4HdsFa6M4Ki4t8ZBLP4Tw4Lto+Z uDHk34vfr8TsBGOgatKPXuY7GS6mCWp/QDIT8g8MsjoyMEF7nNp9gxZYRCKmPvPCO99H 0Z1w== X-Gm-Message-State: AOJu0YwhFQGOYW6Kpx0ZZ85p5S3ld1h8qskZExlCIcCJ3XIWPVGixioT tXp68u4//qmHB+VWzbxNPFJ8L0GYPJ8I5qPx4OJiTDh7ejkG15weAPHMBtmYVQ== X-Received: by 2002:a62:e808:0:b0:6e0:4759:88f2 with SMTP id c8-20020a62e808000000b006e0475988f2mr2859734pfi.4.1707319464393; Wed, 07 Feb 2024 07:24:24 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVXBOpyOHYtegSSIq/IJhy7kL4duoh6ZuyM5qMboZBxX/G7RrHYR++R99WNEcOUnXI6KnarH1HZ5u6ABO+0tqmLJVid+lKKcT9YVau0ZRDJHdHc4m95pvTSHf3bTIk5MuY08HX1ap36lOpemY6l+UV8UzdnHJhSwvS3EFIK4pdxx+hnHKCgOJABdIIdAVxtKAiSoKW06bUcIT3zzIzGYU3ICiTZwFXNny7dl9xkyshUOm6kfpCcbizEGaSpaDiBBhV6dJtBuqwaamzv3R66PGhtnBIm6ttIdELZ1YxBN3bhzXbzECLj5lc4vp9Air1FskXIpdiNhKpISQ== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id m9-20020aa78a09000000b006ddcfbe736esm1754674pfa.97.2024.02.07.07.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:24:23 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , Rasmus Villemoes , Marco Elver , Eric Biggers , Mark Rutland , linux-hardening@vger.kernel.org, "Gustavo A . R . Silva" , Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v5 2/3] overflow: Introduce wrapping_add(), wrapping_sub(), and wrapping_mul() Date: Wed, 7 Feb 2024 07:24:19 -0800 Message-Id: <20240207152422.3855315-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240207152317.do.560-kees@kernel.org> References: <20240207152317.do.560-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=5602; i=keescook@chromium.org; h=from:subject; bh=b+f38qUkFmR/TObhuk4Sc9oAIjIVix3f9cue4x2E+Fo=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlw6Cj3wFGEbnwDxyh6eIuINKUEK64qoLls8qiq SfHBN0h+Y6JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcOgowAKCRCJcvTf3G3A Jv6xD/0XhE55ZAZLm9uMEhycDNqaeAR5W6+/KhWbDn/Fk6wA4T2KzKrV/PJHcl/ZDfJm533RMVj zZa0cP44ZXsejZND+BZzJ7dYJo1Pq5Et5DRjQRd63Af6SIACjAIDJvG/LWKHQjexVvZJP+ykAGA 7SbXzpne4OjrZRbKXRt2lJxcpX8zT4Q1Yn7OYX0GWkH5Z2gtw6Qe3SqAFud1knCAZrwXI4de+bX mfucHuDRhytInGLFdYr/Nz48LdqGJQnxfCKmNhpVANcNPo3eR3j7ewlieG6aNl+b90hfVp8nobx Y4Idi/+T9KgnAXzSlujsb3yrLQbvnuRD96Y3ZnUfhZ71XB8q2APa29ZUZ7d0Gzc99/7S5aVBBcE d6gjh5HU2kg5lvRhlC/sM2oHaXaDabBqF0Xr02kmjxhkEFwSDRvcnUPWtR2v0G4glcdyKNrHqOV ArLh8Gdm2sTR7g4XhC+vChZJgdYcsUlNvFX6eUWa8hOkNyrwtuxQN6+ZxiwWJZa8RTLMFhEg+Qh aA9I0WyCdp6VadzwkgEeG8uiB4RvFRRN4eW5P6DpvHSP0suL2BvDAO7h0m1b/Dg7OyCyVWr6BS9 s5eY5y5+Q7w6lzkWhBCEQM31XmSxe8oNASCpnUbQWnKZUVQVoq4QBDiiKqOSfjANccJdqNiElA1 0q9RcTfa1raCYew== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790254979975578236 X-GMAIL-MSGID: 1790254979975578236 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 Reviewed-by: Marco Elver --- include/linux/overflow.h | 48 ++++++++++++++++++++++++++++++++++++++++ lib/overflow_kunit.c | 24 ++++++++++++++++---- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 4e741ebb8005..849a49fb496e 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -64,6 +64,22 @@ 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; \ + __builtin_add_overflow(a, b, &__val); \ + __val; \ + }) + /** * check_sub_overflow() - Calculate subtraction with overflow checking * @a: minuend; value to subtract from @@ -77,6 +93,22 @@ 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; \ + __builtin_sub_overflow(a, b, &__val); \ + __val; \ + }) + /** * check_mul_overflow() - Calculate multiplication with overflow checking * @a: first factor @@ -90,6 +122,22 @@ 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; \ + __builtin_mul_overflow(a, b, &__val); \ + __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 Wed Feb 7 15:24:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197977 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2312973dyb; Wed, 7 Feb 2024 07:36:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUiy5YdO4wHgUXH5UgO1VTjiad2ELsJT/jLSUryglzcqOtZqstDmsE51a9VWHowHq2780heK3pxEj/yndJGAC9GNa5FJQ== X-Google-Smtp-Source: AGHT+IHL72u8HKqVrAGBAhjv/0g7AQFtc5LYQgHzLv4uJnGWCp9aDQv3R7BavMOo83DDr5s8FYaX X-Received: by 2002:a92:c5ad:0:b0:363:b519:bff0 with SMTP id r13-20020a92c5ad000000b00363b519bff0mr6060736ilt.23.1707320203655; Wed, 07 Feb 2024 07:36:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707320203; cv=pass; d=google.com; s=arc-20160816; b=c2qeotsHdeY41kPI+2zkwB6lAOdH2jYecFhhyW4GUGBET/NqbSAta/OmRepIcwaxHw wd5RahAletZrzRgWTRo+4wU/VaYezDy8KM7WkERlJUjvzuzhLiCn7ALkoWjD8Ahzf5CJ ktyb8+JMEqSVnmWvdycHtKiV0Ovj6mgHr+LnI3LLWUkgTSONWfwy8n8fRLbwfE527nnx BzgOXFY9bUSy1nHA/WpKhNMc17PUTuwJJ54RDMsjeOmFSAL2ib02G9nadT9ugav3CfCV SPBMrjsyG/QralyeoCktEpxyfwghVbHYEMzNYIRBH55SK/VWAKy4HJt/O12schCH5Ehy fxjg== 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=0EnFY1zWcdu8s/mjer9aS3MtbQycrZesMBVuK3m9B0g=; fh=Msh+5nWmuulhNFw9tZRJ3+4qPKxLPC/dZ/4e81cjyG0=; b=VQBKxpMtu/+C+OaN1hnFK9nwXkvspGLld6yFZ6uW/MKfxNEpxfdpz2yUzLBk9LRelR NolFMpNIKQX8LgWbGgXHcVYgwvDWo05w6knkIx3hsVHdFkrSdlXj5WZ35RPCaiFGj+bt DjhJlUx3vs0pXnej4LXrucWrq01hkfHI1rOmJH0IlJBncvJLn2JdukPLCnkkG4AuIaUp +MF5OeEew9HL1zvj19lz0oNAC+YftJfhh0LBZjtCQiE4n/HQCRe+o5UmFzDGtEdtX8Ct kMp+Wg3B5ZNh++d3WyQNR76BYz89mgTIZUF5xljC8MIfsWoZZX584MWJvn9lNXeZN8Wc 8ErA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=it9IdQ+Y; 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-56714-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56714-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCXFGaTlnZ8WwX20SJPrMm05PWjmzOKI3zmACYseW32tEDKba8NaW7QypM37EccUEU8ZBkcGaSTygN9lYf81Meuu1M/buw== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id k192-20020a633dc9000000b005ced5bbfcb3si1691048pga.392.2024.02.07.07.36.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:36:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56714-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=it9IdQ+Y; 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-56714-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56714-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 C9F79B28517 for ; Wed, 7 Feb 2024 15:25:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 89A2380BE2; Wed, 7 Feb 2024 15:24:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="it9IdQ+Y" Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) (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 2EFA57F7DF for ; Wed, 7 Feb 2024 15:24:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319468; cv=none; b=vD146GQQu/iY6SCRnIbcm8ZjZS7xIuZbujHlijc8dP0pF83XFcgz0iHVNohcNQnuFYJNgqgI1hbOK2NMSttAqmRb8teAOSXeEkK1NubcycpKXA0vThxexsn3BagKh1GK4K9gSNNteB9iAMON9wawKZZCJM6/xDJ4SclA2bylXco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319468; c=relaxed/simple; bh=UF4ZOJtI7adR1ITE12VYKN2eWkYypBgjoqWUYT5HRLA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cQu1h67W01+KasnskF2vr7l0C0EU93z3dAh91M2g9MAHqUHjZ8HWCUByWPM3jalUK4ydRQvwSSHHobiFtzp7wGiX0DoaX4XnOqBMBEYt5a1llSHlNjjoT4eVEbpYnx9fL7gihI6r1iZedABix1jDUk4a9zABRb9WVgQ1rpzu1ho= 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=it9IdQ+Y; arc=none smtp.client-ip=209.85.166.182 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-il1-f182.google.com with SMTP id e9e14a558f8ab-363b2cc93a8so2646905ab.3 for ; Wed, 07 Feb 2024 07:24:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707319466; x=1707924266; 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=0EnFY1zWcdu8s/mjer9aS3MtbQycrZesMBVuK3m9B0g=; b=it9IdQ+Y4JrTK1mcLttbVNTGIVvWN7I4MpBJRJJuefxlfo+KY8TScUC/f9iJo9SFG1 ffaDNp+ISbsxgA/vdcYKuUzzAXL5/rQI3k5gYo+AkQmzbVmZk7esj15XjMB5e+itNq9A cncRON5ewJYe40jyiEV2S0swdtwpdSfMjaSjc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707319466; x=1707924266; 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=0EnFY1zWcdu8s/mjer9aS3MtbQycrZesMBVuK3m9B0g=; b=dpXtz03/4K2paAevYiyv/FWzIgeE1YOji2fP2BJthy9zKR7IuuIKM7OP9xEUvqbaT5 GfM8klG2trQfejsYbJWbeprDvzdJk0HUpTDV2/Wp8twOKCCpzo00vfdUIUxSW7mNrrZg hDZn2iQPpba6X3k8ssf05p1iS58ik/U/9r+g17gJ/QonR5IZEHLKS+XKTzGE8rEgSUS2 nZ5e+WZ0HSeqkLKeTtBiM2/OD4wVTvLnZ+50Gahpre4iVW/tsYpK/nfwBE2QHD5UEXvt bj/vBwSr5mLtydYp5KDMT1OAp5/pB5Q5IiGVPypek5P1YvLVwouut1aIhM8k6EmZa1l2 C16Q== X-Forwarded-Encrypted: i=1; AJvYcCWlqTd4+RIt/pYHwujTBEk+xK57M4lhIS6nCmKRJELAz9qi9TRs8LsbvHmhnUV97lPGD7pOwa3qTWAUVo7XEMjpONeqXZTO8vDg37O+ X-Gm-Message-State: AOJu0Yw2O8hzvyYdjXcSBLtSEzRl/28gnu7G4blH6oV1fOx1uCbYtcPf ify/QM5jNDqLhBZGBsCDJbSFtlzXR77VhlABLavIsdSRN2VcoGMCDsHZaBL+cQ== X-Received: by 2002:a92:c5ad:0:b0:363:b519:bff0 with SMTP id r13-20020a92c5ad000000b00363b519bff0mr6021971ilt.23.1707319466265; Wed, 07 Feb 2024 07:24:26 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUeS11GMGkn+N1jnRT9igB6tGXtdjWUgYdh+a9ZUhg+63egyGammrT3oOfUDicT/SFBZod7cWWHqfpaP4Fff84UEy1gDTfc0KLGBBGRGgHBECtjrWYXPlVrjuoz7iZnqF7jb1+U1fMf0ZmxB3RkPuwtR6FSq3pEIOwXfF9yvC7uK2EV2gjvG1/68EjYurTdPZB2HUkpcGCImdt5OiWRKk8hw5Uhn+ZjY9XLNK+pMPoZMF27sUF7p4ZZkg92D9VDr5+dMIB+/0awbiK9bpqsBlfSKbs0ml3RidV2jTlCAWWD3OwC1xYX9Zd0rA8aO27skohWzu7g2vQPxQ== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id bn20-20020a056a02031400b005dbed0ffb10sm1547254pgb.83.2024.02.07.07.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:24:24 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , Rasmus Villemoes , Marco Elver , Eric Biggers , Mark Rutland , "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org, Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v5 3/3] overflow: Introduce wrapping_inc() and wrapping_dec() Date: Wed, 7 Feb 2024 07:24:20 -0800 Message-Id: <20240207152422.3855315-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240207152317.do.560-kees@kernel.org> References: <20240207152317.do.560-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=UF4ZOJtI7adR1ITE12VYKN2eWkYypBgjoqWUYT5HRLA=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlw6Ck4Knv0CZm8IYel3Z4RJZj9vYED+A+0Ru26 cX6qKGKwNmJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcOgpAAKCRCJcvTf3G3A JoQzD/wI+8Lf+6AaeGFKfxalG4dQxHYlwN0aqDm8Alb9xyyctuWdjfXnTerEf7Zbl0mQKHHonps M8lLZGpLU8sHrOb5ycfRTBw09he+nj3AF9xKEuGKkZ9O8ycxUVz2HqcRyF+lqTKV063RfCkqujw zEGzjrVLjgVd0x1FxNUztDEgZV73ow9l+Cfgp8ERi+ikCTsGdUoho3AorJsW8Y0qvR3JXMM97jQ xFbGLZ0PCLojIvcxjdrKESl63H9hPevoJIB0FG/Sn6WVxyN7kot9WPDJfgQ2WIUqtwMGrXBPDjz OR4YrvEdTDY0TkalsfmYhzoanRrSbRXil3oygyW9YDJKtGsa/+yNubbxSCzacaSqIQRs1oeJmnZ 6IcVxM6bLXJWil6fIa6eUCvhAImjxYs1wh5jG9ZzqnjWfugekmyuEBd/0tHmJ+grIqjRmpVJBrb omBgutZBtt0zqBiK9fTx0LQWQmSCtFnzFvDKPYhIIY0IAYoi3Nml87y7pXmeBcuWEW7n6uUNpKx sJZqMfGtB3ozLCe+nRdTGn39Dq7lh/XJNaYN9FHtqFdo9Hi7R7iHMrW4xnR/Xt8vV4E2jesuwhs X0RHoojimonoJ4txcOJ1P4/8GMLM7KbV4havukwMIjKe34cKjTWZBEkWR09W6VfbAhNC0O9sc30 hB55mBv3p5236Ng== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790254989497182827 X-GMAIL-MSGID: 1790254989497182827 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 849a49fb496e..f4b18007fd2b 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -80,6 +80,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 @@ -109,6 +125,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) { \