Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Test for checking return code for the extended prog

This adds test to enforce same check for the return code for the extended prog
as it is enforced for the target program. It asserts failure for a
return code, which is permitted without the patch in this series, while
it is restricted after the application of this patch.

Signed-off-by: Udip Pant <udippant@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200825232003.2877030-4-udippant@fb.com

authored by

Udip Pant and committed by
Alexei Starovoitov
50d19736 6dc03dc7

+59
+40
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c
··· 142 142 prog_name, false); 143 143 } 144 144 145 + static void test_func_replace_return_code(void) 146 + { 147 + /* 148 + * standalone test that asserts failure to load freplace prog 149 + * because of invalid return code. 150 + */ 151 + struct bpf_object *obj = NULL, *pkt_obj; 152 + int err, pkt_fd; 153 + __u32 duration = 0; 154 + const char *target_obj_file = "./connect4_prog.o"; 155 + const char *obj_file = "./freplace_connect_v4_prog.o"; 156 + 157 + err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, 158 + &pkt_obj, &pkt_fd); 159 + /* the target prog should load fine */ 160 + if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", 161 + target_obj_file, err, errno)) 162 + return; 163 + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, 164 + .attach_prog_fd = pkt_fd, 165 + ); 166 + 167 + obj = bpf_object__open_file(obj_file, &opts); 168 + if (CHECK(IS_ERR_OR_NULL(obj), "obj_open", 169 + "failed to open %s: %ld\n", obj_file, 170 + PTR_ERR(obj))) 171 + goto close_prog; 172 + 173 + /* It should fail to load the program */ 174 + err = bpf_object__load(obj); 175 + if (CHECK(!err, "bpf_obj_load should fail", "err %d\n", err)) 176 + goto close_prog; 177 + 178 + close_prog: 179 + if (!IS_ERR_OR_NULL(obj)) 180 + bpf_object__close(obj); 181 + bpf_object__close(pkt_obj); 182 + } 183 + 145 184 void test_fexit_bpf2bpf(void) 146 185 { 147 186 test_target_no_callees(); 148 187 test_target_yes_callees(); 149 188 test_func_replace(); 150 189 test_func_replace_verify(); 190 + test_func_replace_return_code(); 151 191 }
+19
tools/testing/selftests/bpf/progs/freplace_connect_v4_prog.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // Copyright (c) 2020 Facebook 3 + 4 + #include <linux/stddef.h> 5 + #include <linux/ipv6.h> 6 + #include <linux/bpf.h> 7 + #include <linux/in.h> 8 + #include <sys/socket.h> 9 + #include <bpf/bpf_helpers.h> 10 + #include <bpf/bpf_endian.h> 11 + 12 + SEC("freplace/connect_v4_prog") 13 + int new_connect_v4_prog(struct bpf_sock_addr *ctx) 14 + { 15 + // return value thats in invalid range 16 + return 255; 17 + } 18 + 19 + char _license[] SEC("license") = "GPL";