All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH -next v2] unix stream: Fix use-after-free crashes
@ 2011-09-04  5:44 Yan, Zheng
  2011-09-04  7:12 ` Sedat Dilek
  2011-09-06 16:25 ` Tim Chen
  0 siblings, 2 replies; 65+ messages in thread
From: Yan, Zheng @ 2011-09-04  5:44 UTC (permalink / raw)
  To: netdev; +Cc: davem, sfr, tim.c.chen, jirislaby, sedat.dilek

Commit 0856a30409 (Scm: Remove unnecessary pid & credential references
in Unix socket's send and receive path) introduced a use-after-free bug.
It passes the scm reference to the first skb. Skb(s) afterwards may
reference freed data structure because the first skb can be destructed
by the receiver at anytime. The fix is by passing the scm reference to
the very last skb.

Signed-off-by: Zheng Yan <zheng.z.yan@intel.com>
Reported-by: Jiri Slaby <jirislaby@gmail.com>
---
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index e6d9d10..77ec8e8 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -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 */
+		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;
 		}
 		max_level = err + 1;
 		fds_sent = true;
@@ -1650,7 +1654,7 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
 		err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
 		if (err) {
 			kfree_skb(skb);
-			goto out;
+			goto out_err;
 		}
 
 		unix_state_lock(other);
@@ -1667,10 +1671,10 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
 		sent += size;
 	}
 
-	if (skb)
-		scm_release(siocb->scm);
-	else
+	if (scm_ref)
 		scm_destroy(siocb->scm);
+	else
+		scm_release(siocb->scm);
 	siocb->scm = NULL;
 
 	return sent;
@@ -1683,9 +1687,10 @@ pipe_err:
 		send_sig(SIGPIPE, current, 0);
 	err = -EPIPE;
 out_err:
-	if (skb == NULL)
+	if (scm_ref)
 		scm_destroy(siocb->scm);
-out:
+	else
+		scm_release(siocb->scm);
 	siocb->scm = NULL;
 	return sent ? : err;
 }

^ permalink raw reply related	[flat|nested] 65+ messages in thread

end of thread, other threads:[~2011-11-28 13:38 UTC | newest]

Thread overview: 65+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.