• Kumar Kartikeya Dwivedi's avatar
    bpf: Harden register offset checks for release helpers and kfuncs · 24d5bb80
    Kumar Kartikeya Dwivedi authored
    Let's ensure that the PTR_TO_BTF_ID reg being passed in to release BPF
    helpers and kfuncs always has its offset set to 0. While not a real
    problem now, there's a very real possibility this will become a problem
    when more and more kfuncs are exposed, and more BPF helpers are added
    which can release PTR_TO_BTF_ID.
    
    Previous commits already protected against non-zero var_off. One of the
    case we are concerned about now is when we have a type that can be
    returned by e.g. an acquire kfunc:
    
    struct foo {
    	int a;
    	int b;
    	struct bar b;
    };
    
    ... and struct bar is also a type that can be returned by another
    acquire kfunc.
    
    Then, doing the following sequence:
    
    	struct foo *f = bpf_get_foo(); // acquire kfunc
    	if (!f)
    		return 0;
    	bpf_put_bar(&f->b); // release kfunc
    
    ... would work with the current code, since the btf_struct_ids_match
    takes reg->off into account for matching pointer type with release kfunc
    argument type, but would obviously be incorrect, and most likely lead to
    a kernel crash. A test has been included later to prevent regressions in
    this area.
    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-5-memxor@gmail.com
    24d5bb80
btf.c 188 KB