Commit bde74ad1 authored by Moshe Shemesh's avatar Moshe Shemesh Committed by David S. Miller

devlink: Add helper function for safely copy string param

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: default avatarMoshe Shemesh <moshe@mellanox.com>
Acked-by: default avatarJiri Pirko <jiri@mellanox.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1276534c
...@@ -298,7 +298,7 @@ struct devlink_resource { ...@@ -298,7 +298,7 @@ struct devlink_resource {
#define DEVLINK_RESOURCE_ID_PARENT_TOP 0 #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 { enum devlink_param_type {
DEVLINK_PARAM_TYPE_U8, DEVLINK_PARAM_TYPE_U8,
DEVLINK_PARAM_TYPE_U16, DEVLINK_PARAM_TYPE_U16,
...@@ -311,7 +311,7 @@ union devlink_param_value { ...@@ -311,7 +311,7 @@ union devlink_param_value {
u8 vu8; u8 vu8;
u16 vu16; u16 vu16;
u32 vu32; u32 vu32;
char vstr[DEVLINK_PARAM_MAX_STRING_VALUE]; char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];
bool vbool; bool vbool;
}; };
...@@ -553,6 +553,8 @@ int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id, ...@@ -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, int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
union devlink_param_value init_val); union devlink_param_value init_val);
void devlink_param_value_changed(struct devlink *devlink, u32 param_id); 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, struct devlink_region *devlink_region_create(struct devlink *devlink,
const char *region_name, const char *region_name,
u32 region_max_snapshots, u32 region_max_snapshots,
...@@ -789,6 +791,12 @@ devlink_param_value_changed(struct devlink *devlink, u32 param_id) ...@@ -789,6 +791,12 @@ devlink_param_value_changed(struct devlink *devlink, u32 param_id)
{ {
} }
static inline void
devlink_param_value_str_fill(union devlink_param_value *dst_val,
const char *src)
{
}
static inline struct devlink_region * static inline struct devlink_region *
devlink_region_create(struct devlink *devlink, devlink_region_create(struct devlink *devlink,
const char *region_name, const char *region_name,
......
...@@ -3015,7 +3015,7 @@ devlink_param_value_get_from_info(const struct devlink_param *param, ...@@ -3015,7 +3015,7 @@ devlink_param_value_get_from_info(const struct devlink_param *param,
len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]), len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
nla_len(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]) || 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; return -EINVAL;
strcpy(value->vstr, strcpy(value->vstr,
nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])); nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
...@@ -4617,6 +4617,23 @@ void devlink_param_value_changed(struct devlink *devlink, u32 param_id) ...@@ -4617,6 +4617,23 @@ void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
} }
EXPORT_SYMBOL_GPL(devlink_param_value_changed); 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_region_create - create a new address region
* *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment