Skip to content

Commit 0cd88dc

Browse files
HexRabbitopsiff
authored andcommitted
xfrm: esp: avoid in-place decrypt on shared skb frags
commit f4c50a4034e62ab75f1d5cdd191dd5f9c77fdff4 upstream. MSG_SPLICE_PAGES can attach pages from a pipe directly to an skb. TCP marks such skbs with SKBFL_SHARED_FRAG after skb_splice_from_iter(), so later paths that may modify packet data can first make a private copy. The IPv4/IPv6 datagram append paths did not set this flag when splicing pages into UDP skbs. That leaves an ESP-in-UDP packet made from shared pipe pages looking like an ordinary uncloned nonlinear skb. ESP input then takes the no-COW fast path for uncloned skbs without a frag_list and decrypts in place over data that is not owned privately by the skb. Mark IPv4/IPv6 datagram splice frags with SKBFL_SHARED_FRAG, matching TCP. Also make ESP input fall back to skb_cow_data() when the flag is present, so ESP does not decrypt externally backed frags in place. Private nonlinear skb frags still use the existing fast path. This intentionally does not change ESP output. In esp_output_head(), the path that appends the ESP trailer to existing skb tailroom without calling skb_cow_data() is not reachable for nonlinear skbs: skb_tailroom() returns zero when skb->data_len is nonzero, while ESP tailen is positive. Thus ESP output will either use the separate destination-frag path or fall back to skb_cow_data(). Fixes: cac2661 ("esp4: Avoid skb_cow_data whenever possible") Fixes: 03e2a30 ("esp6: Avoid skb_cow_data whenever possible") Fixes: 7da0dde ("ip, udp: Support MSG_SPLICE_PAGES") Fixes: 6d8192b ("ip6, udp6: Support MSG_SPLICE_PAGES") Reported-by: Hyunwoo Kim <imv4bel@gmail.com> Reported-by: Kuan-Ting Chen <h3xrabbit@gmail.com> Tested-by: Hyunwoo Kim <imv4bel@gmail.com> Cc: stable@vger.kernel.org Signed-off-by: Kuan-Ting Chen <h3xrabbit@gmail.com> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> (cherry picked from commit b54edf1e9a3fd3491bdcb82a21f8d21315271e0d) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent f84072b commit 0cd88dc

4 files changed

Lines changed: 8 additions & 2 deletions

File tree

net/ipv4/esp4.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,8 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
872872
nfrags = 1;
873873

874874
goto skip_cow;
875-
} else if (!skb_has_frag_list(skb)) {
875+
} else if (!skb_has_frag_list(skb) &&
876+
!skb_has_shared_frag(skb)) {
876877
nfrags = skb_shinfo(skb)->nr_frags;
877878
nfrags++;
878879

net/ipv4/ip_output.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,8 @@ static int __ip_append_data(struct sock *sk,
12311231
if (err < 0)
12321232
goto error;
12331233
copy = err;
1234+
if (!(flags & MSG_NO_SHARED_FRAGS))
1235+
skb_shinfo(skb)->flags |= SKBFL_SHARED_FRAG;
12341236
wmem_alloc_delta += copy;
12351237
} else if (!zc) {
12361238
int i = skb_shinfo(skb)->nr_frags;

net/ipv6/esp6.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,8 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
914914
nfrags = 1;
915915

916916
goto skip_cow;
917-
} else if (!skb_has_frag_list(skb)) {
917+
} else if (!skb_has_frag_list(skb) &&
918+
!skb_has_shared_frag(skb)) {
918919
nfrags = skb_shinfo(skb)->nr_frags;
919920
nfrags++;
920921

net/ipv6/ip6_output.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,8 @@ static int __ip6_append_data(struct sock *sk,
17611761
if (err < 0)
17621762
goto error;
17631763
copy = err;
1764+
if (!(flags & MSG_NO_SHARED_FRAGS))
1765+
skb_shinfo(skb)->flags |= SKBFL_SHARED_FRAG;
17641766
wmem_alloc_delta += copy;
17651767
} else if (!zc) {
17661768
int i = skb_shinfo(skb)->nr_frags;

0 commit comments

Comments
 (0)