mirror of
https://github.com/torvalds/linux.git
synced 2024-11-15 00:21:59 +00:00
0bc36c0650
user->unix_inflight is changed under spin_lock(unix_gc_lock),
but too_many_unix_fds() reads it locklessly.
Let's annotate the write/read accesses to user->unix_inflight.
BUG: KCSAN: data-race in unix_attach_fds / unix_inflight
write to 0xffffffff8546f2d0 of 8 bytes by task 44798 on cpu 1:
unix_inflight+0x157/0x180 net/unix/scm.c:66
unix_attach_fds+0x147/0x1e0 net/unix/scm.c:123
unix_scm_to_skb net/unix/af_unix.c:1827 [inline]
unix_dgram_sendmsg+0x46a/0x14f0 net/unix/af_unix.c:1950
unix_seqpacket_sendmsg net/unix/af_unix.c:2308 [inline]
unix_seqpacket_sendmsg+0xba/0x130 net/unix/af_unix.c:2292
sock_sendmsg_nosec net/socket.c:725 [inline]
sock_sendmsg+0x148/0x160 net/socket.c:748
____sys_sendmsg+0x4e4/0x610 net/socket.c:2494
___sys_sendmsg+0xc6/0x140 net/socket.c:2548
__sys_sendmsg+0x94/0x140 net/socket.c:2577
__do_sys_sendmsg net/socket.c:2586 [inline]
__se_sys_sendmsg net/socket.c:2584 [inline]
__x64_sys_sendmsg+0x45/0x50 net/socket.c:2584
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x6e/0xd8
read to 0xffffffff8546f2d0 of 8 bytes by task 44814 on cpu 0:
too_many_unix_fds net/unix/scm.c:101 [inline]
unix_attach_fds+0x54/0x1e0 net/unix/scm.c:110
unix_scm_to_skb net/unix/af_unix.c:1827 [inline]
unix_dgram_sendmsg+0x46a/0x14f0 net/unix/af_unix.c:1950
unix_seqpacket_sendmsg net/unix/af_unix.c:2308 [inline]
unix_seqpacket_sendmsg+0xba/0x130 net/unix/af_unix.c:2292
sock_sendmsg_nosec net/socket.c:725 [inline]
sock_sendmsg+0x148/0x160 net/socket.c:748
____sys_sendmsg+0x4e4/0x610 net/socket.c:2494
___sys_sendmsg+0xc6/0x140 net/socket.c:2548
__sys_sendmsg+0x94/0x140 net/socket.c:2577
__do_sys_sendmsg net/socket.c:2586 [inline]
__se_sys_sendmsg net/socket.c:2584 [inline]
__x64_sys_sendmsg+0x45/0x50 net/socket.c:2584
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x6e/0xd8
value changed: 0x000000000000000c -> 0x000000000000000d
Reported by Kernel Concurrency Sanitizer on:
CPU: 0 PID: 44814 Comm: systemd-coredum Not tainted 6.4.0-11989-g6843306689af #6
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014
Fixes: 712f4aad40
("unix: properly account for FDs passed over unix sockets")
Reported-by: syzkaller <syzkaller@googlegroups.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Acked-by: Willy Tarreau <w@1wt.eu>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
162 lines
3.9 KiB
C
162 lines
3.9 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <linux/module.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/string.h>
|
|
#include <linux/socket.h>
|
|
#include <linux/net.h>
|
|
#include <linux/fs.h>
|
|
#include <net/af_unix.h>
|
|
#include <net/scm.h>
|
|
#include <linux/init.h>
|
|
#include <linux/io_uring.h>
|
|
|
|
#include "scm.h"
|
|
|
|
unsigned int unix_tot_inflight;
|
|
EXPORT_SYMBOL(unix_tot_inflight);
|
|
|
|
LIST_HEAD(gc_inflight_list);
|
|
EXPORT_SYMBOL(gc_inflight_list);
|
|
|
|
DEFINE_SPINLOCK(unix_gc_lock);
|
|
EXPORT_SYMBOL(unix_gc_lock);
|
|
|
|
struct sock *unix_get_socket(struct file *filp)
|
|
{
|
|
struct sock *u_sock = NULL;
|
|
struct inode *inode = file_inode(filp);
|
|
|
|
/* Socket ? */
|
|
if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
|
|
struct socket *sock = SOCKET_I(inode);
|
|
const struct proto_ops *ops = READ_ONCE(sock->ops);
|
|
struct sock *s = sock->sk;
|
|
|
|
/* PF_UNIX ? */
|
|
if (s && ops && ops->family == PF_UNIX)
|
|
u_sock = s;
|
|
} else {
|
|
/* Could be an io_uring instance */
|
|
u_sock = io_uring_get_socket(filp);
|
|
}
|
|
return u_sock;
|
|
}
|
|
EXPORT_SYMBOL(unix_get_socket);
|
|
|
|
/* Keep the number of times in flight count for the file
|
|
* descriptor if it is for an AF_UNIX socket.
|
|
*/
|
|
void unix_inflight(struct user_struct *user, struct file *fp)
|
|
{
|
|
struct sock *s = unix_get_socket(fp);
|
|
|
|
spin_lock(&unix_gc_lock);
|
|
|
|
if (s) {
|
|
struct unix_sock *u = unix_sk(s);
|
|
|
|
if (atomic_long_inc_return(&u->inflight) == 1) {
|
|
BUG_ON(!list_empty(&u->link));
|
|
list_add_tail(&u->link, &gc_inflight_list);
|
|
} else {
|
|
BUG_ON(list_empty(&u->link));
|
|
}
|
|
/* Paired with READ_ONCE() in wait_for_unix_gc() */
|
|
WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
|
|
}
|
|
WRITE_ONCE(user->unix_inflight, user->unix_inflight + 1);
|
|
spin_unlock(&unix_gc_lock);
|
|
}
|
|
|
|
void unix_notinflight(struct user_struct *user, struct file *fp)
|
|
{
|
|
struct sock *s = unix_get_socket(fp);
|
|
|
|
spin_lock(&unix_gc_lock);
|
|
|
|
if (s) {
|
|
struct unix_sock *u = unix_sk(s);
|
|
|
|
BUG_ON(!atomic_long_read(&u->inflight));
|
|
BUG_ON(list_empty(&u->link));
|
|
|
|
if (atomic_long_dec_and_test(&u->inflight))
|
|
list_del_init(&u->link);
|
|
/* Paired with READ_ONCE() in wait_for_unix_gc() */
|
|
WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);
|
|
}
|
|
WRITE_ONCE(user->unix_inflight, user->unix_inflight - 1);
|
|
spin_unlock(&unix_gc_lock);
|
|
}
|
|
|
|
/*
|
|
* The "user->unix_inflight" variable is protected by the garbage
|
|
* collection lock, and we just read it locklessly here. If you go
|
|
* over the limit, there might be a tiny race in actually noticing
|
|
* it across threads. Tough.
|
|
*/
|
|
static inline bool too_many_unix_fds(struct task_struct *p)
|
|
{
|
|
struct user_struct *user = current_user();
|
|
|
|
if (unlikely(READ_ONCE(user->unix_inflight) > task_rlimit(p, RLIMIT_NOFILE)))
|
|
return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
|
|
return false;
|
|
}
|
|
|
|
int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
|
|
{
|
|
int i;
|
|
|
|
if (too_many_unix_fds(current))
|
|
return -ETOOMANYREFS;
|
|
|
|
/*
|
|
* Need to duplicate file references for the sake of garbage
|
|
* collection. Otherwise a socket in the fps might become a
|
|
* candidate for GC while the skb is not yet queued.
|
|
*/
|
|
UNIXCB(skb).fp = scm_fp_dup(scm->fp);
|
|
if (!UNIXCB(skb).fp)
|
|
return -ENOMEM;
|
|
|
|
for (i = scm->fp->count - 1; i >= 0; i--)
|
|
unix_inflight(scm->fp->user, scm->fp->fp[i]);
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(unix_attach_fds);
|
|
|
|
void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
|
|
{
|
|
int i;
|
|
|
|
scm->fp = UNIXCB(skb).fp;
|
|
UNIXCB(skb).fp = NULL;
|
|
|
|
for (i = scm->fp->count-1; i >= 0; i--)
|
|
unix_notinflight(scm->fp->user, scm->fp->fp[i]);
|
|
}
|
|
EXPORT_SYMBOL(unix_detach_fds);
|
|
|
|
void unix_destruct_scm(struct sk_buff *skb)
|
|
{
|
|
struct scm_cookie scm;
|
|
|
|
memset(&scm, 0, sizeof(scm));
|
|
scm.pid = UNIXCB(skb).pid;
|
|
if (UNIXCB(skb).fp)
|
|
unix_detach_fds(&scm, skb);
|
|
|
|
/* Alas, it calls VFS */
|
|
/* So fscking what? fput() had been SMP-safe since the last Summer */
|
|
scm_destroy(&scm);
|
|
sock_wfree(skb);
|
|
}
|
|
EXPORT_SYMBOL(unix_destruct_scm);
|
|
|
|
void io_uring_destruct_scm(struct sk_buff *skb)
|
|
{
|
|
unix_destruct_scm(skb);
|
|
}
|
|
EXPORT_SYMBOL(io_uring_destruct_scm);
|