Skip to contentSkip to conversation

libbpf: Use proper errno value in nlattr

PATCH Applied Anton Protopopov 3 participants 3 messages May 10, 2025 – May 12, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")
---
 tools/lib/bpf/nlattr.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
tools/lib/bpf/nlattr.c +5 -7
index 975e265eab3b..0ba527d33ce4 100644
@@ -63,16 +63,16 @@ static int validate_nla(struct nlattr *nla, int maxtype,
 		minlen = nla_attr_minlen[pt->type];
 
 	if (libbpf_nla_len(nla) < minlen)
-		return -1;
+		return -EINVAL;
 
 	if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
-		return -1;
+		return -EINVAL;
 
 	if (pt->type == LIBBPF_NLA_STRING) {
 		char *data = libbpf_nla_data(nla);
 
 		if (data[libbpf_nla_len(nla) - 1] != '\0')
-			return -1;
+			return -EINVAL;
 	}
 
 	return 0;
@@ -118,7 +118,7 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
 		if (policy) {
 			err = validate_nla(nla, maxtype, policy);
 			if (err < 0)
-				goto errout;
+				return err;
 		}
 
 		if (tb[type])
@@ -128,9 +128,7 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
 		tb[type] = nla;
 	}
 
-	err = 0;
-errout:
-	return err;
+	return 0;
 }
 
 /**
Signature · 1 line
2.34.1
#1 lore raw
Quoted 6 lines · Anton Protopopov

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.

Quoted 7 lines
Fixes bbf48c18ee0c ("libbpf: add error reporting in XDP")
---
 tools/lib/bpf/nlattr.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

Thanks for the clean up fix! Applied to bpf-next.

Quoted 47 lines
tools/lib/bpf/nlattr.c +5 -8
index 975e265eab3b..0ba527d33ce4 100644
@@ -63,16 +63,16 @@ static int validate_nla(struct nlattr *nla, int maxtype,
                minlen = nla_attr_minlen[pt->type];
 
        if (libbpf_nla_len(nla) < minlen)
-               return -1;
+               return -EINVAL;
 
        if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
-               return -1;
+               return -EINVAL;
 
        if (pt->type == LIBBPF_NLA_STRING) {
                char *data = libbpf_nla_data(nla);
 
                if (data[libbpf_nla_len(nla) - 1] != '\0')
-                       return -1;
+                       return -EINVAL;
        }
 
        return 0;
@@ -118,7 +118,7 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
                if (policy) {
                        err = validate_nla(nla, maxtype, policy);
                        if (err < 0)
-                               goto errout;
+                               return err;
                }
 
                if (tb[type])
@@ -128,9 +128,7 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
                tb[type] = nla;
        }
 
-       err = 0;
-errout:
-       return err;
+       return 0;
 }
 
 /**
--

2.34.1

#2 lore raw
In reply to Anton Protopopov
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 10 lines · On Sat, 10 May 2025 18:20:11 +0000 you wrote:

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")

[...]

Here is the summary with links:
- [v1,bpf-next] libbpf: Use proper errno value in nlattr

    https://git.kernel.org/bpf/bpf-next/c/fd5fd538a1f4

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html