netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>, bpf@vger.kernel.org
Cc: "Alexei Starovoitov" <ast@kernel.org>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	netdev@vger.kernel.org
Subject: Re: [PATCH bpf-next 3/3] libbpf: add request buffer type for netlink messages
Date: Tue, 15 Jun 2021 14:23:56 +0200	[thread overview]
Message-ID: <75ee8cc8-e793-ca15-0c8f-0de99af451d8@iogearbox.net> (raw)
In-Reply-To: <20210612023502.1283837-4-memxor@gmail.com>

Hey Kumar,

took in first two already, just few small nits in here, but overall looks
good to me.

On 6/12/21 4:35 AM, Kumar Kartikeya Dwivedi wrote:
> 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).
> 
> Remedy this by using an explicit request structure, but we also need to
> start the buffer in case of ifinfomsg without any padding. The alignment
> on netlink wire protocol is 4 byte boundary, so we just insert explicit
> 4 byte buffer to avoid compilers throwing off on read and write from/to
> padding.
> 
> 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 need to align buffer after it).
> 
> More info/discussion on why this was a problem in these links:
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2294.htm#provenance-and-subobjects-container-of-casts-1
> https://twitter.com/rep_stosq_void/status/1298581367442333696

Would be good if you could provide a small summary instead of external
links to twitter, so that this is ideally self-contained and doesn't
get lost from the log.

> CID: 322807
> CID: 322806
> CID: 141815

CIDs are not official commit msg tags, and given this is just a coverity
false positive on top of that, I don't think we need them here.

> Fixes: 715c5ce454a6 ("libbpf: Add low level TC-BPF management API")
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---
>   tools/lib/bpf/netlink.c | 107 +++++++++++++++-------------------------
>   tools/lib/bpf/nlattr.h  |  37 +++++++++-----
>   2 files changed, 65 insertions(+), 79 deletions(-)
> 
[...]
> diff --git a/tools/lib/bpf/nlattr.h b/tools/lib/bpf/nlattr.h
> index 3c780ab6d022..cc59f9c02d88 100644
> --- a/tools/lib/bpf/nlattr.h
> +++ b/tools/lib/bpf/nlattr.h
> @@ -13,6 +13,7 @@
>   #include <string.h>
>   #include <errno.h>
>   #include <linux/netlink.h>
> +#include <linux/rtnetlink.h>
>   
>   /* avoid multiple definition of netlink features */
>   #define __LINUX_NETLINK_H
> @@ -52,6 +53,18 @@ struct libbpf_nla_policy {
>   	uint16_t	maxlen;
>   };
>   
> +struct netlink_request {

nit: Could we either name it struct libbpf_nla_req or just struct nlreq ...
to better fit the naming conventions of nlattr.h and not create yet a new
variant? Either is okay with me..

> +	struct nlmsghdr nh;
> +	union {
> +		struct {
> +			struct ifinfomsg ifinfo;
> +			char _pad[4];
> +		};
> +		struct tcmsg tc;
> +	};
> +	char buf[128];
> +};
> +
>   /**
>    * @ingroup attr
>    * Iterate over a stream of attributes
> @@ -111,44 +124,44 @@ static inline struct nlattr *nla_data(struct nlattr *nla)
>   	return (struct nlattr *)((char *)nla + NLA_HDRLEN);
>   }
>   
> -static inline struct nlattr *nh_tail(struct nlmsghdr *nh)
> +static inline struct nlattr *req_tail(struct netlink_request *req)
>   {
> -	return (struct nlattr *)((char *)nh + NLMSG_ALIGN(nh->nlmsg_len));
> +	return (struct nlattr *)((char *)req + NLMSG_ALIGN(req->nh.nlmsg_len));
>   }
>   
[...]
Thanks,
Daniel

      reply	other threads:[~2021-06-15 12:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-12  2:34 [PATCH bpf-next 0/3] Fixes for TC-BPF series Kumar Kartikeya Dwivedi
2021-06-12  2:35 ` [PATCH bpf-next 1/3] libbpf: remove unneeded check for flags during detach Kumar Kartikeya Dwivedi
2021-06-12  2:35 ` [PATCH bpf-next 2/3] libbpf: set NLM_F_EXCL when creating qdisc Kumar Kartikeya Dwivedi
2021-06-12  2:35 ` [PATCH bpf-next 3/3] libbpf: add request buffer type for netlink messages Kumar Kartikeya Dwivedi
2021-06-15 12:23   ` Daniel Borkmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=75ee8cc8-e793-ca15-0c8f-0de99af451d8@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=toke@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).