Commit 66481ca0 authored by Al Viro's avatar Al Viro Committed by Greg Kroah-Hartman

make skb_copy_datagram_msg() et.al. preserve ->msg_iter on error

commit 32786821 upstream.

Fixes the mess observed in e.g. rsync over a noisy link we'd been
seeing since last Summer.  What happens is that we copy part of
a datagram before noticing a checksum mismatch.  Datagram will be
resent, all right, but we want the next try go into the same place,
not after it...

All this family of primitives (copy/checksum and copy a datagram
into destination) is "all or nothing" sort of interface - either
we get 0 (meaning that copy had been successful) or we get an
error (and no way to tell how much had been copied before we ran
into whatever error it had been).  Make all of them leave iterator
unadvanced in case of errors - all callers must be able to cope
with that (an error might've been caught before the iterator had
been advanced), it costs very little to arrange, it's safer for
callers and actually fixes at least one bug in said callers.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent a99a9ff2
...@@ -398,7 +398,7 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset, ...@@ -398,7 +398,7 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
struct iov_iter *to, int len) struct iov_iter *to, int len)
{ {
int start = skb_headlen(skb); int start = skb_headlen(skb);
int i, copy = start - offset; int i, copy = start - offset, start_off = offset, n;
struct sk_buff *frag_iter; struct sk_buff *frag_iter;
trace_skb_copy_datagram_iovec(skb, len); trace_skb_copy_datagram_iovec(skb, len);
...@@ -407,11 +407,12 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset, ...@@ -407,11 +407,12 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
if (copy > 0) { if (copy > 0) {
if (copy > len) if (copy > len)
copy = len; copy = len;
if (copy_to_iter(skb->data + offset, copy, to) != copy) n = copy_to_iter(skb->data + offset, copy, to);
offset += n;
if (n != copy)
goto short_copy; goto short_copy;
if ((len -= copy) == 0) if ((len -= copy) == 0)
return 0; return 0;
offset += copy;
} }
/* Copy paged appendix. Hmm... why does this look so complicated? */ /* Copy paged appendix. Hmm... why does this look so complicated? */
...@@ -425,13 +426,14 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset, ...@@ -425,13 +426,14 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
if ((copy = end - offset) > 0) { if ((copy = end - offset) > 0) {
if (copy > len) if (copy > len)
copy = len; copy = len;
if (copy_page_to_iter(skb_frag_page(frag), n = copy_page_to_iter(skb_frag_page(frag),
frag->page_offset + offset - frag->page_offset + offset -
start, copy, to) != copy) start, copy, to);
offset += n;
if (n != copy)
goto short_copy; goto short_copy;
if (!(len -= copy)) if (!(len -= copy))
return 0; return 0;
offset += copy;
} }
start = end; start = end;
} }
...@@ -463,6 +465,7 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset, ...@@ -463,6 +465,7 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
*/ */
fault: fault:
iov_iter_revert(to, offset - start_off);
return -EFAULT; return -EFAULT;
short_copy: short_copy:
...@@ -613,7 +616,7 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, ...@@ -613,7 +616,7 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
__wsum *csump) __wsum *csump)
{ {
int start = skb_headlen(skb); int start = skb_headlen(skb);
int i, copy = start - offset; int i, copy = start - offset, start_off = offset;
struct sk_buff *frag_iter; struct sk_buff *frag_iter;
int pos = 0; int pos = 0;
int n; int n;
...@@ -623,11 +626,11 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, ...@@ -623,11 +626,11 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
if (copy > len) if (copy > len)
copy = len; copy = len;
n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to); n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to);
offset += n;
if (n != copy) if (n != copy)
goto fault; goto fault;
if ((len -= copy) == 0) if ((len -= copy) == 0)
return 0; return 0;
offset += copy;
pos = copy; pos = copy;
} }
...@@ -649,12 +652,12 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, ...@@ -649,12 +652,12 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
offset - start, copy, offset - start, copy,
&csum2, to); &csum2, to);
kunmap(page); kunmap(page);
offset += n;
if (n != copy) if (n != copy)
goto fault; goto fault;
*csump = csum_block_add(*csump, csum2, pos); *csump = csum_block_add(*csump, csum2, pos);
if (!(len -= copy)) if (!(len -= copy))
return 0; return 0;
offset += copy;
pos += copy; pos += copy;
} }
start = end; start = end;
...@@ -687,6 +690,7 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, ...@@ -687,6 +690,7 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
return 0; return 0;
fault: fault:
iov_iter_revert(to, offset - start_off);
return -EFAULT; return -EFAULT;
} }
...@@ -771,6 +775,7 @@ int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, ...@@ -771,6 +775,7 @@ int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
} }
return 0; return 0;
csum_error: csum_error:
iov_iter_revert(&msg->msg_iter, chunk);
return -EINVAL; return -EINVAL;
fault: fault:
return -EFAULT; return -EFAULT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment