nfs-utils: nfsdctl fixups
Subsystems (2)
Olga Kornievskaia
reviewercover nfs-utils: nfsdctl fixups
Static memory allocations in the path of creating knfsd listeners has lead to either buffer over flow errors or failure to start the listener when really long hostname are being used.
Furthermore, when listeners were failed to be created knfsd threads were still started (with no working listeners). Also, when threads were not started but we still had already added they were not cleaned up.
This patch series attempts to address the said issues.
(note first patch was set as standalone previous and included here without change).
Olga Kornievskaia (4):
nfs-utils: nfsdctl: fix update_listeners
nfs-utils: nfsdctl: fix autostart
nfs-utils: nfsdctl: cleanup listeners if some failed
nfs-utils: nfsdctl: fix host-limited add listener
utils/nfsdctl/nfsdctl.c | 68 +++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 23 deletions(-)
Signature · 1 line
2.47.1
Olga Kornievskaia
reviewerPATCH 1/4 nfs-utils: nfsdctl: fix update_listeners
When adding a listener via an nfsdctl listener command and passing in a hostname that is longer then 62bytes it leads to a buffer overlow problem.
Instead allocate the needed buffer to be the size of the supplied command-line argument.
--- utils/nfsdctl/nfsdctl.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
utils/nfsdctl/nfsdctl.c +12 -8
@@ -938,8 +938,6 @@ static void print_listeners(void) } } -#define BUFLEN (INET6_ADDRSTRLEN + 16) -/* * Format is <+/-><netid>:<address>:port *
@@ -950,7 +948,7 @@ static void print_listeners(void)*/ static int update_listeners (const char *str) { -char buf[INET6_ADDRSTRLEN +16 ]; +char *buf;char sign = *str;char *netid, *addr, *port, *end;struct addrinfo *res;
@@ -963,6 +961,9 @@ static int update_listeners(const char *str)if (sign !='+' && sign !='-' ) goto out_inval; + buf =malloc (strlen (str) +1 ); +if (!buf) + goto out_inval;strcpy (buf, str +1 );/* netid is start */
@@ -971,18 +972,18 @@ static int update_listeners(const char *str)/* find first ':' */ addr =strchr (buf,':' );if (!addr) - goto out_inval; + goto out_inval_free;if (addr == buf) {/* empty netid */ - goto out_inval; + goto out_inval_free; } *addr ='\0' ; ++addr; port =strrchr (addr,':' );if (!port) - goto out_inval; + goto out_inval_free;if (port == addr) {/* empty address, give gai a NULL ptr */ addr =NULL ;
@@ -992,7 +993,7 @@ static int update_listeners(const char *str)if (*port =='\0' ) {/* empty port */ - goto out_inval; + goto out_inval_free; }/* IPv6 addrs must be in square brackets */
@@ -1001,7 +1002,7 @@ static int update_listeners(const char *str) ++addr; end =strchr (addr,']' );if (!end) - goto out_inval; + goto out_inval_free;if (end == addr) addr =NULL ; *end ='\0' ;
@@ -1070,7 +1071,10 @@ static int update_listeners(const char *str) ++nfsd_socket_count; } } +free (buf);return 0 ; +out_inval_free: +free (buf); out_inval:fprintf (stderr,"Invalid listener update string: %s" , str);return -EINVAL ;
Signature · 1 line
2.47.1
Olga Kornievskaia
reviewerPATCH 2/4 nfs-utils: nfsdctl: fix autostart
In reply to Olga KornievskaiaDuring nfsdctl autostart the nfsdctl reads the nfs.conf file and tries to setup the listeners and start knfsd threads. However, if we failed to start a listener, it currently ignores the error and starts the threads anyway.
Suggesting that if we fail to start a UDP/TCP listener then do not start threads. At the moment ignoring the failure of adding an RDMA listener (because default config might have rdma=y set but not RDMA-enabled interface is available and previously we did not fail the start of knfsd in that case).
--- utils/nfsdctl/nfsdctl.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
utils/nfsdctl/nfsdctl.c +14 -8
@@ -1285,7 +1285,7 @@ static int pool_mode_func(struct nl_sock *sock, int argc, char **argv)#define MAX_LISTENER_LEN (64 * 2 + 16) -static void +static int add_listener (const char *netid,const char *addr,const char *port) {char buf[MAX_LISTENER_LEN ];
@@ -1297,7 +1297,7 @@ add_listener(const char *netid, const char *addr, const char *port)snprintf (buf,MAX_LISTENER_LEN ,"+%s:%s:%s" , netid, addr, port); buf[MAX_LISTENER_LEN -1 ] ='\0' ; -update_listeners (buf); +return update_listeners (buf); } static void
@@ -1350,11 +1350,12 @@ static int configure_versions(void)return 0 ; } -static void configure_listeners (void ) +static int configure_listeners (void ) {char *port, *rdma_port;bool rdma, udp, tcp;struct conf_list *hosts; +int ret =0 ; udp =conf_get_bool ("nfsd" ,"udp" , false); tcp =conf_get_bool ("nfsd" ,"tcp" , true);
@@ -1378,20 +1379,23 @@ static void configure_listeners(void)struct conf_list_node *n; TAILQ_FOREACH(n, &(hosts->fields), link) {if (udp) -add_listener ("udp" , n->field, port); + ret =add_listener ("udp" , n->field, port);if (tcp) -add_listener ("tcp" , n->field, port); + ret =add_listener ("tcp" , n->field, port);if (rdma)add_listener ("rdma" , n->field, rdma_port); +if (ret) +return ret; } }else {if (udp) -add_listener ("udp" ,"" , port); + ret =add_listener ("udp" ,"" , port);if (tcp) -add_listener ("tcp" ,"" , port); + ret =add_listener ("tcp" ,"" , port);if (rdma)add_listener ("rdma" ,"" , rdma_port); } +return ret; }static void autostart_usage (void )
@@ -1438,7 +1442,9 @@ static int autostart_func(struct nl_sock *sock, int argc, char ** argv)if (ret)return ret; -configure_listeners (); + ret =configure_listeners (); +if (ret) +return ret; ret =set_listeners (sock);if (ret)return ret;
Signature · 1 line
2.47.1
Olga Kornievskaia
reviewerPATCH 3/4 nfs-utils: nfsdctl: cleanup listeners if some failed
In reply to Olga KornievskaiaIf we couldn't start some listeners, then make sure to remove already added listeners.
--- utils/nfsdctl/nfsdctl.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
utils/nfsdctl/nfsdctl.c +8 -1
@@ -1413,6 +1413,7 @@ static int autostart_func(struct nl_sock *sock, int argc, char ** argv)struct conf_list *thread_str;struct conf_list_node *n;char *scope, *pool_mode; +bool failed_listeners = false; optind =1 ;while ((opt =getopt_long (argc, argv,"h" , help_only_options,NULL )) !=-1 ) {
@@ -1447,7 +1448,7 @@ static int autostart_func(struct nl_sock *sock, int argc, char ** argv)return ret; ret =set_listeners (sock);if (ret) -return ret; + failed_listeners = true; grace =conf_get_num ("nfsd" ,"grace-time" ,0 ); lease =conf_get_num ("nfsd" ,"lease-time" ,0 );
@@ -1456,6 +1457,12 @@ 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 ;
Signature · 1 line
2.47.1
Olga Kornievskaia
reviewerPATCH 4/4 nfs-utils: nfsdctl: fix host-limited add listener
In reply to Olga KornievskaiaIf in nfs.conf there was "host=<hostname>" configuration that limited knfsd to listen on a particular hostname, then add_listener() had to fit <type>:<hostname>:<port> into 144bytes to create a listener or be truncated which leads to failure of said listener creation.
Instead allocate needed memory dynamically to allow for long hostname values.
--- utils/nfsdctl/nfsdctl.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
utils/nfsdctl/nfsdctl.c +12 -7
@@ -1283,21 +1283,26 @@ static int pool_mode_func(struct nl_sock *sock, int argc, char **argv)return pool_mode_doit (sock, cmd, pool_mode); } -#define MAX_LISTENER_LEN (64 * 2 + 16) -static int add_listener (const char *netid,const char *addr,const char *port) { -char buf[MAX_LISTENER_LEN ]; +char *buf; +int ret; +int size =strlen (addr) +1 +16 ; + buf =calloc (size,sizeof (int )); +if (!buf) +return -ENOMEM ;if (strchr (addr,':' )) -snprintf (buf,MAX_LISTENER_LEN ,"+%s:[%s]:%s" , +snprintf (buf, size,"+%s:[%s]:%s" , netid, addr, port);else -snprintf (buf,MAX_LISTENER_LEN ,"+%s:%s:%s" , +snprintf (buf, size,"+%s:%s:%s" , netid, addr, port); - buf[MAX_LISTENER_LEN -1 ] ='\0' ; -return update_listeners (buf); + buf[size -1 ] ='\0' ; + ret =update_listeners (buf); +free (buf); +return ret; } static void
Signature · 1 line
2.47.1
Quoted 14 lines · Olga Kornievskaia
Static memory allocations in the path of creating knfsd listeners has lead to either buffer over flow errors or failure to start the listener when really long hostname are being used.
Furthermore, when listeners were failed to be created knfsd threads were still started (with no working listeners). Also, when threads were not started but we still had already added they were not cleaned up.
This patch series attempts to address the said issues.
(note first patch was set as standalone previous and included here without change).
Quoted 9 lines
Olga Kornievskaia (4):
nfs-utils: nfsdctl: fixupdate_listeners
nfs-utils: nfsdctl: fix autostart
nfs-utils: nfsdctl: cleanup listeners if some failed
nfs-utils: nfsdctl: fix host-limited add listenerutils/nfsdctl/nfsdctl.c | 68 +++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 23 deletions(-)
Committed (tag: nfs-utils-2-8-3-rc6)
steved.