From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8AAE26FDBA; Tue, 22 Apr 2025 02:18:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745288333; cv=none; b=k/lJvfLZDf3Wlo5lSSxWgDKywpPJCF8vzsbleALTjzLQKg7jHl1vFsEZ0RpkMm3QYafK/lm4BY/UyJe0CfYZ0rfzHXcwhGcpwypkU+GmipnJEX3G2X6/STwmA2fIWY06EarNfBOgFhN+7Qy4iSHVQE8tZoy5JgwBrdb09G8wH/c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745288333; c=relaxed/simple; bh=M8lStOqI4Gjp03xyOAbijLlG+Tsrjyhn2vf4gqu6O7E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ai6tQ9Yr39Z0r5ZxZ4kK0RTUjG/rS98TW0SYYDoEpVm+WsR9BX3rp/mVnxA8U5Z2mSSxbmHteq0n3fhMKzyHMGNg8I6DAa6DJ91Ttvz68o1tOW3CIsKDEjXsW4DEzWGrg01h+UOeXn2ScfcYk3186TZ7tX2kdA6mSplmW8bA0XA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Pb/bTzfs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Pb/bTzfs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5DAC5C4CEE4; Tue, 22 Apr 2025 02:18:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745288333; bh=M8lStOqI4Gjp03xyOAbijLlG+Tsrjyhn2vf4gqu6O7E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Pb/bTzfs4fyAMWbcDgV2fG+XddVRvzdDKhewptkggr+9CFHjd38b/Ng9FuUYp8hLV qtqZ1ckwudNzDDRhiLnZKztc2xz1Dg/nq6DEYR8xa9BFo2BjrUmllhhBIxcB6bhIou tgGapheYvyxORBYgGikENokup5ttI9qKkSI0a56l0Yw5UAsDvADEhFs0+Ma2zHGiwK 4W6Q3klTS7gZRP4U99V1hNEdklZ2hKNqC76LkIPOciXXDOJOTfI9RjVJf+F5mgrV5s J41RscRF45nfLRmnBzUBbXtlGQh51IMKMGtn/Qwp0/Ev/aGJhuvFVj9QfICuIjRVjb GpLg0Zl2NMlcQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Mostafa Saleh , Kees Cook , Sasha Levin , kasan-dev@googlegroups.com, linux-hardening@vger.kernel.org Subject: [PATCH AUTOSEL 5.15 4/6] ubsan: Fix panic from test_ubsan_out_of_bounds Date: Mon, 21 Apr 2025 22:18:44 -0400 Message-Id: <20250422021846.1941972-4-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250422021846.1941972-1-sashal@kernel.org> References: <20250422021846.1941972-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 5.15.180 Content-Transfer-Encoding: 8bit From: Mostafa Saleh [ Upstream commit 9b044614be12d78d3a93767708b8d02fb7dfa9b0 ] Running lib_ubsan.ko on arm64 (without CONFIG_UBSAN_TRAP) panics the kernel: [ 31.616546] Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: test_ubsan_out_of_bounds+0x158/0x158 [test_ubsan] [ 31.646817] CPU: 3 UID: 0 PID: 179 Comm: insmod Not tainted 6.15.0-rc2 #1 PREEMPT [ 31.648153] Hardware name: linux,dummy-virt (DT) [ 31.648970] Call trace: [ 31.649345] show_stack+0x18/0x24 (C) [ 31.650960] dump_stack_lvl+0x40/0x84 [ 31.651559] dump_stack+0x18/0x24 [ 31.652264] panic+0x138/0x3b4 [ 31.652812] __ktime_get_real_seconds+0x0/0x10 [ 31.653540] test_ubsan_load_invalid_value+0x0/0xa8 [test_ubsan] [ 31.654388] init_module+0x24/0xff4 [test_ubsan] [ 31.655077] do_one_initcall+0xd4/0x280 [ 31.655680] do_init_module+0x58/0x2b4 That happens because the test corrupts other data in the stack: 400: d5384108 mrs x8, sp_el0 404: f9426d08 ldr x8, [x8, #1240] 408: f85f83a9 ldur x9, [x29, #-8] 40c: eb09011f cmp x8, x9 410: 54000301 b.ne 470 // b.any As there is no guarantee the compiler will order the local variables as declared in the module: volatile char above[4] = { }; /* Protect surrounding memory. */ volatile int arr[4]; volatile char below[4] = { }; /* Protect surrounding memory. */ There is another problem where the out-of-bound index is 5 which is larger than the extra surrounding memory for protection. So, use a struct to enforce the ordering, and fix the index to be 4. Also, remove some of the volatiles and rely on OPTIMIZER_HIDE_VAR() Signed-off-by: Mostafa Saleh Link: https://lore.kernel.org/r/20250415203354.4109415-1-smostafa@google.com Signed-off-by: Kees Cook Signed-off-by: Sasha Levin --- lib/test_ubsan.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/test_ubsan.c b/lib/test_ubsan.c index 2062be1f2e80f..f90f2b9842ec4 100644 --- a/lib/test_ubsan.c +++ b/lib/test_ubsan.c @@ -35,18 +35,22 @@ static void test_ubsan_shift_out_of_bounds(void) static void test_ubsan_out_of_bounds(void) { - volatile int i = 4, j = 5, k = -1; - volatile char above[4] = { }; /* Protect surrounding memory. */ - volatile int arr[4]; - volatile char below[4] = { }; /* Protect surrounding memory. */ + int i = 4, j = 4, k = -1; + volatile struct { + char above[4]; /* Protect surrounding memory. */ + int arr[4]; + char below[4]; /* Protect surrounding memory. */ + } data; - above[0] = below[0]; + OPTIMIZER_HIDE_VAR(i); + OPTIMIZER_HIDE_VAR(j); + OPTIMIZER_HIDE_VAR(k); UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above"); - arr[j] = i; + data.arr[j] = i; UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below"); - arr[k] = i; + data.arr[k] = i; } enum ubsan_test_enum { -- 2.39.5