Netdev List
 help / color / mirror / Atom feed
From: Eric Dumazet <eric.dumazet@gmail.com>
To: Tim Chen <tim.c.chen@linux.intel.com>
Cc: "Yan, Zheng" <zheng.z.yan@intel.com>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	"davem@davemloft.net" <davem@davemloft.net>,
	"sfr@canb.auug.org.au" <sfr@canb.auug.org.au>,
	"jirislaby@gmail.com" <jirislaby@gmail.com>,
	"sedat.dilek@gmail.com" <sedat.dilek@gmail.com>,
	alex.shi@intel.com
Subject: Re: [PATCH -next v2] unix stream: Fix use-after-free crashes
Date: Tue, 06 Sep 2011 22:19:48 +0200	[thread overview]
Message-ID: <1315340388.3400.28.camel@edumazet-laptop> (raw)
In-Reply-To: <1315339157.2576.3079.camel@schen9-DESK>

Le mardi 06 septembre 2011 à 12:59 -0700, Tim Chen a écrit :
> On Tue, 2011-09-06 at 21:43 +0200, Eric Dumazet wrote:
> > Le mardi 06 septembre 2011 à 12:33 -0700, Tim Chen a écrit :
> > 
> > > Yes, I think locking the sendmsg for the entire duration of
> > > unix_stream_sendmsg makes a lot of sense.  It simplifies the logic a lot
> > > more.  I'll try to cook something up in the next couple of days.
> > 
> > Thats not really possible, we cant hold a spinlock and call
> > sock_alloc_send_skb() and/or memcpy_fromiovec(), wich might sleep.
> > 
> > You would need to prepare the full skb list, then :
> > - stick the ref on the last skb of the list.
> > 
> > Transfert the whole skb list in other->sk_receive_queue in one go,
> > instead of one after another.
> > 
> > Unfortunately, this would break streaming (big send(), and another
> > thread doing the receive)
> > 
> > Listen, I am wondering why hackbench even triggers SCM code. This is
> > really odd. We should not have a _single_ pid/cred ref/unref at all.
> > 
> 
> Hackbench triggers the code because it has a bunch of threads sending
> msgs on UNIX socket.
> > 
> 
> Well, if the lock socket approach doesn't work, then my original patch
> plus Yan Zheng's fix should still work.  I'll try to answer your
> objections below:
> 
> 
> > I was discussing of things after proposed patch, not current net-next.
> > 
> > This reads :
> > 
> > err = unix_scm_to_skb(siocb->scm, skb, !fds_sent, scm_ref);
> > 
> > So first skb is sent without ref taken, as mentioned in Changelog ?
> > 
> 
> No. the first skb is sent *with* ref taken, as scm_ref is set to true for
> first skb.
> 
> > 
> > If second skb cannot be built, we exit this system call with an already
> > queued skb. Receiver can then access to freed memory.
> > 
> 
> No, we do have reference set.  For first skb, in unix_scm_to_skb.  For the 
> second skb (which is the last skb), in scm_sent.  Should the second skb alloc failed,
> we'll release the ref in scm_destroy.  Otherwise, the receiver will release
> the references will consuming the skb.
> 

This is crap. This is not the intent of the code I read from the patch.

unless scm_ref really means scm_noref ?

I really hate this patch. I mean it. 

I read it 10 times, spent 2 hours and still dont understand it.


@@ -1577,6 +1577,7 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
        int sent = 0;
        struct scm_cookie tmp_scm;
        bool fds_sent = false;
+       bool scm_ref = true;
        int max_level;
 
        if (NULL == siocb->scm)
@@ -1637,12 +1638,15 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
                 */
                size = min_t(int, size, skb_tailroom(skb));
 
+               /* pass the scm reference to the very last skb */

HERE: I understand : on the last skb, set scm_ref to false.
So comment is wrong.

+               if (sent + size >= len)
+                       scm_ref = false;
 
-               /* Only send the fds and no ref to pid in the first buffer */
-               err = unix_scm_to_skb(siocb->scm, skb, !fds_sent, fds_sent);
+               /* Only send the fds in the first buffer */
+               err = unix_scm_to_skb(siocb->scm, skb, !fds_sent, scm_ref);
                if (err < 0) {
                        kfree_skb(skb);
-                       goto out;
+                       goto out_err;
                }



As I said, we should revert the buggy patch, and rewrite a performance
fix from scratch, with not a single get_pid()/put_pid() in fast path.

read()/write() on AF_UNIX sockets should not use a single
get_pid()/put_pid().

This is a serious regression we should fix at 100%, not 50% or even 75%,
adding serious bugs.

  reply	other threads:[~2011-09-06 20:20 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-04  5:44 [PATCH -next v2] unix stream: Fix use-after-free crashes Yan, Zheng
2011-09-04  7:12 ` Sedat Dilek
2011-09-04  8:23   ` Yan, Zheng
2011-09-04 15:50     ` Joe Perches
2011-09-06 16:39     ` Tim Chen
2011-09-06 16:25 ` Tim Chen
2011-09-06 17:40   ` Eric Dumazet
2011-09-06 18:50     ` Tim Chen
2011-09-06 19:01       ` Eric Dumazet
2011-09-06 19:33         ` Tim Chen
2011-09-06 19:43           ` Eric Dumazet
2011-09-06 19:59             ` Tim Chen
2011-09-06 20:19               ` Eric Dumazet [this message]
2011-09-06 22:08                 ` Tim Chen
2011-09-07  2:35                   ` Eric Dumazet
2011-09-06 23:09                 ` Yan, Zheng
2011-09-07  2:55                   ` Eric Dumazet
2011-09-16 23:35                     ` David Miller
2011-09-16 16:50                       ` Tim Chen
2011-09-19  7:57                         ` Eric Dumazet
2011-09-07  4:36                 ` Yan, Zheng 
2011-09-07  5:08                   ` Eric Dumazet
2011-09-07  5:20                     ` Yan, Zheng
     [not found]                       ` <1315381503.3400.85.camel@edumazet-laptop>
2011-09-07 12:01                         ` Tim Chen
2011-09-07 20:12                           ` Sedat Dilek
2011-09-07 20:30                             ` Sedat Dilek
2011-09-07 14:37                               ` Tim Chen
2011-09-08  0:27                                 ` Yan, Zheng
2011-09-07 21:06                                   ` Tim Chen
2011-09-07 21:15                                     ` Tim Chen
2011-09-08  6:21                                       ` Eric Dumazet
2011-09-08  4:18                                     ` Yan, Zheng
2011-09-08  5:59                                     ` Eric Dumazet
2011-09-08  6:22                                       ` Yan, Zheng
2011-09-08  7:11                                         ` Eric Dumazet
2011-09-08  7:23                                           ` Yan, Zheng
2011-09-08  7:33                                             ` Eric Dumazet
2011-09-08  9:59                                               ` Sedat Dilek
2011-09-08 13:21                                                 ` [PATCH net-next v3] af_unix: " Eric Dumazet
2011-09-08  8:37                                                   ` Tim Chen
2011-09-09  6:51                                                     ` Eric Dumazet
2011-09-09  7:58                                                       ` [PATCH net-next] af_unix: fix use after free in unix_stream_recvmsg() Eric Dumazet
2011-09-09 10:39                                                         ` Tim Chen
2011-09-09 10:41                                                       ` [PATCH net-next v3] af_unix: Fix use-after-free crashes Tim Chen
2011-09-08  7:56                                           ` [PATCH -next v2] unix stream: " Jiri Slaby
2011-09-08  8:43                                             ` Sedat Dilek
2011-09-08  7:02                                       ` Sedat Dilek
2011-09-07 21:26                           ` Eric Dumazet
2011-09-08 13:28                             ` Eric Dumazet
2011-09-08  9:24                               ` Tim Chen
2011-09-09  5:06                                 ` [PATCH net-next] af_unix: dont send SCM_CREDENTIALS by default Eric Dumazet
2011-09-12 19:15                                   ` Tim Chen
2011-09-19  1:07                                   ` David Miller
2011-09-19  4:28                                     ` Eric Dumazet
2011-09-19 15:02                                       ` Eric Dumazet
2011-09-19 15:52                                         ` [PATCH v2 " Eric Dumazet
2011-09-19 21:39                                           ` Tim Chen
2011-09-20  2:10                                             ` Valdis.Kletnieks
2011-09-20  4:16                                               ` Eric Dumazet
2011-09-22 16:15                                                 ` tim
2011-11-28 13:23                                                 ` Michal Schmidt
2011-11-28 13:38                                                   ` Eric Dumazet
2011-09-28 17:30                                           ` David Miller
2011-09-08 10:05               ` [PATCH -next v2] unix stream: Fix use-after-free crashes Sedat Dilek
2011-09-08  8:50                 ` Tim Chen

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=1315340388.3400.28.camel@edumazet-laptop \
    --to=eric.dumazet@gmail.com \
    --cc=alex.shi@intel.com \
    --cc=davem@davemloft.net \
    --cc=jirislaby@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=sedat.dilek@gmail.com \
    --cc=sfr@canb.auug.org.au \
    --cc=tim.c.chen@linux.intel.com \
    --cc=zheng.z.yan@intel.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