• Andrey Ignatov's avatar
    bpf: Support variable offset stack access from helpers · 2011fccf
    Andrey Ignatov authored
    Currently there is a difference in how verifier checks memory access for
    helper arguments for PTR_TO_MAP_VALUE and PTR_TO_STACK with regard to
    variable part of offset.
    
    check_map_access, that is used for PTR_TO_MAP_VALUE, can handle variable
    offsets just fine, so that BPF program can call a helper like this:
    
      some_helper(map_value_ptr + off, size);
    
    , where offset is unknown at load time, but is checked by program to be
    in a safe rage (off >= 0 && off + size < map_value_size).
    
    But it's not the case for check_stack_boundary, that is used for
    PTR_TO_STACK, and same code with pointer to stack is rejected by
    verifier:
    
      some_helper(stack_value_ptr + off, size);
    
    For example:
      0: (7a) *(u64 *)(r10 -16) = 0
      1: (7a) *(u64 *)(r10 -8) = 0
      2: (61) r2 = *(u32 *)(r1 +0)
      3: (57) r2 &= 4
      4: (17) r2 -= 16
      5: (0f) r2 += r10
      6: (18) r1 = 0xffff888111343a80
      8: (85) call bpf_map_lookup_elem#1
      invalid variable stack read R2 var_off=(0xfffffffffffffff0; 0x4)
    
    Add support for variable offset access to check_stack_boundary so that
    if offset is checked by program to be in a safe range it's accepted by
    verifier.
    Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    2011fccf
verifier.c 229 KB