• Kumar Kartikeya Dwivedi's avatar
    libbpf: Add request buffer type for netlink messages · 0ae64fb6
    Kumar Kartikeya Dwivedi authored
    Coverity complains about OOB writes to nlmsghdr. There is no OOB as we
    write to the trailing buffer, but static analyzers and compilers may
    rightfully be confused as the nlmsghdr pointer has subobject provenance
    (and hence subobject bounds).
    
    Fix this by using an explicit request structure containing the nlmsghdr,
    struct tcmsg/ifinfomsg, and attribute buffer.
    
    Also switch nh_tail (renamed to req_tail) to cast req * to char * so
    that it can be understood as arithmetic on pointer to the representation
    array (hence having same bound as request structure), which should
    further appease analyzers.
    
    As a bonus, callers don't have to pass sizeof(req) all the time now, as
    size is implicitly obtained using the pointer. While at it, also reduce
    the size of attribute buffer to 128 bytes (132 for ifinfomsg using
    functions due to the padding).
    
    Summary of problem:
    
      Even though C standard allows interconvertibility of pointer to first
      member and pointer to struct, for the purposes of alias analysis it
      would still consider the first as having pointer value "pointer to T"
      where T is type of first member hence having subobject bounds,
      allowing analyzers within reason to complain when object is accessed
      beyond the size of pointed to object.
    
      The only exception to this rule may be when a char * is formed to a
      member subobject. It is not possible for the compiler to be able to
      tell the intent of the programmer that it is a pointer to member
      object or the underlying representation array of the containing
      object, so such diagnosis is suppressed.
    
    Fixes: 715c5ce4 ("libbpf: Add low level TC-BPF management API")
    Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20210619041454.417577-1-memxor@gmail.com
    0ae64fb6
netlink.c 17.8 KB