libbpf: Use proper errno value in nlattr

[ Upstream commit fd5fd538a1f4b34cee6823ba0ddda2f7a55aca96 ]

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: bbf48c18ee ("libbpf: add error reporting in XDP")
Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250510182011.2246631-1-a.s.protopopov@gmail.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Anton Protopopov
2025-05-10 18:20:11 +00:00
committed by Greg Kroah-Hartman
parent 07680e38b7
commit b44672c17c

View File

@@ -63,16 +63,16 @@ static int validate_nla(struct nlattr *nla, int maxtype,
minlen = nla_attr_minlen[pt->type]; minlen = nla_attr_minlen[pt->type];
if (libbpf_nla_len(nla) < minlen) if (libbpf_nla_len(nla) < minlen)
return -1; return -EINVAL;
if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen) if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
return -1; return -EINVAL;
if (pt->type == LIBBPF_NLA_STRING) { if (pt->type == LIBBPF_NLA_STRING) {
char *data = libbpf_nla_data(nla); char *data = libbpf_nla_data(nla);
if (data[libbpf_nla_len(nla) - 1] != '\0') if (data[libbpf_nla_len(nla) - 1] != '\0')
return -1; return -EINVAL;
} }
return 0; return 0;
@@ -118,19 +118,18 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
if (policy) { if (policy) {
err = validate_nla(nla, maxtype, policy); err = validate_nla(nla, maxtype, policy);
if (err < 0) if (err < 0)
goto errout; return err;
} }
if (tb[type]) if (tb[type]) {
pr_warn("Attribute of type %#x found multiple times in message, " pr_warn("Attribute of type %#x found multiple times in message, "
"previous attribute is being ignored.\n", type); "previous attribute is being ignored.\n", type);
}
tb[type] = nla; tb[type] = nla;
} }
err = 0; return 0;
errout:
return err;
} }
/** /**