linux-stable/net
Duoming Zhou 8f74cb27c2 net: rose: fix UAF bugs caused by timer handler
commit 9cc02ede69 upstream.

There are UAF bugs in rose_heartbeat_expiry(), rose_timer_expiry()
and rose_idletimer_expiry(). The root cause is that del_timer()
could not stop the timer handler that is running and the refcount
of sock is not managed properly.

One of the UAF bugs is shown below:

    (thread 1)          |        (thread 2)
                        |  rose_bind
                        |  rose_connect
                        |    rose_start_heartbeat
rose_release            |    (wait a time)
  case ROSE_STATE_0     |
  rose_destroy_socket   |  rose_heartbeat_expiry
    rose_stop_heartbeat |
    sock_put(sk)        |    ...
  sock_put(sk) // FREE  |
                        |    bh_lock_sock(sk) // USE

The sock is deallocated by sock_put() in rose_release() and
then used by bh_lock_sock() in rose_heartbeat_expiry().

Although rose_destroy_socket() calls rose_stop_heartbeat(),
it could not stop the timer that is running.

The KASAN report triggered by POC is shown below:

BUG: KASAN: use-after-free in _raw_spin_lock+0x5a/0x110
Write of size 4 at addr ffff88800ae59098 by task swapper/3/0
...
Call Trace:
 <IRQ>
 dump_stack_lvl+0xbf/0xee
 print_address_description+0x7b/0x440
 print_report+0x101/0x230
 ? irq_work_single+0xbb/0x140
 ? _raw_spin_lock+0x5a/0x110
 kasan_report+0xed/0x120
 ? _raw_spin_lock+0x5a/0x110
 kasan_check_range+0x2bd/0x2e0
 _raw_spin_lock+0x5a/0x110
 rose_heartbeat_expiry+0x39/0x370
 ? rose_start_heartbeat+0xb0/0xb0
 call_timer_fn+0x2d/0x1c0
 ? rose_start_heartbeat+0xb0/0xb0
 expire_timers+0x1f3/0x320
 __run_timers+0x3ff/0x4d0
 run_timer_softirq+0x41/0x80
 __do_softirq+0x233/0x544
 irq_exit_rcu+0x41/0xa0
 sysvec_apic_timer_interrupt+0x8c/0xb0
 </IRQ>
 <TASK>
 asm_sysvec_apic_timer_interrupt+0x1b/0x20
RIP: 0010:default_idle+0xb/0x10
RSP: 0018:ffffc9000012fea0 EFLAGS: 00000202
RAX: 000000000000bcae RBX: ffff888006660f00 RCX: 000000000000bcae
RDX: 0000000000000001 RSI: ffffffff843a11c0 RDI: ffffffff843a1180
RBP: dffffc0000000000 R08: dffffc0000000000 R09: ffffed100da36d46
R10: dfffe9100da36d47 R11: ffffffff83cf0950 R12: 0000000000000000
R13: 1ffff11000ccc1e0 R14: ffffffff8542af28 R15: dffffc0000000000
...
Allocated by task 146:
 __kasan_kmalloc+0xc4/0xf0
 sk_prot_alloc+0xdd/0x1a0
 sk_alloc+0x2d/0x4e0
 rose_create+0x7b/0x330
 __sock_create+0x2dd/0x640
 __sys_socket+0xc7/0x270
 __x64_sys_socket+0x71/0x80
 do_syscall_64+0x43/0x90
 entry_SYSCALL_64_after_hwframe+0x46/0xb0

Freed by task 152:
 kasan_set_track+0x4c/0x70
 kasan_set_free_info+0x1f/0x40
 ____kasan_slab_free+0x124/0x190
 kfree+0xd3/0x270
 __sk_destruct+0x314/0x460
 rose_release+0x2fa/0x3b0
 sock_close+0xcb/0x230
 __fput+0x2d9/0x650
 task_work_run+0xd6/0x160
 exit_to_user_mode_loop+0xc7/0xd0
 exit_to_user_mode_prepare+0x4e/0x80
 syscall_exit_to_user_mode+0x20/0x40
 do_syscall_64+0x4f/0x90
 entry_SYSCALL_64_after_hwframe+0x46/0xb0

This patch adds refcount of sock when we use functions
such as rose_start_heartbeat() and so on to start timer,
and decreases the refcount of sock when timer is finished
or deleted by functions such as rose_stop_heartbeat()
and so on. As a result, the UAF bugs could be mitigated.

Fixes: 1da177e4c3 ("Linux-2.6.12-rc2")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Tested-by: Duoming Zhou <duoming@zju.edu.cn>
Link: https://lore.kernel.org/r/20220629002640.5693-1-duoming@zju.edu.cn
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-07-07 17:52:16 +02:00
..
6lowpan 6lowpan: iphc: Fix an off-by-one check of array index 2021-09-15 09:50:34 +02:00
9p xen/9p: use alloc/free_pages_exact() 2022-03-11 12:11:54 +01:00
802
8021q net: vlan: fix underflow for the real_dev refcnt 2021-12-01 09:19:08 +01:00
appletalk
atm
ax25 net: ax25: Fix deadlock caused by skb_recv_datagram in ax25_recvmsg 2022-06-22 14:13:17 +02:00
batman-adv batman-adv: Don't skb_split skbuffs with frag_list 2022-05-18 10:23:42 +02:00
bluetooth Bluetooth: use hdev lock for accept_list and reject_list in conn req 2022-06-09 10:21:09 +02:00
bpf bpf, test, cgroup: Use sk_{alloc,free} for test cases 2021-10-27 09:56:56 +02:00
bpfilter
bridge netfilter: nftables: add nft_parse_register_store() and use it 2022-06-29 08:59:46 +02:00
caif net-caif: avoid user-triggerable WARN_ON(1) 2021-09-22 12:27:56 +02:00
can can: isotp: remove re-binding of bound socket 2022-05-12 12:25:35 +02:00
ceph libceph: fix potential use-after-free on linger ping and resends 2022-05-25 09:17:56 +02:00
core bpf: Fix request_sock leak in sk lookup helpers 2022-06-29 08:59:46 +02:00
dcb net: dcb: disable softirqs in dcbnl_flush_dev() 2022-03-08 19:09:37 +01:00
dccp lsm,selinux: pass flowi_common instead of flowi to the LSM hooks 2022-06-09 10:21:09 +02:00
decnet
dns_resolver
dsa net: dsa: Add missing of_node_put() in dsa_port_link_register_of 2022-05-09 09:05:02 +02:00
ethernet
ethtool ethtool: do not perform operations on net devices being unregistered 2021-12-17 10:14:41 +01:00
hsr
ieee802154 net: ieee802154: Return meaningful error codes from the netlink helpers 2022-02-08 18:30:37 +01:00
ife
ipv4 erspan: do not assume transport header is always set 2022-06-29 08:59:48 +02:00
ipv6 ipv6: take care of disable_policy when restoring routes 2022-07-07 17:52:15 +02:00
iucv
kcm
key Revert "net: af_key: add check for pfkey_broadcast in function pfkey_process" 2022-06-14 18:32:43 +02:00
l2tp ipv6: Fix signed integer overflow in l2tp_ip6_sendmsg 2022-06-22 14:13:15 +02:00
l3mdev l3mdev: l3mdev_master_upper_ifindex_by_index_rcu should be using netdev_master_upper_dev_get_rcu 2022-04-27 13:53:50 +02:00
lapb
llc llc: only change llc->dev when bind() succeeds 2022-03-28 09:57:10 +02:00
mac80211 mac80211: upgrade passive scan to active scan on DFS channels after beacon rx 2022-06-09 10:21:26 +02:00
mac802154
mpls net: mpls: Fix notifications when deleting a device 2021-12-08 09:03:23 +01:00
mptcp mptcp: clear 'kern' flag from fallback sockets 2021-12-22 09:30:54 +01:00
ncsi net/ncsi: check for error return from call to nla_put_u32 2022-01-05 12:40:32 +01:00
netfilter netfilter: use get_random_u32 instead of prandom 2022-06-29 08:59:46 +02:00
netlabel netlabel: fix out-of-bounds memory accesses 2022-04-13 21:01:00 +02:00
netlink netlink: do not reset transport header in netlink_recvmsg() 2022-05-18 10:23:43 +02:00
netrom netrom: fix api breakage in nr_setsockopt() 2022-01-27 10:54:03 +01:00
nfc NFC: NULL out the dev->rfkill to prevent UAF 2022-06-09 10:21:01 +02:00
nsh
openvswitch net: openvswitch: fix parsing of nw_proto for IPv6 fragments 2022-06-29 08:59:45 +02:00
packet net/packet: fix packet_sock xmit return value checking 2022-04-27 13:53:50 +02:00
phonet phonet: refcount leak in pep_sock_accep 2022-01-11 15:25:01 +01:00
psample
qrtr net: qrtr: fix another OOB Read in qrtr_endpoint_post 2021-09-03 10:09:21 +02:00
rds rds: memory leak in __rds_conn_create() 2021-12-22 09:30:54 +01:00
rfkill
rose net: rose: fix UAF bugs caused by timer handler 2022-07-07 17:52:16 +02:00
rxrpc rxrpc: Fix decision on when to generate an IDLE ACK 2022-06-09 10:21:12 +02:00
sched net/sched: sch_netem: Fix arithmetic in netem_dump() for 32-bit platforms 2022-06-29 08:59:47 +02:00
sctp sctp: read sk->sk_bound_dev_if once in sctp_rcv() 2022-06-09 10:21:09 +02:00
smc net/smc: fixes for converting from "struct smc_cdc_tx_pend **" to "struct smc_wr_tx_pend_priv *" 2022-06-14 18:32:36 +02:00
strparser bpf: sockmap, strparser, and tls are reusing qdisc_skb_cb and colliding 2021-11-18 14:04:27 +01:00
sunrpc SUNRPC: Fix READ_PLUS crasher 2022-07-07 17:52:16 +02:00
switchdev
tipc tipc: fix use-after-free Read in tipc_named_reinit 2022-06-29 08:59:47 +02:00
tls Revert "net/tls: fix tls_sk_proto_close executed repeatedly" 2022-06-29 08:59:49 +02:00
unix af_unix: Fix a data-race in unix_dgram_peer_wake_me(). 2022-06-14 18:32:40 +02:00
vmw_vsock vsock: each transport cycles only on its own sockets 2022-03-23 09:13:27 +01:00
wimax
wireless cfg80211: declare MODULE_FIRMWARE for regulatory.db 2022-06-09 10:21:26 +02:00
x25 net/x25: Fix null-ptr-deref caused by x25_disconnect 2022-04-08 14:40:30 +02:00
xdp Revert "xsk: Do not sleep in poll() when need_wakeup set" 2021-12-22 09:30:59 +01:00
xfrm lsm,selinux: pass flowi_common instead of flowi to the LSM hooks 2022-06-09 10:21:09 +02:00
compat.c
devres.c
Kconfig
Makefile
socket.c ethtool: improve compat ioctl handling 2021-09-18 13:40:21 +02:00
sysctl_net.c