From nobody Thu Jul 27 20:12:55 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RBhll0tMgz4pdv8; Thu, 27 Jul 2023 20:12:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RBhlX3LBPz3N7R; Thu, 27 Jul 2023 20:12:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690488776; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mQGUNZ/e9YF15ow8F01SND9mujb86WQ2UqpsvPCWXOY=; b=ZoNG5wqtvyVEprqeLlWe1iDknb5XZyD1UFroCzhxXestQouJTGIM8JZF5i6tPbTmvXBPqT 8IYGY+eaDxd0kyIJOMv30a4TBbvJFkPUSvThY4rK+Y08cJ98SWuAz4jGDFob+P65+X+ryX aGcPGSIwkgtKQh5OaB1MXV4k0qzYVwe3ekm0zrIVYWx4xV/WEOr9dPgSKIg60GnYNgN6C9 dBuNVZqeShcWdf8UFNI0t5xcVjQOvTGHQgryZp0id3QUofxi+Ix7UxW5/8f7CsdgroM0YD +FwgD2PDapz+/gpVnIrcsrlXN4Ajkd+HyAnlYALyYJzdjt7xkO3XMcKDaDKogg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690488776; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mQGUNZ/e9YF15ow8F01SND9mujb86WQ2UqpsvPCWXOY=; b=qHqt5R6eFhSCNhJnApDXQ6bYlFK5ftEvB1Am8s4xPW2jUOlF4e89EpWnRY5J1d93g82Cgv HxQB+euJJcgY6muC/gou9/IgqeyFPPHnvDTwLbUOhShoHgAe4LDHoWfY69Nu3O4F9UJQfU 75Za8/elaEcz3f35xd+ngImfqsnEaMvGt7AeInnlHz4G3X17qpFYVT1RfR4YarTxzv+vx6 y8VoXptVCZWSEw48f6sluRwvkERSQUJSMZehA4K0pGZldfdrx5SzR0mPpGt8wS6hEoK2VC G2iGXEsTYx/3GYaP+DF3Nks7LKR593mvW8fX7GT+EmNAFJwnhZsubRQlu+V8NA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1690488776; a=rsa-sha256; cv=none; b=wUvSNYQlsnw+wV5GVW0NC3OvLGwqNoksJ4prehQdW8uXPVHE/Kg9o/DM4fY+F35mdtVckP m+Z2rpH6lfBxiA7eKGSkFEB/odY6d1FSWHze5GN4yjw4OEAZzOD4PFHckae3X5TgO9tWI0 Oe9R1nTk4S6xAQ7oIe6hl/poZl98XgSxsoGMzYA8p6dafxPTM+avFDSh3pQkBeS+AT5cCa ZlqRNG/oX5hd3V/4gDCnYnsmex/qjdIOCY7hq1s0nkAIQI0tT//W7uuZwhfuUu7YT5Lnn4 VlWXl0gpGmDezKQvukT0mgGSrLjowWFqO0R0X9/IpfovtAIuLSiUYT66PxW4NA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RBhlX1CN9zNX5; Thu, 27 Jul 2023 20:12:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 36RKCt6k087635; Thu, 27 Jul 2023 20:12:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 36RKCtCY087634; Thu, 27 Jul 2023 20:12:55 GMT (envelope-from git) Date: Thu, 27 Jul 2023 20:12:55 GMT Message-Id: <202307272012.36RKCtCY087634@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 1be56e0bb1e8 - main - arm/unwind: Check stack pointer boundaries before dereferencing List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1be56e0bb1e8bd8373e446ff9386bcdd764935aa Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=1be56e0bb1e8bd8373e446ff9386bcdd764935aa commit 1be56e0bb1e8bd8373e446ff9386bcdd764935aa Author: Mark Johnston AuthorDate: 2023-07-27 19:44:00 +0000 Commit: Mark Johnston CommitDate: 2023-07-27 19:44:00 +0000 arm/unwind: Check stack pointer boundaries before dereferencing If the unwinder somehow ends up with a stack pointer that lies outside the stack, then an attempt to dereference can lead to a fault, which causes the kernel to panic again and unwind the stack, which leads to a fault... Add kstack_contains() checks at points where we dereference the stack pointer. This avoids the aforementioned infinite loop in one case I hit where some OpenSSL assembly code apparently confuses the unwinder. Reviewed by: jhb MFC after: 2 weeks Sponsored by: Klara, Inc. Sponsored by: Stormshield Differential Revision: https://reviews.freebsd.org/D41210 --- sys/arm/arm/unwind.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sys/arm/arm/unwind.c b/sys/arm/arm/unwind.c index bf8ffddfd2c2..5d3309d4539d 100644 --- a/sys/arm/arm/unwind.c +++ b/sys/arm/arm/unwind.c @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include @@ -370,6 +371,7 @@ unwind_exec_read_byte(struct unwind_state *state) static int unwind_exec_insn(struct unwind_state *state) { + struct thread *td = curthread; unsigned int insn; uint32_t *vsp = (uint32_t *)state->registers[SP]; int update_vsp = 0; @@ -404,6 +406,10 @@ unwind_exec_insn(struct unwind_state *state) /* Load the registers */ for (reg = 4; mask && reg < 16; mask >>= 1, reg++) { if (mask & 1) { + if (!kstack_contains(td, (uintptr_t)vsp, + sizeof(*vsp))) + return 1; + state->registers[reg] = *vsp++; state->update_mask |= 1 << reg; @@ -430,6 +436,9 @@ unwind_exec_insn(struct unwind_state *state) update_vsp = 1; /* Pop the registers */ + if (!kstack_contains(td, (uintptr_t)vsp, + sizeof(*vsp) * (4 + count))) + return 1; for (reg = 4; reg <= 4 + count; reg++) { state->registers[reg] = *vsp++; state->update_mask |= 1 << reg; @@ -437,6 +446,8 @@ unwind_exec_insn(struct unwind_state *state) /* Check if we are in the pop r14 version */ if ((insn & INSN_POP_TYPE_MASK) != 0) { + if (!kstack_contains(td, (uintptr_t)vsp, sizeof(*vsp))) + return 1; state->registers[14] = *vsp++; } @@ -457,6 +468,9 @@ unwind_exec_insn(struct unwind_state *state) /* Load the registers */ for (reg = 0; mask && reg < 4; mask >>= 1, reg++) { if (mask & 1) { + if (!kstack_contains(td, (uintptr_t)vsp, + sizeof(*vsp))) + return 1; state->registers[reg] = *vsp++; state->update_mask |= 1 << reg; }