RE: get rid of the address_space override in setsockopt v2

2020-07-27 Thread David Laight
From: Al Viro
> Sent: 27 July 2020 14:48
> 
> On Mon, Jul 27, 2020 at 09:51:45AM +, David Laight wrote:
> 
> > I'm sure there is code that processes options in chunks.
> > This probably means it is possible to put a chunk boundary
> > at the end of userspace and continue processing the very start
> > of kernel memory.
> >
> > At best this faults on the kernel copy code and crashes the system.
> 
> Really?  Care to provide some details, or is it another of your "I can't
> be possibly arsed to check what I'm saying, but it stands for reason
> that..." specials?

I did more 'homework' than sometimes :-)
Slightly difficult without a searchable net-next tree.
However, as has been pointed out is a different thread
this code is used to update IPv6 flow labels:

> > -   if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), 
> > olen))
> > +   sockptr_advance(optval, CMSG_ALIGN(sizeof(*freq)));
> > +   if (copy_from_sockptr(fl->opt + 1, optval, olen))
> > goto done;

and doesn't work because the advances are no longer cumulative.

Now access_ok() has to take the base address and length to stop
'running into' kernel space, but the code above can advance from
a valid user pointer (which won't fault) to a kernel address.

If there were always an unmapped 'guard' page in the user address
space the access_ok() check prior to copy_to/from_user() wouldn't
need the length.
So I surmise that no such guard page exists and so the above
can advance from user addresses into kernel ones.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, 
UK
Registration No: 1397386 (Wales)



Re: get rid of the address_space override in setsockopt v2

2020-07-27 Thread Al Viro
On Mon, Jul 27, 2020 at 09:51:45AM +, David Laight wrote:

> I'm sure there is code that processes options in chunks.
> This probably means it is possible to put a chunk boundary
> at the end of userspace and continue processing the very start
> of kernel memory.
> 
> At best this faults on the kernel copy code and crashes the system.

Really?  Care to provide some details, or is it another of your "I can't
be possibly arsed to check what I'm saying, but it stands for reason
that..." specials?


RE: get rid of the address_space override in setsockopt v2

2020-07-27 Thread David Laight
From: David Miller
> Sent: 24 July 2020 23:44
> 
> From: Christoph Hellwig 
> Date: Thu, 23 Jul 2020 08:08:42 +0200
> 
> > setsockopt is the last place in architecture-independ code that still
> > uses set_fs to force the uaccess routines to operate on kernel pointers.
> >
> > This series adds a new sockptr_t type that can contained either a kernel
> > or user pointer, and which has accessors that do the right thing, and
> > then uses it for setsockopt, starting by refactoring some low-level
> > helpers and moving them over to it before finally doing the main
> > setsockopt method.
> >
> > Note that apparently the eBPF selftests do not even cover this path, so
> > the series has been tested with a testing patch that always copies the
> > data first and passes a kernel pointer.  This is something that works for
> > most common sockopts (and is something that the ePBF support relies on),
> > but unfortunately in various corner cases we either don't use the passed
> > in length, or in one case actually copy data back from setsockopt, or in
> > case of bpfilter straight out do not work with kernel pointers at all.
> >
> > Against net-next/master.
> >
> > Changes since v1:
> >  - check that users don't pass in kernel addresses
> >  - more bpfilter cleanups
> >  - cosmetic mptcp tweak
> 
> Series applied to net-next, I'm build testing and will push this out when
> that is done.

Hmmm... this code does:

int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
int optlen)
{
sockptr_t optval;
char *kernel_optval = NULL;
int err, fput_needed;
struct socket *sock;

if (optlen < 0)
return -EINVAL;

err = init_user_sockptr(, user_optval);
if (err)
return err;

And the called code does:
if (copy_from_sockptr(, optbuf, sizeof(opt)))
return -EFAULT;


Which means that only the base of the user's buffer is checked
for being in userspace.

I'm sure there is code that processes options in chunks.
This probably means it is possible to put a chunk boundary
at the end of userspace and continue processing the very start
of kernel memory.

At best this faults on the kernel copy code and crashes the system.

Maybe there wasn't any code that actually incremented the user address.
But it is hardly robust.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, 
UK
Registration No: 1397386 (Wales)



Re: get rid of the address_space override in setsockopt v2

2020-07-26 Thread David Miller
From: Christoph Hellwig 
Date: Sun, 26 Jul 2020 09:03:11 +0200

> On Fri, Jul 24, 2020 at 03:43:42PM -0700, David Miller wrote:
>> > Changes since v1:
>> >  - check that users don't pass in kernel addresses
>> >  - more bpfilter cleanups
>> >  - cosmetic mptcp tweak
>> 
>> Series applied to net-next, I'm build testing and will push this out when
>> that is done.
> 
> The buildbot found one warning with the isdn debug code after a few
> days, here is what I think is the best fix:

I already fixed this in net-next.


Re: get rid of the address_space override in setsockopt v2

2020-07-26 Thread Andreas Schwab
On Jul 26 2020, Christoph Hellwig wrote:

> From 6601732f7a54db5f04efba08f7e9224e5b757112 Mon Sep 17 00:00:00 2001
> From: Christoph Hellwig 
> Date: Sun, 26 Jul 2020 09:00:09 +0200
> Subject: mISDN: remove a debug printk in data_sock_setsockopt
>
> The %p won't work with the new sockptr_t type.  But in the times of
> ftrace, bpftrace and co these kinds of debug printks are pretty anyway,

I think there is a word missing after pretty.

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."


Re: get rid of the address_space override in setsockopt v2

2020-07-26 Thread Christoph Hellwig
On Fri, Jul 24, 2020 at 03:43:42PM -0700, David Miller wrote:
> > Changes since v1:
> >  - check that users don't pass in kernel addresses
> >  - more bpfilter cleanups
> >  - cosmetic mptcp tweak
> 
> Series applied to net-next, I'm build testing and will push this out when
> that is done.

The buildbot found one warning with the isdn debug code after a few
days, here is what I think is the best fix:

---
>From 6601732f7a54db5f04efba08f7e9224e5b757112 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig 
Date: Sun, 26 Jul 2020 09:00:09 +0200
Subject: mISDN: remove a debug printk in data_sock_setsockopt

The %p won't work with the new sockptr_t type.  But in the times of
ftrace, bpftrace and co these kinds of debug printks are pretty anyway,
so just remove the whole debug printk.

Signed-off-by: Christoph Hellwig 
---
 drivers/isdn/mISDN/socket.c | 4 
 1 file changed, 4 deletions(-)

diff --git a/drivers/isdn/mISDN/socket.c b/drivers/isdn/mISDN/socket.c
index 1b2b91479107bc..2c58a6fe6d129e 100644
--- a/drivers/isdn/mISDN/socket.c
+++ b/drivers/isdn/mISDN/socket.c
@@ -406,10 +406,6 @@ static int data_sock_setsockopt(struct socket *sock, int 
level, int optname,
struct sock *sk = sock->sk;
int err = 0, opt = 0;
 
-   if (*debug & DEBUG_SOCKET)
-   printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
-  level, optname, optval, len);
-
lock_sock(sk);
 
switch (optname) {
-- 
2.27.0



Re: get rid of the address_space override in setsockopt v2

2020-07-24 Thread David Miller
From: Christoph Hellwig 
Date: Thu, 23 Jul 2020 08:08:42 +0200

> setsockopt is the last place in architecture-independ code that still
> uses set_fs to force the uaccess routines to operate on kernel pointers.
> 
> This series adds a new sockptr_t type that can contained either a kernel
> or user pointer, and which has accessors that do the right thing, and
> then uses it for setsockopt, starting by refactoring some low-level
> helpers and moving them over to it before finally doing the main
> setsockopt method.
> 
> Note that apparently the eBPF selftests do not even cover this path, so
> the series has been tested with a testing patch that always copies the
> data first and passes a kernel pointer.  This is something that works for
> most common sockopts (and is something that the ePBF support relies on),
> but unfortunately in various corner cases we either don't use the passed
> in length, or in one case actually copy data back from setsockopt, or in
> case of bpfilter straight out do not work with kernel pointers at all.
> 
> Against net-next/master.
> 
> Changes since v1:
>  - check that users don't pass in kernel addresses
>  - more bpfilter cleanups
>  - cosmetic mptcp tweak

Series applied to net-next, I'm build testing and will push this out when
that is done.

Thanks.


get rid of the address_space override in setsockopt v2

2020-07-23 Thread Christoph Hellwig
Hi Dave,

setsockopt is the last place in architecture-independ code that still
uses set_fs to force the uaccess routines to operate on kernel pointers.

This series adds a new sockptr_t type that can contained either a kernel
or user pointer, and which has accessors that do the right thing, and
then uses it for setsockopt, starting by refactoring some low-level
helpers and moving them over to it before finally doing the main
setsockopt method.

Note that apparently the eBPF selftests do not even cover this path, so
the series has been tested with a testing patch that always copies the
data first and passes a kernel pointer.  This is something that works for
most common sockopts (and is something that the ePBF support relies on),
but unfortunately in various corner cases we either don't use the passed
in length, or in one case actually copy data back from setsockopt, or in
case of bpfilter straight out do not work with kernel pointers at all.

Against net-next/master.

Changes since v1:
 - check that users don't pass in kernel addresses
 - more bpfilter cleanups
 - cosmetic mptcp tweak

Diffstat:
 crypto/af_alg.c   |7 
 drivers/crypto/chelsio/chtls/chtls_main.c |   18 -
 drivers/isdn/mISDN/socket.c   |4 
 include/linux/bpfilter.h  |6 
 include/linux/filter.h|3 
 include/linux/mroute.h|5 
 include/linux/mroute6.h   |8 
 include/linux/net.h   |4 
 include/linux/netfilter.h |6 
 include/linux/netfilter/x_tables.h|4 
 include/linux/sockptr.h   |  132 
 include/net/inet_connection_sock.h|3 
 include/net/ip.h  |7 
 include/net/ipv6.h|6 
 include/net/sctp/structs.h|2 
 include/net/sock.h|7 
 include/net/tcp.h |6 
 include/net/udp.h |2 
 include/net/xfrm.h|8 
 net/atm/common.c  |6 
 net/atm/common.h  |2 
 net/atm/pvc.c |2 
 net/atm/svc.c |6 
 net/ax25/af_ax25.c|6 
 net/bluetooth/hci_sock.c  |8 
 net/bluetooth/l2cap_sock.c|   22 +-
 net/bluetooth/rfcomm/sock.c   |   12 -
 net/bluetooth/sco.c   |6 
 net/bpfilter/bpfilter_kern.c  |   55 ++---
 net/bridge/netfilter/ebtables.c   |   46 +---
 net/caif/caif_socket.c|8 
 net/can/j1939/socket.c|   12 -
 net/can/raw.c |   16 -
 net/core/filter.c |6 
 net/core/sock.c   |   36 +--
 net/dccp/dccp.h   |2 
 net/dccp/proto.c  |   20 -
 net/decnet/af_decnet.c|   13 -
 net/ieee802154/socket.c   |6 
 net/ipv4/bpfilter/sockopt.c   |   16 -
 net/ipv4/ip_options.c |   43 +---
 net/ipv4/ip_sockglue.c|   66 +++---
 net/ipv4/ipmr.c   |   14 -
 net/ipv4/netfilter/arp_tables.c   |   33 +--
 net/ipv4/netfilter/ip_tables.c|   29 +-
 net/ipv4/raw.c|8 
 net/ipv4/tcp.c|   30 +-
 net/ipv4/tcp_ipv4.c   |4 
 net/ipv4/udp.c|   11 -
 net/ipv4/udp_impl.h   |4 
 net/ipv6/ip6_flowlabel.c  |  317 --
 net/ipv6/ip6mr.c  |   17 -
 net/ipv6/ipv6_sockglue.c  |  203 +--
 net/ipv6/netfilter/ip6_tables.c   |   28 +-
 net/ipv6/raw.c|   10 
 net/ipv6/tcp_ipv6.c   |4 
 net/ipv6/udp.c|7 
 net/ipv6/udp_impl.h   |4 
 net/iucv/af_iucv.c|4 
 net/kcm/kcmsock.c |6 
 net/l2tp/l2tp_ppp.c   |4 
 net/llc/af_llc.c  |4 
 net/mptcp/protocol.c  |6 
 net/netfilter/ipvs/ip_vs_ctl.c|4 
 net/netfilter/nf_sockopt.c|2 
 net/netfilter/x_tables.c  |   20 -
 net/netlink/af_netlink.c  |4 
 net/netrom/af_netrom.c|4 
 net/nfc/llcp_sock.c   |6 
 net/packet/af_packet.c|   39 +--
 net/phonet/pep.c  |4 
 net/rds/af_rds.c  |   30 +-
 net/rds/rdma.c|   14 -
 net/rds/rds.h |6