mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: alexious@zju.edu.cn
To: "Eric Dumazet" <edumazet@google.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	"David Ahern" <dsahern@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] net: ipv4: fix a memleak in ip_setup_cork
Date: Mon, 29 Jan 2024 17:02:08 +0800 (GMT+08:00)	[thread overview]
Message-ID: <5a97396e.1e36.18d54745ad4.Coremail.alexious@zju.edu.cn> (raw)
In-Reply-To: <CANn89i+3Maf90HUzaGzFQgw9UQDoZLP-Ob+KrE9Ns6jND=6D9w@mail.gmail.com>

> On Fri, Jan 26, 2024 at 11:13 AM Eric Dumazet <edumazet@google.com> wrote:
> >
> > On Fri, Jan 26, 2024 at 8:51 AM Zhipeng Lu <alexious@zju.edu.cn> wrote:
> > >
> > > When inetdev_valid_mtu fails, cork->opt should be freed if it is
> > > allocated in ip_setup_cork. Otherwise there could be a memleak.
> > >
> > > Fixes: 501a90c94510 ("inet: protect against too small mtu values.")
> > > Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
> > > ---
> > >  net/ipv4/ip_output.c | 9 ++++++++-
> > >  1 file changed, 8 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c
> > > index b06f678b03a1..3215ea07d398 100644
> > > --- a/net/ipv4/ip_output.c
> > > +++ b/net/ipv4/ip_output.c
> > > @@ -1282,6 +1282,7 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
> > >  {
> > >         struct ip_options_rcu *opt;
> > >         struct rtable *rt;
> > > +       int free_opt = 0;
> > >
> > >         rt = *rtp;
> > >         if (unlikely(!rt))
> > > @@ -1297,6 +1298,7 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
> > >                                             sk->sk_allocation);
> > >                         if (unlikely(!cork->opt))
> > >                                 return -ENOBUFS;
> > > +                       free_opt = 1;
> > >                 }
> > >                 memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
> > >                 cork->flags |= IPCORK_OPT;
> > > @@ -1306,8 +1308,13 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
> > >         cork->fragsize = ip_sk_use_pmtu(sk) ?
> > >                          dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu);
> > >
> > > -       if (!inetdev_valid_mtu(cork->fragsize))
> > > +       if (!inetdev_valid_mtu(cork->fragsize)) {
> > > +               if (opt && free_opt) {
> > > +                       kfree(cork->opt);
> > > +                       cork->opt = NULL;
> > > +               }
> > >                 return -ENETUNREACH;
> > > +       }
> > >
> > >         cork->gso_size = ipc->gso_size;
> > >
> > > --
> > > 2.34.1
> > >
> >
> > What about something simpler like :
> >
> > diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c
> > index b06f678b03a19b806fd14764a4caad60caf02919..41537d18eecfd6e1163aacc35e047c22468e04e6
> > 100644
> > --- a/net/ipv4/ip_output.c
> > +++ b/net/ipv4/ip_output.c
> > @@ -1287,6 +1287,12 @@ static int ip_setup_cork(struct sock *sk,
> > struct inet_cork *cork,
> >         if (unlikely(!rt))
> >                 return -EFAULT;
> >
> > +       cork->fragsize = ip_sk_use_pmtu(sk) ?
> > +                        dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu);
> > +
> > +       if (!inetdev_valid_mtu(cork->fragsize))
> > +               return -ENETUNREACH;
> > +
> >         /*
> >          * setup for corking.
> >          */
> > @@ -1303,12 +1309,6 @@ static int ip_setup_cork(struct sock *sk,
> > struct inet_cork *cork,
> >                 cork->addr = ipc->addr;
> >         }
> >
> > -       cork->fragsize = ip_sk_use_pmtu(sk) ?
> > -                        dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu);
> > -
> > -       if (!inetdev_valid_mtu(cork->fragsize))
> > -               return -ENETUNREACH;
> > -
> >         cork->gso_size = ipc->gso_size;
> >
> >         cork->dst = &rt->dst;
> 
> Hi Zhipeng Lu
> 
> Could you send a V2 off your patch ? I will then add a Reviewed-by:
> tag, thanks !

Hi Eric
Sure, I'll soon send a v2 version following your suggestion.


      reply	other threads:[~2024-01-29  9:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-26  7:51 Zhipeng Lu
2024-01-26 10:13 ` Eric Dumazet
2024-01-27  8:34   ` Eric Dumazet
2024-01-29  9:02     ` alexious [this message]

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=5a97396e.1e36.18d54745ad4.Coremail.alexious@zju.edu.cn \
    --to=alexious@zju.edu.cn \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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

all inboxes | Powered by JetHome®