Skip to content

Commit fd5fd53

Browse files
aspskanakryiko
authored andcommitted
libbpf: Use proper errno value in nlattr
Return value of the validate_nla() function can be propagated all the way up to users of libbpf API. In case of error this libbpf version of validate_nla returns -1 which will be seen as -EPERM from user's point of view. Instead, return a more reasonable -EINVAL. Fixes: bbf48c1 ("libbpf: add error reporting in XDP") Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Anton Protopopov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 3a320ed commit fd5fd53

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

tools/lib/bpf/nlattr.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ static int validate_nla(struct nlattr *nla, int maxtype,
6363
minlen = nla_attr_minlen[pt->type];
6464

6565
if (libbpf_nla_len(nla) < minlen)
66-
return -1;
66+
return -EINVAL;
6767

6868
if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
69-
return -1;
69+
return -EINVAL;
7070

7171
if (pt->type == LIBBPF_NLA_STRING) {
7272
char *data = libbpf_nla_data(nla);
7373

7474
if (data[libbpf_nla_len(nla) - 1] != '\0')
75-
return -1;
75+
return -EINVAL;
7676
}
7777

7878
return 0;
@@ -118,19 +118,18 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
118118
if (policy) {
119119
err = validate_nla(nla, maxtype, policy);
120120
if (err < 0)
121-
goto errout;
121+
return err;
122122
}
123123

124-
if (tb[type])
124+
if (tb[type]) {
125125
pr_warn("Attribute of type %#x found multiple times in message, "
126126
"previous attribute is being ignored.\n", type);
127+
}
127128

128129
tb[type] = nla;
129130
}
130131

131-
err = 0;
132-
errout:
133-
return err;
132+
return 0;
134133
}
135134

136135
/**

0 commit comments

Comments
 (0)