From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.195 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796086699; b=gq93k0yZdhk98ax1CrXExwfLZ14Rw/EHQGN9a/f//jJ1+U3RiPQj3/oPt99ePisHfgih BmjzGN67UYgwcuZkiQ1j+eNmw1fF60ksCtC24EW9VoaDMWrbSccdhvOz12q4HhTnk1vlQ PdqzJwIIbw6pK/rGue3x/cStxmxlIV2pU4Z/g16IUmKIt+YUzBBmVCXUg0qD1okbQ/yrZ dRYQ6TwmgijwUUY+KaJAL1nyOLYa4OVv5YSq9/NOzaYPnwOeiaurAK0QDlagTzFE3ktY4 g+2s9Gjs9DseKcBkdPY75/EWNKecGVmWZwhXcbohWtEzJox/OaOVcHH7thfLIR1SbEg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796086699; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:X-Developer-Signature:X-Developer-Key: Content-Transfer-Encoding; bh=6ADaLE8jYm8rNT2IAQKAXJepPjBkKDtxdmKU8mc9rGM=; b=4SlXvHzwd5VfJC2EVAAfGu+frxVJxXApQVIMRp1V3GPQNobMpJ5D5lVgLnpEBsOnxUQk 9cyPp3e/cOgN4m7gE0tqW+TrCFZ8w+7P31jaZQ+USaCIWhFNGMIRM93Ic3zR6sijayHRl 5M0t+xxskA1l5ikBQFbZnH0Zs+vL68BclujoH0UEveJG7U1Xbe/G+H+UldrztInYo+8z6 /Bm+lNRFylnEi8WOmz+G5C9uhsNl0M7OQuL3P77K4jCXqnUARnLTd7/1jkPXOZgAeaJ3G osmNsfF+ItR9dxiYqkAQqxfKMvht+1qMOrTFISbGMHJ9B4Vj7G+JFHbWty7o10l9fqA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cCptzuuc; arc=none smtp.remote-ip=74.125.231.195 Received: from mail-oi2-f3.google.com (mail-oi2-f3.google.com [74.125.231.195]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CD45606A32D for ; Tue, 1 Dec 2026 00:58:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4CD45606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cCptzuuc Received: by mail-oi2-f3.google.com with SMTP id 5614622812f47-4af85d1cb34so1017667b6e.0 for ; Mon, 30 Nov 2026 16:58:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796086697; x=1796691497; 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:content-type; bh=6ADaLE8jYm8rNT2IAQKAXJepPjBkKDtxdmKU8mc9rGM=; b=cCptzuucfhyga1xVwvC3NWAMdas50kByNINQ8fQrNdyAx9IN5qkIFBVyRDJA3r/Eli 6AcRYFR295zVlwExYdu8HnOEAvKdXzr7QIb9q50MCCqvAGg7DU+IhSJyLe4v3QqMu0zT gZ2YNkGFYCpjqAhNLhCpQCTmh61655hmuDfZRqRwZNODeY0ZlMpsV+N1gjhOti41Afo1 GLd2ypJAry7gBmm5t7223KeS9OlnUBU3LR5Ol78jw8tMerk/oj/Qx3U/p11d/fSyb/NE 7ivpzswuhVZ6nMHgbip27d69XR0SvSmQOCuSXbVoRUWOnLTonCNpD0mxIUwQXa1NX7Kp KYpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796086697; x=1796691497; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=6ADaLE8jYm8rNT2IAQKAXJepPjBkKDtxdmKU8mc9rGM=; b=EdsYkcgdECOhTpnLwupyYueTtiau0m0Y6tBE2bUQxOcpZteUHp1goJbW76NQqT8bph Is4aLSEOJTR3B1mERaWqTTfi1X8RG9fupz5+t3zyV+2aqTptQuKRgpIDIltZ2LgdPw4D 49FyRmYK01CiwDc09XY2rzmKUujh4AjnHFlgpdvH3O9y5ix3lEQR5kpA7aYSiA4EB6cB fBqD8TKsXZHwkAIB9E0vZoYIbQHLxN3nAH2F6dfOE66rCXJARjHkXTNX/skwS+v2S0Xp IXTaYrPs2qHCMVy++XWsNTHOnEzexxxdWiJUjFGpqivQcLKGlIl4lU24MvgOND9kiPPU Vn0g== X-Forwarded-Encrypted: i=1; AL+hErHK1zvE9QClA7PkvwQfGo+2nagt4GO4WoP+fGHQZGLbTeb/yDysbVUtiFSGMPMJzfqMOy4DI9rSi45iSkc=@vger.kernel.org X-Gm-Message-State: APuU4Z+9IK/noNZvYuYSNtU46j8OFxzizPfg+GFwWktSBkhbEY6lIsxY bDUFdxkP2Zrj+9ybW/7vVNRoTeXT5dmlGx1exEBXCIsITuHStuaawT0S X-Gm-Gg: AZFdTZfLYepdNj1/+uI7jjXNtLkFJRCHFHk6gput3woudBWFj5j5xlb7xZYPX2rgMKh rHzciK9Eb/tOsXXF01FDo0yZuduanLFfKlR6OYLTagk9bI62uVRsk7aDGrQ3p2WhRU5wp3c8fOz o3/xz5uG4DQZBLuAtpGgpYm/ajnedIUtXX6Hn3uX3WgcMioH6kN4FD12DrlZUS6P6tOBWBGhK4l zGNIUOcyIwRfpWTIsRALqA1qJDeQhh5ZwiufTo9fipRT0mGQIoiuM29oFto0Jtmkg2lksCw4Lyh OSc48+wS/vwSv+wWIPIQngjYE5ivOY76pN6BAuGnMU6nRAlWOy1uETpYDT8TVPTHP0csY26WfWG TbMkq4JZyXS7787JzLYdla7gBn1TqvAAPZyk4xwjQrcfV2skByBFCVRDSRNsSSoj2R4fm76ijy5 HnLzeixbz3jt4CbZGWQMXzHnQOIXSzZ/fOEH70Kk5tPnBPnt8Nhui19ptUse5DYAC4SeBnsKPE1 xHP0TBRR93Afhd/kxSFvc8bvj5SDtqlMPUZdQULPXmCUDR3EAm/0/38ilOThsuAWYUQx7kVTGMa eMlgLQ2gp6uR2Mh3uur12iyosrQ5u7onOmwl72jjaET6rk5rftuMT4CHXKj69YMvy3rXW2TSB5O mIFCVxYRsdS9XznCfSwEI+Fbqu23LnzTSFdU= X-Received: by 2002:a05:6808:6413:b0:4f3:68d6:f07a with SMTP id 5614622812f47-57a0bb607c9mr24983488b6e.30.1796086697098; Mon, 30 Nov 2026 16:58:17 -0800 (PST) Received: from localhost ([2a03:2880:10ff:1::]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57a3485e3ecsm24921129b6e.9.2026.11.30.16.58.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 16:58:15 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com, Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Puranjay Mohan , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v3 3/3] selftests/bpf: Test PROBE_MEM loads from invalid addresses Date: Tue, 1 Dec 2026 01:58:02 +0100 Message-ID: X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5118; h=from:subject:message-id; bh=lBEd9gswYQp8x9IxA02i2vKePHOg/na7EqogNIBgb3g=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZtPOtHreEDJrBXyE4T/tLbEpa6dyBFgdbr0xp0ZORL7J6/f bCrfUcrCIMbFICumyFLyfx+T8YnK34G2y7hh5rAygQxh4OIUgImcK2b4p3hk9r9gpXDRg+es6rUiWZ cGHl4ne4OVb/X/o7xpExedb2Zk6JyV5VP5WTVF/glji0lWcnrb0jq7Fe4OZeELYwLSmE9yAAA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Add a test that performs PROBE_MEM loads of three sizes through a bpf_core_cast() pointer whose value is chosen by userspace: NULL, a low user address, the last user page and the guard page of both the four and five level paging layouts, a non-canonical address and, on x86-64, the vsyscall page and an offset into it. Each load must read zero and the kernel must survive. A load through the current task pointer checks that the same loads read real values when the address is valid. NULL and the low user address are left out on s390x, which has its lowcore there. The test passes on kernels that reject the addresses with the JIT's range check and on kernels that rely on the fault handler. With only the JIT change applied, the first subtest oopses. Signed-off-by: Kumar Kartikeya Dwivedi --- .../bpf/prog_tests/probe_mem_fault.c | 77 +++++++++++++++++++ .../selftests/bpf/progs/probe_mem_fault.c | 41 ++++++++++ 2 files changed, 118 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c create mode 100644 tools/testing/selftests/bpf/progs/probe_mem_fault.c diff --git a/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c b/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c new file mode 100644 index 000000000000..9b89864b9ac2 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ +#include +#include "probe_mem_fault.skel.h" + +#if defined(__x86_64__) +#include +#endif + +/* + * Addresses a PROBE_MEM load has to survive. Either the JIT's address check + * or the fault handler must turn each load into a zero result. The user and + * guard page addresses are those of x86-64 with four and five paging levels; + * on other kernels they are merely unmapped. + */ +static const struct { + const char *name; + __u64 addr; +} bad_addrs[] = { +#if !defined(__s390x__) + /* s390x has its lowcore at these addresses, so the loads read it. */ + { "null", 0 }, + { "low_user", 4096 }, +#endif + { "last_user_page_4l", (1ULL << 47) - 2 * 4096 }, + { "guard_page_4l", (1ULL << 47) - 4096 }, + { "last_user_page_5l", (1ULL << 56) - 2 * 4096 }, + { "guard_page_5l", (1ULL << 56) - 4096 }, + { "non_canonical", 1ULL << 63 }, +#if defined(__x86_64__) + { "vsyscall", VSYSCALL_ADDR }, + { "vsyscall_tail", VSYSCALL_ADDR + 0x800 }, +#endif +}; + +static void trigger(struct probe_mem_fault *skel, int *runs) +{ + skel->bss->val_dw = ~0ULL; + skel->bss->val_w = ~0U; + skel->bss->val_b = ~0; + usleep(1); + ASSERT_EQ(skel->bss->runs, ++*runs, "runs"); +} + +void test_probe_mem_fault(void) +{ + struct probe_mem_fault *skel; + int runs = 0, i; + + skel = probe_mem_fault__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + skel->bss->target_pid = getpid(); + if (!ASSERT_OK(probe_mem_fault__attach(skel), "attach")) + goto out; + + /* A valid kernel address is read for real. */ + skel->bss->use_current_task = true; + trigger(skel, &runs); + ASSERT_EQ(skel->bss->val_w, getpid(), "pid"); + ASSERT_NEQ(skel->bss->val_dw, 0, "start_time"); + ASSERT_NEQ(skel->bss->val_b, 0, "comm"); + + skel->bss->use_current_task = false; + for (i = 0; i < ARRAY_SIZE(bad_addrs); i++) { + if (!test__start_subtest(bad_addrs[i].name)) + continue; + skel->bss->addr = bad_addrs[i].addr; + trigger(skel, &runs); + ASSERT_EQ(skel->bss->val_dw, 0, "start_time"); + ASSERT_EQ(skel->bss->val_w, 0, "pid"); + ASSERT_EQ(skel->bss->val_b, 0, "comm"); + } +out: + probe_mem_fault__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/probe_mem_fault.c b/tools/testing/selftests/bpf/progs/probe_mem_fault.c new file mode 100644 index 000000000000..f95db455722e --- /dev/null +++ b/tools/testing/selftests/bpf/progs/probe_mem_fault.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ +#include +#include +#include +#include +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +int target_pid; +bool use_current_task; +__u64 addr; +int runs; +__u64 val_dw; +__u32 val_w; +__u8 val_b; + +SEC("fentry/" SYS_PREFIX "sys_nanosleep") +int probe_mem_fault(void *ctx) +{ + struct task_struct *task; + __u64 p = addr; + + if ((bpf_get_current_pid_tgid() >> 32) != target_pid) + return 0; + + if (use_current_task) + p = (__u64)bpf_get_current_task_btf(); + /* + * bpf_core_cast() yields an untrusted pointer, so every load through it + * is a PROBE_MEM load. Whatever the address is, the load must either + * read the field or produce zero; the kernel must not oops. + */ + task = bpf_core_cast((void *)p, struct task_struct); + val_dw = task->start_time; + val_w = task->pid; + val_b = task->comm[0]; + runs++; + return 0; +} -- 2.53.1-Meta