stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7 1/9] net/compat: Add missing sock updates for SCM_RIGHTS
       [not found] <20200709182642.1773477-1-keescook@chromium.org>
@ 2020-07-09 18:26 ` Kees Cook
  2020-07-10 11:28   ` Christian Brauner
  2020-07-09 18:26 ` [PATCH v7 2/9] pidfd: Add missing sock updates for pidfd_getfd() Kees Cook
  1 sibling, 1 reply; 6+ messages in thread
From: Kees Cook @ 2020-07-09 18:26 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kees Cook, stable, Sargun Dhillon, Christian Brauner,
	Tycho Andersen, David Laight, Christoph Hellwig, David S. Miller,
	Jakub Kicinski, Alexander Viro, Aleksa Sarai, Matt Denton,
	Jann Horn, Chris Palmer, Robert Sesek, Giuseppe Scrivano,
	Greg Kroah-Hartman, Andy Lutomirski, Will Drewry, Shuah Khan,
	netdev, containers, linux-api, linux-fsdevel, linux-kselftest

Add missed sock updates to compat path via a new helper, which will be
used more in coming patches. (The net/core/scm.c code is left as-is here
to assist with -stable backports for the compat path.)

Cc: stable@vger.kernel.org
Fixes: 48a87cc26c13 ("net: netprio: fd passed in SCM_RIGHTS datagram not set correctly")
Fixes: d84295067fc7 ("net: net_cls: fd passed in SCM_RIGHTS datagram not set correctly")
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 include/net/sock.h |  4 ++++
 net/compat.c       |  1 +
 net/core/sock.c    | 21 +++++++++++++++++++++
 3 files changed, 26 insertions(+)

diff --git a/include/net/sock.h b/include/net/sock.h
index c53cc42b5ab9..2be67f1ee8b1 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -890,6 +890,8 @@ static inline int sk_memalloc_socks(void)
 {
 	return static_branch_unlikely(&memalloc_socks_key);
 }
+
+void __receive_sock(struct file *file);
 #else
 
 static inline int sk_memalloc_socks(void)
@@ -897,6 +899,8 @@ static inline int sk_memalloc_socks(void)
 	return 0;
 }
 
+static inline void __receive_sock(struct file *file)
+{ }
 #endif
 
 static inline gfp_t sk_gfp_mask(const struct sock *sk, gfp_t gfp_mask)
diff --git a/net/compat.c b/net/compat.c
index 5e3041a2c37d..2937b816107d 100644
--- a/net/compat.c
+++ b/net/compat.c
@@ -309,6 +309,7 @@ void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
 			break;
 		}
 		/* Bump the usage count and install the file. */
+		__receive_sock(fp[i]);
 		fd_install(new_fd, get_file(fp[i]));
 	}
 
diff --git a/net/core/sock.c b/net/core/sock.c
index 6c4acf1f0220..bde394979041 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -2840,6 +2840,27 @@ int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *
 }
 EXPORT_SYMBOL(sock_no_mmap);
 
+/*
+ * When a file is received (via SCM_RIGHTS, etc), we must bump the
+ * various sock-based usage counts.
+ */
+void __receive_sock(struct file *file)
+{
+	struct socket *sock;
+	int error;
+
+	/*
+	 * The resulting value of "error" is ignored here since we only
+	 * need to take action when the file is a socket and testing
+	 * "sock" for NULL is sufficient.
+	 */
+	sock = sock_from_file(file, &error);
+	if (sock) {
+		sock_update_netprioidx(&sock->sk->sk_cgrp_data);
+		sock_update_classid(&sock->sk->sk_cgrp_data);
+	}
+}
+
 ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
 {
 	ssize_t res;
-- 
2.25.1


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

* [PATCH v7 2/9] pidfd: Add missing sock updates for pidfd_getfd()
       [not found] <20200709182642.1773477-1-keescook@chromium.org>
  2020-07-09 18:26 ` [PATCH v7 1/9] net/compat: Add missing sock updates for SCM_RIGHTS Kees Cook
@ 2020-07-09 18:26 ` Kees Cook
  2020-07-09 20:00   ` Jann Horn
  1 sibling, 1 reply; 6+ messages in thread
From: Kees Cook @ 2020-07-09 18:26 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kees Cook, stable, Sargun Dhillon, Christian Brauner,
	Tycho Andersen, David Laight, Christoph Hellwig, David S. Miller,
	Jakub Kicinski, Alexander Viro, Aleksa Sarai, Matt Denton,
	Jann Horn, Chris Palmer, Robert Sesek, Giuseppe Scrivano,
	Greg Kroah-Hartman, Andy Lutomirski, Will Drewry, Shuah Khan,
	netdev, containers, linux-api, linux-fsdevel, linux-kselftest

The sock counting (sock_update_netprioidx() and sock_update_classid())
was missing from pidfd's implementation of received fd installation. Add
a call to the new __receive_sock() helper.

Cc: stable@vger.kernel.org
Fixes: 8649c322f75c ("pid: Implement pidfd_getfd syscall")
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 kernel/pid.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/kernel/pid.c b/kernel/pid.c
index f1496b757162..85ed00abdc7c 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -42,6 +42,7 @@
 #include <linux/sched/signal.h>
 #include <linux/sched/task.h>
 #include <linux/idr.h>
+#include <net/sock.h>
 
 struct pid init_struct_pid = {
 	.count		= REFCOUNT_INIT(1),
@@ -642,10 +643,12 @@ static int pidfd_getfd(struct pid *pid, int fd)
 	}
 
 	ret = get_unused_fd_flags(O_CLOEXEC);
-	if (ret < 0)
+	if (ret < 0) {
 		fput(file);
-	else
+	} else {
 		fd_install(ret, file);
+		__receive_sock(file);
+	}
 
 	return ret;
 }
-- 
2.25.1


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

* Re: [PATCH v7 2/9] pidfd: Add missing sock updates for pidfd_getfd()
  2020-07-09 18:26 ` [PATCH v7 2/9] pidfd: Add missing sock updates for pidfd_getfd() Kees Cook
@ 2020-07-09 20:00   ` Jann Horn
  2020-07-09 21:17     ` Kees Cook
  2020-07-09 22:35     ` Kees Cook
  0 siblings, 2 replies; 6+ messages in thread
From: Jann Horn @ 2020-07-09 20:00 UTC (permalink / raw)
  To: Kees Cook
  Cc: kernel list, stable, Sargun Dhillon, Christian Brauner,
	Tycho Andersen, David Laight, Christoph Hellwig, David S. Miller,
	Jakub Kicinski, Alexander Viro, Aleksa Sarai, Matt Denton,
	Chris Palmer, Robert Sesek, Giuseppe Scrivano, Greg Kroah-Hartman,
	Andy Lutomirski, Will Drewry, Shuah Khan, Network Development,
	Linux Containers, Linux API, linux-fsdevel,
	open list:KERNEL SELFTEST FRAMEWORK

On Thu, Jul 9, 2020 at 8:26 PM Kees Cook <keescook@chromium.org> wrote:
> The sock counting (sock_update_netprioidx() and sock_update_classid())
> was missing from pidfd's implementation of received fd installation. Add
> a call to the new __receive_sock() helper.
[...]
> diff --git a/kernel/pid.c b/kernel/pid.c
[...]
> @@ -642,10 +643,12 @@ static int pidfd_getfd(struct pid *pid, int fd)
>         }
>
>         ret = get_unused_fd_flags(O_CLOEXEC);
> -       if (ret < 0)
> +       if (ret < 0) {
>                 fput(file);
> -       else
> +       } else {
>                 fd_install(ret, file);
> +               __receive_sock(file);
> +       }

__receive_sock() has to be before fd_install(), otherwise `file` can
be a dangling pointer.

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

* Re: [PATCH v7 2/9] pidfd: Add missing sock updates for pidfd_getfd()
  2020-07-09 20:00   ` Jann Horn
@ 2020-07-09 21:17     ` Kees Cook
  2020-07-09 22:35     ` Kees Cook
  1 sibling, 0 replies; 6+ messages in thread
From: Kees Cook @ 2020-07-09 21:17 UTC (permalink / raw)
  To: Jann Horn
  Cc: kernel list, stable, Sargun Dhillon, Christian Brauner,
	Tycho Andersen, David Laight, Christoph Hellwig, David S. Miller,
	Jakub Kicinski, Alexander Viro, Aleksa Sarai, Matt Denton,
	Chris Palmer, Robert Sesek, Giuseppe Scrivano, Greg Kroah-Hartman,
	Andy Lutomirski, Will Drewry, Shuah Khan, Network Development,
	Linux Containers, Linux API, linux-fsdevel,
	open list:KERNEL SELFTEST FRAMEWORK

On Thu, Jul 09, 2020 at 10:00:42PM +0200, Jann Horn wrote:
> On Thu, Jul 9, 2020 at 8:26 PM Kees Cook <keescook@chromium.org> wrote:
> > The sock counting (sock_update_netprioidx() and sock_update_classid())
> > was missing from pidfd's implementation of received fd installation. Add
> > a call to the new __receive_sock() helper.
> [...]
> > diff --git a/kernel/pid.c b/kernel/pid.c
> [...]
> > @@ -642,10 +643,12 @@ static int pidfd_getfd(struct pid *pid, int fd)
> >         }
> >
> >         ret = get_unused_fd_flags(O_CLOEXEC);
> > -       if (ret < 0)
> > +       if (ret < 0) {
> >                 fput(file);
> > -       else
> > +       } else {
> >                 fd_install(ret, file);
> > +               __receive_sock(file);
> > +       }
> 
> __receive_sock() has to be before fd_install(), otherwise `file` can
> be a dangling pointer.

Burned by fd_install()'s API again. Thanks. I will respin.

-- 
Kees Cook

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

* Re: [PATCH v7 2/9] pidfd: Add missing sock updates for pidfd_getfd()
  2020-07-09 20:00   ` Jann Horn
  2020-07-09 21:17     ` Kees Cook
@ 2020-07-09 22:35     ` Kees Cook
  1 sibling, 0 replies; 6+ messages in thread
From: Kees Cook @ 2020-07-09 22:35 UTC (permalink / raw)
  To: Jann Horn
  Cc: kernel list, stable, Sargun Dhillon, Christian Brauner,
	Tycho Andersen, David Laight, Christoph Hellwig, David S. Miller,
	Jakub Kicinski, Alexander Viro, Aleksa Sarai, Matt Denton,
	Chris Palmer, Robert Sesek, Giuseppe Scrivano, Greg Kroah-Hartman,
	Andy Lutomirski, Will Drewry, Shuah Khan, Network Development,
	Linux Containers, Linux API, linux-fsdevel,
	open list:KERNEL SELFTEST FRAMEWORK

On Thu, Jul 09, 2020 at 10:00:42PM +0200, Jann Horn wrote:
> On Thu, Jul 9, 2020 at 8:26 PM Kees Cook <keescook@chromium.org> wrote:
> > The sock counting (sock_update_netprioidx() and sock_update_classid())
> > was missing from pidfd's implementation of received fd installation. Add
> > a call to the new __receive_sock() helper.
> [...]
> > diff --git a/kernel/pid.c b/kernel/pid.c
> [...]
> > @@ -642,10 +643,12 @@ static int pidfd_getfd(struct pid *pid, int fd)
> >         }
> >
> >         ret = get_unused_fd_flags(O_CLOEXEC);
> > -       if (ret < 0)
> > +       if (ret < 0) {
> >                 fput(file);
> > -       else
> > +       } else {
> >                 fd_install(ret, file);
> > +               __receive_sock(file);
> > +       }
> 
> __receive_sock() has to be before fd_install(), otherwise `file` can
> be a dangling pointer.

I've swapped the order now and double-checked the other uses. Everything
else seems fine.

-- 
Kees Cook

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

* Re: [PATCH v7 1/9] net/compat: Add missing sock updates for SCM_RIGHTS
  2020-07-09 18:26 ` [PATCH v7 1/9] net/compat: Add missing sock updates for SCM_RIGHTS Kees Cook
@ 2020-07-10 11:28   ` Christian Brauner
  0 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2020-07-10 11:28 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kernel, stable, Sargun Dhillon, Christian Brauner,
	Tycho Andersen, David Laight, Christoph Hellwig, David S. Miller,
	Jakub Kicinski, Alexander Viro, Aleksa Sarai, Matt Denton,
	Jann Horn, Chris Palmer, Robert Sesek, Giuseppe Scrivano,
	Greg Kroah-Hartman, Andy Lutomirski, Will Drewry, Shuah Khan,
	netdev, containers, linux-api, linux-fsdevel, linux-kselftest

On Thu, Jul 09, 2020 at 11:26:34AM -0700, Kees Cook wrote:
> Add missed sock updates to compat path via a new helper, which will be
> used more in coming patches. (The net/core/scm.c code is left as-is here
> to assist with -stable backports for the compat path.)
> 
> Cc: stable@vger.kernel.org
> Fixes: 48a87cc26c13 ("net: netprio: fd passed in SCM_RIGHTS datagram not set correctly")
> Fixes: d84295067fc7 ("net: net_cls: fd passed in SCM_RIGHTS datagram not set correctly")
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---

Thanks!
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>

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

end of thread, other threads:[~2020-07-10 11:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20200709182642.1773477-1-keescook@chromium.org>
2020-07-09 18:26 ` [PATCH v7 1/9] net/compat: Add missing sock updates for SCM_RIGHTS Kees Cook
2020-07-10 11:28   ` Christian Brauner
2020-07-09 18:26 ` [PATCH v7 2/9] pidfd: Add missing sock updates for pidfd_getfd() Kees Cook
2020-07-09 20:00   ` Jann Horn
2020-07-09 21:17     ` Kees Cook
2020-07-09 22:35     ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).