hisock_sockops_prog Loading Failures Due to BPF Verifier Rejection
Symptom
In some environments, during the compilation and loading of HiSock sample BPF programs, hisock_sockops_prog may fail to be loaded due to verification failure by the BPF verifier. The typical error message is as follows:
libbpf: prog 'hisock_sockops_prog': BPF program load failed: Permission denied ... dereference of modified ctx ptr ... disallowed
Analysis
This issue typically arises from how the BPF program accesses the sockops or sk_msg context fields. Variations in the BPF instruction sequences generated by different versions of the Clang/LLVM toolchain can make the program more susceptible to the verification failures mentioned above under specific kernel and BPF verifier combinations. Consequently, even if the same source code loads successfully in one environment, it may still be rejected by the verifier in another.
Solution
- Go to the kernel source code directory.
cd /usr/src/linux-6.6.0-145.3.18.151.oe2403sp3.aarch64/
- Save the following content as bpf_fix_verifier.patch:
From c5588c0d1c4c64672187294245f155f1e1e4f0e7 Mon Sep 17 00:00:00 2001 From: Pu Lehui <pulehui@huawei.com> Date: Thu, 16 Jul 2026 07:41:48 +0000 Subject: [PATCH] hisock: avoid llvm bug in olk-6.6 --- samples/bpf/hisock/bpf.c | 56 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/samples/bpf/hisock/bpf.c b/samples/bpf/hisock/bpf.c index 0b7a2784b59b..7ba07c85dfbc 100644 --- a/samples/bpf/hisock/bpf.c +++ b/samples/bpf/hisock/bpf.c @@ -90,16 +90,16 @@ static inline bool is_speed_flow(u16 port) } static inline bool -is_local_conn(struct bpf_sock_ops *skops, u32 *laddr, u32 *raddr) +is_local_conn(struct bpf_sock_ops *skops, u32 laddr, u32 raddr) { struct sock_tuple key = { 0 }; struct bpf_sock *sk; - if (*laddr == *raddr) + if (laddr == raddr) return true; - key.saddr = *laddr; - key.daddr = *raddr; + key.saddr = laddr; + key.daddr = raddr; key.sport = skops->local_port; key.dport = bpf_ntohl(skops->remote_port); @@ -154,12 +154,12 @@ static void handle_listen_cb(struct bpf_sock_ops *skops) } static void -handle_local_estd_cb(struct bpf_sock_ops *skops, u32 *laddr, u32 *raddr) +handle_local_estd_cb(struct bpf_sock_ops *skops, u32 laddr, u32 raddr) { struct sock_tuple key = { 0 }; - key.saddr = *raddr; - key.daddr = *laddr; + key.saddr = raddr; + key.daddr = laddr; key.sport = bpf_ntohl(skops->remote_port); key.dport = skops->local_port; @@ -167,13 +167,13 @@ handle_local_estd_cb(struct bpf_sock_ops *skops, u32 *laddr, u32 *raddr) } static void -handle_remote_estd_cb(struct bpf_sock_ops *skops, u32 *laddr, u32 *raddr) +handle_remote_estd_cb(struct bpf_sock_ops *skops, u32 laddr, u32 raddr) { struct sock_tuple key = { 0 }; struct sock_value val = { 0 }; - key.saddr = *raddr; - key.daddr = *laddr; + key.saddr = raddr; + key.daddr = laddr; key.sport = bpf_ntohl(skops->remote_port); key.dport = skops->local_port; @@ -187,32 +187,32 @@ handle_remote_estd_cb(struct bpf_sock_ops *skops, u32 *laddr, u32 *raddr) static inline void handle_passive_estd_inet_cb(struct bpf_sock_ops *skops) { - if (is_local_conn(skops, &skops->local_ip4, &skops->remote_ip4)) - handle_local_estd_cb(skops, &skops->local_ip4, &skops->remote_ip4); + if (is_local_conn(skops, skops->local_ip4, skops->remote_ip4)) + handle_local_estd_cb(skops, skops->local_ip4, skops->remote_ip4); else - handle_remote_estd_cb(skops, &skops->local_ip4, &skops->remote_ip4); + handle_remote_estd_cb(skops, skops->local_ip4, skops->remote_ip4); } static inline void handle_passive_estd_inet6_cb(struct bpf_sock_ops *skops) { - if (is_local_conn(skops, &skops->local_ip6[3], &skops->remote_ip6[3])) - handle_local_estd_cb(skops, &skops->local_ip6[3], &skops->remote_ip6[3]); + if (is_local_conn(skops, skops->local_ip6[3], skops->remote_ip6[3])) + handle_local_estd_cb(skops, skops->local_ip6[3], skops->remote_ip6[3]); else - handle_remote_estd_cb(skops, &skops->local_ip6[3], &skops->remote_ip6[3]); + handle_remote_estd_cb(skops, skops->local_ip6[3], skops->remote_ip6[3]); } static inline void handle_active_estd_inet_cb(struct bpf_sock_ops *skops) { - handle_local_estd_cb(skops, &skops->local_ip4, &skops->remote_ip4); + handle_local_estd_cb(skops, skops->local_ip4, skops->remote_ip4); } static inline void handle_active_estd_inet6_cb(struct bpf_sock_ops *skops) { - handle_local_estd_cb(skops, &skops->local_ip6[3], &skops->remote_ip6[3]); + handle_local_estd_cb(skops, skops->local_ip6[3], skops->remote_ip6[3]); } static void -handle_terminate_cb(struct bpf_sock_ops *skops, u32 *laddr, u32 *raddr) +handle_terminate_cb(struct bpf_sock_ops *skops, u32 laddr, u32 raddr) { struct sock_tuple key = { 0 }; @@ -221,8 +221,8 @@ handle_terminate_cb(struct bpf_sock_ops *skops, u32 *laddr, u32 *raddr) skops->args[1] != BPF_TCP_CLOSE) return; - key.saddr = *raddr; - key.daddr = *laddr; + key.saddr = raddr; + key.daddr = laddr; key.sport = bpf_ntohl(skops->remote_port); key.dport = skops->local_port; @@ -234,12 +234,12 @@ handle_terminate_cb(struct bpf_sock_ops *skops, u32 *laddr, u32 *raddr) static inline void handle_terminate_inet_cb(struct bpf_sock_ops *skops) { - handle_terminate_cb(skops, &skops->local_ip4, &skops->remote_ip4); + handle_terminate_cb(skops, skops->local_ip4, skops->remote_ip4); } static inline void handle_terminate_inet6_cb(struct bpf_sock_ops *skops) { - handle_terminate_cb(skops, &skops->local_ip6[3], &skops->remote_ip6[3]); + handle_terminate_cb(skops, skops->local_ip6[3], skops->remote_ip6[3]); } SEC("hisock_sockops") @@ -293,12 +293,12 @@ int hisock_sockops_prog(struct bpf_sock_ops *skops) } static void -msg_redirect_cb(struct sk_msg_md *msg, u32 *laddr, u32 *raddr) +msg_redirect_cb(struct sk_msg_md *msg, u32 laddr, u32 raddr) { struct sock_tuple key = { 0 }; - key.daddr = *raddr; - key.saddr = *laddr; + key.daddr = raddr; + key.saddr = laddr; key.dport = bpf_ntohl(msg->remote_port); key.sport = msg->local_port; @@ -307,12 +307,12 @@ msg_redirect_cb(struct sk_msg_md *msg, u32 *laddr, u32 *raddr) static inline void msg_redirect_inet_cb(struct sk_msg_md *msg) { - msg_redirect_cb(msg, &msg->local_ip4, &msg->remote_ip4); + msg_redirect_cb(msg, msg->local_ip4, msg->remote_ip4); } static inline void msg_redirect_inet6_cb(struct sk_msg_md *msg) { - msg_redirect_cb(msg, &msg->local_ip6[3], &msg->remote_ip6[3]); + msg_redirect_cb(msg, msg->local_ip6[3], msg->remote_ip6[3]); } SEC("hisock_skmsg") -- 2.34.1 - Run the following command to apply the optimization patch to bpf.c:
patch -p1 < bpf_fix_verifier.patch
- Go to the kernel source code directory and recompile the HiSock sample programs.
cd /usr/src/linux-6.6.0-145.3.18.151.oe2403sp3.aarch64/ export VMLINUX_BTF=/sys/kernel/btf/vmlinux make -C tools/lib/bpf/ -j$(nproc) make -C samples/bpf/ -j$(nproc)
Parent topic: Troubleshooting