Skip to contentSkip to conversation

net: print error if SO_BUSY_POLL_BUDGET is large

PATCH In review Joe Damato 3 participants 3 messages Jan 25, 2024 – Jan 26, 2024

Subsystems (3)
lore
Share & tools
lore

Joe Damato

author
#0 lore raw

PATCH 1/1 net: print error if SO_BUSY_POLL_BUDGET is large

When drivers call netif_napi_add_weight with a weight that is larger than NAPI_POLL_WEIGHT, the networking code allows the larger weight, but prints an error.

Replicate this check for SO_BUSY_POLL_BUDGET; check if the user specified amount exceeds NAPI_POLL_WEIGHT, allow it anyway, but print an error.

---
 net/core/sock.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/sock.c +3 -0
index 158dbdebce6a..ed243bd0dd77 100644
@@ -1153,6 +1153,9 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
 			return -EPERM;
 		if (val < 0 || val > U16_MAX)
 			return -EINVAL;
+		if (val > NAPI_POLL_WEIGHT)
+			pr_err("SO_BUSY_POLL_BUDGET %u exceeds suggested maximum %u\n", val,
+			       NAPI_POLL_WEIGHT);
 		WRITE_ONCE(sk->sk_busy_poll_budget, val);
 		return 0;
 #endif
Signature · 1 line
2.25.1
#1 lore raw
Quoted 19 lines · Joe Damato

When drivers call netif_napi_add_weight with a weight that is larger than NAPI_POLL_WEIGHT, the networking code allows the larger weight, but prints an error.

Replicate this check for SO_BUSY_POLL_BUDGET; check if the user specified amount exceeds NAPI_POLL_WEIGHT, allow it anyway, but print an error.

---
 net/core/sock.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/sock.c +0 -0
index 158dbdebce6a..ed243bd0dd77 100644

net/core/sock.c int sk_setsockopt(struct sock *sk, int level, int optname, · line 1160

Quoted 8 lines
net/core/sock.c +3 -0
@@ -1153,6 +1153,9 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
                        return -EPERM;
                if (val < 0 || val > U16_MAX)
                        return -EINVAL;
+               if (val > NAPI_POLL_WEIGHT)
+                       pr_err("SO_BUSY_POLL_BUDGET %u exceeds suggested maximum %u\n", val,
+                              NAPI_POLL_WEIGHT);
                WRITE_ONCE(sk->sk_busy_poll_budget, val);
                return 0;

This is code run by privileged (CAP_NET_ADMIN) users, please do not spam the console with such a message.

My point was : Do not allow an unpriv user to set an arbitrary value.

netif_napi_add_weight() is used from kernel drivers, we network maintainers usually object
if a driver attempts to use a big value, at code review time.

#2 lore raw
In reply to Joe Damato

Hi Joe,

Quoted 18 lines · Joe Damato

When drivers call netif_napi_add_weight with a weight that is larger than NAPI_POLL_WEIGHT, the networking code allows the larger weight, but prints an error.

Replicate this check for SO_BUSY_POLL_BUDGET; check if the user specified amount exceeds NAPI_POLL_WEIGHT, allow it anyway, but print an error.

---
 net/core/sock.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/sock.c +0 -0
index 158dbdebce6a..ed243bd0dd77 100644

net/core/sock.c int sk_setsockopt(struct sock *sk, int level, int optname, · line 1158

Quoted 6 lines
net/core/sock.c +3 -0
@@ -1153,6 +1153,9 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
 			return -EPERM;
 		if (val < 0 || val > U16_MAX)
 			return -EINVAL;
+		if (val > NAPI_POLL_WEIGHT)
+			pr_err("SO_BUSY_POLL_BUDGET %u exceeds suggested maximum %u\n", val,
+			       NAPI_POLL_WEIGHT);

'val' is a signed value variable. I suspect it will never be negative given the line above (val < 0 || val > U16_MAX), but, I am wondering if you should still print it as signed integer (%d) to keep it consistent.