diff mbox series

[PATCHv2,opensm] osm_console_io.c: Avoid potential no NUL-terminated strncpy in osm_console_init

Message ID 83baceda-a0fb-e8e6-5f20-3bd9117a3e80@dev.mellanox.co.il (mailing list archive)
State Not Applicable
Headers show
Series [PATCHv2,opensm] osm_console_io.c: Avoid potential no NUL-terminated strncpy in osm_console_init | expand

Commit Message

Hal Rosenstock July 25, 2018, 12:03 p.m. UTC
The default string of opt-console is OSM_DEFAULT_CONSOLE, which equal
"off". It is safe to copy 32 bytes in ‘osm_console_init’, when the
'console' field was initialized with default value. But this minor fix
avoids potential no NUL-terminated strncpy.

make[2]: Entering directory '/home/honli/upstream-repos/opensm/opensm'
depbase=`echo osm_console_io.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
gcc -DHAVE_CONFIG_H -I. -I../include -I../include/opensm  -I./../include -I./../../libibumad/include -I/usr/local/include   -Werror -Wall -Wwrite-strings -g -D_XOPEN_SOURCE=600 -D_DEFAULT_SOURCE=1 -g -O2 -MT osm_console_io.o -MD -MP -MF $depbase.Tpo -c -o osm_console_io.o osm_console_io.c &&\
mv -f $depbase.Tpo $depbase.Po
osm_console_io.c: In function ‘osm_console_init’:
osm_console_io.c:186:2: error: ‘strncpy’ specified bound 32 equals destination size [-Werror=stringop-truncation]
  strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type));
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Signed-off-by: Honggang Li <honli@redhat.com>
Signed-off-by: Hal Rosenstock <hal@mellanox.com>
---
 opensm/osm_console_io.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Honggang LI July 26, 2018, 12:07 a.m. UTC | #1
On Wed, Jul 25, 2018 at 08:03:57AM -0400, Hal Rosenstock wrote:
> 
> The default string of opt-console is OSM_DEFAULT_CONSOLE, which equal
> "off". It is safe to copy 32 bytes in ‘osm_console_init’, when the
> 'console' field was initialized with default value. But this minor fix
> avoids potential no NUL-terminated strncpy.
> 
> make[2]: Entering directory '/home/honli/upstream-repos/opensm/opensm'
> depbase=`echo osm_console_io.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
> gcc -DHAVE_CONFIG_H -I. -I../include -I../include/opensm  -I./../include -I./../../libibumad/include -I/usr/local/include   -Werror -Wall -Wwrite-strings -g -D_XOPEN_SOURCE=600 -D_DEFAULT_SOURCE=1 -g -O2 -MT osm_console_io.o -MD -MP -MF $depbase.Tpo -c -o osm_console_io.o osm_console_io.c &&\
> mv -f $depbase.Tpo $depbase.Po
> osm_console_io.c: In function ‘osm_console_init’:
> osm_console_io.c:186:2: error: ‘strncpy’ specified bound 32 equals destination size [-Werror=stringop-truncation]
>   strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type));
>   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> cc1: all warnings being treated as errors
> 
> Signed-off-by: Honggang Li <honli@redhat.com>
> Signed-off-by: Hal Rosenstock <hal@mellanox.com>
> ---
>  opensm/osm_console_io.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/opensm/osm_console_io.c b/opensm/osm_console_io.c
> index f529e25..97fe674 100644
> --- a/opensm/osm_console_io.c
> +++ b/opensm/osm_console_io.c
> @@ -182,7 +182,8 @@ void osm_console_prompt(FILE * out)
>  int osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p_log)
>  {
>  	p_oct->socket = -1;
> -	strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type));
> +	strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type) - 1);
> +	p_oct->client_type[sizeof(p_oct->client_type) - 1] = '\0';
>  
>  	/* set up the file descriptors for the console */
>  	if (strcmp(opt->console, OSM_LOCAL_CONSOLE) == 0) {

Acked-by: Honggang Li <honli@redhat.com>

> -- 
> 2.8.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox series

Patch

diff --git a/opensm/osm_console_io.c b/opensm/osm_console_io.c
index f529e25..97fe674 100644
--- a/opensm/osm_console_io.c
+++ b/opensm/osm_console_io.c
@@ -182,7 +182,8 @@  void osm_console_prompt(FILE * out)
 int osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p_log)
 {
 	p_oct->socket = -1;
-	strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type));
+	strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type) - 1);
+	p_oct->client_type[sizeof(p_oct->client_type) - 1] = '\0';
 
 	/* set up the file descriptors for the console */
 	if (strcmp(opt->console, OSM_LOCAL_CONSOLE) == 0) {