Skip to content

Commit 82f31eb

Browse files
zenczykowskidavem330
authored andcommitted
net: port < inet_prot_sock(net) --> inet_port_requires_bind_service(net, port)
Note that the sysctl write accessor functions guarantee that: net->ipv4.sysctl_ip_prot_sock <= net->ipv4.ip_local_ports.range[0] invariant is maintained, and as such the max() in selinux hooks is actually spurious. ie. even though if (snum < max(inet_prot_sock(sock_net(sk)), low) || snum > high) { per logic is the same as if ((snum < inet_prot_sock(sock_net(sk)) && snum < low) || snum > high) { it is actually functionally equivalent to: if (snum < low || snum > high) { which is equivalent to: if (snum < inet_prot_sock(sock_net(sk)) || snum < low || snum > high) { even though the first clause is spurious. But we want to hold on to it in case we ever want to change what what inet_port_requires_bind_service() means (for example by changing it from a, by default, [0..1024) range to some sort of set). Test: builds, git 'grep inet_prot_sock' finds no other references Cc: Eric Dumazet <[email protected]> Signed-off-by: Maciej Żenczykowski <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e94a5d1 commit 82f31eb

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

include/net/ip.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,9 @@ static inline bool sysctl_dev_name_is_allowed(const char *name)
351351
return strcmp(name, "default") != 0 && strcmp(name, "all") != 0;
352352
}
353353

354-
static inline int inet_prot_sock(struct net *net)
354+
static inline bool inet_port_requires_bind_service(struct net *net, unsigned short port)
355355
{
356-
return net->ipv4.sysctl_ip_prot_sock;
356+
return port < net->ipv4.sysctl_ip_prot_sock;
357357
}
358358

359359
#else
@@ -362,9 +362,9 @@ static inline bool inet_is_local_reserved_port(struct net *net, int port)
362362
return false;
363363
}
364364

365-
static inline int inet_prot_sock(struct net *net)
365+
static inline bool inet_port_requires_bind_service(struct net *net, unsigned short port)
366366
{
367-
return PROT_SOCK;
367+
return port < PROT_SOCK;
368368
}
369369
#endif
370370

net/ipv4/af_inet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
495495

496496
snum = ntohs(addr->sin_port);
497497
err = -EACCES;
498-
if (snum && snum < inet_prot_sock(net) &&
498+
if (snum && inet_port_requires_bind_service(net, snum) &&
499499
!ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
500500
goto out;
501501

net/ipv6/af_inet6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
292292
return -EINVAL;
293293

294294
snum = ntohs(addr->sin6_port);
295-
if (snum && snum < inet_prot_sock(net) &&
295+
if (snum && inet_port_requires_bind_service(net, snum) &&
296296
!ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
297297
return -EACCES;
298298

net/netfilter/ipvs/ip_vs_ctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol
423423

424424
if (!svc && protocol == IPPROTO_TCP &&
425425
atomic_read(&ipvs->ftpsvc_counter) &&
426-
(vport == FTPDATA || ntohs(vport) >= inet_prot_sock(ipvs->net))) {
426+
(vport == FTPDATA || !inet_port_requires_bind_service(ipvs->net, ntohs(vport)))) {
427427
/*
428428
* Check if ftp service entry exists, the packet
429429
* might belong to FTP data connections.

net/sctp/socket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
384384
}
385385
}
386386

387-
if (snum && snum < inet_prot_sock(net) &&
387+
if (snum && inet_port_requires_bind_service(net, snum) &&
388388
!ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
389389
return -EACCES;
390390

@@ -1061,7 +1061,7 @@ static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
10611061
if (sctp_autobind(sk))
10621062
return -EAGAIN;
10631063
} else {
1064-
if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1064+
if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) &&
10651065
!ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
10661066
return -EACCES;
10671067
}

security/selinux/hooks.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4623,8 +4623,8 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, in
46234623

46244624
inet_get_local_port_range(sock_net(sk), &low, &high);
46254625

4626-
if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4627-
snum > high) {
4626+
if (inet_port_requires_bind_service(sock_net(sk), snum) ||
4627+
snum < low || snum > high) {
46284628
err = sel_netport_sid(sk->sk_protocol,
46294629
snum, &sid);
46304630
if (err)

0 commit comments

Comments
 (0)