• Yonghong Song's avatar
    selftests/bpf: add a selftest with __user tag · 696c3901
    Yonghong Song authored
    Added a selftest with three__user usages: a __user pointer-type argument
    in bpf_testmod, a __user pointer-type struct member in bpf_testmod,
    and a __user pointer-type struct member in vmlinux. In all cases,
    directly accessing the user memory will result verification failure.
    
      $ ./test_progs -v -n 22/3
      ...
      libbpf: prog 'test_user1': BPF program load failed: Permission denied
      libbpf: prog 'test_user1': -- BEGIN PROG LOAD LOG --
      R1 type=ctx expected=fp
      0: R1=ctx(id=0,off=0,imm=0) R10=fp0
      ; int BPF_PROG(test_user1, struct bpf_testmod_btf_type_tag_1 *arg)
      0: (79) r1 = *(u64 *)(r1 +0)
      func 'bpf_testmod_test_btf_type_tag_user_1' arg0 has btf_id 136561 type STRUCT 'bpf_testmod_btf_type_tag_1'
      1: R1_w=user_ptr_bpf_testmod_btf_type_tag_1(id=0,off=0,imm=0)
      ; g = arg->a;
      1: (61) r1 = *(u32 *)(r1 +0)
      R1 invalid mem access 'user_ptr_'
      ...
      #22/3 btf_tag/btf_type_tag_user_mod1:OK
    
      $ ./test_progs -v -n 22/4
      ...
      libbpf: prog 'test_user2': BPF program load failed: Permission denied
      libbpf: prog 'test_user2': -- BEGIN PROG LOAD LOG --
      R1 type=ctx expected=fp
      0: R1=ctx(id=0,off=0,imm=0) R10=fp0
      ; int BPF_PROG(test_user2, struct bpf_testmod_btf_type_tag_2 *arg)
      0: (79) r1 = *(u64 *)(r1 +0)
      func 'bpf_testmod_test_btf_type_tag_user_2' arg0 has btf_id 136563 type STRUCT 'bpf_testmod_btf_type_tag_2'
      1: R1_w=ptr_bpf_testmod_btf_type_tag_2(id=0,off=0,imm=0)
      ; g = arg->p->a;
      1: (79) r1 = *(u64 *)(r1 +0)          ; R1_w=user_ptr_bpf_testmod_btf_type_tag_1(id=0,off=0,imm=0)
      ; g = arg->p->a;
      2: (61) r1 = *(u32 *)(r1 +0)
      R1 invalid mem access 'user_ptr_'
      ...
      #22/4 btf_tag/btf_type_tag_user_mod2:OK
    
      $ ./test_progs -v -n 22/5
      ...
      libbpf: prog 'test_sys_getsockname': BPF program load failed: Permission denied
      libbpf: prog 'test_sys_getsockname': -- BEGIN PROG LOAD LOG --
      R1 type=ctx expected=fp
      0: R1=ctx(id=0,off=0,imm=0) R10=fp0
      ; int BPF_PROG(test_sys_getsockname, int fd, struct sockaddr *usockaddr,
      0: (79) r1 = *(u64 *)(r1 +8)
      func '__sys_getsockname' arg1 has btf_id 2319 type STRUCT 'sockaddr'
      1: R1_w=user_ptr_sockaddr(id=0,off=0,imm=0)
      ; g = usockaddr->sa_family;
      1: (69) r1 = *(u16 *)(r1 +0)
      R1 invalid mem access 'user_ptr_'
      ...
      #22/5 btf_tag/btf_type_tag_user_vmlinux:OK
    Signed-off-by: default avatarYonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20220127154616.659314-1-yhs@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    696c3901
bpf_testmod.c 3.98 KB