From patchwork Wed Jan 2 13:13:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Honggang LI X-Patchwork-Id: 10746355 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CB06591E for ; Wed, 2 Jan 2019 13:14:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B9CB628848 for ; Wed, 2 Jan 2019 13:14:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE24B28857; Wed, 2 Jan 2019 13:14:44 +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 54F9328848 for ; Wed, 2 Jan 2019 13:14:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729916AbfABNOo (ORCPT ); Wed, 2 Jan 2019 08:14:44 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43104 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729901AbfABNOo (ORCPT ); Wed, 2 Jan 2019 08:14:44 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E6AC12D2BD3; Wed, 2 Jan 2019 13:14:43 +0000 (UTC) Received: from localhost (ovpn-12-97.pek2.redhat.com [10.72.12.97]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 41B2660C66; Wed, 2 Jan 2019 13:14:42 +0000 (UTC) From: Honggang Li To: hal@dev.mellanox.co.il Cc: linux-rdma@vger.kernel.org, Honggang Li Subject: [ibsim patch 17/23] umad2sim.c: Fix a few potential buffer overflow issues Date: Wed, 2 Jan 2019 21:13:12 +0800 Message-Id: <20190102131318.5765-17-honli@redhat.com> In-Reply-To: <20190102131318.5765-1-honli@redhat.com> References: <20190102131318.5765-1-honli@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 02 Jan 2019 13:14:43 +0000 (UTC) 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 Issue was detected by Coveiry. ---- Error: CLANG_WARNING: [#def21] ibsim-0.7/umad2sim/umad2sim.c:374:26: warning: Potential buffer overflow. Replace with 'sizeof(path) - strlen(path) - 1' or use a safer 'strlcat' API // strncat(path, "/pkeys", sizeof(path) - 1); // ^~~~~~~~~~~~~~~~ ibsim-0.7/umad2sim/umad2sim.c:374:26: note: Potential buffer overflow. Replace with 'sizeof(path) - strlen(path) - 1' or use a safer 'strlcat' API // strncat(path, "/pkeys", sizeof(path) - 1); // ^~~~~~~~~~~~~~~~ // 372| /* /sys/class/infiniband/mthca0/ports/1/pkeys/0 */ // 373| str = path + strlen(path); // 374|-> strncat(path, "/pkeys", sizeof(path) - 1); // 375| make_path(path); // 376| for (i = 0; i < sizeof(sc->pkeys)/sizeof(sc->pkeys[0]); i++) { ---- Signed-off-by: Honggang Li --- umad2sim/umad2sim.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/umad2sim/umad2sim.c b/umad2sim/umad2sim.c index 80e7b8166638..c1bd8dc9d8b0 100644 --- a/umad2sim/umad2sim.c +++ b/umad2sim/umad2sim.c @@ -165,8 +165,8 @@ static int file_printf(char *path, char *name, const char *fmt, ...) int ret; convert_sysfs_path(file_name, sizeof(file_name), path); - strncat(file_name, "/", sizeof(file_name) - 1); - strncat(file_name, name, sizeof(file_name) - 1); + strncat(file_name, "/", sizeof(file_name) - strlen(file_name) - 1); + strncat(file_name, name, sizeof(file_name) - strlen(file_name) - 1); unlink(file_name); f = fopen(file_name, "w"); if (!f) { @@ -239,7 +239,7 @@ static int dev_sysfs_create(struct umad2sim_dev *dev) (uint16_t) ((guid >> 0) & 0xffff)); /* /sys/class/infiniband/mthca0/ports/ */ - strncat(path, "/ports", sizeof(path) - 1); + strncat(path, "/ports", sizeof(path) - strlen(path) - 1); make_path(path); portinfo = sc->portinfo; @@ -351,7 +351,7 @@ static int dev_sysfs_create(struct umad2sim_dev *dev) /* /sys/class/infiniband/mthca0/ports/1/gids/0 */ str = path + strlen(path); - strncat(path, "/gids", sizeof(path) - 1); + strncat(path, "/gids", sizeof(path) - strlen(path) - 1); make_path(path); *str = '\0'; gid = mad_get_field64(portinfo, 0, IB_PORT_GID_PREFIX_F); @@ -370,7 +370,7 @@ static int dev_sysfs_create(struct umad2sim_dev *dev) /* /sys/class/infiniband/mthca0/ports/1/pkeys/0 */ str = path + strlen(path); - strncat(path, "/pkeys", sizeof(path) - 1); + strncat(path, "/pkeys", sizeof(path) - strlen(path) - 1); make_path(path); for (i = 0; i < sizeof(sc->pkeys)/sizeof(sc->pkeys[0]); i++) { char name[8];