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

sock: Introduce sk_set_prio_allowed helper function

Simplify priority setting permissions with the 'sk_set_prio_allowed'
function, centralizing the validation logic. This change is made in
anticipation of a second caller in a following patch.
No functional changes.

Reviewed-by: Willem de Bruijn <willemb@google.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Suggested-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: Anna Emese Nyiri <annaemesenyiri@gmail.com>
Link: https://patch.msgid.link/20241213084457.45120-2-annaemesenyiri@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Anna Emese Nyiri and committed by
Jakub Kicinski
77ec16be 4fe20553

+8 -3
+8 -3
net/core/sock.c
··· 454 454 return 0; 455 455 } 456 456 457 + static bool sk_set_prio_allowed(const struct sock *sk, int val) 458 + { 459 + return ((val >= TC_PRIO_BESTEFFORT && val <= TC_PRIO_INTERACTIVE) || 460 + sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) || 461 + sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)); 462 + } 463 + 457 464 static bool sock_needs_netstamp(const struct sock *sk) 458 465 { 459 466 switch (sk->sk_family) { ··· 1200 1193 /* handle options which do not require locking the socket. */ 1201 1194 switch (optname) { 1202 1195 case SO_PRIORITY: 1203 - if ((val >= 0 && val <= 6) || 1204 - sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) || 1205 - sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { 1196 + if (sk_set_prio_allowed(sk, val)) { 1206 1197 sock_set_priority(sk, val); 1207 1198 return 0; 1208 1199 }