Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp968115imm; Wed, 10 Oct 2018 07:10:37 -0700 (PDT) X-Google-Smtp-Source: ACcGV63ebTNPiR1Afk+hXv/zX18ld4ut2ntbaXVs9LZ0bXPV8l9tozhF6kepDk9iDo30J8RROivZ X-Received: by 2002:a63:145f:: with SMTP id 31-v6mr29144256pgu.35.1539180637609; Wed, 10 Oct 2018 07:10:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539180637; cv=none; d=google.com; s=arc-20160816; b=N4oeZMn41XbD1yUEnOgjT893qd166wlT1U5RS+ydRd+w4UxIEnitkhOeUgsvfEHme+ J05pIqjZetDWx8Hm/aM6tN7bU+eIAVA0ziMy3uBII/f2Ua19bjNGK52xB5rJHuRFeUvw 0Dt8FDKUNXwv4K6dd2UjCiMWPjjBa07oMGD+Dl1FQ3DRYykL+KGUtwyUEMPyQyu1E3gL 82k6JN4zS13V7ZguMdHMtJDlTmbdryl541HB+24ei9GHuMvkM8QPP+Ka7v1uimdq2OGk 3p8eEYn5BNg3vKmZg6Fi0TV9j6tmWSjVVl/SEqM3f5K0e3hKXihAGeD1BFRyKWZH0iNT 3LGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=7Z1RbN+8/N2qqz93DML+XGv+H0ocLEa0DQmpngohMBc=; b=Qeb7Zakg2O9edmmifmI3n+oRP9W0B8ACUKzkFoqI7uMyRYLliOL1IhiHi4fxS4vHRc L3NYY52uyJ3k+R34WVw8fPnWa2okhxItzMoPBqM5Uceu/SFOWdtXWhWQouos8u0wCX+v T5niH53E7G9gDL3qX6OWRYF5tfdLYioNxFaDoH8N0BW31aCnGVMYIfp1LEje3rvLYWV6 Xjjf7LPD59/sGrf7s9NF2fT5SS15FvE5XTLzNUCtRE1JVhZXPtfJYPIHJkH/WwGHTdJp 8vqE1LWuFr28hadcIC5zLFF+sAeM8evK4Twws5+hOu9TuRnBHFwmxfNCXHwhdajFd4EQ p69A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mellanox.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e3-v6si24447699pga.369.2018.10.10.07.10.22; Wed, 10 Oct 2018 07:10:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mellanox.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727298AbeJJVbz (ORCPT + 99 others); Wed, 10 Oct 2018 17:31:55 -0400 Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:49424 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726902AbeJJVby (ORCPT ); Wed, 10 Oct 2018 17:31:54 -0400 Received: from Internal Mail-Server by MTLPINE1 (envelope-from moshe@mellanox.com) with ESMTPS (AES256-SHA encrypted); 10 Oct 2018 15:14:45 +0200 Received: from dev-l-vrt-136.mtl.labs.mlnx (dev-l-vrt-136.mtl.labs.mlnx [10.134.136.1]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id w9AD9sJG003298; Wed, 10 Oct 2018 16:09:54 +0300 Received: from dev-l-vrt-136.mtl.labs.mlnx (localhost [127.0.0.1]) by dev-l-vrt-136.mtl.labs.mlnx (8.14.7/8.14.7) with ESMTP id w9AD9sEk022240; Wed, 10 Oct 2018 16:09:54 +0300 Received: (from moshe@localhost) by dev-l-vrt-136.mtl.labs.mlnx (8.14.7/8.14.7/Submit) id w9AD9sgu022239; Wed, 10 Oct 2018 16:09:54 +0300 From: Moshe Shemesh To: "David S. Miller" Cc: Jiri Pirko , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Moshe Shemesh Subject: [PATCH net 3/3] devlink: Add helper function for safely copy string param Date: Wed, 10 Oct 2018 16:09:27 +0300 Message-Id: <1539176967-22172-4-git-send-email-moshe@mellanox.com> X-Mailer: git-send-email 1.8.4.3 In-Reply-To: <1539176967-22172-1-git-send-email-moshe@mellanox.com> References: <1539176967-22172-1-git-send-email-moshe@mellanox.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Devlink string param buffer is allocated at the size of DEVLINK_PARAM_MAX_STRING_VALUE. Add helper function which makes sure this size is not exceeded. Renamed DEVLINK_PARAM_MAX_STRING_VALUE to __DEVLINK_PARAM_MAX_STRING_VALUE to emphasize that it should be used by devlink only. The driver should use the helper function instead to verify it doesn't exceed the allowed length. Signed-off-by: Moshe Shemesh Acked-by: Jiri Pirko --- include/net/devlink.h | 12 ++++++++++-- net/core/devlink.c | 19 ++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/net/devlink.h b/include/net/devlink.h index b0e17c0..99efc15 100644 --- a/include/net/devlink.h +++ b/include/net/devlink.h @@ -298,7 +298,7 @@ struct devlink_resource { #define DEVLINK_RESOURCE_ID_PARENT_TOP 0 -#define DEVLINK_PARAM_MAX_STRING_VALUE 32 +#define __DEVLINK_PARAM_MAX_STRING_VALUE 32 enum devlink_param_type { DEVLINK_PARAM_TYPE_U8, DEVLINK_PARAM_TYPE_U16, @@ -311,7 +311,7 @@ enum devlink_param_type { u8 vu8; u16 vu16; u32 vu32; - char vstr[DEVLINK_PARAM_MAX_STRING_VALUE]; + char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE]; bool vbool; }; @@ -553,6 +553,8 @@ int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id, int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id, union devlink_param_value init_val); void devlink_param_value_changed(struct devlink *devlink, u32 param_id); +void devlink_param_value_str_fill(union devlink_param_value *dst_val, + const char *src); struct devlink_region *devlink_region_create(struct devlink *devlink, const char *region_name, u32 region_max_snapshots, @@ -789,6 +791,12 @@ static inline bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, { } +static inline void +devlink_param_value_str_fill(union devlink_param_value *dst_val, + const char *src) +{ +} + static inline struct devlink_region * devlink_region_create(struct devlink *devlink, const char *region_name, diff --git a/net/core/devlink.c b/net/core/devlink.c index 1a0de16..6bc4293 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -3015,7 +3015,7 @@ static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg, len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]), nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])); if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) || - len >= DEVLINK_PARAM_MAX_STRING_VALUE) + len >= __DEVLINK_PARAM_MAX_STRING_VALUE) return -EINVAL; strcpy(value->vstr, nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])); @@ -4618,6 +4618,23 @@ void devlink_param_value_changed(struct devlink *devlink, u32 param_id) EXPORT_SYMBOL_GPL(devlink_param_value_changed); /** + * devlink_param_value_str_fill - Safely fill-up the string preventing + * from overflow of the preallocated buffer + * + * @dst_val: destination devlink_param_value + * @src: source buffer + */ +void devlink_param_value_str_fill(union devlink_param_value *dst_val, + const char *src) +{ + size_t len; + + len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE); + WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE); +} +EXPORT_SYMBOL_GPL(devlink_param_value_str_fill); + +/** * devlink_region_create - create a new address region * * @devlink: devlink -- 1.8.3.1