• Kumar Kartikeya Dwivedi's avatar
    bpf: Fix PTR_TO_BTF_ID var_off check · 655efe50
    Kumar Kartikeya Dwivedi authored
    When kfunc support was added, check_ctx_reg was called for PTR_TO_CTX
    register, but no offset checks were made for PTR_TO_BTF_ID. Only
    reg->off was taken into account by btf_struct_ids_match, which protected
    against type mismatch due to non-zero reg->off, but when reg->off was
    zero, a user could set the variable offset of the register and allow it
    to be passed to kfunc, leading to bad pointer being passed into the
    kernel.
    
    Fix this by reusing the extracted helper check_func_arg_reg_off from
    previous commit, and make one call before checking all supported
    register types. Since the list is maintained, any future changes will be
    taken into account by updating check_func_arg_reg_off. This function
    prevents non-zero var_off to be set for PTR_TO_BTF_ID, but still allows
    a fixed non-zero reg->off, which is needed for type matching to work
    correctly when using pointer arithmetic.
    
    ARG_DONTCARE is passed as arg_type, since kfunc doesn't support
    accepting a ARG_PTR_TO_ALLOC_MEM without relying on size of parameter
    type from BTF (in case of pointer), or using a mem, len pair. The
    forcing of offset check for ARG_PTR_TO_ALLOC_MEM is done because ringbuf
    helpers obtain the size from the header located at the beginning of the
    memory region, hence any changes to the original pointer shouldn't be
    allowed. In case of kfunc, size is always known, either at verification
    time, or using the length parameter, hence this forcing is not required.
    
    Since this check will happen once already for PTR_TO_CTX, remove the
    check_ptr_off_reg call inside its block.
    
    Fixes: e6ac2450 ("bpf: Support bpf program calling kernel function")
    Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220304224645.3677453-3-memxor@gmail.com
    655efe50
btf.c 188 KB