03 Oct, 2020

2 commits

  • Add bpf_per_cpu_ptr() to help bpf programs access percpu vars.
    bpf_per_cpu_ptr() has the same semantic as per_cpu_ptr() in the kernel
    except that it may return NULL. This happens when the cpu parameter is
    out of range. So the caller must check the returned value.

    Signed-off-by: Hao Luo
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200929235049.2533242-5-haoluo@google.com

    Hao Luo
     
  • Pseudo_btf_id is a type of ld_imm insn that associates a btf_id to a
    ksym so that further dereferences on the ksym can use the BTF info
    to validate accesses. Internally, when seeing a pseudo_btf_id ld insn,
    the verifier reads the btf_id stored in the insn[0]'s imm field and
    marks the dst_reg as PTR_TO_BTF_ID. The btf_id points to a VAR_KIND,
    which is encoded in btf_vminux by pahole. If the VAR is not of a struct
    type, the dst reg will be marked as PTR_TO_MEM instead of PTR_TO_BTF_ID
    and the mem_size is resolved to the size of the VAR's type.

    >From the VAR btf_id, the verifier can also read the address of the
    ksym's corresponding kernel var from kallsyms and use that to fill
    dst_reg.

    Therefore, the proper functionality of pseudo_btf_id depends on (1)
    kallsyms and (2) the encoding of kernel global VARs in pahole, which
    should be available since pahole v1.18.

    Signed-off-by: Hao Luo
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200929235049.2533242-2-haoluo@google.com

    Hao Luo
     

30 Sep, 2020

2 commits

  • Eelco reported we can't properly access arguments if the tracing
    program is attached to extension program.

    Having following program:

    SEC("classifier/test_pkt_md_access")
    int test_pkt_md_access(struct __sk_buff *skb)

    with its extension:

    SEC("freplace/test_pkt_md_access")
    int test_pkt_md_access_new(struct __sk_buff *skb)

    and tracing that extension with:

    SEC("fentry/test_pkt_md_access_new")
    int BPF_PROG(fentry, struct sk_buff *skb)

    It's not possible to access skb argument in the fentry program,
    with following error from verifier:

    ; int BPF_PROG(fentry, struct sk_buff *skb)
    0: (79) r1 = *(u64 *)(r1 +0)
    invalid bpf_context access off=0 size=8

    The problem is that btf_ctx_access gets the context type for the
    traced program, which is in this case the extension.

    But when we trace extension program, we want to get the context
    type of the program that the extension is attached to, so we can
    access the argument properly in the trace program.

    This version of the patch is tweaked slightly from Jiri's original one,
    since the refactoring in the previous patches means we have to get the
    target prog type from the new variable in prog->aux instead of directly
    from the target prog.

    Reported-by: Eelco Chaudron
    Suggested-by: Jiri Olsa
    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/160138355278.48470.17057040257274725638.stgit@toke.dk

    Toke Høiland-Jørgensen
     
  • In preparation for allowing multiple attachments of freplace programs, move
    the references to the target program and trampoline into the
    bpf_tracing_link structure when that is created. To do this atomically,
    introduce a new mutex in prog->aux to protect writing to the two pointers
    to target prog and trampoline, and rename the members to make it clear that
    they are related.

    With this change, it is no longer possible to attach the same tracing
    program multiple times (detaching in-between), since the reference from the
    tracing program to the target disappears on the first attach. However,
    since the next patch will let the caller supply an attach target, that will
    also make it possible to attach to the same place multiple times.

    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/160138355059.48470.2503076992210324984.stgit@toke.dk

    Toke Høiland-Jørgensen
     

29 Sep, 2020

3 commits

  • A helper is added to allow seq file writing of kernel data
    structures using vmlinux BTF. Its signature is

    long bpf_seq_printf_btf(struct seq_file *m, struct btf_ptr *ptr,
    u32 btf_ptr_size, u64 flags);

    Flags and struct btf_ptr definitions/use are identical to the
    bpf_snprintf_btf helper, and the helper returns 0 on success
    or a negative error value.

    Suggested-by: Alexei Starovoitov
    Signed-off-by: Alan Maguire
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/1601292670-1616-8-git-send-email-alan.maguire@oracle.com

    Alan Maguire
     
  • generalize the "seq_show" seq file support in btf.c to support
    a generic show callback of which we support two instances; the
    current seq file show, and a show with snprintf() behaviour which
    instead writes the type data to a supplied string.

    Both classes of show function call btf_type_show() with different
    targets; the seq file or the string to be written. In the string
    case we need to track additional data - length left in string to write
    and length to return that we would have written (a la snprintf).

    By default show will display type information, field members and
    their types and values etc, and the information is indented
    based upon structure depth. Zeroed fields are omitted.

    Show however supports flags which modify its behaviour:

    BTF_SHOW_COMPACT - suppress newline/indent.
    BTF_SHOW_NONAME - suppress show of type and member names.
    BTF_SHOW_PTR_RAW - do not obfuscate pointer values.
    BTF_SHOW_UNSAFE - do not copy data to safe buffer before display.
    BTF_SHOW_ZERO - show zeroed values (by default they are not shown).

    Signed-off-by: Alan Maguire
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/1601292670-1616-3-git-send-email-alan.maguire@oracle.com

    Alan Maguire
     
  • In preparation for moving code around, change a bunch of references to
    env->log (and the verbose() logging helper) to use bpf_log() and a direct
    pointer to struct bpf_verifier_log. While we're touching the function
    signature, mark the 'prog' argument to bpf_check_type_match() as const.

    Also enhance the bpf_verifier_log_needed() check to handle NULL pointers
    for the log struct so we can re-use the code with logging disabled.

    Acked-by: Andrii Nakryiko
    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Alexei Starovoitov

    Toke Høiland-Jørgensen
     

22 Sep, 2020

2 commits

  • Function prototypes using ARG_PTR_TO_BTF_ID currently use two ways to signal
    which BTF IDs are acceptable. First, bpf_func_proto.btf_id is an array of
    IDs, one for each argument. This array is only accessed up to the highest
    numbered argument that uses ARG_PTR_TO_BTF_ID and may therefore be less than
    five arguments long. It usually points at a BTF_ID_LIST. Second, check_btf_id
    is a function pointer that is called by the verifier if present. It gets the
    actual BTF ID of the register, and the argument number we're currently checking.
    It turns out that the only user check_arg_btf_id ignores the argument, and is
    simply used to check whether the BTF ID has a struct sock_common at it's start.

    Replace both of these mechanisms with an explicit BTF ID for each argument
    in a function proto. Thanks to btf_struct_ids_match this is very flexible:
    check_arg_btf_id can be replaced by requiring struct sock_common.

    Signed-off-by: Lorenz Bauer
    Signed-off-by: Alexei Starovoitov
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/20200921121227.255763-5-lmb@cloudflare.com

    Lorenz Bauer
     
  • bsearch doesn't modify the contents of the array, so we can take a const pointer.

    Signed-off-by: Lorenz Bauer
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200921121227.255763-2-lmb@cloudflare.com

    Lorenz Bauer
     

26 Aug, 2020

7 commits

  • Adding support to define sorted set of BTF ID values.

    Following defines sorted set of BTF ID values:

    BTF_SET_START(btf_allowlist_d_path)
    BTF_ID(func, vfs_truncate)
    BTF_ID(func, vfs_fallocate)
    BTF_ID(func, dentry_open)
    BTF_ID(func, vfs_getattr)
    BTF_ID(func, filp_close)
    BTF_SET_END(btf_allowlist_d_path)

    It defines following 'struct btf_id_set' variable to access
    values and count:

    struct btf_id_set btf_allowlist_d_path;

    Adding 'allowed' callback to struct bpf_func_proto, to allow
    verifier the check on allowed callers.

    Adding btf_id_set_contains function, which will be used by
    allowed callbacks to verify the caller's BTF ID value is
    within allowed set.

    Also removing extra '\' in __BTF_ID_LIST macro.

    Added BTF_SET_START_GLOBAL macro for global sets.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200825192124.710397-10-jolsa@kernel.org

    Jiri Olsa
     
  • Adding btf_struct_ids_match function to check if given address provided
    by BTF object + offset is also address of another nested BTF object.

    This allows to pass an argument to helper, which is defined via parent
    BTF object + offset, like for bpf_d_path (added in following changes):

    SEC("fentry/filp_close")
    int BPF_PROG(prog_close, struct file *file, void *id)
    {
    ...
    ret = bpf_d_path(&file->f_path, ...

    The first bpf_d_path argument is hold by verifier as BTF file object
    plus offset of f_path member.

    The btf_struct_ids_match function will walk the struct file object and
    check if there's nested struct path object on the given offset.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200825192124.710397-9-jolsa@kernel.org

    Jiri Olsa
     
  • Adding btf_struct_walk function that walks through the
    struct type + given offset and returns following values:

    enum bpf_struct_walk_result {
    /* < 0 error */
    WALK_SCALAR = 0,
    WALK_PTR,
    WALK_STRUCT,
    };

    WALK_SCALAR - when SCALAR_VALUE is found
    WALK_PTR - when pointer value is found, its ID is stored
    in 'next_btf_id' output param
    WALK_STRUCT - when nested struct object is found, its ID is stored
    in 'next_btf_id' output param

    It will be used in following patches to get all nested
    struct objects for given type and offset.

    The btf_struct_access now calls btf_struct_walk function,
    as long as it gets nested structs as return value.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200825192124.710397-8-jolsa@kernel.org

    Jiri Olsa
     
  • Andrii suggested we can simply jump to again label
    instead of making recursion call.

    Suggested-by: Andrii Nakryiko
    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200825192124.710397-7-jolsa@kernel.org

    Jiri Olsa
     
  • Adding type_id pointer as argument to __btf_resolve_size
    to return also BTF ID of the resolved type. It will be
    used in following changes.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200825192124.710397-6-jolsa@kernel.org

    Jiri Olsa
     
  • If the resolved type is array, make btf_resolve_size return also
    ID of the elem type. It will be needed in following changes.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200825192124.710397-5-jolsa@kernel.org

    Jiri Olsa
     
  • Moving btf_resolve_size into __btf_resolve_size and
    keeping btf_resolve_size public with just first 3
    arguments, because the rest of the arguments are not
    used by outside callers.

    Following changes are adding more arguments, which
    are not useful to outside callers. They will be added
    to the __btf_resolve_size function.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200825192124.710397-4-jolsa@kernel.org

    Jiri Olsa
     

04 Aug, 2020

1 commit

  • Daniel Borkmann says:

    ====================
    pull-request: bpf-next 2020-08-04

    The following pull-request contains BPF updates for your *net-next* tree.

    We've added 73 non-merge commits during the last 9 day(s) which contain
    a total of 135 files changed, 4603 insertions(+), 1013 deletions(-).

    The main changes are:

    1) Implement bpf_link support for XDP. Also add LINK_DETACH operation for the BPF
    syscall allowing processes with BPF link FD to force-detach, from Andrii Nakryiko.

    2) Add BPF iterator for map elements and to iterate all BPF programs for efficient
    in-kernel inspection, from Yonghong Song and Alexei Starovoitov.

    3) Separate bpf_get_{stack,stackid}() helpers for perf events in BPF to avoid
    unwinder errors, from Song Liu.

    4) Allow cgroup local storage map to be shared between programs on the same
    cgroup. Also extend BPF selftests with coverage, from YiFei Zhu.

    5) Add BPF exception tables to ARM64 JIT in order to be able to JIT BPF_PROBE_MEM
    load instructions, from Jean-Philippe Brucker.

    6) Follow-up fixes on BPF socket lookup in combination with reuseport group
    handling. Also add related BPF selftests, from Jakub Sitnicki.

    7) Allow to use socket storage in BPF_PROG_TYPE_CGROUP_SOCK-typed programs for
    socket create/release as well as bind functions, from Stanislav Fomichev.

    8) Fix an info leak in xsk_getsockopt() when retrieving XDP stats via old struct
    xdp_statistics, from Peilin Ye.

    9) Fix PT_REGS_RC{,_CORE}() macros in libbpf for MIPS arch, from Jerry Crunchtime.

    10) Extend BPF kernel test infra with skb->family and skb->{local,remote}_ip{4,6}
    fields and allow user space to specify skb->dev via ifindex, from Dmitry Yakunin.

    11) Fix a bpftool segfault due to missing program type name and make it more robust
    to prevent them in future gaps, from Quentin Monnet.

    12) Consolidate cgroup helper functions across selftests and fix a v6 localhost
    resolver issue, from John Fastabend.
    ====================

    Signed-off-by: David S. Miller

    David S. Miller
     

02 Aug, 2020

1 commit


26 Jul, 2020

1 commit

  • Readonly and readwrite buffer register states
    are introduced. Totally four states,
    PTR_TO_RDONLY_BUF[_OR_NULL] and PTR_TO_RDWR_BUF[_OR_NULL]
    are supported. As suggested by their respective
    names, PTR_TO_RDONLY_BUF[_OR_NULL] are for
    readonly buffers and PTR_TO_RDWR_BUF[_OR_NULL]
    for read/write buffers.

    These new register states will be used
    by later bpf map element iterator.

    New register states share some similarity to
    PTR_TO_TP_BUFFER as it will calculate accessed buffer
    size during verification time. The accessed buffer
    size will be later compared to other metrics during
    later attach/link_create time.

    Similar to reg_state PTR_TO_BTF_ID_OR_NULL in bpf
    iterator programs, PTR_TO_RDONLY_BUF_OR_NULL or
    PTR_TO_RDWR_BUF_OR_NULL reg_types can be set at
    prog->aux->bpf_ctx_arg_aux, and bpf verifier will
    retrieve the values during btf_ctx_access().
    Later bpf map element iterator implementation
    will show how such information will be assigned
    during target registeration time.

    The verifier is also enhanced such that PTR_TO_RDONLY_BUF
    can be passed to ARG_PTR_TO_MEM[_OR_NULL] helper argument, and
    PTR_TO_RDWR_BUF can be passed to ARG_PTR_TO_MEM[_OR_NULL] or
    ARG_PTR_TO_UNINIT_MEM.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20200723184111.590274-1-yhs@fb.com

    Yonghong Song
     

22 Jul, 2020

2 commits

  • One additional field btf_id is added to struct
    bpf_ctx_arg_aux to store the precomputed btf_ids.
    The btf_id is computed at build time with
    BTF_ID_LIST or BTF_ID_LIST_GLOBAL macro definitions.
    All existing bpf iterators are changed to used
    pre-compute btf_ids.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20200720163403.1393551-1-yhs@fb.com

    Yonghong Song
     
  • Currently, socket types (struct tcp_sock, udp_sock, etc.)
    used by bpf_skc_to_*() helpers are computed when vmlinux_btf
    is first built in the kernel.

    Commit 5a2798ab32ba
    ("bpf: Add BTF_ID_LIST/BTF_ID/BTF_ID_UNUSED macros")
    implemented a mechanism to compute btf_ids at kernel build
    time which can simplify kernel implementation and reduce
    runtime overhead by removing in-kernel btf_id calculation.
    This patch did exactly this, removing in-kernel btf_id
    computation and utilizing build-time btf_id computation.

    If CONFIG_DEBUG_INFO_BTF is not defined, BTF_ID_LIST will
    define an array with size of 5, which is not enough for
    btf_sock_ids. So define its own static array if
    CONFIG_DEBUG_INFO_BTF is not defined.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20200720163358.1393023-1-yhs@fb.com

    Yonghong Song
     

16 Jul, 2020

1 commit

  • Prevent __btf_resolve_helper_id() from dereferencing `btf_vmlinux`
    as NULL. This patch fixes the following syzbot bug:

    https://syzkaller.appspot.com/bug?id=f823224ada908fa5c207902a5a62065e53ca0fcc

    Reported-by: syzbot+ee09bda7017345f1fbe6@syzkaller.appspotmail.com
    Signed-off-by: Peilin Ye
    Signed-off-by: Daniel Borkmann
    Link: https://lore.kernel.org/bpf/20200714180904.277512-1-yepeilin.cs@gmail.com

    Peilin Ye
     

14 Jul, 2020

3 commits

  • Alexei Starovoitov says:

    ====================
    pull-request: bpf-next 2020-07-13

    The following pull-request contains BPF updates for your *net-next* tree.

    We've added 36 non-merge commits during the last 7 day(s) which contain
    a total of 62 files changed, 2242 insertions(+), 468 deletions(-).

    The main changes are:

    1) Avoid trace_printk warning banner by switching bpf_trace_printk to use
    its own tracing event, from Alan.

    2) Better libbpf support on older kernels, from Andrii.

    3) Additional AF_XDP stats, from Ciara.

    4) build time resolution of BTF IDs, from Jiri.

    5) BPF_CGROUP_INET_SOCK_RELEASE hook, from Stanislav.
    ====================

    Signed-off-by: David S. Miller

    David S. Miller
     
  • This way the ID is resolved during compile time,
    and we can remove the runtime name search.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Tested-by: Andrii Nakryiko
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200711215329.41165-7-jolsa@kernel.org

    Jiri Olsa
     
  • Now when we moved the helpers btf_id arrays into .BTF_ids section,
    we can remove the code that resolve those IDs in runtime.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Tested-by: Andrii Nakryiko
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200711215329.41165-6-jolsa@kernel.org

    Jiri Olsa
     

11 Jul, 2020

1 commit


25 Jun, 2020

2 commits

  • To ensure btf_ctx_access() is safe the verifier checks that the BTF
    arg type is an int, enum, or pointer. When the function does the
    BTF arg lookup it uses the calculation 'arg = off / 8' using the
    fact that registers are 8B. This requires that the first arg is
    in the first reg, the second in the second, and so on. However,
    for __int128 the arg will consume two registers by default LLVM
    implementation. So this will cause the arg layout assumed by the
    'arg = off / 8' calculation to be incorrect.

    Because __int128 is uncommon this patch applies the easiest fix and
    will force int types to be sizeof(u64) or smaller so that they will
    fit in a single register.

    v2: remove unneeded parens per Andrii's feedback

    Fixes: 9e15db66136a1 ("bpf: Implement accurate raw_tp context access via BTF")
    Signed-off-by: John Fastabend
    Signed-off-by: Daniel Borkmann
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/159303723962.11287.13309537171132420717.stgit@john-Precision-5820-Tower

    John Fastabend
     
  • The helper is used in tracing programs to cast a socket
    pointer to a tcp6_sock pointer.
    The return value could be NULL if the casting is illegal.

    A new helper return type RET_PTR_TO_BTF_ID_OR_NULL is added
    so the verifier is able to deduce proper return types for the helper.

    Different from the previous BTF_ID based helpers,
    the bpf_skc_to_tcp6_sock() argument can be several possible
    btf_ids. More specifically, all possible socket data structures
    with sock_common appearing in the first in the memory layout.
    This patch only added socket types related to tcp and udp.

    All possible argument btf_id and return value btf_id
    for helper bpf_skc_to_tcp6_sock() are pre-calculcated and
    cached. In the future, it is even possible to precompute
    these btf_id's at kernel build time.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/20200623230809.3988195-1-yhs@fb.com

    Yonghong Song
     

23 Jun, 2020

2 commits

  • There are multiple use-cases when it's convenient to have access to bpf
    map fields, both `struct bpf_map` and map type specific struct-s such as
    `struct bpf_array`, `struct bpf_htab`, etc.

    For example while working with sock arrays it can be necessary to
    calculate the key based on map->max_entries (some_hash % max_entries).
    Currently this is solved by communicating max_entries via "out-of-band"
    channel, e.g. via additional map with known key to get info about target
    map. That works, but is not very convenient and error-prone while
    working with many maps.

    In other cases necessary data is dynamic (i.e. unknown at loading time)
    and it's impossible to get it at all. For example while working with a
    hash table it can be convenient to know how much capacity is already
    used (bpf_htab.count.counter for BPF_F_NO_PREALLOC case).

    At the same time kernel knows this info and can provide it to bpf
    program.

    Fill this gap by adding support to access bpf map fields from bpf
    program for both `struct bpf_map` and map type specific fields.

    Support is implemented via btf_struct_access() so that a user can define
    their own `struct bpf_map` or map type specific struct in their program
    with only necessary fields and preserve_access_index attribute, cast a
    map to this struct and use a field.

    For example:

    struct bpf_map {
    __u32 max_entries;
    } __attribute__((preserve_access_index));

    struct bpf_array {
    struct bpf_map map;
    __u32 elem_size;
    } __attribute__((preserve_access_index));

    struct {
    __uint(type, BPF_MAP_TYPE_ARRAY);
    __uint(max_entries, 4);
    __type(key, __u32);
    __type(value, __u32);
    } m_array SEC(".maps");

    SEC("cgroup_skb/egress")
    int cg_skb(void *ctx)
    {
    struct bpf_array *array = (struct bpf_array *)&m_array;
    struct bpf_map *map = (struct bpf_map *)&m_array;

    /* .. use map->max_entries or array->map.max_entries .. */
    }

    Similarly to other btf_struct_access() use-cases (e.g. struct tcp_sock
    in net/ipv4/bpf_tcp_ca.c) the patch allows access to any fields of
    corresponding struct. Only reading from map fields is supported.

    For btf_struct_access() to work there should be a way to know btf id of
    a struct that corresponds to a map type. To get btf id there should be a
    way to get a stringified name of map-specific struct, such as
    "bpf_array", "bpf_htab", etc for a map type. Two new fields are added to
    `struct bpf_map_ops` to handle it:
    * .map_btf_name keeps a btf name of a struct returned by map_alloc();
    * .map_btf_id is used to cache btf id of that struct.

    To make btf ids calculation cheaper they're calculated once while
    preparing btf_vmlinux and cached same way as it's done for btf_id field
    of `struct bpf_func_proto`

    While calculating btf ids, struct names are NOT checked for collision.
    Collisions will be checked as a part of the work to prepare btf ids used
    in verifier in compile time that should land soon. The only known
    collision for `struct bpf_htab` (kernel/bpf/hashtab.c vs
    net/core/sock_map.c) was fixed earlier.

    Both new fields .map_btf_name and .map_btf_id must be set for a map type
    for the feature to work. If neither is set for a map type, verifier will
    return ENOTSUPP on a try to access map_ptr of corresponding type. If
    just one of them set, it's verifier misconfiguration.

    Only `struct bpf_array` for BPF_MAP_TYPE_ARRAY and `struct bpf_htab` for
    BPF_MAP_TYPE_HASH are supported by this patch. Other map types will be
    supported separately.

    The feature is available only for CONFIG_DEBUG_INFO_BTF=y and gated by
    perfmon_capable() so that unpriv programs won't have access to bpf map
    fields.

    Signed-off-by: Andrey Ignatov
    Signed-off-by: Daniel Borkmann
    Acked-by: John Fastabend
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/6479686a0cd1e9067993df57b4c3eef0e276fec9.1592600985.git.rdna@fb.com

    Andrey Ignatov
     
  • btf_parse_vmlinux() implements manual search for struct bpf_ctx_convert
    since at the time of implementing btf_find_by_name_kind() was not
    available.

    Later btf_find_by_name_kind() was introduced in 27ae7997a661 ("bpf:
    Introduce BPF_PROG_TYPE_STRUCT_OPS"). It provides similar search
    functionality and can be leveraged in btf_parse_vmlinux(). Do it.

    Signed-off-by: Andrey Ignatov
    Signed-off-by: Daniel Borkmann
    Acked-by: Andrii Nakryiko
    Acked-by: John Fastabend
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/6e12d5c3e8a3d552925913ef73a695dd1bb27800.1592600985.git.rdna@fb.com

    Andrey Ignatov
     

14 May, 2020

1 commit

  • Commit b121b341e598 ("bpf: Add PTR_TO_BTF_ID_OR_NULL
    support") adds a field btf_id_or_null_non0_off to
    bpf_prog->aux structure to indicate that the
    first ctx argument is PTR_TO_BTF_ID reg_type and
    all others are PTR_TO_BTF_ID_OR_NULL.
    This approach does not really scale if we have
    other different reg types in the future, e.g.,
    a pointer to a buffer.

    This patch enables bpf_iter targets registering ctx argument
    reg types which may be different from the default one.
    For example, for pointers to structures, the default reg_type
    is PTR_TO_BTF_ID for tracing program. The target can register
    a particular pointer type as PTR_TO_BTF_ID_OR_NULL which can
    be used by the verifier to enforce accesses.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200513180221.2949882-1-yhs@fb.com

    Yonghong Song
     

10 May, 2020

2 commits

  • In /proc/net/ipv6_route, we have
    struct fib6_info {
    struct fib6_table *fib6_table;
    ...
    struct fib6_nh fib6_nh[0];
    }
    struct fib6_nh {
    struct fib_nh_common nh_common;
    struct rt6_info **rt6i_pcpu;
    struct rt6_exception_bucket *rt6i_exception_bucket;
    };
    struct fib_nh_common {
    ...
    u8 nhc_gw_family;
    ...
    }

    The access:
    struct fib6_nh *fib6_nh = &rt->fib6_nh;
    ... fib6_nh->nh_common.nhc_gw_family ...

    This patch ensures such an access is handled properly.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200509175916.2476853-1-yhs@fb.com

    Yonghong Song
     
  • Add bpf_reg_type PTR_TO_BTF_ID_OR_NULL support.
    For tracing/iter program, the bpf program context
    definition, e.g., for previous bpf_map target, looks like
    struct bpf_iter__bpf_map {
    struct bpf_iter_meta *meta;
    struct bpf_map *map;
    };

    The kernel guarantees that meta is not NULL, but
    map pointer maybe NULL. The NULL map indicates that all
    objects have been traversed, so bpf program can take
    proper action, e.g., do final aggregation and/or send
    final report to user space.

    Add btf_id_or_null_non0_off to prog->aux structure, to
    indicate that if the context access offset is not 0,
    set to PTR_TO_BTF_ID_OR_NULL instead of PTR_TO_BTF_ID.
    This bit is set for tracing/iter program.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200509175912.2476576-1-yhs@fb.com

    Yonghong Song
     

29 Apr, 2020

1 commit

  • Add ability to fetch bpf_link details through BPF_OBJ_GET_INFO_BY_FD command.
    Also enhance show_fdinfo to potentially include bpf_link type-specific
    information (similarly to obj_info).

    Also introduce enum bpf_link_type stored in bpf_link itself and expose it in
    UAPI. bpf_link_tracing also now will store and return bpf_attach_type.

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20200429001614.1544-5-andriin@fb.com

    Andrii Nakryiko
     

31 Mar, 2020

2 commits

  • Signed-off-by: David S. Miller

    David S. Miller
     
  • The bounds checking for the arguments accessed in the BPF program breaks
    when the expected_attach_type is not BPF_TRACE_FEXIT, BPF_LSM_MAC or
    BPF_MODIFY_RETURN resulting in no check being done for the default case
    (the programs which do not receive the return value of the attached
    function in its arguments) when the index of the argument being accessed
    is equal to the number of arguments (nr_args).

    This was a result of a misplaced "else if" block introduced by the
    Commit 6ba43b761c41 ("bpf: Attachment verification for
    BPF_MODIFY_RETURN")

    Fixes: 6ba43b761c41 ("bpf: Attachment verification for BPF_MODIFY_RETURN")
    Reported-by: Jann Horn
    Signed-off-by: KP Singh
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20200330144246.338-1-kpsingh@chromium.org

    KP Singh
     

30 Mar, 2020

2 commits

  • Minor comment conflict in mac80211.

    Signed-off-by: David S. Miller

    David S. Miller
     
  • JITed BPF programs are dynamically attached to the LSM hooks
    using BPF trampolines. The trampoline prologue generates code to handle
    conversion of the signature of the hook to the appropriate BPF context.

    The allocated trampoline programs are attached to the nop functions
    initialized as LSM hooks.

    BPF_PROG_TYPE_LSM programs must have a GPL compatible license and
    and need CAP_SYS_ADMIN (required for loading eBPF programs).

    Upon attachment:

    * A BPF fexit trampoline is used for LSM hooks with a void return type.
    * A BPF fmod_ret trampoline is used for LSM hooks which return an
    int. The attached programs can override the return value of the
    bpf LSM hook to indicate a MAC Policy decision.

    Signed-off-by: KP Singh
    Signed-off-by: Daniel Borkmann
    Reviewed-by: Brendan Jackman
    Reviewed-by: Florent Revest
    Acked-by: Andrii Nakryiko
    Acked-by: James Morris
    Link: https://lore.kernel.org/bpf/20200329004356.27286-5-kpsingh@chromium.org

    KP Singh
     

26 Mar, 2020

1 commit


21 Mar, 2020

1 commit

  • Trying to initialize a structure with "= {};" will not always clean out
    all padding locations in a structure. So be explicit and call memset to
    initialize everything for a number of bpf information structures that
    are then copied from userspace, sometimes from smaller memory locations
    than the size of the structure.

    Reported-by: Daniel Borkmann
    Signed-off-by: Greg Kroah-Hartman
    Signed-off-by: Daniel Borkmann
    Acked-by: Yonghong Song
    Link: https://lore.kernel.org/bpf/20200320162258.GA794295@kroah.com

    Greg Kroah-Hartman