bpf: sockmap, decrement copied count correctly in redirect error case
authorJohn Fastabend <john.fastabend@gmail.com>
Sat, 25 Aug 2018 00:37:00 +0000 (17:37 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Tue, 28 Aug 2018 07:01:06 +0000 (09:01 +0200)
commit501ca81760c204ec59b73e4a00bee5971fc0f1b1
treea63a623bae798db9e5754f6d1aa9aa85aca6922e
parent3f6e138d41ddff196f452993528cfe75762ede0f
bpf: sockmap, decrement copied count correctly in redirect error case

Currently, when a redirect occurs in sockmap and an error occurs in
the redirect call we unwind the scatterlist once in the error path
of bpf_tcp_sendmsg_do_redirect() and then again in sendmsg(). Then
in the error path of sendmsg we decrement the copied count by the
send size.

However, its possible we partially sent data before the error was
generated. This can happen if do_tcp_sendpages() partially sends the
scatterlist before encountering a memory pressure error. If this
happens we need to decrement the copied value (the value tracking
how many bytes were actually sent to TCP stack) by the number of
remaining bytes _not_ the entire send size. Otherwise we risk
confusing userspace.

Also we don't need two calls to free the scatterlist one is
good enough. So remove the one in bpf_tcp_sendmsg_do_redirect() and
then properly reduce copied by the number of remaining bytes which
may in fact be the entire send size if no bytes were sent.

To do this use bool to indicate if free_start_sg() should do mem
accounting or not.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
kernel/bpf/sockmap.c