Skip to content

Commit 6e237d0

Browse files
dsaherndavem330
authored andcommitted
netlink: Relax attr validation for fixed length types
Commit 28033ae ("net: netlink: Update attr validation to require exact length for some types") requires attributes using types NLA_U* and NLA_S* to have an exact length. This change is exposing bugs in various userspace commands that are sending attributes with an invalid length (e.g., attribute has type NLA_U8 and userspace sends NLA_U32). While the commands are clearly broken and need to be fixed, users are arguing that the sudden change in enforcement is breaking older commands on newer kernels for use cases that otherwise "worked". Relax the validation to print a warning mesage similar to what is done for messages containing extra bytes after parsing. Fixes: 28033ae ("net: netlink: Update attr validation to require exact length for some types") Signed-off-by: David Ahern <[email protected]> Reviewed-by: Johannes Berg <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 74c4b65 commit 6e237d0

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/nlattr.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
#include <linux/types.h>
1616
#include <net/netlink.h>
1717

18-
/* for these data types attribute length must be exactly given size */
18+
/* For these data types, attribute length should be exactly the given
19+
* size. However, to maintain compatibility with broken commands, if the
20+
* attribute length does not match the expected size a warning is emitted
21+
* to the user that the command is sending invalid data and needs to be fixed.
22+
*/
1923
static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
2024
[NLA_U8] = sizeof(u8),
2125
[NLA_U16] = sizeof(u16),
@@ -28,8 +32,16 @@ static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
2832
};
2933

3034
static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
35+
[NLA_U8] = sizeof(u8),
36+
[NLA_U16] = sizeof(u16),
37+
[NLA_U32] = sizeof(u32),
38+
[NLA_U64] = sizeof(u64),
3139
[NLA_MSECS] = sizeof(u64),
3240
[NLA_NESTED] = NLA_HDRLEN,
41+
[NLA_S8] = sizeof(s8),
42+
[NLA_S16] = sizeof(s16),
43+
[NLA_S32] = sizeof(s32),
44+
[NLA_S64] = sizeof(s64),
3345
};
3446

3547
static int validate_nla_bitfield32(const struct nlattr *nla,
@@ -69,11 +81,9 @@ static int validate_nla(const struct nlattr *nla, int maxtype,
6981

7082
BUG_ON(pt->type > NLA_TYPE_MAX);
7183

72-
/* for data types NLA_U* and NLA_S* require exact length */
73-
if (nla_attr_len[pt->type]) {
74-
if (attrlen != nla_attr_len[pt->type])
75-
return -ERANGE;
76-
return 0;
84+
if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
85+
pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
86+
current->comm, type);
7787
}
7888

7989
switch (pt->type) {

0 commit comments

Comments
 (0)