nfsdctl: allow it to handle rpcbind registration and deregistration
Subsystems (2)
Jeff Layton
maintainerv2 cover nfsdctl: allow it to handle rpcbind registration and deregistration
This version fixes some problems that Sashiko flagged in review.
This patchset adds support for NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND, which tells the kernel to not do rpcbind registration and to instead allow userland to handle it. After setting listeners, nfsdctl will then call out to rpcbind using the normal libtirpc client interfaces.
This is a companion set to the kernel set I recently posted (and which also should have been marked RFC).
---
Changes in v2:
- Fixes for bugs v1 introduced are folded into the patches that
introduced them. Three pre-existing bugs found along the way are
separate patches at the end, so the set is 10 patches instead of 7.
- "register nfsd listeners with rpcbind": register only once the listener_set reply has been parsed. On a reply carrying no rpcbind attributes the sweep cleared every NFS and NFSACL entry and put
nothing back, leaving a running server advertising nothing. Also
abort rather than send a listener_set that lost the ownership flag.
- "clear rpcbind entries when the serv is torn down" (was "... when the last thread stops"): also sweep on the autostart teardown path.
- New, pre-existing: fix a heap overflow in autostart_func(). A failed listener set zeroed the pool count before the thread array was sized,
but the array is filled from the nfs.conf list, so any [nfsd] threads setting wrote past the allocation.
- New, pre-existing: reject a listener insertion when the table is full, instead of writing one element past nfsd_sockets[].
- New, pre-existing: check the nla_put() calls in set_listeners().
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jeff Layton (10):
nfsdctl: sync nfsd_netlink.h with the kernel
nfsdctl: probe the policy of any command
nfsdctl: share the listener nest parser
nfsdctl: add rpcbind registration helpers
nfsdctl: register nfsd listeners with rpcbind
nfsdctl: clear rpcbind entries when the serv is torn down
nfsdctl: add missing break in update_listeners()
nfsdctl: fix heap overflow in autostart_func() listener cleanup
nfsdctl: reject a listener insertion when the table is full
nfsdctl: check the netlink attribute puts in set_listeners()
configure.ac | 2 +- support/include/nfsd_netlink.h | 18 +++ utils/nfsdctl/Makefile.am | 10 +- utils/nfsdctl/nfsdctl.8 | 16 +++ utils/nfsdctl/nfsdctl.adoc | 17 +++ utils/nfsdctl/nfsdctl.c | 256 +++++++++++++++++++++++++++++++++-------- utils/nfsdctl/rpcbind.c | 251 ++++++++++++++++++++++++++++++++++++++++ utils/nfsdctl/rpcbind.h | 42 +++++++ 8 files changed, 562 insertions(+), 50 deletions(-)
---
base-commit: 630393e9c73667b3fda6a7507cb5171ec0ec38dc
change-id: 20260908-nfsdctl-rpcb-adc5a1fc090b
Best regards,
Signature · 1 line
Jeff Layton <[email protected]>
Jeff Layton
maintainerRFC PATCH v2 1/10 nfsdctl: sync nfsd_netlink.h with the kernel
Import the attributes that let a caller own rpcbind registration:
- NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND on the request
- NFSD_A_SERVER_SOCK_RPCBIND, and the NFSD_A_RPCBIND_* nest, on the reply
The file comes from include/uapi/linux/nfsd_netlink.h. No code reads the new attributes yet.
Bump the USE_SYSTEM_NFSD_NETLINK_H probe to one of the new attributes. It tested NFSD_CMD_SERVER_STATS_GET, which has been in the uapi header since v6.9, so any host with headers that new picks the system copy and loses everything imported here.
--- configure.ac | 2 +- support/include/nfsd_netlink.h | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-)
configure.ac +1 -1
@@ -259,7 +259,7 @@ AC_CHECK_HEADERS(linux/nfsd_netlink.h) # ensure the system netlink headers have the latest features AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <linux/nfsd_netlink.h>]], - [[int foo = NFSD_CMD_SERVER_STATS_GET;]])], + [[int foo = NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND;]])], [AC_DEFINE([USE_SYSTEM_NFSD_NETLINK_H], 1, ["Use system's linux/nfsd_netlink.h"])]) AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <linux/lockd_netlink.h>]],
support/include/nfsd_netlink.h +18 -0
@@ -50,6 +50,13 @@ enum nfsd_xprtsec_mode { NFSD_XPRTSEC_MODE_MTLS = 4, }; +/* +* Constraints that apply to an rpcbind registration. +*/ +enum nfsd_rpcbind_flags { +NFSD_RPCBIND_FLAGS_NO_UDP =1 , +}; +enum {NFSD_A_CACHE_NOTIFY_CACHE_TYPE =1 ,
@@ -113,8 +120,19 @@ enum {NFSD_A_SOCK_MAX = (__NFSD_A_SOCK_MAX -1 ) }; +enum { +NFSD_A_RPCBIND_PROGRAM =1 , +NFSD_A_RPCBIND_VERSION , +NFSD_A_RPCBIND_FLAGS , + + __NFSD_A_RPCBIND_MAX, +NFSD_A_RPCBIND_MAX = (__NFSD_A_RPCBIND_MAX -1 ) +}; +enum {NFSD_A_SERVER_SOCK_ADDR =1 , +NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND , +NFSD_A_SERVER_SOCK_RPCBIND , __NFSD_A_SERVER_SOCK_MAX,NFSD_A_SERVER_SOCK_MAX = (__NFSD_A_SERVER_SOCK_MAX -1 )
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 2/10 nfsdctl: probe the policy of any command
In reply to Jeff Laytonquery_nfsd_nl_policy() asks CTRL_CMD_GETPOLICY about NFSD_CMD_THREADS_SET only. getpolicy_handler() writes the highest attribute straight into a global. A second command cannot use either function.
query_nfsd_nl_cmd_policy() now takes the command number. getpolicy_handler() now writes through its callback argument. query_nfsd_nl_policy() keeps its name and still probes THREADS_SET.
No functional change.
--- utils/nfsdctl/nfsdctl.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
utils/nfsdctl/nfsdctl.c +13 -5
@@ -500,6 +500,7 @@ static int nfsd_nl_family_setup(struct nl_sock *sock)static int getpolicy_handler (struct nl_msg *msg,void *arg) {struct genlmsghdr *gnlh =nlmsg_data (nlmsg_hdr (msg)); +int *max_nlattr = arg;struct nlattr *attr;int rem;
@@ -512,8 +513,8 @@ static int getpolicy_handler(struct nl_msg *msg, void *arg)nla_for_each_nested (b, a, j) {int idx =nla_type (b); -if (nfsd_threads_max_nlattr < idx) - nfsd_threads_max_nlattr = idx; +if (*max_nlattr < idx) + *max_nlattr = idx; } } }
@@ -521,7 +522,8 @@ static int getpolicy_handler(struct nl_msg *msg, void *arg)return NL_SKIP ; } -static int query_nfsd_nl_policy (struct nl_sock *sock) +static int query_nfsd_nl_cmd_policy (struct nl_sock *sock,int cmd, +int *max_nlattr) {struct genlmsghdr *ghdr;struct nlmsghdr *nlh;
@@ -549,7 +551,7 @@ static int query_nfsd_nl_policy(struct nl_sock *sock) }nla_put_u16 (msg,CTRL_ATTR_FAMILY_ID , nfsd_nl_family); -nla_put_u32 (msg,CTRL_ATTR_OP ,NFSD_CMD_THREADS_SET ); +nla_put_u32 (msg,CTRL_ATTR_OP , cmd); ret =nl_send_auto (sock, msg);if (ret <0 )
@@ -559,7 +561,7 @@ static int query_nfsd_nl_policy(struct nl_sock *sock)nl_cb_err (cb,NL_CB_CUSTOM , error_handler, &ret);nl_cb_set (cb,NL_CB_FINISH ,NL_CB_CUSTOM , finish_handler, &ret);nl_cb_set (cb,NL_CB_ACK ,NL_CB_CUSTOM , ack_handler, &ret); -nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , getpolicy_handler,NULL ); +nl_cb_set (cb,NL_CB_VALID ,NL_CB_CUSTOM , getpolicy_handler, max_nlattr);while (ret >0 )nl_recvmsgs (sock, cb);
@@ -574,6 +576,12 @@ out:return ret; } +static int query_nfsd_nl_policy (struct nl_sock *sock) +{ +return query_nfsd_nl_cmd_policy (sock,NFSD_CMD_THREADS_SET , + &nfsd_threads_max_nlattr); +} +static void status_usage (void ) {printf ("Usage: %s status\n" , taskname);
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 3/10 nfsdctl: share the listener nest parser
In reply to Jeff Laytonparse_listener_get() unpacks a NFSD_A_SERVER_SOCK_ADDR nest inline. It also treats every top-level attribute as one of those nests. A LISTENER_GET reply carries nothing else, so the loop is correct today. A reply that mixes attributes would count the other attributes as listeners.
Move the nest walk into parse_sock_nest(). Skip an attribute that is not a listener. Stop at MAX_NFSD_SOCKETS.
No functional change.
--- utils/nfsdctl/nfsdctl.c | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-)
utils/nfsdctl/nfsdctl.c +27 -18
@@ -273,6 +273,28 @@ static void parse_version_get(struct genlmsghdr *gnlh) } } +/* Unpack one NFSD_A_SERVER_SOCK_ADDR nest. */ +static void parse_sock_nest (struct nlattr *attr,struct server_socket *sock) +{ +struct nlattr *a; +char *res; +int i; + +nla_for_each_nested (a, attr, i) { +switch (nla_type (a)) { +case NFSD_A_SOCK_TRANSPORT_NAME : + res =strncpy (sock->name,nla_data (a), +MAX_CLASS_NAME_LEN ); + res[MAX_CLASS_NAME_LEN -1 ] ='\0' ;// just to be sure +break ; +case NFSD_A_SOCK_ADDR : +memcpy (&sock->ss,nla_data (a),sizeof (sock->ss)); +break ; + } + sock->active = true; + } +} +static void parse_listener_get (struct genlmsghdr *gnlh) {struct nlattr *attr;
@@ -283,24 +305,11 @@ static void parse_listener_get(struct genlmsghdr *gnlh)nla_for_each_attr (attr,genlmsg_attrdata (gnlh,0 ),genlmsg_attrlen (gnlh,0 ), rem) { -struct nlattr *a; -char *res; -int i; - -nla_for_each_nested (a, attr, i) { -switch (nla_type (a)) { -case NFSD_A_SOCK_TRANSPORT_NAME : - res =strncpy (nfsd_sockets[idx].name,nla_data (a), -MAX_CLASS_NAME_LEN ); - res[MAX_CLASS_NAME_LEN -1 ] ='\0' ;// just to be sure -break ; -case NFSD_A_SOCK_ADDR : -memcpy (&nfsd_sockets[idx].ss,nla_data (a), -sizeof (nfsd_sockets[idx].ss)); -break ; - } - nfsd_sockets[idx].active = true; - } +if (nla_type (attr) !=NFSD_A_SERVER_SOCK_ADDR ) +continue ; +if (idx >=MAX_NFSD_SOCKETS ) +break ; +parse_sock_nest (attr, &nfsd_sockets[idx]); ++idx; } nfsd_socket_count = idx;
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 4/10 nfsdctl: add rpcbind registration helpers
In reply to Jeff LaytonAdd the code that calls the local rpcbind. The callers come next.
nfsd_rpcb_register() matches each listener against each program that the kernel reported. It calls rpcb_set() for every pair.
nfsd_rpcb_unset_all() clears every nfsd entry with nfs_svc_unregister(). That helper calls rpcb_unset() with no netconfig, so one call covers every netid.
Three rules match what the kernel did:
- nfsdctl registers the wildcard address with the listener's port. It does not register the address that the listener uses. __svc_rpcb_register4() and __svc_rpcb_register6() did the same.
- nfsdctl does not register an RDMA listener. svc_register() runs only from svc_setup_socket(), so an RDMA listener never reached rpcbind.
- nfsdctl does not register a program that carries the no-udp flag on udp or udp6. NFSv4 sets vs_need_cong_ctrl, so the kernel never advertised NFSv4 there.
A failed call writes a warning and does not stop the caller. NFSv4 does not need rpcbind. A v2 or v3 client that already knows the port does not need it either.
rpcb_set() and the netconfig database are TI-RPC only, so the registration side sits under HAVE_LIBTIRPC with a warning stub, matching getport.c and rpc_socket.c. nfsd_rpcb_unset_all() needs neither and stays unconditional; nfs_svc_unregister() has a pmap_unset() variant.
struct server_socket moves to the new header, because both files read it. The netlink spec generates nfsdctl.h, so the definition cannot live there.
--- utils/nfsdctl/Makefile.am | 10 +- utils/nfsdctl/nfsdctl.c | 13 +-- utils/nfsdctl/rpcbind.c | 251 ++++++++++++++++++++++++++++++++++++++++++++++ utils/nfsdctl/rpcbind.h | 42 ++++++++ 4 files changed, 300 insertions(+), 16 deletions(-)
utils/nfsdctl/Makefile.am +6 -4
@@ -5,9 +5,11 @@ EXTRA_DIST = $(man8_MANS) sbin_PROGRAMS = nfsdctl -noinst_HEADERS = nfsdctl.h -nfsdctl_SOURCES = nfsdctl.c -nfsdctl_CFLAGS = $(LIBNL3_CFLAGS) $(LIBNLGENL3_CFLAGS) $(LIBREADLINE_CFLAGS) -nfsdctl_LDADD = ../../support/nfs/libnfs.la $(LIBNL3_LIBS) $(LIBNLGENL3_LIBS) $(LIBREADLINE_LIBS) +noinst_HEADERS = nfsdctl.h rpcbind.h +nfsdctl_SOURCES = nfsdctl.c rpcbind.c +nfsdctl_CFLAGS = $(LIBNL3_CFLAGS) $(LIBNLGENL3_CFLAGS) $(LIBREADLINE_CFLAGS) \ + $(TIRPC_CFLAGS) +nfsdctl_LDADD = ../../support/nfs/libnfs.la $(LIBNL3_LIBS) $(LIBNLGENL3_LIBS) \ + $(LIBREADLINE_LIBS) $(LIBTIRPC) MAINTAINERCLEANFILES = Makefile.in
utils/nfsdctl/nfsdctl.c +1 -12
@@ -46,6 +46,7 @@#include "nfslib.h" #include "nfsdctl.h" +#include "rpcbind.h" #include "conffile.h" #include "xlog.h"
@@ -72,18 +73,6 @@ struct nfs_version {struct nfs_version nfsd_versions[MAX_NFS_VERSIONS ]; -/* -* All of the existing netids are short strings (3-4 chars), but let's allow -* for up to 16. -*/ -#define MAX_CLASS_NAME_LEN 16 - -struct server_socket { -struct sockaddr_storage ss; -char name[MAX_CLASS_NAME_LEN ]; -bool active; -}; -#define MAX_NFSD_SOCKETS 256int nfsd_socket_count;
utils/nfsdctl/rpcbind.c new file +251 -0
251 lines not on this page · raw message
utils/nfsdctl/rpcbind.h new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 5/10 nfsdctl: register nfsd listeners with rpcbind
In reply to Jeff LaytonThe kernel registered NFS and NFSACL under nfsd_mutex. Every call was synchronous. A local rpcbind that never replied therefore blocked every other NFSD netlink command, and tripped the hung-task watchdog.
A kernel that offers NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND makes no rpcbind call of its own. It answers a listener_set that carries the flag with the programs to register and the listeners that started. This program now does three steps instead:
1: send the flag
2: read the reply
3: register the listeners, where nothing holds a lock
Read the maximum attribute of LISTENER_SET to detect support. This adds no configuration. An older kernel does not offer the attribute, so that kernel keeps the registration. A build without TI-RPC has no rpcb_set(), so it reports no support either and leaves the kernel in charge.
nfsdctl clears every nfsd entry before it registers. rpcbind matches an unset on [program, version, netid] and takes no address, so an unset cannot drop one of two TCP ports. The sweep also replaces the svc_unregister() that svc_rpcb_setup() ran at serv creation. An sv_no_rpcbind serv no longer reaches that call.
That sweep makes step 2 load-bearing, so register only once the reply has been parsed. A reply that carries no rpcbind attributes would otherwise clear every NFS and NFSACL entry and put nothing back, leaving a running server that advertises nothing.
Abort if the flag does not fit in the message. Sending without it leaves the kernel owning rpcbind while this program rewrites the same entries.
The reply names only the listeners that started. A request that failed part way therefore registers what exists, not what the caller asked for.
--- utils/nfsdctl/nfsdctl.8 | 16 ++++++ utils/nfsdctl/nfsdctl.adoc | 17 ++++++ utils/nfsdctl/nfsdctl.c | 125 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 156 insertions(+), 2 deletions(-)
utils/nfsdctl/nfsdctl.8 +16 -0
@@ -337,6 +337,22 @@ interface to achieve the same goals. Most of the commands that query the NFS server can be run as an unprivileged user, but configuring the server typically requires an account with elevated privileges. +.SS "rpcbind registration" +.sp +On a kernel that supports it, nfsdctl registers the NFS and NFSACL programs +with the local rpcbind itself. The kernel made those calls while it held an +internal mutex, where a slow rpcbind blocked every other NFSD netlink +command. nfsdctl detects support and needs no configuration. +.sp +nfsdctl registers the listeners after it sets them, and it clears the +entries when the last thread stops. A failed registration is not fatal. The +server starts, but a client that looks the server up in rpcbind cannot find +it. NFSv4 does not need rpcbind, and a v2 or v3 client that already knows +the port does not need it either. +.sp +RDMA listeners are not registered, which matches what the kernel did. +.sp +lockd still registers NLM itself on every kernel. .SH "SEE ALSO" .sp nfs.conf(5), rpc.nfsd(8), rpc.mountd(8), exports(5), exportfs(8), rpc.rquotad(8), nfsstat(8), netconfig(5)
utils/nfsdctl/nfsdctl.adoc +17 -0
@@ -176,6 +176,23 @@ Most of the commands that query the NFS server can be run as an unprivileged user, but configuring the server typically requires an account with elevated privileges. +=== rpcbind registration + +On a kernel that supports it, nfsdctl registers the NFS and NFSACL programs +with the local rpcbind itself. The kernel made those calls while it held an +internal mutex, where a slow rpcbind blocked every other NFSD netlink +command. nfsdctl detects support and needs no configuration. + +nfsdctl registers the listeners after it sets them, and it clears the +entries when the last thread stops. A failed registration is not fatal. The +server starts, but a client that looks the server up in rpcbind cannot find +it. NFSv4 does not need rpcbind, and a v2 or v3 client that already knows +the port does not need it either. + +RDMA listeners are not registered, which matches what the kernel did. + +lockd still registers NLM itself on every kernel. + == SEE ALSO nfs.conf(5), rpc.nfsd(8), rpc.mountd(8), exports(5), exportfs(8), rpc.rquotad(8), nfsstat(8), netconfig(5)
utils/nfsdctl/nfsdctl.c +123 -2
179 lines not on this page · raw message
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 6/10 nfsdctl: clear rpcbind entries when the serv is torn down
In reply to Jeff Laytonnfsd destroys the serv when the last thread stops. The kernel used to clear the rpcbind entries on that path, through svc_rpcb_cleanup(). A serv that let this program own rpcbind does not clear them. The entries would then outlive the server and name a port that nothing listens on.
Clear them from "nfsdctl threads 0". nfs-server.service runs that command to stop the server. autostart_func() tears the serv back down the same way when a listener fails to start, so sweep there too.
zero_threads was true when any one pool argument was zero. That is the wrong test to hang the sweep off: "nfsdctl threads 8 0" empties pool 1, keeps the serv, and would have cleared every entry under a running server. Sum the pool counts instead.
--- utils/nfsdctl/nfsdctl.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-)
utils/nfsdctl/nfsdctl.c +28 -5
@@ -836,7 +836,7 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)uint8_t cmd =NFSD_CMD_THREADS_GET ;int *pool_threads =NULL ;int minthreads =-1 ; -int opt, pools =0 ; +int opt, pools =0 , ret;uuid_t fh_key;bool zero_threads = false;
@@ -877,7 +877,7 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)if (optind < argc) {char **targv = &argv[optind]; -int i; +int i, total =0 ; pools = argc - optind; pool_threads =alloca (pools *sizeof (*pool_threads));
@@ -893,13 +893,18 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv) } pool_threads[i] =strtol (targv[i], &endptr,0 ); -if (!pool_threads[i]) - zero_threads = true; + total += pool_threads[i];if (!endptr || *endptr !='\0' ) {xlog (L_ERROR ,"Invalid threads value %s." , argv[1 ]);return 1 ; } } + +/* +* The serv only goes away when no pool has a thread left. +* "threads 8 0" empties one pool and keeps the server up. +*/ + zero_threads = (total ==0 ); }/* check if there are active listeners added */ if (fetch_current_listeners (sock)) {
@@ -912,14 +917,25 @@ static int threads_func(struct nl_sock *sock, int argc, char **argv)* listener. If we ever add functionality to remove * listeners on an active server, we need to revisit this. */ +if (userspace_rpcbind_supported ()) +nfsd_rpcb_unset_all ();return 0 ; }xlog (L_ERROR ,"No active listeners added, not starting threads" );return 1 ; } -return threads_doit (sock, cmd,0 ,0 , pools, pool_threads,NULL , + ret =threads_doit (sock, cmd,0 ,0 , pools, pool_threads,NULL , minthreads, fh_key); + +/* +* The last thread destroys the serv, and a serv that never called +* rpcb_create_local() leaves its entries behind. +*/ +if (!ret && zero_threads &&userspace_rpcbind_supported ()) +nfsd_rpcb_unset_all (); + +return ret; }/*
@@ -2041,6 +2057,13 @@ static int autostart_func(struct nl_sock *sock, int argc, char ** argv) ret =threads_doit (sock,NFSD_CMD_THREADS_SET , grace, lease, pools, threads, scope, minthreads, fh_key); + +/* +* That teardown destroys the serv, and a serv that never called +* rpcb_create_local() leaves its entries behind. +*/ +if (!ret && !pools &&userspace_rpcbind_supported ()) +nfsd_rpcb_unset_all (); out:free (threads);return ret;
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 7/10 nfsdctl: add missing break in update_listeners()
In reply to Jeff LaytonThe AF_INET arm fell through into AF_INET6, so a v4 listener that matched on port and address was then re-tested as a sockaddr_in6 and skipped. -Wimplicit-fallthrough is not enabled, so nothing caught it.
Terminate every arm. The AF_INET6 fallthrough was harmless, since "default:" is empty, but it is a trap for the next case added.
--- utils/nfsdctl/nfsdctl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
utils/nfsdctl/nfsdctl.c +3 -1
@@ -1410,13 +1410,15 @@ static int update_listeners(const char *str)if (r4->sin_port != l4->sin_port ||memcmp (&r4->sin_addr, &l4->sin_addr,sizeof (l4->sin_addr)))continue ; +break ;case AF_INET6 :if (r6->sin6_port != l6->sin6_port ||memcmp (&r6->sin6_addr, &l6->sin6_addr,sizeof (l6->sin6_addr)) || !ipv6_is_enabled ())continue ; +break ;default : - +break ; } sock->active = (sign =='+' ); found = true;
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 8/10 nfsdctl: fix heap overflow in autostart_func() listener cleanup
In reply to Jeff LaytonThe listener-failure cleanup forced pools to 0 before sizing the array:
pools = thread_str ? thread_str->cnt : 1; if (failed_listeners) pools = 0; threads = calloc(pools, sizeof(int)); ... if (thread_str) TAILQ_FOREACH(n, &(thread_str->fields), link) threads[idx++] = strtol(n->field, &endptr, 0);
The TAILQ walk is driven by thread_str, not by pools, so it writes thread_str->cnt ints into a calloc(0) allocation. Any /etc/nfs.conf with a [nfsd] threads setting overflows the heap once a listener fails to come up.
Size the array from the list and clamp pools to 0 just before threads_doit(), which is the only consumer. The netlink message is unchanged.
Also switch the hash_fh_key_file() failure to goto out; it returned directly and leaked threads.
--- utils/nfsdctl/nfsdctl.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
utils/nfsdctl/nfsdctl.c +7 -7
@@ -2014,12 +2014,6 @@ static int autostart_func(struct nl_sock *sock, int argc, char ** argv) thread_str =conf_get_list ("nfsd" ,"threads" ); pools = thread_str ? thread_str->cnt :1 ; -/* if we fail to start one or more listeners, then cleanup by -* starting 0 knfsd threads -*/ -if (failed_listeners) - pools =0 ; - threads =calloc (pools,sizeof (int ));if (!threads)return -ENOMEM ;
@@ -2045,7 +2039,7 @@ static int autostart_func(struct nl_sock *sock, int argc, char ** argv)if (fh_key_file) { ret =hash_fh_key_file (fh_key_file, fh_key);if (ret) -return ret; + goto out; } lease =conf_get_num ("nfsd" ,"lease-time" ,0 );
@@ -2057,6 +2051,12 @@ static int autostart_func(struct nl_sock *sock, int argc, char ** argv) minthreads =-1 ; } +/* if we fail to start one or more listeners, then cleanup by +* starting 0 knfsd threads +*/ +if (failed_listeners) + pools =0 ; + ret =threads_doit (sock,NFSD_CMD_THREADS_SET , grace, lease, pools, threads, scope, minthreads, fh_key);
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 9/10 nfsdctl: reject a listener insertion when the table is full
In reply to Jeff Laytonparse_listener_get() stops at MAX_NFSD_SOCKETS and publishes nfsd_socket_count = MAX_NFSD_SOCKETS. update_listeners() then scans the whole table for a match, finds none because every slot is taken, and forms &nfsd_sockets[MAX_NFSD_SOCKETS] for the insert. The memcpy() of res->ai_addr writes one element past the array.
Reachable only with 256 listeners already up, and the count comes from the kernel rather than the network, so this is hardening.
--- utils/nfsdctl/nfsdctl.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
utils/nfsdctl/nfsdctl.c +9 -1
@@ -1425,8 +1425,16 @@ static int update_listeners(const char *str)break ; }if (!found && sign =='+' ) { -struct server_socket *sock = &nfsd_sockets[nfsd_socket_count]; +struct server_socket *sock; +if (nfsd_socket_count >=MAX_NFSD_SOCKETS ) { +xlog (L_ERROR ,"Too many listeners, max is %d" , +MAX_NFSD_SOCKETS ); +free (buf); +return -ENOSPC ; + } + + sock = &nfsd_sockets[nfsd_socket_count];memcpy (&sock->ss, res->ai_addr, res->ai_addrlen);strncpy (sock->name, netid,MAX_CLASS_NAME_LEN ); sock->name[MAX_CLASS_NAME_LEN -1 ] ='\0' ;
Signature · 1 line
2.55.0
Jeff Layton
maintainerRFC PATCH v2 10/10 nfsdctl: check the netlink attribute puts in set_listeners()
In reply to Jeff Laytonnla_nest_start() only reserves a 4-byte header, so it still succeeds on a message that has no room for the 128-byte sockaddr that follows. The puts inside the nest went unchecked, so an over-full message was sent with silently truncated listener nests. The kernel then rejects it with EINVAL, which says nothing about what went wrong.
MAX_NFSD_SOCKETS is 256 and nlmsg_alloc() defaults to one page, so roughly 28 listeners overflow.
--- utils/nfsdctl/nfsdctl.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
utils/nfsdctl/nfsdctl.c +8 -2
@@ -1493,8 +1493,14 @@ static int set_listeners(struct nl_sock *sock) goto out; } -nla_put (msg,NFSD_A_SOCK_ADDR ,sizeof (sock->ss), &sock->ss); -nla_put_string (msg,NFSD_A_SOCK_TRANSPORT_NAME , sock->name); +if (nla_put (msg,NFSD_A_SOCK_ADDR ,sizeof (sock->ss), + &sock->ss) || +nla_put_string (msg,NFSD_A_SOCK_TRANSPORT_NAME , + sock->name)) { +xlog (L_ERROR ,"Too many listeners for one netlink message" ); + ret =1 ; + goto out; + }nla_nest_end (msg, a); }
Signature · 1 line
2.55.0
Jeff Layton
maintainerQuoted 54 lines · Jeff Layton
This version fixes some problems that Sashiko flagged in review.
This patchset adds support for
NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND, which tells the kernel to not do rpcbind registration and to instead allow userland to handle it. After setting listeners, nfsdctl will then call out to rpcbind using the normal libtirpc client interfaces.This is a companion set to the kernel set I recently posted (and which also should have been marked RFC).
Signed-off-by Jeff Layton <[email protected]>---
Changes in v2:
- Fixes for bugs v1 introduced are folded into the patches that
introduced them. Three pre-existing bugs found along the way are
separate patches at the end, so the set is 10 patches instead of 7.
- "register nfsd listeners with rpcbind": register only once thelistener_setreply has been parsed. On a reply carrying no rpcbind attributes the sweep cleared every NFS and NFSACL entry and put
nothing back, leaving a running server advertising nothing. Also
abort rather than send alistener_setthat lost the ownership flag.
- "clear rpcbind entries when the serv is torn down" (was "... when the last thread stops"): also sweep on the autostart teardown path.
- New, pre-existing: fix a heap overflow inautostart_func(). A failed listener set zeroed the pool count before the thread array was sized,
but the array is filled from the nfs.conf list, so any [nfsd] threads setting wrote past the allocation.
- New, pre-existing: reject a listener insertion when the table is full, instead of writing one element past nfsd_sockets[].
- New, pre-existing: check thenla_put()calls inset_listeners().
- Link to v1: https://lore.kernel.org/r/[email protected]---
Jeff Layton (10):
nfsdctl: syncnfsd_netlink.hwith the kernel
nfsdctl: probe the policy of any command
nfsdctl: share the listener nest parser
nfsdctl: add rpcbind registration helpers
nfsdctl: register nfsd listeners with rpcbind
nfsdctl: clear rpcbind entries when the serv is torn down
nfsdctl: add missing break inupdate_listeners()
nfsdctl: fix heap overflow inautostart_func()listener cleanup
nfsdctl: reject a listener insertion when the table is full
nfsdctl: check the netlink attribute puts inset_listeners()configure.ac | 2 +- support/include/nfsd_netlink.h | 18 +++ utils/nfsdctl/Makefile.am | 10 +- utils/nfsdctl/nfsdctl.8 | 16 +++ utils/nfsdctl/nfsdctl.adoc | 17 +++ utils/nfsdctl/nfsdctl.c | 256 +++++++++++++++++++++++++++++++++-------- utils/nfsdctl/rpcbind.c | 251 ++++++++++++++++++++++++++++++++++++++++ utils/nfsdctl/rpcbind.h | 42 +++++++ 8 files changed, 562 insertions(+), 50 deletions(-)
Quoted 5 lines
---
base-commit:630393e9c73667b3fda6a7507cb5171ec0ec38dc
change-id: 20260908-nfsdctl-rpcb-adc5a1fc090bBest regards,
Forgot to remove the "RFC" from the subject line for this one. Consider this an "official" submission.
Thanks,
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 11 lines · Jeff Layton
This version fixes some problems that Sashiko flagged in review.
This patchset adds support for
NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND, which tells the kernel to not do rpcbind registration and to instead allow userland to handle it. After setting listeners, nfsdctl will then call out to rpcbind using the normal libtirpc client interfaces.This is a companion set to the kernel set I recently posted (and which also should have been marked RFC).
Signed-off-by Jeff Layton <[email protected]>
Committed... (tag: nfs-utils-2-9-3-rc7)
steved.> ---
Quoted 47 lines
Changes in v2:
- Fixes for bugs v1 introduced are folded into the patches that
introduced them. Three pre-existing bugs found along the way are separate patches at the end, so the set is 10 patches instead of 7.
- "register nfsd listeners with rpcbind": register only once thelistener_setreply has been parsed. On a reply carrying no rpcbind attributes the sweep cleared every NFS and NFSACL entry and put
nothing back, leaving a running server advertising nothing. Also
abort rather than send alistener_setthat lost the ownership flag.
- "clear rpcbind entries when the serv is torn down" (was "... when the last thread stops"): also sweep on the autostart teardown path.
- New, pre-existing: fix a heap overflow inautostart_func(). A failed listener set zeroed the pool count before the thread array was sized, but the array is filled from the nfs.conf list, so any [nfsd] threads setting wrote past the allocation.
- New, pre-existing: reject a listener insertion when the table is full, instead of writing one element past nfsd_sockets[].
- New, pre-existing: check thenla_put()calls inset_listeners().
- Link to v1: https://lore.kernel.org/r/[email protected]---
Jeff Layton (10):
nfsdctl: syncnfsd_netlink.hwith the kernel
nfsdctl: probe the policy of any command
nfsdctl: share the listener nest parser
nfsdctl: add rpcbind registration helpers
nfsdctl: register nfsd listeners with rpcbind
nfsdctl: clear rpcbind entries when the serv is torn down
nfsdctl: add missing break inupdate_listeners()
nfsdctl: fix heap overflow inautostart_func()listener cleanup
nfsdctl: reject a listener insertion when the table is full
nfsdctl: check the netlink attribute puts inset_listeners()configure.ac | 2 +- support/include/nfsd_netlink.h | 18 +++ utils/nfsdctl/Makefile.am | 10 +- utils/nfsdctl/nfsdctl.8 | 16 +++ utils/nfsdctl/nfsdctl.adoc | 17 +++ utils/nfsdctl/nfsdctl.c | 256 +++++++++++++++++++++++++++++++++-------- utils/nfsdctl/rpcbind.c | 251 ++++++++++++++++++++++++++++++++++++++++ utils/nfsdctl/rpcbind.h | 42 +++++++ 8 files changed, 562 insertions(+), 50 deletions(-)---
base-commit:630393e9c73667b3fda6a7507cb5171ec0ec38dc
change-id: 20260908-nfsdctl-rpcb-adc5a1fc090bBest regards,