Skip to content

Commit 6f455f5

Browse files
jmberg-inteldavem330
authored andcommitted
netlink: add NLA_MIN_LEN
Rather than using NLA_UNSPEC for this type of thing, use NLA_MIN_LEN so we can make NLA_UNSPEC be NLA_REJECT under certain conditions for future attributes. While at it, also use NLA_EXACT_LEN for the struct example. Signed-off-by: Johannes Berg <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f6ad55a commit 6f455f5

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

include/net/netlink.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ enum {
183183
NLA_REJECT,
184184
NLA_EXACT_LEN,
185185
NLA_EXACT_LEN_WARN,
186+
NLA_MIN_LEN,
186187
__NLA_TYPE_MAX,
187188
};
188189

@@ -212,6 +213,7 @@ enum nla_policy_validation {
212213
* NLA_NUL_STRING Maximum length of string (excluding NUL)
213214
* NLA_FLAG Unused
214215
* NLA_BINARY Maximum length of attribute payload
216+
* NLA_MIN_LEN Minimum length of attribute payload
215217
* NLA_NESTED,
216218
* NLA_NESTED_ARRAY Length verification is done by checking len of
217219
* nested header (or empty); len field is used if
@@ -230,6 +232,7 @@ enum nla_policy_validation {
230232
* it is rejected.
231233
* NLA_EXACT_LEN_WARN Attribute should have exactly this length, a warning
232234
* is logged if it is longer, shorter is rejected.
235+
* NLA_MIN_LEN Minimum length of attribute payload
233236
* All other Minimum length of attribute payload
234237
*
235238
* Meaning of `validation_data' field:
@@ -281,7 +284,7 @@ enum nla_policy_validation {
281284
* static const struct nla_policy my_policy[ATTR_MAX+1] = {
282285
* [ATTR_FOO] = { .type = NLA_U16 },
283286
* [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
284-
* [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
287+
* [ATTR_BAZ] = { .type = NLA_EXACT_LEN, .len = sizeof(struct mystruct) },
285288
* [ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags },
286289
* };
287290
*/
@@ -302,6 +305,7 @@ struct nla_policy {
302305
#define NLA_POLICY_EXACT_LEN(_len) { .type = NLA_EXACT_LEN, .len = _len }
303306
#define NLA_POLICY_EXACT_LEN_WARN(_len) { .type = NLA_EXACT_LEN_WARN, \
304307
.len = _len }
308+
#define NLA_POLICY_MIN_LEN(_len) { .type = NLA_MIN_LEN, .len = _len }
305309

306310
#define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN)
307311
#define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)

lib/nlattr.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,17 @@ static int validate_nla(const struct nlattr *nla, int maxtype,
278278
}
279279
}
280280
break;
281+
282+
case NLA_UNSPEC:
283+
case NLA_MIN_LEN:
284+
if (attrlen < pt->len)
285+
goto out_err;
286+
break;
287+
281288
default:
282289
if (pt->len)
283290
minlen = pt->len;
284-
else if (pt->type != NLA_UNSPEC)
291+
else
285292
minlen = nla_attr_minlen[pt->type];
286293

287294
if (attrlen < minlen)

0 commit comments

Comments
 (0)