From nobody Sat Feb 7 21:08:20 2026 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (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 AC52D274B40 for ; Sun, 4 Jan 2026 16:25:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767543903; cv=none; b=cj1IlzkYYh9CI2IkL2ItS/AFQn7UtqGI+DaF+bl+BVhvHpei9FO/m7k1VhLnWJdTVpoRPUjZeCuoydFv8x4nP7fnkcNwL/aSO2taTZRpIMDYSfE++J1rYu8RqQaO+jtYvImckmLSZ4virtK4Az2XOMCgLqgWB5CkJK7kv+e0NEo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767543903; c=relaxed/simple; bh=j7cR8+ufxEC6b3PZjjtmQAAcUCXybtKLeeEyN+Kt9zs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EDHjuvyxqadXjyvNGVGY4hRW9TSnY0AiCtGaE7DYUufvoEoEbKt2yTQrEpfWHkA244oUF3D9rq14uNQSTLb7BsyOYePKiRkwdYJ4BYbB41EtytZUEUpxN8AMuiMFTD7d/qEwNY8vn0hp4FXi/VNu5JuD6Pz5krsouP7OcnN6UC0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=wqGYwy3r; arc=none smtp.client-ip=91.218.175.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="wqGYwy3r" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767543899; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CvUp10h0Mn9exzUZ/n1dBrd2YDkqgmgwZbLLzqYBLM4=; b=wqGYwy3rPPwjUgcwc+Zk72fRwu9rJHd4cpXwPFYdXQyo3aLdr2BFjuogY9pVKKvMo27hRB MJoMlLw7LAHxkey1r+/58EPUG5f81OtXgLn0IPa8CtwgPehxf7xadVLLtvI06qmcLb0YRz Ett+KwBbweZPn8l+dQiGwaC6Q4/vKc4= From: KaFai Wan To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, hawk@kernel.org, shuah@kernel.org, aleksander.lobakin@intel.com, toke@redhat.com, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: KaFai Wan , Yinhao Hu , Kaiyan Mei , Dongliang Mu Subject: [PATCH bpf-next 1/2] bpf, test_run: Fix user-memory-access vulnerability for LIVE_FRAMES Date: Mon, 5 Jan 2026 00:23:49 +0800 Message-ID: <20260104162350.347403-2-kafai.wan@linux.dev> In-Reply-To: <20260104162350.347403-1-kafai.wan@linux.dev> References: <20260104162350.347403-1-kafai.wan@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" When testing XDP programs with LIVE_FRAMES mode, if the metalen is set to >=3D (XDP_PACKET_HEADROOM - sizeof(struct xdp_frame)), there won't be enough space for the xdp_frame conversion in xdp_update_frame_from_buff(). Additionally, the xdp_frame structure may be filled with user-provided data, which can lead to a memory access vulnerability when converting to skb. This fix reverts to the original version and ensures data_hard_start correctly points to the xdp_frame structure, eliminating the security risk. Reported-by: Yinhao Hu Reported-by: Kaiyan Mei Reviewed-by: Dongliang Mu Fixes: 294635a8165a ("bpf, test_run: fix &xdp_frame misplacement for LIVE_F= RAMES") Signed-off-by: KaFai Wan --- net/bpf/test_run.c | 23 +++++++++---------- .../bpf/prog_tests/xdp_do_redirect.c | 6 ++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index 655efac6f133..00234eba7c76 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -90,11 +90,9 @@ static bool bpf_test_timer_continue(struct bpf_test_time= r *t, int iterations, struct xdp_page_head { struct xdp_buff orig_ctx; struct xdp_buff ctx; - union { - /* ::data_hard_start starts here */ - DECLARE_FLEX_ARRAY(struct xdp_frame, frame); - DECLARE_FLEX_ARRAY(u8, data); - }; + /* ::data_hard_start starts here */ + struct xdp_frame frame; + DECLARE_FLEX_ARRAY(u8, data); }; =20 struct xdp_test_data { @@ -131,10 +129,11 @@ static void xdp_test_run_init_page(netmem_ref netmem,= void *arg) frm_len =3D orig_ctx->data_end - orig_ctx->data_meta; meta_len =3D orig_ctx->data - orig_ctx->data_meta; headroom -=3D meta_len; + headroom +=3D sizeof(head->frame); =20 new_ctx =3D &head->ctx; - frm =3D head->frame; - data =3D head->data; + frm =3D &head->frame; + data =3D frm; memcpy(data + headroom, orig_ctx->data_meta, frm_len); =20 xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq); @@ -215,8 +214,8 @@ static bool frame_was_changed(const struct xdp_page_hea= d *head) * i.e. has the highest chances to be overwritten. If those two are * untouched, it's most likely safe to skip the context reset. */ - return head->frame->data !=3D head->orig_ctx.data || - head->frame->flags !=3D head->orig_ctx.flags; + return head->frame.data !=3D head->orig_ctx.data || + head->frame.flags !=3D head->orig_ctx.flags; } =20 static bool ctx_was_changed(struct xdp_page_head *head) @@ -234,8 +233,8 @@ static void reset_ctx(struct xdp_page_head *head) head->ctx.data =3D head->orig_ctx.data; head->ctx.data_meta =3D head->orig_ctx.data_meta; head->ctx.data_end =3D head->orig_ctx.data_end; - xdp_update_frame_from_buff(&head->ctx, head->frame); - head->frame->mem_type =3D head->orig_ctx.rxq->mem.type; + xdp_update_frame_from_buff(&head->ctx, &head->frame); + head->frame.mem_type =3D head->orig_ctx.rxq->mem.type; } =20 static int xdp_recv_frames(struct xdp_frame **frames, int nframes, @@ -301,7 +300,7 @@ static int xdp_test_run_batch(struct xdp_test_data *xdp= , struct bpf_prog *prog, head =3D phys_to_virt(page_to_phys(page)); reset_ctx(head); ctx =3D &head->ctx; - frm =3D head->frame; + frm =3D &head->frame; xdp->frame_cnt++; =20 act =3D bpf_prog_run_xdp(prog, ctx); diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c b/too= ls/testing/selftests/bpf/prog_tests/xdp_do_redirect.c index dd34b0cc4b4e..f7615c265e6e 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c @@ -59,12 +59,12 @@ static int attach_tc_prog(struct bpf_tc_hook *hook, int= fd) =20 /* The maximum permissible size is: PAGE_SIZE - sizeof(struct xdp_page_hea= d) - * SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) - XDP_PACKET_HEADROOM = =3D - * 3408 bytes for 64-byte cacheline and 3216 for 256-byte one. + * 3368 bytes for 64-byte cacheline and 3216 for 256-byte one. */ #if defined(__s390x__) -#define MAX_PKT_SIZE 3216 +#define MAX_PKT_SIZE 3176 #else -#define MAX_PKT_SIZE 3408 +#define MAX_PKT_SIZE 3368 #endif =20 #define PAGE_SIZE_4K 4096 --=20 2.43.0 From nobody Sat Feb 7 21:08:20 2026 Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (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 3C056234973; Sun, 4 Jan 2026 16:25:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767543911; cv=none; b=VvLH0oP5drHGnhRPhOWTQMgvVhxmP9kkQ7P379Yj59V3DdqCt5Cc5zh22uzAhqwaQieKIFlqTrPjIvWqSbe0HbiKm4dxNslIBxFORicx8dFFzR7H6IfQ1KxoW69hEn+aYBG0aJiyica+vhRMIjEF5T6p6cw7k6yt+nk0KDxz1sE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767543911; c=relaxed/simple; bh=LSTTQ1Bw6k7njO8lwuxwQO6h9yNeUWCg0KXwR2Yikd8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sUcoo27Q+R4WGRl71E1dDUxqXvENq+Db6Xt6J7su4OUoqLiLMEU8Rvuhwnnv4bgoxzAnWg19BsHI1TY7AC869aaEPzKpTz8xyLZj+mYQQjy/MCmObc29atL+vKOu8vkhp4nmfUPvfQ4UllM/01JVcNw0LClTFz/qekbVYO9QtsA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=vuVRnV8K; arc=none smtp.client-ip=91.218.175.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="vuVRnV8K" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767543906; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pqbsIbMnN1SRJEl+iPFxJCqCnnA13u+xQvRbspdzZhg=; b=vuVRnV8KsWEl32XjLibUJqTVNuyAgu2Rfq8RAwC0U+f3yHLB0jE+t/S/hmO3+uRW8TzkY1 Igqj2rbXzsy0k2snLJkZd8fi0/Z4YVnnVT9yrGEce7ypmn/GIYwbswno5s2vtQk8OlNSXx qghU0C9WepW1socVTmvBocKOfMzllHM= From: KaFai Wan To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, hawk@kernel.org, shuah@kernel.org, aleksander.lobakin@intel.com, toke@redhat.com, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: KaFai Wan Subject: [PATCH bpf-next 2/2] selftests/bpf: Add test for xdp_md context with LIVE_FRAMES in BPF_PROG_TEST_RUN Date: Mon, 5 Jan 2026 00:23:50 +0800 Message-ID: <20260104162350.347403-3-kafai.wan@linux.dev> In-Reply-To: <20260104162350.347403-1-kafai.wan@linux.dev> References: <20260104162350.347403-1-kafai.wan@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Add a test case uses xdp_md as context parameter for BPF_PROG_TEST_RUN with LIVE_FRAMES flag. The test ensures that potential user-memory-access vulnerabilities are properly prevented. Signed-off-by: KaFai Wan --- .../bpf/prog_tests/xdp_context_test_run.c | 19 +++++++++++++++++++ .../bpf/progs/test_xdp_context_test_run.c | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_context_test_run.c = b/tools/testing/selftests/bpf/prog_tests/xdp_context_test_run.c index ee94c281888a..0276daaae45c 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_context_test_run.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_context_test_run.c @@ -45,6 +45,7 @@ void test_xdp_context_error(int prog_fd, struct bpf_test_= run_opts opts, void test_xdp_context_test_run(void) { struct test_xdp_context_test_run *skel =3D NULL; + char data_xdp[sizeof(pkt_v4) + XDP_PACKET_HEADROOM]; char data[sizeof(pkt_v4) + sizeof(__u32)]; char bad_ctx[sizeof(struct xdp_md) + 1]; struct xdp_md ctx_in, ctx_out; @@ -55,6 +56,12 @@ void test_xdp_context_test_run(void) .ctx_size_out =3D sizeof(ctx_out), .repeat =3D 1, ); + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts_xdp, + .data_in =3D &data_xdp, + .data_size_in =3D sizeof(data_xdp), + .flags =3D BPF_F_TEST_XDP_LIVE_FRAMES, + .repeat =3D 1, + ); int err, prog_fd; =20 skel =3D test_xdp_context_test_run__open_and_load(); @@ -70,6 +77,18 @@ void test_xdp_context_test_run(void) ASSERT_EQ(errno, E2BIG, "extradata-errno"); ASSERT_ERR(err, "bpf_prog_test_run(extradata)"); =20 + memset(&ctx_in, 0, sizeof(ctx_in)); + ctx_in.data_meta =3D 0; + ctx_in.data =3D 0xf4; + ctx_in.data_end =3D sizeof(data_xdp); + opts_xdp.ctx_in =3D &ctx_in; + opts_xdp.ctx_size_in =3D sizeof(ctx_in); + *(__u32 *)(data_xdp + 0) =3D 0x28d6a0b5; + *(__u32 *)(data_xdp + 4) =3D 0xf273eea3; + *(struct ipv4_packet *)(data_xdp + ctx_in.data) =3D pkt_v4; + err =3D bpf_prog_test_run_opts(bpf_program__fd(skel->progs.xdp_pass), &op= ts_xdp); + ASSERT_OK(err, "bpf_prog_test_run(valid meta)"); + *(__u32 *)data =3D XDP_PASS; *(struct ipv4_packet *)(data + sizeof(__u32)) =3D pkt_v4; opts.ctx_in =3D &ctx_in; diff --git a/tools/testing/selftests/bpf/progs/test_xdp_context_test_run.c = b/tools/testing/selftests/bpf/progs/test_xdp_context_test_run.c index d7b88cd05afd..2166928d4680 100644 --- a/tools/testing/selftests/bpf/progs/test_xdp_context_test_run.c +++ b/tools/testing/selftests/bpf/progs/test_xdp_context_test_run.c @@ -17,4 +17,10 @@ int xdp_context(struct xdp_md *xdp) return ret; } =20 +SEC("xdp") +int xdp_pass(struct xdp_md *xdp) +{ + return XDP_PASS; +} + char _license[] SEC("license") =3D "GPL"; --=20 2.43.0