From: Hannes Frederic Sowa <hannes@stressinduktion.org>
To: David Miller <davem@davemloft.net>
Cc: xiyou.wangcong@gmail.com, davej@codemonkey.org.uk,
netdev@vger.kernel.org
Subject: Re: ipv6: handle -EFAULT from skb_copy_bits
Date: Wed, 21 Dec 2016 22:33:20 +0100 [thread overview]
Message-ID: <1482356000.2260.13.camel@stressinduktion.org> (raw)
In-Reply-To: <20161221.140431.1651188849352763159.davem@davemloft.net>
On Wed, 2016-12-21 at 14:04 -0500, David Miller wrote:
> From: Hannes Frederic Sowa <hannes@stressinduktion.org>
> Date: Wed, 21 Dec 2016 13:41:13 +0100
>
> > On Wed, 2016-12-21 at 13:27 +0100, Hannes Frederic Sowa wrote:
> >> @@ -555,8 +566,8 @@ static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
> >> goto out;
> >>
> >> offset = rp->offset;
> >> - total_len = inet_sk(sk)->cork.base.length;
> >> - if (offset >= total_len - 1) {
> >> + transport_len = raw6_corked_transport_len(sk);
> >> + if (offset >= transport_len - 1) {
> >> err = -EINVAL;
> >> ip6_flush_pending_frames(sk);
> >> goto out;
> >> @@ -598,7 +609,7 @@ static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
> >> tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
> >>
> >> csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
> >> - total_len, fl6->flowi6_proto, tmp_csum);
> >> + transport_len, fl6->flowi6_proto, tmp_csum);
> >>
> >>
> >
> > Ops, here we need actually the total_len plus the opt->opt_nflen to
> > always calculate the correct checksum.
>
> It's a real shame we can't just use skb_transport_offset(). This value
> has essentially been calculated for us already.
This code path is called when we want to send out the socket write
queue. Because of MSG_MORE we might have multiple skbs in there, but
only the first one actually carries the true header, the others are
optimized for the ipv6 fragmentation fast path, thus we need to use
skb_transport_offset to find the correct offset but we must not
touch/read the data area before, as it is undefined data at that point.
Basically total_len so far accumulated all the payload length written
to the socket from the syscall argument from the user, but
unfortunately it also accounts for the first sendmsg's synthesized
extension header length in the write queue. We need to reverse this
calculation at this point in time.
So total_len is independent from the loop we do below and the length
should always reflect the length of all skbs stored in the write queue.
The loop adjusts the offset of the checksum in case the checksum offset
points to data in one of the later skbs, but we know before if that can
happen or not because of the cork length.
> Also, if we iterate over multiple SKBs in the write queue, don't you have
> to redo this calculation for every SKB we iterate over?
All the skb's payload length are summed up in the cork's length field,
so we don't need to sum it up again but just can use that value as is
minus the extension header adjustment.
> Furthermore, what if the user queued up some SKBs in the raw socket
> with MSG_MORE, and then changed some of the options for a subsequent
> sendmsg() call?
We only refresh the extension headers the next time we prepare an ipv6
header, which is the first sendmsg after one without MSG_MORE. We are
protected in this situation and don't change header lengths later
during further MSG_MORE sendmsgs.
> Given all of this, I think the best thing to do is validate the offset
> after the queue walks, which is pretty much what Dave Jones's original
> patch was doing.
I think both approaches protect against the bug reasonably well, but
Dave's patch has a bug: we must either call ip6_flush_pending_frames to
clear the socket write queue with the buggy send request.
> Sigh... well, at least we now understand what's going on here.
Yep, the code is more than a bit complex. :/
Bye,
Hannes
next prev parent reply other threads:[~2016-12-21 21:33 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-14 15:47 ipv6: handle -EFAULT from skb_copy_bits Dave Jones
2016-12-17 15:41 ` David Miller
2016-12-17 16:43 ` Dave Jones
2016-12-19 17:03 ` Dave Jones
2016-12-19 19:48 ` David Miller
2016-12-20 0:31 ` Dave Jones
2016-12-20 0:40 ` Dave Jones
2016-12-20 1:36 ` David Miller
2016-12-20 18:17 ` Dave Jones
2016-12-20 18:28 ` David Miller
2016-12-20 19:34 ` Dave Jones
2016-12-20 19:31 ` Cong Wang
2016-12-20 22:12 ` Dave Jones
2016-12-21 6:09 ` Cong Wang
2016-12-21 12:27 ` Hannes Frederic Sowa
2016-12-21 12:41 ` Hannes Frederic Sowa
2016-12-21 19:04 ` David Miller
2016-12-21 21:33 ` Hannes Frederic Sowa [this message]
2016-12-22 1:40 ` Dave Jones
2016-12-22 3:29 ` David Miller
-- strict thread matches above, loose matches on Subject: below --
2016-12-22 16:16 Dave Jones
2016-12-23 17:21 ` 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=1482356000.2260.13.camel@stressinduktion.org \
--to=hannes@stressinduktion.org \
--cc=davej@codemonkey.org.uk \
--cc=davem@davemloft.net \
--cc=netdev@vger.kernel.org \
--cc=xiyou.wangcong@gmail.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).