1. Jan 06, 2022
    • Daniel Borkmann's avatar
      bpf, selftests: Add verifier test for mem_or_null register with offset. · ca796fe6
      Daniel Borkmann authored
      
      
      Add a new test case with mem_or_null typed register with off > 0 to ensure
      it gets rejected by the verifier:
      
        # ./test_verifier 1011
        #1009/u check with invalid reg offset 0 OK
        #1009/p check with invalid reg offset 0 OK
        Summary: 2 PASSED, 0 SKIPPED, 0 FAILED
      
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      ca796fe6
    • Daniel Borkmann's avatar
      bpf: Don't promote bogus looking registers after null check. · e60b0d12
      Daniel Borkmann authored
      If we ever get to a point again where we convert a bogus looking <ptr>_or_null
      typed register containing a non-zero fixed or variable offset, then lets not
      reset these bounds to zero since they are not and also don't promote the register
      to a <ptr> type, but instead leave it as <ptr>_or_null. Converting to a unknown
      register could be an avenue as well, but then if we run into this case it would
      allow to leak a kernel pointer this way.
      
      Fixes: f1174f77
      
       ("bpf/verifier: rework value tracking")
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      e60b0d12
    • John Fastabend's avatar
      bpf, sockmap: Fix double bpf_prog_put on error case in map_link · 218d747a
      John Fastabend authored
      sock_map_link() is called to update a sockmap entry with a sk. But, if the
      sock_map_init_proto() call fails then we return an error to the map_update
      op against the sockmap. In the error path though we need to cleanup psock
      and dec the refcnt on any programs associated with the map, because we
      refcnt them early in the update process to ensure they are pinned for the
      psock. (This avoids a race where user deletes programs while also updating
      the map with new socks.)
      
      In current code we do the prog refcnt dec explicitely by calling
      bpf_prog_put() when the program was found in the map. But, after commit
      '38207a5e' in this error path we've already done the prog to psock
      assignment so the programs have a reference from the psock as well. This
      then causes the psock tear down logic, invoked by sk_psock_put() in the
      error path, to similarly call bpf_prog_put on the programs there.
      
      To be explicit this logic does the prog->psock assignment:
      
        if (msg_*)
          psock_set_prog(...)
      
      Then the error path under the out_progs label does a similar check and
      dec with:
      
        if (msg_*)
           bpf_prog_put(...)
      
      And the teardown logic sk_psock_put() does ...
      
        psock_set_prog(msg_*, NULL)
      
      ... triggering another bpf_prog_put(...). Then KASAN gives us this splat,
      found by syzbot because we've created an inbalance between bpf_prog_inc and
      bpf_prog_put calling put twice on the program.
      
        BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_put kernel/bpf/syscall.c:1812 [inline]
        BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_put kernel/bpf/syscall.c:1812 [inline] kernel/bpf/syscall.c:1829
        BUG: KASAN: vmalloc-out-of-bounds in bpf_prog_put+0x8c/0x4f0 kernel/bpf/syscall.c:1829 kernel/bpf/syscall.c:1829
        Read of size 8 at addr ffffc90000e76038 by task syz-executor020/3641
      
      To fix clean up error path so it doesn't try to do the bpf_prog_put in the
      error path once progs are assigned then it relies on the normal psock
      tear down logic to do complete cleanup.
      
      For completness we also cover the case whereh sk_psock_init_strp() fails,
      but this is not expected because it indicates an incorrect socket type
      and should be caught earlier.
      
      Fixes: 38207a5e
      
       ("bpf, sockmap: Attach map progs to psock early for feature probes")
      Reported-by: default avatar <syzbot+bb73e71cf4b8fd376a4f@syzkaller.appspotmail.com>
      Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220104214645.290900-1-john.fastabend@gmail.com
      218d747a
    • John Fastabend's avatar
      bpf, sockmap: Fix return codes from tcp_bpf_recvmsg_parser() · 5b2c5540
      John Fastabend authored
      Applications can be confused slightly because we do not always return the
      same error code as expected, e.g. what the TCP stack normally returns. For
      example on a sock err sk->sk_err instead of returning the sock_error we
      return EAGAIN. This usually means the application will 'try again'
      instead of aborting immediately. Another example, when a shutdown event
      is received we should immediately abort instead of waiting for data when
      the user provides a timeout.
      
      These tend to not be fatal, applications usually recover, but introduces
      bogus errors to the user or introduces unexpected latency. Before
      'c5d2177a' we fell back to the TCP stack when no data was available
      so we managed to catch many of the cases here, although with the extra
      latency cost of calling tcp_msg_wait_data() first.
      
      To fix lets duplicate the error handling in TCP stack into tcp_bpf so
      that we get the same error codes.
      
      These were found in our CI tests that run applications against sockmap
      and do longer lived testing, at least compared to test_sockmap that
      does short-lived ping/pong tests, and in some of our test clusters
      we deploy.
      
      Its non-trivial to do these in a shorter form CI tests that would be
      appropriate for BPF selftests, but we are looking into it so we can
      ensure this keeps working going forward. As a preview one idea is to
      pull in the packetdrill testing which catches some of this.
      
      Fixes: c5d2177a
      
       ("bpf, sockmap: Fix race in ingress receive verdict with redirect to self")
      Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220104205918.286416-1-john.fastabend@gmail.com
      5b2c5540
    • Hou Tao's avatar
      bpf, arm64: Use emit_addr_mov_i64() for BPF_PSEUDO_FUNC · e4a41c2c
      Hou Tao authored
      The following error is reported when running "./test_progs -t for_each"
      under arm64:
      
        bpf_jit: multi-func JIT bug 58 != 56
        [...]
        JIT doesn't support bpf-to-bpf calls
      
      The root cause is the size of BPF_PSEUDO_FUNC instruction increases
      from 2 to 3 after the address of called bpf-function is settled and
      there are two bpf-to-bpf calls in test_pkt_access. The generated
      instructions are shown below:
      
        0x48:  21 00 C0 D2    movz x1, #0x1, lsl #32
        0x4c:  21 00 80 F2    movk x1, #0x1
      
        0x48:  E1 3F C0 92    movn x1, #0x1ff, lsl #32
        0x4c:  41 FE A2 F2    movk x1, #0x17f2, lsl #16
        0x50:  81 70 9F F2    movk x1, #0xfb84
      
      Fixing it by using emit_addr_mov_i64() for BPF_PSEUDO_FUNC, so
      the size of jited image will not change.
      
      Fixes: 69c087ba
      
       ("bpf: Add bpf_for_each_map_elem() helper")
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211231151018.3781550-1-houtao1@huawei.com
      e4a41c2c
  2. Jan 05, 2022
  3. Jan 04, 2022
  4. Jan 03, 2022