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 8 char _license[] SEC("license") = "GPL"; 9 10 /* Verify that the verifier rejects direct access to nullable PTR_TO_BUF. */ 11 SEC("iter/bpf_map_elem") 12 __failure __msg("invalid mem access") 13 int 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. */ 26 SEC("iter/bpf_map_elem") 27 __success 28 int 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 } 40