Commit c889a99a authored by Jordan Rife's avatar Jordan Rife Committed by David S. Miller

net: prevent address rewrite in kernel_bind()

Similar to the change in commit 0bdf3993("net: Avoid address
overwrite in kernel_connect"), BPF hooks run on bind may rewrite the
address passed to kernel_bind(). This change

1) Makes a copy of the bind address in kernel_bind() to insulate
   callers.
2) Replaces direct calls to sock->ops->bind() in net with kernel_bind()

Link: https://lore.kernel.org/netdev/20230912013332.2048422-1-jrife@google.com/
Fixes: 4fbac77d ("bpf: Hooks for sys_bind")
Cc: stable@vger.kernel.org
Reviewed-by: default avatarWillem de Bruijn <willemb@google.com>
Signed-off-by: default avatarJordan Rife <jrife@google.com>
Reviewed-by: default avatarSimon Horman <horms@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 86a7e0b6
...@@ -1439,7 +1439,7 @@ static int bind_mcastif_addr(struct socket *sock, struct net_device *dev) ...@@ -1439,7 +1439,7 @@ static int bind_mcastif_addr(struct socket *sock, struct net_device *dev)
sin.sin_addr.s_addr = addr; sin.sin_addr.s_addr = addr;
sin.sin_port = 0; sin.sin_port = 0;
return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin)); return kernel_bind(sock, (struct sockaddr *)&sin, sizeof(sin));
} }
static void get_mcast_sockaddr(union ipvs_sockaddr *sa, int *salen, static void get_mcast_sockaddr(union ipvs_sockaddr *sa, int *salen,
...@@ -1546,7 +1546,7 @@ static int make_receive_sock(struct netns_ipvs *ipvs, int id, ...@@ -1546,7 +1546,7 @@ static int make_receive_sock(struct netns_ipvs *ipvs, int id,
get_mcast_sockaddr(&mcast_addr, &salen, &ipvs->bcfg, id); get_mcast_sockaddr(&mcast_addr, &salen, &ipvs->bcfg, id);
sock->sk->sk_bound_dev_if = dev->ifindex; sock->sk->sk_bound_dev_if = dev->ifindex;
result = sock->ops->bind(sock, (struct sockaddr *)&mcast_addr, salen); result = kernel_bind(sock, (struct sockaddr *)&mcast_addr, salen);
if (result < 0) { if (result < 0) {
pr_err("Error binding to the multicast addr\n"); pr_err("Error binding to the multicast addr\n");
goto error; goto error;
......
...@@ -145,7 +145,7 @@ int rds_tcp_conn_path_connect(struct rds_conn_path *cp) ...@@ -145,7 +145,7 @@ int rds_tcp_conn_path_connect(struct rds_conn_path *cp)
addrlen = sizeof(sin); addrlen = sizeof(sin);
} }
ret = sock->ops->bind(sock, addr, addrlen); ret = kernel_bind(sock, addr, addrlen);
if (ret) { if (ret) {
rdsdebug("bind failed with %d at address %pI6c\n", rdsdebug("bind failed with %d at address %pI6c\n",
ret, &conn->c_laddr); ret, &conn->c_laddr);
......
...@@ -306,7 +306,7 @@ struct socket *rds_tcp_listen_init(struct net *net, bool isv6) ...@@ -306,7 +306,7 @@ struct socket *rds_tcp_listen_init(struct net *net, bool isv6)
addr_len = sizeof(*sin); addr_len = sizeof(*sin);
} }
ret = sock->ops->bind(sock, (struct sockaddr *)&ss, addr_len); ret = kernel_bind(sock, (struct sockaddr *)&ss, addr_len);
if (ret < 0) { if (ret < 0) {
rdsdebug("could not bind %s listener socket: %d\n", rdsdebug("could not bind %s listener socket: %d\n",
isv6 ? "IPv6" : "IPv4", ret); isv6 ? "IPv6" : "IPv4", ret);
......
...@@ -3516,7 +3516,12 @@ static long compat_sock_ioctl(struct file *file, unsigned int cmd, ...@@ -3516,7 +3516,12 @@ static long compat_sock_ioctl(struct file *file, unsigned int cmd,
int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen) int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
{ {
return READ_ONCE(sock->ops)->bind(sock, addr, addrlen); struct sockaddr_storage address;
memcpy(&address, addr, addrlen);
return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr *)&address,
addrlen);
} }
EXPORT_SYMBOL(kernel_bind); EXPORT_SYMBOL(kernel_bind);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment