2023-10-06 11:42:46

by Ma Ke

[permalink] [raw]
Subject: [PATCH] net: xfrm: fix return value check in ipcomp_compress

In ipcomp_compress, to avoid an unexpected result returned by
pskb_trim, we should check the return value of pskb_trim().

Signed-off-by: Ma Ke <[email protected]>
---
net/xfrm/xfrm_ipcomp.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/xfrm/xfrm_ipcomp.c b/net/xfrm/xfrm_ipcomp.c
index 9c0fa0e1786a..5f2e6edadf48 100644
--- a/net/xfrm/xfrm_ipcomp.c
+++ b/net/xfrm/xfrm_ipcomp.c
@@ -144,7 +144,9 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
local_bh_enable();

- pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
+ err = pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
+ if (unlikely(err))
+ goto out;
return 0;

out:
--
2.37.2


2023-10-06 11:48:18

by Florian Westphal

[permalink] [raw]
Subject: Re: [PATCH] net: xfrm: fix return value check in ipcomp_compress

Ma Ke <[email protected]> wrote:
> In ipcomp_compress, to avoid an unexpected result returned by
> pskb_trim, we should check the return value of pskb_trim().
>
> Signed-off-by: Ma Ke <[email protected]>
> ---
> net/xfrm/xfrm_ipcomp.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/net/xfrm/xfrm_ipcomp.c b/net/xfrm/xfrm_ipcomp.c
> index 9c0fa0e1786a..5f2e6edadf48 100644
> --- a/net/xfrm/xfrm_ipcomp.c
> +++ b/net/xfrm/xfrm_ipcomp.c
> @@ -144,7 +144,9 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
> memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
> local_bh_enable();
>
> - pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
> + err = pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
> + if (unlikely(err))
> + goto out;

This can't be right, this now calls local_bh_enable() twice.

2023-10-06 11:59:40

by Florian Westphal

[permalink] [raw]
Subject: Re: [PATCH] net: xfrm: fix return value check in ipcomp_compress

Florian Westphal <[email protected]> wrote:
> Ma Ke <[email protected]> wrote:
> > In ipcomp_compress, to avoid an unexpected result returned by
> > pskb_trim, we should check the return value of pskb_trim().
> >
> > Signed-off-by: Ma Ke <[email protected]>
> > ---
> > net/xfrm/xfrm_ipcomp.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/net/xfrm/xfrm_ipcomp.c b/net/xfrm/xfrm_ipcomp.c
> > index 9c0fa0e1786a..5f2e6edadf48 100644
> > --- a/net/xfrm/xfrm_ipcomp.c
> > +++ b/net/xfrm/xfrm_ipcomp.c
> > @@ -144,7 +144,9 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
> > memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
> > local_bh_enable();
> >
> > - pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
> > + err = pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
> > + if (unlikely(err))
> > + goto out;
>
> This can't be right, this now calls local_bh_enable() twice.

Furthermore, looking at this:

1. skb went through skb_linearize_cow() before, so no paged data anymore

2. Right before there is a check to bail in case compression inflated
packet size.

IOW, this pskb_trim cannot fail, it boils down to __skb_trim().