From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
Kaiyan Mei <M202472210@hust.edu.cn>,
Yinhao Hu <dddddd@hust.edu.cn>, Dongliang Mu <dzm91@hust.edu.cn>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
Matthieu Baerts <matttbe@kernel.org>,
David Verbeiren <david.verbeiren@tessares.net>,
linux-kernel@vger.kernel.org
Subject: [PATCH bpf] bpf: Fix out-of-bounds read in bpf_obj_memcpy
Date: Wed, 8 Apr 2026 18:04:54 +0800 [thread overview]
Message-ID: <20260408100455.190561-1-jiayuan.chen@linux.dev> (raw)
When copying map value between two maps in BPF program, an out-of-bounds
read can occur in bpf_obj_memcpy(). Consider the following scenario:
// map1: BPF_MAP_TYPE_CGROUP_STORAGE, value_size = 4
// map2: BPF_MAP_TYPE_LRU_PERCPU_HASH, value_size = 4
void *src = bpf_get_local_storage(&map1, 0); // 4-byte buffer
bpf_map_update_elem(&map2, &key, src, 0); // copy src to map2
The verifier validates that source buffer size >= destination map's
value_size through check_helper_mem_access(). Since both maps have
value_size=4, verification passes.
However, at runtime bpf_obj_memcpy() rounds up the copy size to 8 bytes
for long-aligned atomic copy:
bpf_long_memcpy(dst, src, round_up(size, 8)); // reads 8 bytes
This causes a 4-byte over-read from the source buffer. Fix this by using
round_down() to only copy complete 8-byte chunks with bpf_long_memcpy(),
then copy any remaining bytes with regular memcpy().
This ensures we never read beyond the validated buffer size while still
maintaining atomic operations where possible.
Fixes: d3bec0138bfbe ("bpf: Zero-fill re-used per-cpu map element")
Closes: https://lore.kernel.org/bpf/14e6c70c.6c121.19c0399d948.Coremail.kaiyanm@hust.edu.cn/
Reported-by: Kaiyan Mei <M202472210@hust.edu.cn>
Reported-by: Yinhao Hu <dddddd@hust.edu.cn>
Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
include/linux/bpf.h | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 05b34a6355b03..1b789f9f8a095 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -535,10 +535,15 @@ static inline void bpf_obj_memcpy(struct btf_record *rec,
int i;
if (IS_ERR_OR_NULL(rec)) {
- if (long_memcpy)
- bpf_long_memcpy(dst, src, round_up(size, 8));
- else
+ u32 aligned = round_down(size, 8);
+
+ if (long_memcpy && aligned) {
+ bpf_long_memcpy(dst, src, aligned);
+ if (size > aligned)
+ memcpy(dst + aligned, src + aligned, size - aligned);
+ } else {
memcpy(dst, src, size);
+ }
return;
}
--
2.43.0
next reply other threads:[~2026-04-08 10:05 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-08 10:04 Jiayuan Chen [this message]
2026-04-08 10:52 ` [PATCH bpf] bpf: Fix out-of-bounds read in bpf_obj_memcpy Jiayuan Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260408100455.190561-1-jiayuan.chen@linux.dev \
--to=jiayuan.chen@linux.dev \
--cc=M202472210@hust.edu.cn \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=david.verbeiren@tessares.net \
--cc=dddddd@hust.edu.cn \
--cc=dzm91@hust.edu.cn \
--cc=eddyz87@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=matttbe@kernel.org \
--cc=memxor@gmail.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox