mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-12-22 12:14:01 +08:00
6fa19f5637
syzbot was able to catch a bug in rds [1]
The issue here is that the socket might be found in a hash table
but that its refcount has already be set to 0 by another cpu.
We need to use refcount_inc_not_zero() to be safe here.
[1]
refcount_t: increment on 0; use-after-free.
WARNING: CPU: 1 PID: 23129 at lib/refcount.c:153 refcount_inc_checked lib/refcount.c:153 [inline]
WARNING: CPU: 1 PID: 23129 at lib/refcount.c:153 refcount_inc_checked+0x61/0x70 lib/refcount.c:151
Kernel panic - not syncing: panic_on_warn set ...
CPU: 1 PID: 23129 Comm: syz-executor3 Not tainted 5.0.0-rc4+ #53
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x1db/0x2d0 lib/dump_stack.c:113
panic+0x2cb/0x65c kernel/panic.c:214
__warn.cold+0x20/0x48 kernel/panic.c:571
report_bug+0x263/0x2b0 lib/bug.c:186
fixup_bug arch/x86/kernel/traps.c:178 [inline]
fixup_bug arch/x86/kernel/traps.c:173 [inline]
do_error_trap+0x11b/0x200 arch/x86/kernel/traps.c:271
do_invalid_op+0x37/0x50 arch/x86/kernel/traps.c:290
invalid_op+0x14/0x20 arch/x86/entry/entry_64.S:973
RIP: 0010:refcount_inc_checked lib/refcount.c:153 [inline]
RIP: 0010:refcount_inc_checked+0x61/0x70 lib/refcount.c:151
Code: 1d 51 63 c8 06 31 ff 89 de e8 eb 1b f2 fd 84 db 75 dd e8 a2 1a f2 fd 48 c7 c7 60 9f 81 88 c6 05 31 63 c8 06 01 e8 af 65 bb fd <0f> 0b eb c1 90 66 2e 0f 1f 84 00 00 00 00 00 55 48 89 e5 41 54 49
RSP: 0018:ffff8880a0cbf1e8 EFLAGS: 00010282
RAX: 0000000000000000 RBX: 0000000000000000 RCX: ffffc90006113000
RDX: 000000000001047d RSI: ffffffff81685776 RDI: 0000000000000005
RBP: ffff8880a0cbf1f8 R08: ffff888097c9e100 R09: ffffed1015ce5021
R10: ffffed1015ce5020 R11: ffff8880ae728107 R12: ffff8880723c20c0
R13: ffff8880723c24b0 R14: dffffc0000000000 R15: ffffed1014197e64
sock_hold include/net/sock.h:647 [inline]
rds_sock_addref+0x19/0x20 net/rds/af_rds.c:675
rds_find_bound+0x97c/0x1080 net/rds/bind.c:82
rds_recv_incoming+0x3be/0x1430 net/rds/recv.c:362
rds_loop_xmit+0xf3/0x2a0 net/rds/loop.c:96
rds_send_xmit+0x1355/0x2a10 net/rds/send.c:355
rds_sendmsg+0x323c/0x44e0 net/rds/send.c:1368
sock_sendmsg_nosec net/socket.c:621 [inline]
sock_sendmsg+0xdd/0x130 net/socket.c:631
__sys_sendto+0x387/0x5f0 net/socket.c:1788
__do_sys_sendto net/socket.c:1800 [inline]
__se_sys_sendto net/socket.c:1796 [inline]
__x64_sys_sendto+0xe1/0x1a0 net/socket.c:1796
do_syscall_64+0x1a3/0x800 arch/x86/entry/common.c:290
entry_SYSCALL_64_after_hwframe+0x49/0xbe
RIP: 0033:0x458089
Code: 6d b7 fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 0f 83 3b b7 fb ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:00007fc266df8c78 EFLAGS: 00000246 ORIG_RAX: 000000000000002c
RAX: ffffffffffffffda RBX: 0000000000000006 RCX: 0000000000458089
RDX: 0000000000000000 RSI: 00000000204b3fff RDI: 0000000000000005
RBP: 000000000073bf00 R08: 00000000202b4000 R09: 0000000000000010
R10: 0000000000000000 R11: 0000000000000246 R12: 00007fc266df96d4
R13: 00000000004c56e4 R14: 00000000004d94a8 R15: 00000000ffffffff
Fixes: cc4dfb7f70
("rds: fix two RCU related problems")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reported-by: syzbot <syzkaller@googlegroups.com>
Cc: Sowmini Varadhan <sowmini.varadhan@oracle.com>
Cc: Santosh Shilimkar <santosh.shilimkar@oracle.com>
Cc: rds-devel@oss.oracle.com
Cc: Cong Wang <xiyou.wangcong@gmail.com>
Acked-by: Santosh Shilimkar <santosh.shilimkar@oracle.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
283 lines
7.6 KiB
C
283 lines
7.6 KiB
C
/*
|
|
* Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
|
|
*
|
|
* This software is available to you under a choice of one of two
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
* General Public License (GPL) Version 2, available from the file
|
|
* COPYING in the main directory of this source tree, or the
|
|
* OpenIB.org BSD license below:
|
|
*
|
|
* Redistribution and use in source and binary forms, with or
|
|
* without modification, are permitted provided that the following
|
|
* conditions are met:
|
|
*
|
|
* - Redistributions of source code must retain the above
|
|
* copyright notice, this list of conditions and the following
|
|
* disclaimer.
|
|
*
|
|
* - Redistributions in binary form must reproduce the above
|
|
* copyright notice, this list of conditions and the following
|
|
* disclaimer in the documentation and/or other materials
|
|
* provided with the distribution.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <net/sock.h>
|
|
#include <linux/in.h>
|
|
#include <linux/ipv6.h>
|
|
#include <linux/if_arp.h>
|
|
#include <linux/jhash.h>
|
|
#include <linux/ratelimit.h>
|
|
#include "rds.h"
|
|
|
|
static struct rhashtable bind_hash_table;
|
|
|
|
static const struct rhashtable_params ht_parms = {
|
|
.nelem_hint = 768,
|
|
.key_len = RDS_BOUND_KEY_LEN,
|
|
.key_offset = offsetof(struct rds_sock, rs_bound_key),
|
|
.head_offset = offsetof(struct rds_sock, rs_bound_node),
|
|
.max_size = 16384,
|
|
.min_size = 1024,
|
|
};
|
|
|
|
/* Create a key for the bind hash table manipulation. Port is in network byte
|
|
* order.
|
|
*/
|
|
static inline void __rds_create_bind_key(u8 *key, const struct in6_addr *addr,
|
|
__be16 port, __u32 scope_id)
|
|
{
|
|
memcpy(key, addr, sizeof(*addr));
|
|
key += sizeof(*addr);
|
|
memcpy(key, &port, sizeof(port));
|
|
key += sizeof(port);
|
|
memcpy(key, &scope_id, sizeof(scope_id));
|
|
}
|
|
|
|
/*
|
|
* Return the rds_sock bound at the given local address.
|
|
*
|
|
* The rx path can race with rds_release. We notice if rds_release() has
|
|
* marked this socket and don't return a rs ref to the rx path.
|
|
*/
|
|
struct rds_sock *rds_find_bound(const struct in6_addr *addr, __be16 port,
|
|
__u32 scope_id)
|
|
{
|
|
u8 key[RDS_BOUND_KEY_LEN];
|
|
struct rds_sock *rs;
|
|
|
|
__rds_create_bind_key(key, addr, port, scope_id);
|
|
rcu_read_lock();
|
|
rs = rhashtable_lookup(&bind_hash_table, key, ht_parms);
|
|
if (rs && (sock_flag(rds_rs_to_sk(rs), SOCK_DEAD) ||
|
|
!refcount_inc_not_zero(&rds_rs_to_sk(rs)->sk_refcnt)))
|
|
rs = NULL;
|
|
|
|
rcu_read_unlock();
|
|
|
|
rdsdebug("returning rs %p for %pI6c:%u\n", rs, addr,
|
|
ntohs(port));
|
|
|
|
return rs;
|
|
}
|
|
|
|
/* returns -ve errno or +ve port */
|
|
static int rds_add_bound(struct rds_sock *rs, const struct in6_addr *addr,
|
|
__be16 *port, __u32 scope_id)
|
|
{
|
|
int ret = -EADDRINUSE;
|
|
u16 rover, last;
|
|
u8 key[RDS_BOUND_KEY_LEN];
|
|
|
|
if (*port != 0) {
|
|
rover = be16_to_cpu(*port);
|
|
if (rover == RDS_FLAG_PROBE_PORT)
|
|
return -EINVAL;
|
|
last = rover;
|
|
} else {
|
|
rover = max_t(u16, prandom_u32(), 2);
|
|
last = rover - 1;
|
|
}
|
|
|
|
do {
|
|
if (rover == 0)
|
|
rover++;
|
|
|
|
if (rover == RDS_FLAG_PROBE_PORT)
|
|
continue;
|
|
__rds_create_bind_key(key, addr, cpu_to_be16(rover),
|
|
scope_id);
|
|
if (rhashtable_lookup_fast(&bind_hash_table, key, ht_parms))
|
|
continue;
|
|
|
|
memcpy(rs->rs_bound_key, key, sizeof(rs->rs_bound_key));
|
|
rs->rs_bound_addr = *addr;
|
|
net_get_random_once(&rs->rs_hash_initval,
|
|
sizeof(rs->rs_hash_initval));
|
|
rs->rs_bound_port = cpu_to_be16(rover);
|
|
rs->rs_bound_node.next = NULL;
|
|
rds_sock_addref(rs);
|
|
if (!rhashtable_insert_fast(&bind_hash_table,
|
|
&rs->rs_bound_node, ht_parms)) {
|
|
*port = rs->rs_bound_port;
|
|
rs->rs_bound_scope_id = scope_id;
|
|
ret = 0;
|
|
rdsdebug("rs %p binding to %pI6c:%d\n",
|
|
rs, addr, (int)ntohs(*port));
|
|
break;
|
|
} else {
|
|
rs->rs_bound_addr = in6addr_any;
|
|
rds_sock_put(rs);
|
|
ret = -ENOMEM;
|
|
break;
|
|
}
|
|
} while (rover++ != last);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void rds_remove_bound(struct rds_sock *rs)
|
|
{
|
|
|
|
if (ipv6_addr_any(&rs->rs_bound_addr))
|
|
return;
|
|
|
|
rdsdebug("rs %p unbinding from %pI6c:%d\n",
|
|
rs, &rs->rs_bound_addr,
|
|
ntohs(rs->rs_bound_port));
|
|
|
|
rhashtable_remove_fast(&bind_hash_table, &rs->rs_bound_node, ht_parms);
|
|
rds_sock_put(rs);
|
|
rs->rs_bound_addr = in6addr_any;
|
|
}
|
|
|
|
int rds_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
struct rds_sock *rs = rds_sk_to_rs(sk);
|
|
struct in6_addr v6addr, *binding_addr;
|
|
struct rds_transport *trans;
|
|
__u32 scope_id = 0;
|
|
int ret = 0;
|
|
__be16 port;
|
|
|
|
/* We allow an RDS socket to be bound to either IPv4 or IPv6
|
|
* address.
|
|
*/
|
|
if (uaddr->sa_family == AF_INET) {
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
|
|
|
|
if (addr_len < sizeof(struct sockaddr_in) ||
|
|
sin->sin_addr.s_addr == htonl(INADDR_ANY) ||
|
|
sin->sin_addr.s_addr == htonl(INADDR_BROADCAST) ||
|
|
IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
|
|
return -EINVAL;
|
|
ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &v6addr);
|
|
binding_addr = &v6addr;
|
|
port = sin->sin_port;
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
} else if (uaddr->sa_family == AF_INET6) {
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)uaddr;
|
|
int addr_type;
|
|
|
|
if (addr_len < sizeof(struct sockaddr_in6))
|
|
return -EINVAL;
|
|
addr_type = ipv6_addr_type(&sin6->sin6_addr);
|
|
if (!(addr_type & IPV6_ADDR_UNICAST)) {
|
|
__be32 addr4;
|
|
|
|
if (!(addr_type & IPV6_ADDR_MAPPED))
|
|
return -EINVAL;
|
|
|
|
/* It is a mapped address. Need to do some sanity
|
|
* checks.
|
|
*/
|
|
addr4 = sin6->sin6_addr.s6_addr32[3];
|
|
if (addr4 == htonl(INADDR_ANY) ||
|
|
addr4 == htonl(INADDR_BROADCAST) ||
|
|
IN_MULTICAST(ntohl(addr4)))
|
|
return -EINVAL;
|
|
}
|
|
/* The scope ID must be specified for link local address. */
|
|
if (addr_type & IPV6_ADDR_LINKLOCAL) {
|
|
if (sin6->sin6_scope_id == 0)
|
|
return -EINVAL;
|
|
scope_id = sin6->sin6_scope_id;
|
|
}
|
|
binding_addr = &sin6->sin6_addr;
|
|
port = sin6->sin6_port;
|
|
#endif
|
|
} else {
|
|
return -EINVAL;
|
|
}
|
|
lock_sock(sk);
|
|
|
|
/* RDS socket does not allow re-binding. */
|
|
if (!ipv6_addr_any(&rs->rs_bound_addr)) {
|
|
ret = -EINVAL;
|
|
goto out;
|
|
}
|
|
/* Socket is connected. The binding address should have the same
|
|
* scope ID as the connected address, except the case when one is
|
|
* non-link local address (scope_id is 0).
|
|
*/
|
|
if (!ipv6_addr_any(&rs->rs_conn_addr) && scope_id &&
|
|
rs->rs_bound_scope_id &&
|
|
scope_id != rs->rs_bound_scope_id) {
|
|
ret = -EINVAL;
|
|
goto out;
|
|
}
|
|
|
|
sock_set_flag(sk, SOCK_RCU_FREE);
|
|
ret = rds_add_bound(rs, binding_addr, &port, scope_id);
|
|
if (ret)
|
|
goto out;
|
|
|
|
if (rs->rs_transport) { /* previously bound */
|
|
trans = rs->rs_transport;
|
|
if (trans->laddr_check(sock_net(sock->sk),
|
|
binding_addr, scope_id) != 0) {
|
|
ret = -ENOPROTOOPT;
|
|
rds_remove_bound(rs);
|
|
} else {
|
|
ret = 0;
|
|
}
|
|
goto out;
|
|
}
|
|
trans = rds_trans_get_preferred(sock_net(sock->sk), binding_addr,
|
|
scope_id);
|
|
if (!trans) {
|
|
ret = -EADDRNOTAVAIL;
|
|
rds_remove_bound(rs);
|
|
pr_info_ratelimited("RDS: %s could not find a transport for %pI6c, load rds_tcp or rds_rdma?\n",
|
|
__func__, binding_addr);
|
|
goto out;
|
|
}
|
|
|
|
rs->rs_transport = trans;
|
|
ret = 0;
|
|
|
|
out:
|
|
release_sock(sk);
|
|
return ret;
|
|
}
|
|
|
|
void rds_bind_lock_destroy(void)
|
|
{
|
|
rhashtable_destroy(&bind_hash_table);
|
|
}
|
|
|
|
int rds_bind_lock_init(void)
|
|
{
|
|
return rhashtable_init(&bind_hash_table, &ht_parms);
|
|
}
|