Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2026 Qi Tang */
3
4#include <vmlinux.h>
5#include <bpf/bpf_helpers.h>
6#include "bpf_misc.h"
7
8char _license[] SEC("license") = "GPL";
9
10/* Verify that the verifier rejects direct access to nullable PTR_TO_BUF. */
11SEC("iter/bpf_map_elem")
12__failure __msg("invalid mem access")
13int iter_buf_null_deref(struct bpf_iter__bpf_map_elem *ctx)
14{
15 /*
16 * ctx->key is PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY.
17 * Direct access without null check must be rejected.
18 */
19 volatile __u32 v = *(__u32 *)ctx->key;
20
21 (void)v;
22 return 0;
23}
24
25/* Verify that access after a null check is still accepted. */
26SEC("iter/bpf_map_elem")
27__success
28int iter_buf_null_check_ok(struct bpf_iter__bpf_map_elem *ctx)
29{
30 __u32 *key = ctx->key;
31
32 if (!key)
33 return 0;
34
35 volatile __u32 v = *key;
36
37 (void)v;
38 return 0;
39}