From patchwork Fri Jul 20 02:50:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Honggang LI X-Patchwork-Id: 10535897 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9CE486029B for ; Fri, 20 Jul 2018 02:51:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C694298A6 for ; Fri, 20 Jul 2018 02:51:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7F443299DA; Fri, 20 Jul 2018 02:51:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2B452298A6 for ; Fri, 20 Jul 2018 02:51:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731012AbeGTDhX (ORCPT ); Thu, 19 Jul 2018 23:37:23 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:41136 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730514AbeGTDhX (ORCPT ); Thu, 19 Jul 2018 23:37:23 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B66974005F6C; Fri, 20 Jul 2018 02:51:21 +0000 (UTC) Received: from dhcp-129-124.nay.redhat.com (unknown [10.66.129.124]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C1AB111E406; Fri, 20 Jul 2018 02:51:19 +0000 (UTC) From: Honggang LI To: hal@mellanox.com Cc: linux-rdma@vger.kernel.org, Honggang Li Subject: [PATCH 3/6] osm_console_io.c: To avoid potential no NUL-terminated strncpy Date: Fri, 20 Jul 2018 10:50:35 +0800 Message-Id: <20180720025038.9365-4-honli@redhat.com> In-Reply-To: <20180720025038.9365-3-honli@redhat.com> References: <20180720025038.9365-1-honli@redhat.com> <20180720025038.9365-2-honli@redhat.com> <20180720025038.9365-3-honli@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 20 Jul 2018 02:51:21 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 20 Jul 2018 02:51:21 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'honli@redhat.com' RCPT:'' Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Honggang Li The default string of opt-console is OSM_DEFAULT_CONSOLE, which equal "off". It is safe to copy 32 bites in ‘osm_console_init’, when the 'console' field was initialized with default value. But this minor fix avoid 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 --- opensm/osm_console_io.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/opensm/osm_console_io.c b/opensm/osm_console_io.c index e358a55cd5d8..f82bf9cf65f0 100644 --- a/opensm/osm_console_io.c +++ b/opensm/osm_console_io.c @@ -182,8 +182,11 @@ 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) { + int cnt = strlen(opt->console); p_oct->socket = -1; - strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type)); + if (strlen(opt->console) > sizeof(p_oct->client_type)-1) + cnt = sizeof(p_oct->client_type)-1; + strncpy(p_oct->client_type, opt->console, cnt); /* set up the file descriptors for the console */ if (strcmp(opt->console, OSM_LOCAL_CONSOLE) == 0) {