Skip to content

Commit a7371be

Browse files
committed
Merge branch 'devlink-sanitize-variable-typed-attributes'
Jiri Pirko says: ==================== devlink: sanitize variable typed attributes This is continuation based on first two patches of https://lore.kernel.org/[email protected] Better to take it as a separate patchset, as the rest of the original patchset is probably settled. This patchset is taking care of incorrect usage of internal NLA_* values in uapi, introduces new enum (in patch kernel-patches#2) that shadows NLA_* values and makes that part of UAPI. The last two patches removes unnecessary translations with maintaining clear devlink param driver api. ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents df6a69b + 88debb5 commit a7371be

File tree

7 files changed

+147
-87
lines changed

7 files changed

+147
-87
lines changed

Documentation/netlink/specs/devlink.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,28 @@ definitions:
202202
name: exception
203203
-
204204
name: control
205+
-
206+
type: enum
207+
name: var-attr-type
208+
entries:
209+
-
210+
name: u8
211+
value: 1
212+
-
213+
name: u16
214+
-
215+
name: u32
216+
-
217+
name: u64
218+
-
219+
name: string
220+
-
221+
name: flag
222+
-
223+
name: nul_string
224+
value: 10
225+
-
226+
name: binary
205227

206228
attribute-sets:
207229
-
@@ -498,6 +520,7 @@ attribute-sets:
498520
-
499521
name: param-type
500522
type: u8
523+
enum: var-attr-type
501524

502525
# TODO: fill in the attributes in between
503526

@@ -592,6 +615,7 @@ attribute-sets:
592615
-
593616
name: fmsg-obj-value-type
594617
type: u8
618+
enum: var-attr-type
595619

596620
# TODO: fill in the attributes in between
597621

include/net/devlink.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,11 @@ typedef u64 devlink_resource_occ_get_t(void *priv);
420420

421421
#define __DEVLINK_PARAM_MAX_STRING_VALUE 32
422422
enum devlink_param_type {
423-
DEVLINK_PARAM_TYPE_U8,
424-
DEVLINK_PARAM_TYPE_U16,
425-
DEVLINK_PARAM_TYPE_U32,
426-
DEVLINK_PARAM_TYPE_STRING,
427-
DEVLINK_PARAM_TYPE_BOOL,
423+
DEVLINK_PARAM_TYPE_U8 = DEVLINK_VAR_ATTR_TYPE_U8,
424+
DEVLINK_PARAM_TYPE_U16 = DEVLINK_VAR_ATTR_TYPE_U16,
425+
DEVLINK_PARAM_TYPE_U32 = DEVLINK_VAR_ATTR_TYPE_U32,
426+
DEVLINK_PARAM_TYPE_STRING = DEVLINK_VAR_ATTR_TYPE_STRING,
427+
DEVLINK_PARAM_TYPE_BOOL = DEVLINK_VAR_ATTR_TYPE_FLAG,
428428
};
429429

430430
union devlink_param_value {

include/uapi/linux/devlink.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,21 @@ enum devlink_linecard_state {
385385
DEVLINK_LINECARD_STATE_MAX = __DEVLINK_LINECARD_STATE_MAX - 1
386386
};
387387

388+
/* Variable attribute type. */
389+
enum devlink_var_attr_type {
390+
/* Following values relate to the internal NLA_* values */
391+
DEVLINK_VAR_ATTR_TYPE_U8 = 1,
392+
DEVLINK_VAR_ATTR_TYPE_U16,
393+
DEVLINK_VAR_ATTR_TYPE_U32,
394+
DEVLINK_VAR_ATTR_TYPE_U64,
395+
DEVLINK_VAR_ATTR_TYPE_STRING,
396+
DEVLINK_VAR_ATTR_TYPE_FLAG,
397+
DEVLINK_VAR_ATTR_TYPE_NUL_STRING = 10,
398+
DEVLINK_VAR_ATTR_TYPE_BINARY,
399+
__DEVLINK_VAR_ATTR_TYPE_CUSTOM_BASE = 0x80,
400+
/* Any possible custom types, unrelated to NLA_* values go below */
401+
};
402+
388403
enum devlink_attr {
389404
/* don't change the order or add anything between, this is ABI! */
390405
DEVLINK_ATTR_UNSPEC,

net/devlink/health.c

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ static void devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
735735
return;
736736
}
737737

738-
item->nla_type = NLA_NUL_STRING;
738+
item->nla_type = DEVLINK_VAR_ATTR_TYPE_NUL_STRING;
739739
item->len = strlen(name) + 1;
740740
item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
741741
memcpy(&item->value, name, item->len);
@@ -822,32 +822,37 @@ static void devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
822822
static void devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
823823
{
824824
devlink_fmsg_err_if_binary(fmsg);
825-
devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
825+
devlink_fmsg_put_value(fmsg, &value, sizeof(value),
826+
DEVLINK_VAR_ATTR_TYPE_FLAG);
826827
}
827828

828829
static void devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
829830
{
830831
devlink_fmsg_err_if_binary(fmsg);
831-
devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
832+
devlink_fmsg_put_value(fmsg, &value, sizeof(value),
833+
DEVLINK_VAR_ATTR_TYPE_U8);
832834
}
833835

834836
void devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
835837
{
836838
devlink_fmsg_err_if_binary(fmsg);
837-
devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
839+
devlink_fmsg_put_value(fmsg, &value, sizeof(value),
840+
DEVLINK_VAR_ATTR_TYPE_U32);
838841
}
839842
EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
840843

841844
static void devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
842845
{
843846
devlink_fmsg_err_if_binary(fmsg);
844-
devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
847+
devlink_fmsg_put_value(fmsg, &value, sizeof(value),
848+
DEVLINK_VAR_ATTR_TYPE_U64);
845849
}
846850

847851
void devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
848852
{
849853
devlink_fmsg_err_if_binary(fmsg);
850-
devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, NLA_NUL_STRING);
854+
devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
855+
DEVLINK_VAR_ATTR_TYPE_NUL_STRING);
851856
}
852857
EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
853858

@@ -857,7 +862,8 @@ void devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
857862
if (!fmsg->err && !fmsg->putting_binary)
858863
fmsg->err = -EINVAL;
859864

860-
devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
865+
devlink_fmsg_put_value(fmsg, value, value_len,
866+
DEVLINK_VAR_ATTR_TYPE_BINARY);
861867
}
862868
EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
863869

@@ -927,44 +933,27 @@ void devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
927933
}
928934
EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
929935

930-
static int
931-
devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
932-
{
933-
switch (msg->nla_type) {
934-
case NLA_FLAG:
935-
case NLA_U8:
936-
case NLA_U32:
937-
case NLA_U64:
938-
case NLA_NUL_STRING:
939-
case NLA_BINARY:
940-
return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
941-
msg->nla_type);
942-
default:
943-
return -EINVAL;
944-
}
945-
}
946-
947936
static int
948937
devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
949938
{
950939
int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
951940
u8 tmp;
952941

953942
switch (msg->nla_type) {
954-
case NLA_FLAG:
943+
case DEVLINK_VAR_ATTR_TYPE_FLAG:
955944
/* Always provide flag data, regardless of its value */
956945
tmp = *(bool *)msg->value;
957946

958947
return nla_put_u8(skb, attrtype, tmp);
959-
case NLA_U8:
948+
case DEVLINK_VAR_ATTR_TYPE_U8:
960949
return nla_put_u8(skb, attrtype, *(u8 *)msg->value);
961-
case NLA_U32:
950+
case DEVLINK_VAR_ATTR_TYPE_U32:
962951
return nla_put_u32(skb, attrtype, *(u32 *)msg->value);
963-
case NLA_U64:
952+
case DEVLINK_VAR_ATTR_TYPE_U64:
964953
return devlink_nl_put_u64(skb, attrtype, *(u64 *)msg->value);
965-
case NLA_NUL_STRING:
954+
case DEVLINK_VAR_ATTR_TYPE_NUL_STRING:
966955
return nla_put_string(skb, attrtype, (char *)&msg->value);
967-
case NLA_BINARY:
956+
case DEVLINK_VAR_ATTR_TYPE_BINARY:
968957
return nla_put(skb, attrtype, msg->len, (void *)&msg->value);
969958
default:
970959
return -EINVAL;
@@ -998,7 +987,8 @@ devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
998987
err = nla_put_flag(skb, item->attrtype);
999988
break;
1000989
case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
1001-
err = devlink_fmsg_item_fill_type(item, skb);
990+
err = nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
991+
item->nla_type);
1002992
if (err)
1003993
break;
1004994
err = devlink_fmsg_item_fill_data(item, skb);

net/devlink/netlink_gen.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@
1010

1111
#include <uapi/linux/devlink.h>
1212

13+
/* Sparse enums validation callbacks */
14+
static int
15+
devlink_attr_param_type_validate(const struct nlattr *attr,
16+
struct netlink_ext_ack *extack)
17+
{
18+
switch (nla_get_u8(attr)) {
19+
case DEVLINK_VAR_ATTR_TYPE_U8:
20+
fallthrough;
21+
case DEVLINK_VAR_ATTR_TYPE_U16:
22+
fallthrough;
23+
case DEVLINK_VAR_ATTR_TYPE_U32:
24+
fallthrough;
25+
case DEVLINK_VAR_ATTR_TYPE_U64:
26+
fallthrough;
27+
case DEVLINK_VAR_ATTR_TYPE_STRING:
28+
fallthrough;
29+
case DEVLINK_VAR_ATTR_TYPE_FLAG:
30+
fallthrough;
31+
case DEVLINK_VAR_ATTR_TYPE_NUL_STRING:
32+
fallthrough;
33+
case DEVLINK_VAR_ATTR_TYPE_BINARY:
34+
return 0;
35+
}
36+
NL_SET_ERR_MSG_ATTR(extack, attr, "invalid enum value");
37+
return -EINVAL;
38+
}
39+
1340
/* Common nested types */
1441
const struct nla_policy devlink_dl_port_function_nl_policy[DEVLINK_PORT_FN_ATTR_CAPS + 1] = {
1542
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY, },
@@ -273,7 +300,7 @@ static const struct nla_policy devlink_param_set_nl_policy[DEVLINK_ATTR_PARAM_VA
273300
[DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING, },
274301
[DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, },
275302
[DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING, },
276-
[DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8, },
303+
[DEVLINK_ATTR_PARAM_TYPE] = NLA_POLICY_VALIDATE_FN(NLA_U8, &devlink_attr_param_type_validate),
277304
[DEVLINK_ATTR_PARAM_VALUE_CMODE] = NLA_POLICY_MAX(NLA_U8, 2),
278305
};
279306

net/devlink/param.c

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,6 @@ static int devlink_param_set(struct devlink *devlink,
166166
return param->set(devlink, param->id, ctx, extack);
167167
}
168168

169-
static int
170-
devlink_param_type_to_nla_type(enum devlink_param_type param_type)
171-
{
172-
switch (param_type) {
173-
case DEVLINK_PARAM_TYPE_U8:
174-
return NLA_U8;
175-
case DEVLINK_PARAM_TYPE_U16:
176-
return NLA_U16;
177-
case DEVLINK_PARAM_TYPE_U32:
178-
return NLA_U32;
179-
case DEVLINK_PARAM_TYPE_STRING:
180-
return NLA_STRING;
181-
case DEVLINK_PARAM_TYPE_BOOL:
182-
return NLA_FLAG;
183-
default:
184-
return -EINVAL;
185-
}
186-
}
187-
188169
static int
189170
devlink_nl_param_value_fill_one(struct sk_buff *msg,
190171
enum devlink_param_type type,
@@ -247,7 +228,6 @@ static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
247228
struct devlink_param_gset_ctx ctx;
248229
struct nlattr *param_values_list;
249230
struct nlattr *param_attr;
250-
int nla_type;
251231
void *hdr;
252232
int err;
253233
int i;
@@ -293,11 +273,7 @@ static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
293273
goto param_nest_cancel;
294274
if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
295275
goto param_nest_cancel;
296-
297-
nla_type = devlink_param_type_to_nla_type(param->type);
298-
if (nla_type < 0)
299-
goto param_nest_cancel;
300-
if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
276+
if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, param->type))
301277
goto param_nest_cancel;
302278

303279
param_values_list = nla_nest_start_noflag(msg,
@@ -419,25 +395,7 @@ devlink_param_type_get_from_info(struct genl_info *info,
419395
if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_TYPE))
420396
return -EINVAL;
421397

422-
switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
423-
case NLA_U8:
424-
*param_type = DEVLINK_PARAM_TYPE_U8;
425-
break;
426-
case NLA_U16:
427-
*param_type = DEVLINK_PARAM_TYPE_U16;
428-
break;
429-
case NLA_U32:
430-
*param_type = DEVLINK_PARAM_TYPE_U32;
431-
break;
432-
case NLA_STRING:
433-
*param_type = DEVLINK_PARAM_TYPE_STRING;
434-
break;
435-
case NLA_FLAG:
436-
*param_type = DEVLINK_PARAM_TYPE_BOOL;
437-
break;
438-
default:
439-
return -EINVAL;
440-
}
398+
*param_type = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE]);
441399

442400
return 0;
443401
}

0 commit comments

Comments
 (0)