From patchwork Mon Mar 24 18:06:09 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hal Rosenstock X-Patchwork-Id: 3883391 X-Patchwork-Delegate: hal@mellanox.com Return-Path: X-Original-To: patchwork-linux-rdma@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 2BE999F334 for ; Mon, 24 Mar 2014 18:06:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 30225201DE for ; Mon, 24 Mar 2014 18:06:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BEBF520136 for ; Mon, 24 Mar 2014 18:06:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753275AbaCXSGQ (ORCPT ); Mon, 24 Mar 2014 14:06:16 -0400 Received: from mail-bk0-f51.google.com ([209.85.214.51]:45117 "EHLO mail-bk0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753147AbaCXSGP (ORCPT ); Mon, 24 Mar 2014 14:06:15 -0400 Received: by mail-bk0-f51.google.com with SMTP id 6so587497bkj.24 for ; Mon, 24 Mar 2014 11:06:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:content-type:content-transfer-encoding; bh=yiuCkF/gX6eD1z23PYKDh9KHn+5mIVNhRqU3167C9m8=; b=Q38WX+uCWpYnO4cIHpx/y67F0vuEV2pnHvb2ffP9qg5VCGXyNPQiPyxrrBtMJXUbGb 1pl9fLY1HUK1ErH1rOSBWYVF8RsJW6vcBM7wJUPUqduiA5WWa2Njz+aOZx0TF0AM23Rt H5iGPAyc/qH2qzzt+wi67kmiX3TO5N6NXtfe5GzmUVQ1yMReXIC8o29n00q1G3zJN56N Xa+q/fdlAwupkLBcUTPrBiMI13D/nJGVkMT5FSzCjtgoWn3nLpsj4NEofp2fwqXftLXx wWGWeb+/0SXHQCYlIakcvTrFnQeaVHWbOVoWqE+bLHb6R4iaeDzWMvPcfgb5z+1dyM5M ig3Q== X-Gm-Message-State: ALoCoQmv8E58DgREZqMYFYACPdJBsAzI5FaKeYi5VA54rs6u9htGhYPHmEk+s43BUpdcjLZmGVhY X-Received: by 10.205.75.133 with SMTP id za5mr166537bkb.140.1395684374181; Mon, 24 Mar 2014 11:06:14 -0700 (PDT) Received: from [192.168.1.102] (c-98-229-118-119.hsd1.ma.comcast.net. [98.229.118.119]) by mx.google.com with ESMTPSA id c15sm15500233bky.13.2014.03.24.11.06.11 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 24 Mar 2014 11:06:13 -0700 (PDT) Message-ID: <53307411.2090605@dev.mellanox.co.il> Date: Mon, 24 Mar 2014 14:06:09 -0400 From: Hal Rosenstock User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: "linux-rdma (linux-rdma@vger.kernel.org)" CC: Tomer Cohen Subject: [PATCH opensm] osm_subnet.c: Fix bug in parsing configuration file Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Tomer Cohen Date: Sun, 16 Mar 2014 17:35:13 +0200 Added handling a non numeric input for a numeric field (of the opensm configuration file) and a range check for those parameters. Signed-off-by: Tomer Cohen Signed-off-by: Hal Rosenstock --- opensm/osm_subnet.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 68 insertions(+), 7 deletions(-) diff --git a/opensm/osm_subnet.c b/opensm/osm_subnet.c index aedfbd5..61e1fc2 100644 --- a/opensm/osm_subnet.c +++ b/opensm/osm_subnet.c @@ -275,12 +275,56 @@ static void opts_setup_sm_priority(osm_subn_t *p_subn, void *p_val) osm_set_sm_priority(p_sm, sm_priority); } +static int opts_strtoul(uint32_t *val, IN char *p_val_str, + IN char *p_key, uint32_t max_value) +{ + char *endptr; + unsigned long int tmp_val; + + tmp_val = strtoul(p_val_str, &endptr, 0); + *val = tmp_val; + if (*p_val_str == '\0' || *endptr != '\0') { + log_report("-E- Parsing error in field %s, expected " + "numeric input received: %s\n", p_key, p_val_str); + return -1; + } + if (tmp_val > max_value || + ((tmp_val == ULONG_MAX) && errno == ERANGE)) { + log_report("-E- Parsing error in field %s, value out of range\n", p_key); + return -1; + } + return 0; +} + +static int opts_strtoull(uint64_t *val, IN char *p_val_str, + IN char *p_key, uint64_t max_value) +{ + char *endptr; + unsigned long long int tmp_val; + + tmp_val = strtoull(p_val_str, &endptr, 0); + *val = tmp_val; + if (*p_val_str == '\0' || *endptr != '\0') { + log_report("-E- Parsing error in field %s, expected " + "numeric input received: %s\n", p_key, p_val_str); + return -1; + } + if (tmp_val > max_value || (tmp_val == ULLONG_MAX && errno == ERANGE)) { + log_report("-E- Parsing error in field %s, value out of range", p_key); + return -1; + } + return 0; +} + static void opts_parse_net64(IN osm_subn_t *p_subn, IN char *p_key, IN char *p_val_str, void *p_v1, void *p_v2, void (*pfn)(osm_subn_t *, void *)) { uint64_t *p_val1 = p_v1, *p_val2 = p_v2; - uint64_t val = strtoull(p_val_str, NULL, 0); + uint64_t val; + + if (opts_strtoull(&val, p_val_str, p_key, UINT64_MAX)) + return; if (cl_hton64(val) != *p_val1) { log_config_value(p_key, "0x%016" PRIx64, val); @@ -295,7 +339,10 @@ static void opts_parse_uint32(IN osm_subn_t *p_subn, IN char *p_key, void (*pfn)(osm_subn_t *, void *)) { uint32_t *p_val1 = p_v1, *p_val2 = p_v2; - uint32_t val = strtoul(p_val_str, NULL, 0); + uint32_t val; + + if (opts_strtoul(&val, p_val_str, p_key, UINT32_MAX)) + return; if (val != *p_val1) { log_config_value(p_key, "%u", val); @@ -310,7 +357,10 @@ static void opts_parse_net32(IN osm_subn_t *p_subn, IN char *p_key, void (*pfn)(osm_subn_t *, void *)) { uint32_t *p_val1 = p_v1, *p_val2 = p_v2; - uint32_t val = strtoul(p_val_str, NULL, 0); + uint32_t val; + + if (opts_strtoul(&val, p_val_str, p_key, UINT32_MAX)) + return; if (cl_hton32(val) != *p_val1) { log_config_value(p_key, "%u", val); @@ -320,7 +370,6 @@ static void opts_parse_net32(IN osm_subn_t *p_subn, IN char *p_key, } } - static void opts_parse_int32(IN osm_subn_t *p_subn, IN char *p_key, IN char *p_val_str, void *p_v1, void *p_v2, void (*pfn)(osm_subn_t *, void *)) @@ -341,8 +390,12 @@ static void opts_parse_uint16(IN osm_subn_t *p_subn, IN char *p_key, void (*pfn)(osm_subn_t *, void *)) { uint16_t *p_val1 = p_v1, *p_val2 = p_v2; - uint16_t val = (uint16_t) strtoul(p_val_str, NULL, 0); + uint32_t tmp_val; + + if (opts_strtoul(&tmp_val, p_val_str, p_key, UINT16_MAX)) + return; + uint16_t val = (uint16_t) tmp_val; if (val != *p_val1) { log_config_value(p_key, "%u", val); if (pfn) @@ -356,8 +409,12 @@ static void opts_parse_net16(IN osm_subn_t *p_subn, IN char *p_key, void (*pfn)(osm_subn_t *, void *)) { uint16_t *p_val1 = p_v1, *p_val2 = p_v2; - uint16_t val = strtoul(p_val_str, NULL, 0); + uint32_t tmp_val; + if (opts_strtoul(&tmp_val, p_val_str, p_key, UINT16_MAX)) + return; + + uint16_t val = (uint16_t) tmp_val; if (cl_hton16(val) != *p_val1) { log_config_value(p_key, "0x%04x", val); if (pfn) @@ -371,8 +428,12 @@ static void opts_parse_uint8(IN osm_subn_t *p_subn, IN char *p_key, void (*pfn)(osm_subn_t *, void *)) { uint8_t *p_val1 = p_v1, *p_val2 = p_v2; - uint8_t val = strtoul(p_val_str, NULL, 0); + uint32_t tmp_val; + + if (opts_strtoul(&tmp_val, p_val_str, p_key, UINT8_MAX)) + return; + uint8_t val = (uint8_t) tmp_val; if (val != *p_val1) { log_config_value(p_key, "%u", val); if (pfn)