5738ea6
From 415e3d3e90ce9e18727e8843ae343eda5a58fad6 Mon Sep 17 00:00:00 2001
5738ea6
From: Hannes Frederic Sowa <hannes@stressinduktion.org>
5738ea6
Date: Wed, 3 Feb 2016 02:11:03 +0100
5738ea6
Subject: [PATCH] unix: correctly track in-flight fds in sending process
5738ea6
 user_struct
5738ea6
5738ea6
The commit referenced in the Fixes tag incorrectly accounted the number
5738ea6
of in-flight fds over a unix domain socket to the original opener
5738ea6
of the file-descriptor. This allows another process to arbitrary
5738ea6
deplete the original file-openers resource limit for the maximum of
5738ea6
open files. Instead the sending processes and its struct cred should
5738ea6
be credited.
5738ea6
5738ea6
To do so, we add a reference counted struct user_struct pointer to the
5738ea6
scm_fp_list and use it to account for the number of inflight unix fds.
5738ea6
5738ea6
Fixes: 712f4aad406bb1 ("unix: properly account for FDs passed over unix sockets")
5738ea6
Reported-by: David Herrmann <dh.herrmann@gmail.com>
5738ea6
Cc: David Herrmann <dh.herrmann@gmail.com>
5738ea6
Cc: Willy Tarreau <w@1wt.eu>
5738ea6
Cc: Linus Torvalds <torvalds@linux-foundation.org>
5738ea6
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
5738ea6
Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
5738ea6
Signed-off-by: David S. Miller <davem@davemloft.net>
5738ea6
---
5738ea6
 include/net/af_unix.h | 4 ++--
5738ea6
 include/net/scm.h     | 1 +
5738ea6
 net/core/scm.c        | 7 +++++++
5738ea6
 net/unix/af_unix.c    | 4 ++--
5738ea6
 net/unix/garbage.c    | 8 ++++----
5738ea6
 5 files changed, 16 insertions(+), 8 deletions(-)
5738ea6
5738ea6
diff --git a/include/net/af_unix.h b/include/net/af_unix.h
5738ea6
index 2a91a0561a47..9b4c418bebd8 100644
5738ea6
--- a/include/net/af_unix.h
5738ea6
+++ b/include/net/af_unix.h
5738ea6
@@ -6,8 +6,8 @@
5738ea6
 #include <linux/mutex.h>
5738ea6
 #include <net/sock.h>
5738ea6
 
5738ea6
-void unix_inflight(struct file *fp);
5738ea6
-void unix_notinflight(struct file *fp);
5738ea6
+void unix_inflight(struct user_struct *user, struct file *fp);
5738ea6
+void unix_notinflight(struct user_struct *user, struct file *fp);
5738ea6
 void unix_gc(void);
5738ea6
 void wait_for_unix_gc(void);
5738ea6
 struct sock *unix_get_socket(struct file *filp);
5738ea6
diff --git a/include/net/scm.h b/include/net/scm.h
5738ea6
index 262532d111f5..59fa93c01d2a 100644
5738ea6
--- a/include/net/scm.h
5738ea6
+++ b/include/net/scm.h
5738ea6
@@ -21,6 +21,7 @@ struct scm_creds {
5738ea6
 struct scm_fp_list {
5738ea6
 	short			count;
5738ea6
 	short			max;
5738ea6
+	struct user_struct	*user;
5738ea6
 	struct file		*fp[SCM_MAX_FD];
5738ea6
 };
5738ea6
 
5738ea6
diff --git a/net/core/scm.c b/net/core/scm.c
5738ea6
index 14596fb37172..2696aefdc148 100644
5738ea6
--- a/net/core/scm.c
5738ea6
+++ b/net/core/scm.c
5738ea6
@@ -87,6 +87,7 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
5738ea6
 		*fplp = fpl;
5738ea6
 		fpl->count = 0;
5738ea6
 		fpl->max = SCM_MAX_FD;
5738ea6
+		fpl->user = NULL;
5738ea6
 	}
5738ea6
 	fpp = &fpl->fp[fpl->count];
5738ea6
 
5738ea6
@@ -107,6 +108,10 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
5738ea6
 		*fpp++ = file;
5738ea6
 		fpl->count++;
5738ea6
 	}
5738ea6
+
5738ea6
+	if (!fpl->user)
5738ea6
+		fpl->user = get_uid(current_user());
5738ea6
+
5738ea6
 	return num;
5738ea6
 }
5738ea6
 
5738ea6
@@ -119,6 +124,7 @@ void __scm_destroy(struct scm_cookie *scm)
5738ea6
 		scm->fp = NULL;
5738ea6
 		for (i=fpl->count-1; i>=0; i--)
5738ea6
 			fput(fpl->fp[i]);
5738ea6
+		free_uid(fpl->user);
5738ea6
 		kfree(fpl);
5738ea6
 	}
5738ea6
 }
5738ea6
@@ -336,6 +342,7 @@ struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
5738ea6
 		for (i = 0; i < fpl->count; i++)
5738ea6
 			get_file(fpl->fp[i]);
5738ea6
 		new_fpl->max = new_fpl->count;
5738ea6
+		new_fpl->user = get_uid(fpl->user);
5738ea6
 	}
5738ea6
 	return new_fpl;
5738ea6
 }
5738ea6
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
5738ea6
index 49d5093eb055..29be035f9c65 100644
5738ea6
--- a/net/unix/af_unix.c
5738ea6
+++ b/net/unix/af_unix.c
5738ea6
@@ -1496,7 +1496,7 @@ static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
5738ea6
 	UNIXCB(skb).fp = NULL;
5738ea6
 
5738ea6
 	for (i = scm->fp->count-1; i >= 0; i--)
5738ea6
-		unix_notinflight(scm->fp->fp[i]);
5738ea6
+		unix_notinflight(scm->fp->user, scm->fp->fp[i]);
5738ea6
 }
5738ea6
 
5738ea6
 static void unix_destruct_scm(struct sk_buff *skb)
5738ea6
@@ -1561,7 +1561,7 @@ static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
5738ea6
 		return -ENOMEM;
5738ea6
 
5738ea6
 	for (i = scm->fp->count - 1; i >= 0; i--)
5738ea6
-		unix_inflight(scm->fp->fp[i]);
5738ea6
+		unix_inflight(scm->fp->user, scm->fp->fp[i]);
5738ea6
 	return max_level;
5738ea6
 }
5738ea6
 
5738ea6
diff --git a/net/unix/garbage.c b/net/unix/garbage.c
5738ea6
index 8fcdc2283af5..6a0d48525fcf 100644
5738ea6
--- a/net/unix/garbage.c
5738ea6
+++ b/net/unix/garbage.c
5738ea6
@@ -116,7 +116,7 @@ struct sock *unix_get_socket(struct file *filp)
5738ea6
  * descriptor if it is for an AF_UNIX socket.
5738ea6
  */
5738ea6
 
5738ea6
-void unix_inflight(struct file *fp)
5738ea6
+void unix_inflight(struct user_struct *user, struct file *fp)
5738ea6
 {
5738ea6
 	struct sock *s = unix_get_socket(fp);
5738ea6
 
5738ea6
@@ -133,11 +133,11 @@ void unix_inflight(struct file *fp)
5738ea6
 		}
5738ea6
 		unix_tot_inflight++;
5738ea6
 	}
5738ea6
-	fp->f_cred->user->unix_inflight++;
5738ea6
+	user->unix_inflight++;
5738ea6
 	spin_unlock(&unix_gc_lock);
5738ea6
 }
5738ea6
 
5738ea6
-void unix_notinflight(struct file *fp)
5738ea6
+void unix_notinflight(struct user_struct *user, struct file *fp)
5738ea6
 {
5738ea6
 	struct sock *s = unix_get_socket(fp);
5738ea6
 
5738ea6
@@ -152,7 +152,7 @@ void unix_notinflight(struct file *fp)
5738ea6
 			list_del_init(&u->link);
5738ea6
 		unix_tot_inflight--;
5738ea6
 	}
5738ea6
-	fp->f_cred->user->unix_inflight--;
5738ea6
+	user->unix_inflight--;
5738ea6
 	spin_unlock(&unix_gc_lock);
5738ea6
 }
5738ea6
 
5738ea6
-- 
5738ea6
2.5.0
5738ea6