2020-05-29 12:12:22

by Christoph Hellwig

[permalink] [raw]
Subject: remove kernel_setsockopt v4

Hi Dave and Marcelo,

now that only the dlm calls to sctp are left for kernel_setsockopt,
while we haven't really made much progress with the sctp setsockopt
refactoring, how about this small series that splits out a
sctp_setsockopt_bindx_kernel that takes a kernel space address array
to share more code as requested by Marcelo. This should fit in with
whatever variant of the refator of sctp setsockopt we go with, but
just solved the immediate problem for now.

Changes since v3:
- dropped all the merged patches, just sctp setsockopt left now
- factor out a new sctp_setsockopt_bindx_kernel helper instead of
duplicating a small amount of logic

Changes since v2:
- drop the separately merged kernel_getopt_removal
- drop the sctp patches, as there is conflicting cleanup going on
- add an additional ACK for the rxrpc changes

Changes since v1:
- use ->getname for sctp sockets in dlm
- add a new ->bind_add struct proto method for dlm/sctp
- switch the ipv6 and remaining sctp helpers to inline function so that
the ipv6 and sctp modules are not pulled in by any module that could
potentially use ipv6 or sctp connections
- remove arguments to various sock_* helpers that are always used with
the same constant arguments


2020-05-29 12:12:24

by Christoph Hellwig

[permalink] [raw]
Subject: [PATCH 1/4] sctp: add sctp_sock_set_nodelay

Add a helper to directly set the SCTP_NODELAY sockopt from kernel space
without going through a fake uaccess.

Signed-off-by: Christoph Hellwig <[email protected]>
---
fs/dlm/lowcomms.c | 10 ++--------
include/net/sctp/sctp.h | 7 +++++++
2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/fs/dlm/lowcomms.c b/fs/dlm/lowcomms.c
index 69333728d871b..9f1c3cdc9d653 100644
--- a/fs/dlm/lowcomms.c
+++ b/fs/dlm/lowcomms.c
@@ -914,7 +914,6 @@ static int sctp_bind_addrs(struct connection *con, uint16_t port)
static void sctp_connect_to_sock(struct connection *con)
{
struct sockaddr_storage daddr;
- int one = 1;
int result;
int addr_len;
struct socket *sock;
@@ -961,8 +960,7 @@ static void sctp_connect_to_sock(struct connection *con)
log_print("connecting to %d", con->nodeid);

/* Turn off Nagle's algorithm */
- kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
- sizeof(one));
+ sctp_sock_set_nodelay(sock->sk);

/*
* Make sock->ops->connect() function return in specified time,
@@ -1176,7 +1174,6 @@ static int sctp_listen_for_all(void)
struct socket *sock = NULL;
int result = -EINVAL;
struct connection *con = nodeid2con(0, GFP_NOFS);
- int one = 1;

if (!con)
return -ENOMEM;
@@ -1191,10 +1188,7 @@ static int sctp_listen_for_all(void)
}

sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
- result = kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
- sizeof(one));
- if (result < 0)
- log_print("Could not set SCTP NODELAY error %d\n", result);
+ sctp_sock_set_nodelay(sock->sk);

write_lock_bh(&sock->sk->sk_callback_lock);
/* Init con struct */
diff --git a/include/net/sctp/sctp.h b/include/net/sctp/sctp.h
index 3ab5c6bbb90bd..f8bcb75bb0448 100644
--- a/include/net/sctp/sctp.h
+++ b/include/net/sctp/sctp.h
@@ -615,4 +615,11 @@ static inline bool sctp_newsk_ready(const struct sock *sk)
return sock_flag(sk, SOCK_DEAD) || sk->sk_socket;
}

+static inline void sctp_sock_set_nodelay(struct sock *sk)
+{
+ lock_sock(sk);
+ sctp_sk(sk)->nodelay = true;
+ release_sock(sk);
+}
+
#endif /* __net_sctp_h__ */
--
2.26.2

2020-05-29 12:12:29

by Christoph Hellwig

[permalink] [raw]
Subject: [PATCH 4/4] net: remove kernel_setsockopt

No users left.

Signed-off-by: Christoph Hellwig <[email protected]>
---
include/linux/net.h | 2 --
net/socket.c | 31 -------------------------------
2 files changed, 33 deletions(-)

diff --git a/include/linux/net.h b/include/linux/net.h
index 74ef5d7315f70..e10f378194a59 100644
--- a/include/linux/net.h
+++ b/include/linux/net.h
@@ -303,8 +303,6 @@ int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
int flags);
int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
-int kernel_setsockopt(struct socket *sock, int level, int optname, char *optval,
- unsigned int optlen);
int kernel_sendpage(struct socket *sock, struct page *page, int offset,
size_t size, int flags);
int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
diff --git a/net/socket.c b/net/socket.c
index 81a98b6cbd087..976426d03f099 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -3624,37 +3624,6 @@ int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
}
EXPORT_SYMBOL(kernel_getpeername);

-/**
- * kernel_setsockopt - set a socket option (kernel space)
- * @sock: socket
- * @level: API level (SOL_SOCKET, ...)
- * @optname: option tag
- * @optval: option value
- * @optlen: option length
- *
- * Returns 0 or an error.
- */
-
-int kernel_setsockopt(struct socket *sock, int level, int optname,
- char *optval, unsigned int optlen)
-{
- mm_segment_t oldfs = get_fs();
- char __user *uoptval;
- int err;
-
- uoptval = (char __user __force *) optval;
-
- set_fs(KERNEL_DS);
- if (level == SOL_SOCKET)
- err = sock_setsockopt(sock, level, optname, uoptval, optlen);
- else
- err = sock->ops->setsockopt(sock, level, optname, uoptval,
- optlen);
- set_fs(oldfs);
- return err;
-}
-EXPORT_SYMBOL(kernel_setsockopt);
-
/**
* kernel_sendpage - send a &page through a socket (kernel space)
* @sock: socket
--
2.26.2

2020-05-29 12:13:04

by Christoph Hellwig

[permalink] [raw]
Subject: [PATCH 3/4] net: add a new bind_add method

The SCTP protocol allows to bind multiple address to a socket. That
feature is currently only exposed as a socket option. Add a bind_add
method struct proto that allows to bind additional addresses, and
switch the dlm code to use the method instead of going through the
socket option from kernel space.

Signed-off-by: Christoph Hellwig <[email protected]>
---
fs/dlm/lowcomms.c | 9 +++------
include/net/sock.h | 6 +++++-
net/core/sock.c | 8 ++++++++
net/sctp/socket.c | 14 ++++++++++++++
4 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/fs/dlm/lowcomms.c b/fs/dlm/lowcomms.c
index 9f1c3cdc9d653..3543a8fec9075 100644
--- a/fs/dlm/lowcomms.c
+++ b/fs/dlm/lowcomms.c
@@ -882,6 +882,7 @@ static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
static int sctp_bind_addrs(struct connection *con, uint16_t port)
{
struct sockaddr_storage localaddr;
+ struct sockaddr *addr = (struct sockaddr *)&localaddr;
int i, addr_len, result = 0;

for (i = 0; i < dlm_local_count; i++) {
@@ -889,13 +890,9 @@ static int sctp_bind_addrs(struct connection *con, uint16_t port)
make_sockaddr(&localaddr, port, &addr_len);

if (!i)
- result = kernel_bind(con->sock,
- (struct sockaddr *)&localaddr,
- addr_len);
+ result = kernel_bind(con->sock, addr, addr_len);
else
- result = kernel_setsockopt(con->sock, SOL_SCTP,
- SCTP_SOCKOPT_BINDX_ADD,
- (char *)&localaddr, addr_len);
+ result = sock_bind_add(con->sock->sk, addr, addr_len);

if (result < 0) {
log_print("Can't bind to %d addr number %d, %d.\n",
diff --git a/include/net/sock.h b/include/net/sock.h
index d994daa418ec2..6e9f713a78607 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1156,7 +1156,9 @@ struct proto {
int (*sendpage)(struct sock *sk, struct page *page,
int offset, size_t size, int flags);
int (*bind)(struct sock *sk,
- struct sockaddr *uaddr, int addr_len);
+ struct sockaddr *addr, int addr_len);
+ int (*bind_add)(struct sock *sk,
+ struct sockaddr *addr, int addr_len);

int (*backlog_rcv) (struct sock *sk,
struct sk_buff *skb);
@@ -2698,4 +2700,6 @@ void sock_set_reuseaddr(struct sock *sk);
void sock_set_reuseport(struct sock *sk);
void sock_set_sndtimeo(struct sock *sk, s64 secs);

+int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len);
+
#endif /* _SOCK_H */
diff --git a/net/core/sock.c b/net/core/sock.c
index 2ca3425b519c0..61ec573221a60 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3712,3 +3712,11 @@ bool sk_busy_loop_end(void *p, unsigned long start_time)
}
EXPORT_SYMBOL(sk_busy_loop_end);
#endif /* CONFIG_NET_RX_BUSY_POLL */
+
+int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len)
+{
+ if (!sk->sk_prot->bind_add)
+ return -EOPNOTSUPP;
+ return sk->sk_prot->bind_add(sk, addr, addr_len);
+}
+EXPORT_SYMBOL(sock_bind_add);
diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index 6e745ac3c4a59..d57e1a002ffc8 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -1052,6 +1052,18 @@ static int sctp_setsockopt_bindx(struct sock *sk,
return err;
}

+static int sctp_bind_add(struct sock *sk, struct sockaddr *addrs,
+ int addrlen)
+{
+ int err;
+
+ lock_sock(sk);
+ err = sctp_setsockopt_bindx_kernel(sk, addrs, addrlen,
+ SCTP_BINDX_ADD_ADDR);
+ release_sock(sk);
+ return err;
+}
+
static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
const union sctp_addr *daddr,
const struct sctp_initmsg *init,
@@ -9620,6 +9632,7 @@ struct proto sctp_prot = {
.sendmsg = sctp_sendmsg,
.recvmsg = sctp_recvmsg,
.bind = sctp_bind,
+ .bind_add = sctp_bind_add,
.backlog_rcv = sctp_backlog_rcv,
.hash = sctp_hash,
.unhash = sctp_unhash,
@@ -9662,6 +9675,7 @@ struct proto sctpv6_prot = {
.sendmsg = sctp_sendmsg,
.recvmsg = sctp_recvmsg,
.bind = sctp_bind,
+ .bind_add = sctp_bind_add,
.backlog_rcv = sctp_backlog_rcv,
.hash = sctp_hash,
.unhash = sctp_unhash,
--
2.26.2

2020-05-29 12:15:35

by Christoph Hellwig

[permalink] [raw]
Subject: [PATCH 2/4] sctp: refactor sctp_setsockopt_bindx

Split out a sctp_setsockopt_bindx_kernel that takes a kernel pointer
to the sockaddr and make sctp_setsockopt_bindx a small wrapper around
it. This prepares for adding a new bind_add proto op.

Signed-off-by: Christoph Hellwig <[email protected]>
---
net/sctp/socket.c | 61 ++++++++++++++++++++++-------------------------
1 file changed, 28 insertions(+), 33 deletions(-)

diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index 827a9903ee288..6e745ac3c4a59 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -972,23 +972,22 @@ int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
* it.
*
* sk The sk of the socket
- * addrs The pointer to the addresses in user land
+ * addrs The pointer to the addresses
* addrssize Size of the addrs buffer
* op Operation to perform (add or remove, see the flags of
* sctp_bindx)
*
* Returns 0 if ok, <0 errno code on error.
*/
-static int sctp_setsockopt_bindx(struct sock *sk,
- struct sockaddr __user *addrs,
- int addrs_size, int op)
+static int sctp_setsockopt_bindx_kernel(struct sock *sk,
+ struct sockaddr *addrs, int addrs_size,
+ int op)
{
- struct sockaddr *kaddrs;
int err;
int addrcnt = 0;
int walk_size = 0;
struct sockaddr *sa_addr;
- void *addr_buf;
+ void *addr_buf = addrs;
struct sctp_af *af;

pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
@@ -997,17 +996,10 @@ static int sctp_setsockopt_bindx(struct sock *sk,
if (unlikely(addrs_size <= 0))
return -EINVAL;

- kaddrs = memdup_user(addrs, addrs_size);
- if (IS_ERR(kaddrs))
- return PTR_ERR(kaddrs);
-
/* Walk through the addrs buffer and count the number of addresses. */
- addr_buf = kaddrs;
while (walk_size < addrs_size) {
- if (walk_size + sizeof(sa_family_t) > addrs_size) {
- kfree(kaddrs);
+ if (walk_size + sizeof(sa_family_t) > addrs_size)
return -EINVAL;
- }

sa_addr = addr_buf;
af = sctp_get_af_specific(sa_addr->sa_family);
@@ -1015,10 +1007,8 @@ static int sctp_setsockopt_bindx(struct sock *sk,
/* If the address family is not supported or if this address
* causes the address buffer to overflow return EINVAL.
*/
- if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
- kfree(kaddrs);
+ if (!af || (walk_size + af->sockaddr_len) > addrs_size)
return -EINVAL;
- }
addrcnt++;
addr_buf += af->sockaddr_len;
walk_size += af->sockaddr_len;
@@ -1029,31 +1019,36 @@ static int sctp_setsockopt_bindx(struct sock *sk,
case SCTP_BINDX_ADD_ADDR:
/* Allow security module to validate bindx addresses. */
err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
- (struct sockaddr *)kaddrs,
- addrs_size);
+ addrs, addrs_size);
if (err)
- goto out;
- err = sctp_bindx_add(sk, kaddrs, addrcnt);
+ return err;
+ err = sctp_bindx_add(sk, addrs, addrcnt);
if (err)
- goto out;
- err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
- break;
-
+ return err;
+ return sctp_send_asconf_add_ip(sk, addrs, addrcnt);
case SCTP_BINDX_REM_ADDR:
- err = sctp_bindx_rem(sk, kaddrs, addrcnt);
+ err = sctp_bindx_rem(sk, addrs, addrcnt);
if (err)
- goto out;
- err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
- break;
+ return err;
+ return sctp_send_asconf_del_ip(sk, addrs, addrcnt);

default:
- err = -EINVAL;
- break;
+ return -EINVAL;
}
+}

-out:
- kfree(kaddrs);
+static int sctp_setsockopt_bindx(struct sock *sk,
+ struct sockaddr __user *addrs,
+ int addrs_size, int op)
+{
+ struct sockaddr *kaddrs;
+ int err;

+ kaddrs = memdup_user(addrs, addrs_size);
+ if (IS_ERR(kaddrs))
+ return PTR_ERR(kaddrs);
+ err = sctp_setsockopt_bindx_kernel(sk, kaddrs, addrs_size, op);
+ kfree(kaddrs);
return err;
}

--
2.26.2

2020-05-29 12:30:02

by David Laight

[permalink] [raw]
Subject: RE: [PATCH 4/4] net: remove kernel_setsockopt

From: Christoph Hellwig
> Sent: 29 May 2020 13:10
>
> No users left.

There is no point even proposing this until all the changes to remove
its use have made it at least as far into 'net-next' and probably 'net'.

David

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

2020-05-29 12:31:39

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH 4/4] net: remove kernel_setsockopt

On Fri, May 29, 2020 at 12:27:12PM +0000, David Laight wrote:
> From: Christoph Hellwig
> > Sent: 29 May 2020 13:10
> >
> > No users left.
>
> There is no point even proposing this until all the changes to remove
> its use have made it at least as far into 'net-next' and probably 'net'.

If you look at net-next, the only two users left are the two removed in
this series.

2020-05-29 16:07:22

by Marcelo Ricardo Leitner

[permalink] [raw]
Subject: Re: [PATCH 1/4] sctp: add sctp_sock_set_nodelay

On Fri, May 29, 2020 at 02:09:40PM +0200, Christoph Hellwig wrote:
> Add a helper to directly set the SCTP_NODELAY sockopt from kernel space
> without going through a fake uaccess.
>
> Signed-off-by: Christoph Hellwig <[email protected]>

Acked-by: Marcelo Ricardo Leitner <[email protected]>

I'm taking the action item to make sctp_setsockopt_nodelay() use the
new helper. Will likely create a __ variant of it, due to sock lock.

> ---
> fs/dlm/lowcomms.c | 10 ++--------
> include/net/sctp/sctp.h | 7 +++++++
> 2 files changed, 9 insertions(+), 8 deletions(-)
>
> diff --git a/fs/dlm/lowcomms.c b/fs/dlm/lowcomms.c
> index 69333728d871b..9f1c3cdc9d653 100644
> --- a/fs/dlm/lowcomms.c
> +++ b/fs/dlm/lowcomms.c
> @@ -914,7 +914,6 @@ static int sctp_bind_addrs(struct connection *con, uint16_t port)
> static void sctp_connect_to_sock(struct connection *con)
> {
> struct sockaddr_storage daddr;
> - int one = 1;
> int result;
> int addr_len;
> struct socket *sock;
> @@ -961,8 +960,7 @@ static void sctp_connect_to_sock(struct connection *con)
> log_print("connecting to %d", con->nodeid);
>
> /* Turn off Nagle's algorithm */
> - kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
> - sizeof(one));
> + sctp_sock_set_nodelay(sock->sk);
>
> /*
> * Make sock->ops->connect() function return in specified time,
> @@ -1176,7 +1174,6 @@ static int sctp_listen_for_all(void)
> struct socket *sock = NULL;
> int result = -EINVAL;
> struct connection *con = nodeid2con(0, GFP_NOFS);
> - int one = 1;
>
> if (!con)
> return -ENOMEM;
> @@ -1191,10 +1188,7 @@ static int sctp_listen_for_all(void)
> }
>
> sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
> - result = kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
> - sizeof(one));
> - if (result < 0)
> - log_print("Could not set SCTP NODELAY error %d\n", result);
> + sctp_sock_set_nodelay(sock->sk);
>
> write_lock_bh(&sock->sk->sk_callback_lock);
> /* Init con struct */
> diff --git a/include/net/sctp/sctp.h b/include/net/sctp/sctp.h
> index 3ab5c6bbb90bd..f8bcb75bb0448 100644
> --- a/include/net/sctp/sctp.h
> +++ b/include/net/sctp/sctp.h
> @@ -615,4 +615,11 @@ static inline bool sctp_newsk_ready(const struct sock *sk)
> return sock_flag(sk, SOCK_DEAD) || sk->sk_socket;
> }
>
> +static inline void sctp_sock_set_nodelay(struct sock *sk)
> +{
> + lock_sock(sk);
> + sctp_sk(sk)->nodelay = true;
> + release_sock(sk);
> +}
> +
> #endif /* __net_sctp_h__ */
> --
> 2.26.2
>

2020-05-29 16:09:03

by Marcelo Ricardo Leitner

[permalink] [raw]
Subject: Re: [PATCH 3/4] net: add a new bind_add method

On Fri, May 29, 2020 at 02:09:42PM +0200, Christoph Hellwig wrote:
> The SCTP protocol allows to bind multiple address to a socket. That
> feature is currently only exposed as a socket option. Add a bind_add
> method struct proto that allows to bind additional addresses, and
> switch the dlm code to use the method instead of going through the
> socket option from kernel space.
>
> Signed-off-by: Christoph Hellwig <[email protected]>

Acked-by: Marcelo Ricardo Leitner <[email protected]>

Even though checkpatch complained about bad alignment here:
> +static int sctp_bind_add(struct sock *sk, struct sockaddr *addrs,
> + int addrlen)

2020-05-29 16:10:24

by Marcelo Ricardo Leitner

[permalink] [raw]
Subject: Re: [PATCH 2/4] sctp: refactor sctp_setsockopt_bindx

On Fri, May 29, 2020 at 02:09:41PM +0200, Christoph Hellwig wrote:
> Split out a sctp_setsockopt_bindx_kernel that takes a kernel pointer
> to the sockaddr and make sctp_setsockopt_bindx a small wrapper around
> it. This prepares for adding a new bind_add proto op.
>
> Signed-off-by: Christoph Hellwig <[email protected]>

Acked-by: Marcelo Ricardo Leitner <[email protected]>

> ---
> net/sctp/socket.c | 61 ++++++++++++++++++++++-------------------------
> 1 file changed, 28 insertions(+), 33 deletions(-)
>
> diff --git a/net/sctp/socket.c b/net/sctp/socket.c
> index 827a9903ee288..6e745ac3c4a59 100644
> --- a/net/sctp/socket.c
> +++ b/net/sctp/socket.c
> @@ -972,23 +972,22 @@ int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
> * it.
> *
> * sk The sk of the socket
> - * addrs The pointer to the addresses in user land
> + * addrs The pointer to the addresses
> * addrssize Size of the addrs buffer
> * op Operation to perform (add or remove, see the flags of
> * sctp_bindx)
> *
> * Returns 0 if ok, <0 errno code on error.
> */
> -static int sctp_setsockopt_bindx(struct sock *sk,
> - struct sockaddr __user *addrs,
> - int addrs_size, int op)
> +static int sctp_setsockopt_bindx_kernel(struct sock *sk,
> + struct sockaddr *addrs, int addrs_size,
> + int op)
> {
> - struct sockaddr *kaddrs;
> int err;
> int addrcnt = 0;
> int walk_size = 0;
> struct sockaddr *sa_addr;
> - void *addr_buf;
> + void *addr_buf = addrs;
> struct sctp_af *af;
>
> pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
> @@ -997,17 +996,10 @@ static int sctp_setsockopt_bindx(struct sock *sk,
> if (unlikely(addrs_size <= 0))
> return -EINVAL;
>
> - kaddrs = memdup_user(addrs, addrs_size);
> - if (IS_ERR(kaddrs))
> - return PTR_ERR(kaddrs);
> -
> /* Walk through the addrs buffer and count the number of addresses. */
> - addr_buf = kaddrs;
> while (walk_size < addrs_size) {
> - if (walk_size + sizeof(sa_family_t) > addrs_size) {
> - kfree(kaddrs);
> + if (walk_size + sizeof(sa_family_t) > addrs_size)
> return -EINVAL;
> - }
>
> sa_addr = addr_buf;
> af = sctp_get_af_specific(sa_addr->sa_family);
> @@ -1015,10 +1007,8 @@ static int sctp_setsockopt_bindx(struct sock *sk,
> /* If the address family is not supported or if this address
> * causes the address buffer to overflow return EINVAL.
> */
> - if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
> - kfree(kaddrs);
> + if (!af || (walk_size + af->sockaddr_len) > addrs_size)
> return -EINVAL;
> - }
> addrcnt++;
> addr_buf += af->sockaddr_len;
> walk_size += af->sockaddr_len;
> @@ -1029,31 +1019,36 @@ static int sctp_setsockopt_bindx(struct sock *sk,
> case SCTP_BINDX_ADD_ADDR:
> /* Allow security module to validate bindx addresses. */
> err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
> - (struct sockaddr *)kaddrs,
> - addrs_size);
> + addrs, addrs_size);
> if (err)
> - goto out;
> - err = sctp_bindx_add(sk, kaddrs, addrcnt);
> + return err;
> + err = sctp_bindx_add(sk, addrs, addrcnt);
> if (err)
> - goto out;
> - err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
> - break;
> -
> + return err;
> + return sctp_send_asconf_add_ip(sk, addrs, addrcnt);
> case SCTP_BINDX_REM_ADDR:
> - err = sctp_bindx_rem(sk, kaddrs, addrcnt);
> + err = sctp_bindx_rem(sk, addrs, addrcnt);
> if (err)
> - goto out;
> - err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
> - break;
> + return err;
> + return sctp_send_asconf_del_ip(sk, addrs, addrcnt);
>
> default:
> - err = -EINVAL;
> - break;
> + return -EINVAL;
> }
> +}
>
> -out:
> - kfree(kaddrs);
> +static int sctp_setsockopt_bindx(struct sock *sk,
> + struct sockaddr __user *addrs,
> + int addrs_size, int op)
> +{
> + struct sockaddr *kaddrs;
> + int err;
>
> + kaddrs = memdup_user(addrs, addrs_size);
> + if (IS_ERR(kaddrs))
> + return PTR_ERR(kaddrs);
> + err = sctp_setsockopt_bindx_kernel(sk, kaddrs, addrs_size, op);
> + kfree(kaddrs);
> return err;
> }
>
> --
> 2.26.2
>

2020-05-29 16:12:28

by Marcelo Ricardo Leitner

[permalink] [raw]
Subject: Re: [PATCH 4/4] net: remove kernel_setsockopt

On Fri, May 29, 2020 at 02:09:43PM +0200, Christoph Hellwig wrote:
> No users left.
>
> Signed-off-by: Christoph Hellwig <[email protected]>

Reviewed-by: Marcelo Ricardo Leitner <[email protected]>

Thanks.

> ---
> include/linux/net.h | 2 --
> net/socket.c | 31 -------------------------------
> 2 files changed, 33 deletions(-)
>
> diff --git a/include/linux/net.h b/include/linux/net.h
> index 74ef5d7315f70..e10f378194a59 100644
> --- a/include/linux/net.h
> +++ b/include/linux/net.h
> @@ -303,8 +303,6 @@ int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
> int flags);
> int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
> int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
> -int kernel_setsockopt(struct socket *sock, int level, int optname, char *optval,
> - unsigned int optlen);
> int kernel_sendpage(struct socket *sock, struct page *page, int offset,
> size_t size, int flags);
> int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
> diff --git a/net/socket.c b/net/socket.c
> index 81a98b6cbd087..976426d03f099 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -3624,37 +3624,6 @@ int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
> }
> EXPORT_SYMBOL(kernel_getpeername);
>
> -/**
> - * kernel_setsockopt - set a socket option (kernel space)
> - * @sock: socket
> - * @level: API level (SOL_SOCKET, ...)
> - * @optname: option tag
> - * @optval: option value
> - * @optlen: option length
> - *
> - * Returns 0 or an error.
> - */
> -
> -int kernel_setsockopt(struct socket *sock, int level, int optname,
> - char *optval, unsigned int optlen)
> -{
> - mm_segment_t oldfs = get_fs();
> - char __user *uoptval;
> - int err;
> -
> - uoptval = (char __user __force *) optval;
> -
> - set_fs(KERNEL_DS);
> - if (level == SOL_SOCKET)
> - err = sock_setsockopt(sock, level, optname, uoptval, optlen);
> - else
> - err = sock->ops->setsockopt(sock, level, optname, uoptval,
> - optlen);
> - set_fs(oldfs);
> - return err;
> -}
> -EXPORT_SYMBOL(kernel_setsockopt);
> -
> /**
> * kernel_sendpage - send a &page through a socket (kernel space)
> * @sock: socket
> --
> 2.26.2
>

2020-05-29 18:14:04

by David Teigland

[permalink] [raw]
Subject: Re: [PATCH 1/4] sctp: add sctp_sock_set_nodelay

On Fri, May 29, 2020 at 02:09:40PM +0200, Christoph Hellwig wrote:
> Add a helper to directly set the SCTP_NODELAY sockopt from kernel space
> without going through a fake uaccess.

Ack, they look fine to me, thanks.
Dave

2020-05-29 20:13:43

by David Miller

[permalink] [raw]
Subject: Re: remove kernel_setsockopt v4

From: Christoph Hellwig <[email protected]>
Date: Fri, 29 May 2020 14:09:39 +0200

> now that only the dlm calls to sctp are left for kernel_setsockopt,
> while we haven't really made much progress with the sctp setsockopt
> refactoring, how about this small series that splits out a
> sctp_setsockopt_bindx_kernel that takes a kernel space address array
> to share more code as requested by Marcelo. This should fit in with
> whatever variant of the refator of sctp setsockopt we go with, but
> just solved the immediate problem for now.
...

Series applied, thanks.

2020-06-01 08:29:26

by David Laight

[permalink] [raw]
Subject: RE: [PATCH 2/4] sctp: refactor sctp_setsockopt_bindx

From: Marcelo Ricardo Leitner
> Sent: 29 May 2020 17:06
> On Fri, May 29, 2020 at 02:09:41PM +0200, Christoph Hellwig wrote:
> > Split out a sctp_setsockopt_bindx_kernel that takes a kernel pointer
> > to the sockaddr and make sctp_setsockopt_bindx a small wrapper around
> > it. This prepares for adding a new bind_add proto op.
> >
> > Signed-off-by: Christoph Hellwig <[email protected]>
>
> Acked-by: Marcelo Ricardo Leitner <[email protected]>
>
> > ---
> > net/sctp/socket.c | 61 ++++++++++++++++++++++-------------------------
> > 1 file changed, 28 insertions(+), 33 deletions(-)
> >
> > diff --git a/net/sctp/socket.c b/net/sctp/socket.c
> > index 827a9903ee288..6e745ac3c4a59 100644
> > --- a/net/sctp/socket.c
> > +++ b/net/sctp/socket.c
> > @@ -972,23 +972,22 @@ int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
> > * it.
> > *
> > * sk The sk of the socket
> > - * addrs The pointer to the addresses in user land
> > + * addrs The pointer to the addresses
> > * addrssize Size of the addrs buffer
> > * op Operation to perform (add or remove, see the flags of
> > * sctp_bindx)
> > *
> > * Returns 0 if ok, <0 errno code on error.
> > */
> > -static int sctp_setsockopt_bindx(struct sock *sk,
> > - struct sockaddr __user *addrs,
> > - int addrs_size, int op)
> > +static int sctp_setsockopt_bindx_kernel(struct sock *sk,
const
> > + struct sockaddr *addrs, int addrs_size,
> > + int op)

The list of addresses ought to be 'const'.

IIRC that requires the test for 'port == 0' be moved down a few layers.

David

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