public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: johannes@sipsolutions.net, davem@davemloft.net,
	Netdev <netdev@vger.kernel.org>,
	linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH] af_netlink: give correct bounds to dump skb for NLMSG_DONE
Date: Tue,  7 Nov 2017 20:29:14 +0900	[thread overview]
Message-ID: <20171107112914.30576-1-Jason@zx2c4.com> (raw)

The way people generally use netlink_dump is that they fill in the skb
as much as possible, breaking when nla_put returns an error. Then, they
get called again and start filling out the next skb, and again, and so
forth. The mechanism at work here is the ability for the iterative
dumping function to detect when the skb is filled up and not fill it
past the brim, waiting for a fresh skb for the rest of the data.

However, if the attributes are small and nicely packed, it is possible
that a dump callback function successfully fills in attributes until the
skb is of size 4080 (libmnl's default page-sized receive buffer size).
The dump function completes, satisfied, and then, if it happens to be
that this is actually the last skb, and no further ones are to be sent,
then netlink_dump will add on the NLMSG_DONE part:

  nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);

It is very important that netlink_dump does this, of course. However, in
this example, that call to nlmsg_put_answer will fail, because the
previous filling by the dump function did not leave it enough room. And
how could it possibly have done so? All of the nla_put variety of
functions simply check to see if the skb has enough tailroom,
independent of the context it is in.

In order to keep the important assumptions of all netlink dump users, it
is therefore important to give them an skb that has this end part of the
tail already reserved, so that the call to nlmsg_put_answer does not
fail. Otherwise, library authors are forced to find some bizarre sized
receive buffer that has a large modulo relative to the common sizes of
messages received, which is ugly and buggy.

This patch thus reserves and restores the required length for
NLMSG_DONE during the call to the dump function.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
See you all at netdevconf tomorrow!

 net/netlink/af_netlink.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index b93148e8e9fb..b2d0a2fb1939 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -2183,7 +2183,9 @@ static int netlink_dump(struct sock *sk)
 	skb_reserve(skb, skb_tailroom(skb) - alloc_size);
 	netlink_skb_set_owner_r(skb, sk);
 
+	skb->end -= nlmsg_total_size(sizeof(len));
 	len = cb->dump(skb, cb);
+	skb->end += nlmsg_total_size(sizeof(len));
 
 	if (len > 0) {
 		mutex_unlock(nlk->cb_mutex);
-- 
2.15.0

             reply	other threads:[~2017-11-07 11:29 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-07 11:29 Jason A. Donenfeld [this message]
2017-11-08  6:03 ` [PATCH] af_netlink: give correct bounds to dump skb for NLMSG_DONE Jason A. Donenfeld
2017-11-08  6:16 ` Johannes Berg
2017-11-08  6:35   ` Jason A. Donenfeld
2017-11-08  7:06     ` Jason A. Donenfeld
2017-11-08  7:21   ` [PATCH v2] " Jason A. Donenfeld
2017-11-09  1:42     ` [PATCH v3] af_netlink: ensure that NLMSG_DONE never fails in dumps Jason A. Donenfeld
2017-11-09  2:02       ` Johannes Berg
2017-11-09  2:57         ` Jason A. Donenfeld
2017-11-09  4:04       ` [PATCH v4] " Jason A. Donenfeld
2017-11-11  2:26         ` Jason A. Donenfeld
2017-11-11  2:37           ` David Miller
2017-11-11  2:47             ` Jason A. Donenfeld
2017-11-11 14:09         ` David Miller
2017-11-11 14:15           ` Johannes Berg
2017-11-11 14:18             ` Johannes Berg
2017-11-11 15:18               ` Jason A. Donenfeld
2017-11-11 14:21             ` David Miller
2017-11-13  1:18               ` David Miller

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=20171107112914.30576-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=davem@davemloft.net \
    --cc=johannes@sipsolutions.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /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