• Andrii Nakryiko's avatar
    libbpf: Add safer high-level wrappers for map operations · 737d0646
    Andrii Nakryiko authored
    Add high-level API wrappers for most common and typical BPF map
    operations that works directly on instances of struct bpf_map * (so
    you don't have to call bpf_map__fd()) and validate key/value size
    expectations.
    
    These helpers require users to specify key (and value, where
    appropriate) sizes when performing lookup/update/delete/etc. This forces
    user to actually think and validate (for themselves) those. This is
    a good thing as user is expected by kernel to implicitly provide correct
    key/value buffer sizes and kernel will just read/write necessary amount
    of data. If it so happens that user doesn't set up buffers correctly
    (which bit people for per-CPU maps especially) kernel either randomly
    overwrites stack data or return -EFAULT, depending on user's luck and
    circumstances. These high-level APIs are meant to prevent such
    unpleasant and hard to debug bugs.
    
    This patch also adds bpf_map_delete_elem_flags() low-level API and
    requires passing flags to bpf_map__delete_elem() API for consistency
    across all similar APIs, even though currently kernel doesn't expect
    any extra flags for BPF_MAP_DELETE_ELEM operation.
    
    List of map operations that get these high-level APIs:
    
      - bpf_map_lookup_elem;
      - bpf_map_update_elem;
      - bpf_map_delete_elem;
      - bpf_map_lookup_and_delete_elem;
      - bpf_map_get_next_key.
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220512220713.2617964-1-andrii@kernel.org
    737d0646
libbpf.c 345 KB