Skip to content

Commit 155be2e

Browse files
q2venpelwell
authored andcommitted
af_unix: Read sk->sk_hash under bindlock during bind().
[ Upstream commit 51d1b25 ] syzkaller reported data-race of sk->sk_hash in unix_autobind() [0], and the same ones exist in unix_bind_bsd() and unix_bind_abstract(). The three bind() functions prefetch sk->sk_hash locklessly and use it later after validating that unix_sk(sk)->addr is NULL under unix_sk(sk)->bindlock. The prefetched sk->sk_hash is the hash value of unbound socket set in unix_create1() and does not change until bind() completes. There could be a chance that sk->sk_hash changes after the lockless read. However, in such a case, non-NULL unix_sk(sk)->addr is visible under unix_sk(sk)->bindlock, and bind() returns -EINVAL without using the prefetched value. The KCSAN splat is false-positive, but let's silence it by reading sk->sk_hash under unix_sk(sk)->bindlock. [0]: BUG: KCSAN: data-race in unix_autobind / unix_autobind write to 0xffff888034a9fb88 of 4 bytes by task 4468 on cpu 0: __unix_set_addr_hash net/unix/af_unix.c:331 [inline] unix_autobind+0x47a/0x7d0 net/unix/af_unix.c:1185 unix_dgram_connect+0x7e3/0x890 net/unix/af_unix.c:1373 __sys_connect_file+0xd7/0xe0 net/socket.c:2048 __sys_connect+0x114/0x140 net/socket.c:2065 __do_sys_connect net/socket.c:2075 [inline] __se_sys_connect net/socket.c:2072 [inline] __x64_sys_connect+0x40/0x50 net/socket.c:2072 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x4f/0x110 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x46/0x4e read to 0xffff888034a9fb88 of 4 bytes by task 4465 on cpu 1: unix_autobind+0x28/0x7d0 net/unix/af_unix.c:1134 unix_dgram_connect+0x7e3/0x890 net/unix/af_unix.c:1373 __sys_connect_file+0xd7/0xe0 net/socket.c:2048 __sys_connect+0x114/0x140 net/socket.c:2065 __do_sys_connect net/socket.c:2075 [inline] __se_sys_connect net/socket.c:2072 [inline] __x64_sys_connect+0x40/0x50 net/socket.c:2072 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x4f/0x110 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x46/0x4e value changed: 0x000000e4 -> 0x000001e3 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 4465 Comm: syz-executor.0 Not tainted 6.8.0-12822-gcd51db110a7e #12 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 Fixes: afd20b9 ("af_unix: Replace the big lock with small locks.") Reported-by: syzkaller <[email protected]> Signed-off-by: Kuniyuki Iwashima <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 884615a commit 155be2e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

net/unix/af_unix.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,8 @@ static struct sock *unix_find_other(struct net *net,
11451145

11461146
static int unix_autobind(struct sock *sk)
11471147
{
1148-
unsigned int new_hash, old_hash = sk->sk_hash;
11491148
struct unix_sock *u = unix_sk(sk);
1149+
unsigned int new_hash, old_hash;
11501150
struct net *net = sock_net(sk);
11511151
struct unix_address *addr;
11521152
u32 lastnum, ordernum;
@@ -1169,6 +1169,7 @@ static int unix_autobind(struct sock *sk)
11691169
addr->name->sun_family = AF_UNIX;
11701170
refcount_set(&addr->refcnt, 1);
11711171

1172+
old_hash = sk->sk_hash;
11721173
ordernum = get_random_u32();
11731174
lastnum = ordernum & 0xFFFFF;
11741175
retry:
@@ -1209,8 +1210,8 @@ static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr,
12091210
{
12101211
umode_t mode = S_IFSOCK |
12111212
(SOCK_INODE(sk->sk_socket)->i_mode & ~current_umask());
1212-
unsigned int new_hash, old_hash = sk->sk_hash;
12131213
struct unix_sock *u = unix_sk(sk);
1214+
unsigned int new_hash, old_hash;
12141215
struct net *net = sock_net(sk);
12151216
struct mnt_idmap *idmap;
12161217
struct unix_address *addr;
@@ -1248,6 +1249,7 @@ static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr,
12481249
if (u->addr)
12491250
goto out_unlock;
12501251

1252+
old_hash = sk->sk_hash;
12511253
new_hash = unix_bsd_hash(d_backing_inode(dentry));
12521254
unix_table_double_lock(net, old_hash, new_hash);
12531255
u->path.mnt = mntget(parent.mnt);
@@ -1275,8 +1277,8 @@ static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr,
12751277
static int unix_bind_abstract(struct sock *sk, struct sockaddr_un *sunaddr,
12761278
int addr_len)
12771279
{
1278-
unsigned int new_hash, old_hash = sk->sk_hash;
12791280
struct unix_sock *u = unix_sk(sk);
1281+
unsigned int new_hash, old_hash;
12801282
struct net *net = sock_net(sk);
12811283
struct unix_address *addr;
12821284
int err;
@@ -1294,6 +1296,7 @@ static int unix_bind_abstract(struct sock *sk, struct sockaddr_un *sunaddr,
12941296
goto out_mutex;
12951297
}
12961298

1299+
old_hash = sk->sk_hash;
12971300
new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
12981301
unix_table_double_lock(net, old_hash, new_hash);
12991302

0 commit comments

Comments
 (0)