Commit a7371be8 authored by Jakub Kicinski's avatar Jakub Kicinski
Browse files

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/20250425214808.507732-1-saeed@kernel.org

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 #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/20250505114513.53370-1-jiri@resnulli.us


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents df6a69bc 88debb52
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -202,6 +202,28 @@ definitions:
        name: exception
      -
        name: control
  -
    type: enum
    name: var-attr-type
    entries:
      -
        name: u8
        value: 1
      -
        name: u16
      -
        name: u32
      -
        name: u64
      -
        name: string
      -
        name: flag
      -
        name: nul_string
        value: 10
      -
        name: binary

attribute-sets:
  -
@@ -498,6 +520,7 @@ attribute-sets:
      -
        name: param-type
        type: u8
        enum: var-attr-type

      # TODO: fill in the attributes in between

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

      # TODO: fill in the attributes in between

+5 −5
Original line number Diff line number Diff line
@@ -420,11 +420,11 @@ typedef u64 devlink_resource_occ_get_t(void *priv);

#define __DEVLINK_PARAM_MAX_STRING_VALUE 32
enum devlink_param_type {
	DEVLINK_PARAM_TYPE_U8,
	DEVLINK_PARAM_TYPE_U16,
	DEVLINK_PARAM_TYPE_U32,
	DEVLINK_PARAM_TYPE_STRING,
	DEVLINK_PARAM_TYPE_BOOL,
	DEVLINK_PARAM_TYPE_U8 = DEVLINK_VAR_ATTR_TYPE_U8,
	DEVLINK_PARAM_TYPE_U16 = DEVLINK_VAR_ATTR_TYPE_U16,
	DEVLINK_PARAM_TYPE_U32 = DEVLINK_VAR_ATTR_TYPE_U32,
	DEVLINK_PARAM_TYPE_STRING = DEVLINK_VAR_ATTR_TYPE_STRING,
	DEVLINK_PARAM_TYPE_BOOL = DEVLINK_VAR_ATTR_TYPE_FLAG,
};

union devlink_param_value {
+15 −0
Original line number Diff line number Diff line
@@ -385,6 +385,21 @@ enum devlink_linecard_state {
	DEVLINK_LINECARD_STATE_MAX = __DEVLINK_LINECARD_STATE_MAX - 1
};

/* Variable attribute type. */
enum devlink_var_attr_type {
	/* Following values relate to the internal NLA_* values */
	DEVLINK_VAR_ATTR_TYPE_U8 = 1,
	DEVLINK_VAR_ATTR_TYPE_U16,
	DEVLINK_VAR_ATTR_TYPE_U32,
	DEVLINK_VAR_ATTR_TYPE_U64,
	DEVLINK_VAR_ATTR_TYPE_STRING,
	DEVLINK_VAR_ATTR_TYPE_FLAG,
	DEVLINK_VAR_ATTR_TYPE_NUL_STRING = 10,
	DEVLINK_VAR_ATTR_TYPE_BINARY,
	__DEVLINK_VAR_ATTR_TYPE_CUSTOM_BASE = 0x80,
	/* Any possible custom types, unrelated to NLA_* values go below */
};

enum devlink_attr {
	/* don't change the order or add anything between, this is ABI! */
	DEVLINK_ATTR_UNSPEC,
+21 −31
Original line number Diff line number Diff line
@@ -735,7 +735,7 @@ static void devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
		return;
	}

	item->nla_type = NLA_NUL_STRING;
	item->nla_type = DEVLINK_VAR_ATTR_TYPE_NUL_STRING;
	item->len = strlen(name) + 1;
	item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
	memcpy(&item->value, name, item->len);
@@ -822,32 +822,37 @@ static void devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
static void devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
{
	devlink_fmsg_err_if_binary(fmsg);
	devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
	devlink_fmsg_put_value(fmsg, &value, sizeof(value),
			       DEVLINK_VAR_ATTR_TYPE_FLAG);
}

static void devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
{
	devlink_fmsg_err_if_binary(fmsg);
	devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
	devlink_fmsg_put_value(fmsg, &value, sizeof(value),
			       DEVLINK_VAR_ATTR_TYPE_U8);
}

void devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
{
	devlink_fmsg_err_if_binary(fmsg);
	devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
	devlink_fmsg_put_value(fmsg, &value, sizeof(value),
			       DEVLINK_VAR_ATTR_TYPE_U32);
}
EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);

static void devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
{
	devlink_fmsg_err_if_binary(fmsg);
	devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
	devlink_fmsg_put_value(fmsg, &value, sizeof(value),
			       DEVLINK_VAR_ATTR_TYPE_U64);
}

void devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
{
	devlink_fmsg_err_if_binary(fmsg);
	devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, NLA_NUL_STRING);
	devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
			       DEVLINK_VAR_ATTR_TYPE_NUL_STRING);
}
EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);

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

	devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
	devlink_fmsg_put_value(fmsg, value, value_len,
			       DEVLINK_VAR_ATTR_TYPE_BINARY);
}
EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);

@@ -927,23 +933,6 @@ void devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
}
EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);

static int
devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
{
	switch (msg->nla_type) {
	case NLA_FLAG:
	case NLA_U8:
	case NLA_U32:
	case NLA_U64:
	case NLA_NUL_STRING:
	case NLA_BINARY:
		return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
				  msg->nla_type);
	default:
		return -EINVAL;
	}
}

static int
devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
{
@@ -951,20 +940,20 @@ devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
	u8 tmp;

	switch (msg->nla_type) {
	case NLA_FLAG:
	case DEVLINK_VAR_ATTR_TYPE_FLAG:
		/* Always provide flag data, regardless of its value */
		tmp = *(bool *)msg->value;

		return nla_put_u8(skb, attrtype, tmp);
	case NLA_U8:
	case DEVLINK_VAR_ATTR_TYPE_U8:
		return nla_put_u8(skb, attrtype, *(u8 *)msg->value);
	case NLA_U32:
	case DEVLINK_VAR_ATTR_TYPE_U32:
		return nla_put_u32(skb, attrtype, *(u32 *)msg->value);
	case NLA_U64:
	case DEVLINK_VAR_ATTR_TYPE_U64:
		return devlink_nl_put_u64(skb, attrtype, *(u64 *)msg->value);
	case NLA_NUL_STRING:
	case DEVLINK_VAR_ATTR_TYPE_NUL_STRING:
		return nla_put_string(skb, attrtype, (char *)&msg->value);
	case NLA_BINARY:
	case DEVLINK_VAR_ATTR_TYPE_BINARY:
		return nla_put(skb, attrtype, msg->len, (void *)&msg->value);
	default:
		return -EINVAL;
@@ -998,7 +987,8 @@ devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
			err = nla_put_flag(skb, item->attrtype);
			break;
		case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
			err = devlink_fmsg_item_fill_type(item, skb);
			err = nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
					 item->nla_type);
			if (err)
				break;
			err = devlink_fmsg_item_fill_data(item, skb);
+28 −1
Original line number Diff line number Diff line
@@ -10,6 +10,33 @@

#include <uapi/linux/devlink.h>

/* Sparse enums validation callbacks */
static int
devlink_attr_param_type_validate(const struct nlattr *attr,
				 struct netlink_ext_ack *extack)
{
	switch (nla_get_u8(attr)) {
	case DEVLINK_VAR_ATTR_TYPE_U8:
		fallthrough;
	case DEVLINK_VAR_ATTR_TYPE_U16:
		fallthrough;
	case DEVLINK_VAR_ATTR_TYPE_U32:
		fallthrough;
	case DEVLINK_VAR_ATTR_TYPE_U64:
		fallthrough;
	case DEVLINK_VAR_ATTR_TYPE_STRING:
		fallthrough;
	case DEVLINK_VAR_ATTR_TYPE_FLAG:
		fallthrough;
	case DEVLINK_VAR_ATTR_TYPE_NUL_STRING:
		fallthrough;
	case DEVLINK_VAR_ATTR_TYPE_BINARY:
		return 0;
	}
	NL_SET_ERR_MSG_ATTR(extack, attr, "invalid enum value");
	return -EINVAL;
}

/* Common nested types */
const struct nla_policy devlink_dl_port_function_nl_policy[DEVLINK_PORT_FN_ATTR_CAPS + 1] = {
	[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
	[DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING, },
	[DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, },
	[DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING, },
	[DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8, },
	[DEVLINK_ATTR_PARAM_TYPE] = NLA_POLICY_VALIDATE_FN(NLA_U8, &devlink_attr_param_type_validate),
	[DEVLINK_ATTR_PARAM_VALUE_CMODE] = NLA_POLICY_MAX(NLA_U8, 2),
};

Loading