netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] af_netlink: give correct bounds to dump skb for NLMSG_DONE
@ 2017-11-07 11:29 Jason A. Donenfeld
  2017-11-08  6:03 ` Jason A. Donenfeld
  2017-11-08  6:16 ` Johannes Berg
  0 siblings, 2 replies; 19+ messages in thread
From: Jason A. Donenfeld @ 2017-11-07 11:29 UTC (permalink / raw)
  To: johannes, davem, Netdev, linux-kernel; +Cc: Jason A. Donenfeld

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

^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2017-11-13  1:18 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-07 11:29 [PATCH] af_netlink: give correct bounds to dump skb for NLMSG_DONE Jason A. Donenfeld
2017-11-08  6:03 ` 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

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).