From patchwork Tue Jan 23 00:27:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13526713 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ABD71C47DDB for ; Tue, 23 Jan 2024 00:37:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=/LXgCGP9MMgIYvPzx3GNlaPbd9V1+jlohSvDr1XqHL0=; b=dvLr6FL6AdEJ9F oIUP5eSD0kgNNx/VJtZf36mDJlG/20KG5p4I43agLcAwfUUSAOpRQwtmZUqgxgF8VDLGKWdBYbG8F cDgFWD3leUTlejy45KEM7a+f22TIpkrmMgk4+albj2XePvIv6I/3AEs+5erDQvUVOxFaheYVN3CeT xHw/8rFYzJmPOTs1ISXYfDgZX3ZZB9JoLD7j4XAHyDBjbWVgLCnehTigoHlw6/Ei9iTRCtx9eIaFb mVCP5vJLWHdvQwt9lKgD8JK5uq774QRhVRcyQ12ZaoAvZ0YmA2+QzvT+7iwLQqXlBqJITmWsc2EdC kAp4i9L+3rdR0lGMzsqw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rS4mm-00EcQi-2B; Tue, 23 Jan 2024 00:37:04 +0000 Received: from mail-ot1-f52.google.com ([209.85.210.52]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rS4mj-00EcOU-17 for linux-arm-kernel@lists.infradead.org; Tue, 23 Jan 2024 00:37:02 +0000 Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-6e0a64d9449so2435795a34.2 for ; Mon, 22 Jan 2024 16:36:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1705970157; x=1706574957; darn=lists.infradead.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=wG9hLC1Y8Qz4XIU+37knJUuWpfkMo15LFrj6UcsKSzg=; b=i5n3cWo+x7cGCi/dbGckc6czkNVyqsvlL//VEmtRFwxWtEkymhWqtSA81YiBYtcFOu mkm46uvjMvyO/X35Wk+IYJyuTtJ0Mav8AViJ3sdpyqAfwiiTSIonxOs0Nb6zwvbDbgQx 8tKd6geAtPzFcOYpVkv827wt0UcC6v2zUIP24= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705970157; x=1706574957; 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=wG9hLC1Y8Qz4XIU+37knJUuWpfkMo15LFrj6UcsKSzg=; b=sOIiv8EVSVpQdID7q2n8qCn8taDHtD9DDwF/z52+FQYRMEwcCKDWuR8nTVKmSSBgHz EhCfMU0ohvIfJGnP8nraRkfKQGeVepr3DIROo7ZVzOKlXWKfFh/1asrPkKYrJ6wRmXIl CerkKxCg11MwgFBANo2X2HLxV3Oxklp/X6tiHDxOIRpKAqPW2qK15bnm7Vtm4jT367d7 WcpIX+qwSPfp9FKfiT0c1caCSErQNzsmyUQV4107p9ZQwaLp1BNh9bCa545Lse6byaBL r60/eLoun054vqvvqPhu4ZGKTIoW/+VZ4Kxo8CquzCu8OQjsA5cMdNR4hsAo5oCi4dwH nuww== X-Gm-Message-State: AOJu0YzlDkL7GLa8gTFvb0+3bioDZpSocy1JusZAx3bZcHUvnR3OyCWu wEXWVu5BsWtUIZ+aiw8xd+6Flw+z6wmD1iUYTJqxoJXBo2q8X6F3zNNHuY+tuw== X-Google-Smtp-Source: AGHT+IGbgL8Un0khLBlsN1pl13RP4p4uyS26GBQlOHuEQ2+m+4aEmkVM5e6HKgLY7UQK+FMw+Gq5gg== X-Received: by 2002:a05:6358:3a1b:b0:176:49e6:d1be with SMTP id g27-20020a0563583a1b00b0017649e6d1bemr2746628rwe.7.1705970157322; Mon, 22 Jan 2024 16:35:57 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id cz4-20020a17090ad44400b0028ddfb484bfsm10223874pjb.49.2024.01.22.16.35.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 16:35:55 -0800 (PST) From: Kees Cook To: linux-hardening@vger.kernel.org Cc: Kees Cook , Catalin Marinas , Will Deacon , Kalesh Singh , Fuad Tabba , Mark Brown , "Madhavan T. Venkataraman" , Marc Zyngier , Mark Rutland , linux-arm-kernel@lists.infradead.org, "Gustavo A. R. Silva" , Bill Wendling , Justin Stitt , linux-kernel@vger.kernel.org Subject: [PATCH 40/82] arm64: stacktrace: Refactor intentional wrap-around test Date: Mon, 22 Jan 2024 16:27:15 -0800 Message-Id: <20240123002814.1396804-40-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240122235208.work.748-kees@kernel.org> References: <20240122235208.work.748-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2174; i=keescook@chromium.org; h=from:subject; bh=uCq9BZALf0j9/ETUS3uuALuSJ7AAwOFns9U/7aqT71s=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlrwgIWLuyCnEjUOpchm9ZW7j82YoUv1l18xa4s B46n9dQEkGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZa8ICAAKCRCJcvTf3G3A JuwlD/9oy+jGtAZ3FCSqVsESv3Dn6Ts4xfGimKqQuQt33Bce9y47K5whT43UwUX/FxTZH/tqTgB TW3eKjRzRffUEDvN7ZT3GaQZJdk/dhoJFA5OwB26dEaeTJJ87F2CEIcM/XPxpKfWWV8laKhxXPW Azdz8NmHKXvqukygGhhgSEkQ2kpnbOFr/4IlSgZyItDfY4qACe9CngG6WRi14g/HSCU9Gywjg69 ZJ4iAQu4qte9m5QXTwr49Fk4KN/ToizWcH4VTLjQyoJ5q/FZ71Xgw4QOa1223/YsX+bcqYMH4bu wrszgLp+AmxgO/JkCu2LfeuzW/PA81WplhvzGnpP0ARDAU9f1b3mGW4MnOu7+xSVcXxEPNBdHUd 3qHBNykVoYEpQLXhzHp3489ya3gM8EnFQsefLnWYwE5at2WX4AB+AugITjOHRUKIKxVcMw/Yj5L vgdwRoSoXJjDDET92k/xcXhH34opUBHZT54vAmgUzK7pw3pfz8gKLwhZviN2r+BHKkBnSdMYFsM WjXNz1X2Tyl2wSrR4TmFPmaN+5owOGL0Rtmy9lrY6g0VnA1Ef5oRUD2D3FpyEPLXRpyKfY+/dY9 jSlhYv7D+Rklx7QQ6QyLp0T/HQfyi460ttQUAt1JSZq2cjQhlrAimUKh31uhzT1WykiwMZzlyPc hxrjR+5ut598BkQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240122_163701_382669_F7ECF277 X-CRM114-Status: GOOD ( 17.20 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In an effort to separate intentional arithmetic wrap-around from unexpected wrap-around, we need to refactor places that depend on this kind of math. One of the most common code patterns of this is: VAR + value < VAR Notably, this is considered "undefined behavior" for signed and pointer types, which the kernel works around by using the -fno-strict-overflow option in the build[1] (which used to just be -fwrapv). Regardless, we want to get the kernel source to the position where we can meaningfully instrument arithmetic wrap-around conditions and catch them when they are unexpected, regardless of whether they are signed[2], unsigned[3], or pointer[4] types. Refactor open-coded wrap-around addition test to use add_would_overflow(). This paves the way to enabling the wrap-around sanitizers in the future. Link: https://git.kernel.org/linus/68df3755e383e6fecf2354a67b08f92f18536594 [1] Link: https://github.com/KSPP/linux/issues/26 [2] Link: https://github.com/KSPP/linux/issues/27 [3] Link: https://github.com/KSPP/linux/issues/344 [4] Cc: Catalin Marinas Cc: Will Deacon Cc: Kalesh Singh Cc: Fuad Tabba Cc: Mark Brown Cc: "Madhavan T. Venkataraman" Cc: Marc Zyngier Cc: Mark Rutland Cc: linux-arm-kernel@lists.infradead.org Signed-off-by: Kees Cook Acked-by: Mark Rutland --- arch/arm64/include/asm/stacktrace/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/stacktrace/common.h b/arch/arm64/include/asm/stacktrace/common.h index f63dc654e545..6e0cb84961f8 100644 --- a/arch/arm64/include/asm/stacktrace/common.h +++ b/arch/arm64/include/asm/stacktrace/common.h @@ -49,7 +49,7 @@ static inline bool stackinfo_on_stack(const struct stack_info *info, if (!info->low) return false; - if (sp < info->low || sp + size < sp || sp + size > info->high) + if (sp < info->low || add_would_overflow(sp, size) || sp + size > info->high) return false; return true;