Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: bbf48c18ee0c ("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

authored by

Anton Protopopov and committed by
Andrii Nakryiko
fd5fd538 3a320ed3

+7 -8
+7 -8
tools/lib/bpf/nlattr.c
··· 63 63 minlen = nla_attr_minlen[pt->type]; 64 64 65 65 if (libbpf_nla_len(nla) < minlen) 66 - return -1; 66 + return -EINVAL; 67 67 68 68 if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen) 69 - return -1; 69 + return -EINVAL; 70 70 71 71 if (pt->type == LIBBPF_NLA_STRING) { 72 72 char *data = libbpf_nla_data(nla); 73 73 74 74 if (data[libbpf_nla_len(nla) - 1] != '\0') 75 - return -1; 75 + return -EINVAL; 76 76 } 77 77 78 78 return 0; ··· 118 118 if (policy) { 119 119 err = validate_nla(nla, maxtype, policy); 120 120 if (err < 0) 121 - goto errout; 121 + return err; 122 122 } 123 123 124 - if (tb[type]) 124 + if (tb[type]) { 125 125 pr_warn("Attribute of type %#x found multiple times in message, " 126 126 "previous attribute is being ignored.\n", type); 127 + } 127 128 128 129 tb[type] = nla; 129 130 } 130 131 131 - err = 0; 132 - errout: 133 - return err; 132 + return 0; 134 133 } 135 134 136 135 /**