mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
synced 2025-01-08 15:04:45 +00:00
9f16eb106a
Syzkaller reports the following failure:
BUG: KASAN: use-after-free in kref_put include/linux/kref.h:64 [inline]
BUG: KASAN: use-after-free in j1939_priv_put+0x25/0xa0 net/can/j1939/main.c:172
Write of size 4 at addr ffff888141c15058 by task swapper/3/0
CPU: 3 PID: 0 Comm: swapper/3 Not tainted 5.10.144-syzkaller #0
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
Call Trace:
<IRQ>
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x107/0x167 lib/dump_stack.c:118
print_address_description.constprop.0+0x1c/0x220 mm/kasan/report.c:385
__kasan_report mm/kasan/report.c:545 [inline]
kasan_report.cold+0x1f/0x37 mm/kasan/report.c:562
check_memory_region_inline mm/kasan/generic.c:186 [inline]
check_memory_region+0x145/0x190 mm/kasan/generic.c:192
instrument_atomic_read_write include/linux/instrumented.h:101 [inline]
atomic_fetch_sub_release include/asm-generic/atomic-instrumented.h:220 [inline]
__refcount_sub_and_test include/linux/refcount.h:272 [inline]
__refcount_dec_and_test include/linux/refcount.h:315 [inline]
refcount_dec_and_test include/linux/refcount.h:333 [inline]
kref_put include/linux/kref.h:64 [inline]
j1939_priv_put+0x25/0xa0 net/can/j1939/main.c:172
j1939_sk_sock_destruct+0x44/0x90 net/can/j1939/socket.c:374
__sk_destruct+0x4e/0x820 net/core/sock.c:1784
rcu_do_batch kernel/rcu/tree.c:2485 [inline]
rcu_core+0xb35/0x1a30 kernel/rcu/tree.c:2726
__do_softirq+0x289/0x9a3 kernel/softirq.c:298
asm_call_irq_on_stack+0x12/0x20
</IRQ>
__run_on_irqstack arch/x86/include/asm/irq_stack.h:26 [inline]
run_on_irqstack_cond arch/x86/include/asm/irq_stack.h:77 [inline]
do_softirq_own_stack+0xaa/0xe0 arch/x86/kernel/irq_64.c:77
invoke_softirq kernel/softirq.c:393 [inline]
__irq_exit_rcu kernel/softirq.c:423 [inline]
irq_exit_rcu+0x136/0x200 kernel/softirq.c:435
sysvec_apic_timer_interrupt+0x4d/0x100 arch/x86/kernel/apic/apic.c:1095
asm_sysvec_apic_timer_interrupt+0x12/0x20 arch/x86/include/asm/idtentry.h:635
Allocated by task 1141:
kasan_save_stack+0x1b/0x40 mm/kasan/common.c:48
kasan_set_track mm/kasan/common.c:56 [inline]
__kasan_kmalloc.constprop.0+0xc9/0xd0 mm/kasan/common.c:461
kmalloc include/linux/slab.h:552 [inline]
kzalloc include/linux/slab.h:664 [inline]
j1939_priv_create net/can/j1939/main.c:131 [inline]
j1939_netdev_start+0x111/0x860 net/can/j1939/main.c:268
j1939_sk_bind+0x8ea/0xd30 net/can/j1939/socket.c:485
__sys_bind+0x1f2/0x260 net/socket.c:1645
__do_sys_bind net/socket.c:1656 [inline]
__se_sys_bind net/socket.c:1654 [inline]
__x64_sys_bind+0x6f/0xb0 net/socket.c:1654
do_syscall_64+0x33/0x40 arch/x86/entry/common.c:46
entry_SYSCALL_64_after_hwframe+0x61/0xc6
Freed by task 1141:
kasan_save_stack+0x1b/0x40 mm/kasan/common.c:48
kasan_set_track+0x1c/0x30 mm/kasan/common.c:56
kasan_set_free_info+0x1b/0x30 mm/kasan/generic.c:355
__kasan_slab_free+0x112/0x170 mm/kasan/common.c:422
slab_free_hook mm/slub.c:1542 [inline]
slab_free_freelist_hook+0xad/0x190 mm/slub.c:1576
slab_free mm/slub.c:3149 [inline]
kfree+0xd9/0x3b0 mm/slub.c:4125
j1939_netdev_start+0x5ee/0x860 net/can/j1939/main.c:300
j1939_sk_bind+0x8ea/0xd30 net/can/j1939/socket.c:485
__sys_bind+0x1f2/0x260 net/socket.c:1645
__do_sys_bind net/socket.c:1656 [inline]
__se_sys_bind net/socket.c:1654 [inline]
__x64_sys_bind+0x6f/0xb0 net/socket.c:1654
do_syscall_64+0x33/0x40 arch/x86/entry/common.c:46
entry_SYSCALL_64_after_hwframe+0x61/0xc6
It can be caused by this scenario:
CPU0 CPU1
j1939_sk_bind(socket0, ndev0, ...)
j1939_netdev_start()
j1939_sk_bind(socket1, ndev0, ...)
j1939_netdev_start()
mutex_lock(&j1939_netdev_lock)
j1939_priv_set(ndev0, priv)
mutex_unlock(&j1939_netdev_lock)
if (priv_new)
kref_get(&priv_new->rx_kref)
return priv_new;
/* inside j1939_sk_bind() */
jsk->priv = priv
j1939_can_rx_register(priv) // fails
j1939_priv_set(ndev, NULL)
kfree(priv)
j1939_sk_sock_destruct()
j1939_priv_put() // <- uaf
To avoid this, call j1939_can_rx_register() under j1939_netdev_lock so
that a concurrent thread cannot process j1939_priv before
j1939_can_rx_register() returns.
Found by Linux Verification Center (linuxtesting.org) with Syzkaller.
Fixes: 9d71dd0c70
("can: add support of SAE J1939 protocol")
Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
Tested-by: Oleksij Rempel <o.rempel@pengutronix.de>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Link: https://lore.kernel.org/r/20230526171910.227615-3-pchelkin@ispras.ru
Cc: stable@vger.kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
430 lines
9.8 KiB
C
430 lines
9.8 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
// Copyright (c) 2010-2011 EIA Electronics,
|
|
// Pieter Beyens <pieter.beyens@eia.be>
|
|
// Copyright (c) 2010-2011 EIA Electronics,
|
|
// Kurt Van Dijck <kurt.van.dijck@eia.be>
|
|
// Copyright (c) 2018 Protonic,
|
|
// Robin van der Gracht <robin@protonic.nl>
|
|
// Copyright (c) 2017-2019 Pengutronix,
|
|
// Marc Kleine-Budde <kernel@pengutronix.de>
|
|
// Copyright (c) 2017-2019 Pengutronix,
|
|
// Oleksij Rempel <kernel@pengutronix.de>
|
|
|
|
/* Core of can-j1939 that links j1939 to CAN. */
|
|
|
|
#include <linux/can/can-ml.h>
|
|
#include <linux/can/core.h>
|
|
#include <linux/can/skb.h>
|
|
#include <linux/if_arp.h>
|
|
#include <linux/module.h>
|
|
|
|
#include "j1939-priv.h"
|
|
|
|
MODULE_DESCRIPTION("PF_CAN SAE J1939");
|
|
MODULE_LICENSE("GPL v2");
|
|
MODULE_AUTHOR("EIA Electronics (Kurt Van Dijck & Pieter Beyens)");
|
|
MODULE_ALIAS("can-proto-" __stringify(CAN_J1939));
|
|
|
|
/* LOWLEVEL CAN interface */
|
|
|
|
/* CAN_HDR: #bytes before can_frame data part */
|
|
#define J1939_CAN_HDR (offsetof(struct can_frame, data))
|
|
|
|
/* CAN_FTR: #bytes beyond data part */
|
|
#define J1939_CAN_FTR (sizeof(struct can_frame) - J1939_CAN_HDR - \
|
|
sizeof(((struct can_frame *)0)->data))
|
|
|
|
/* lowest layer */
|
|
static void j1939_can_recv(struct sk_buff *iskb, void *data)
|
|
{
|
|
struct j1939_priv *priv = data;
|
|
struct sk_buff *skb;
|
|
struct j1939_sk_buff_cb *skcb, *iskcb;
|
|
struct can_frame *cf;
|
|
|
|
/* make sure we only get Classical CAN frames */
|
|
if (!can_is_can_skb(iskb))
|
|
return;
|
|
|
|
/* create a copy of the skb
|
|
* j1939 only delivers the real data bytes,
|
|
* the header goes into sockaddr.
|
|
* j1939 may not touch the incoming skb in such way
|
|
*/
|
|
skb = skb_clone(iskb, GFP_ATOMIC);
|
|
if (!skb)
|
|
return;
|
|
|
|
j1939_priv_get(priv);
|
|
can_skb_set_owner(skb, iskb->sk);
|
|
|
|
/* get a pointer to the header of the skb
|
|
* the skb payload (pointer) is moved, so that the next skb_data
|
|
* returns the actual payload
|
|
*/
|
|
cf = (void *)skb->data;
|
|
skb_pull(skb, J1939_CAN_HDR);
|
|
|
|
/* fix length, set to dlc, with 8 maximum */
|
|
skb_trim(skb, min_t(uint8_t, cf->len, 8));
|
|
|
|
/* set addr */
|
|
skcb = j1939_skb_to_cb(skb);
|
|
memset(skcb, 0, sizeof(*skcb));
|
|
|
|
iskcb = j1939_skb_to_cb(iskb);
|
|
skcb->tskey = iskcb->tskey;
|
|
skcb->priority = (cf->can_id >> 26) & 0x7;
|
|
skcb->addr.sa = cf->can_id;
|
|
skcb->addr.pgn = (cf->can_id >> 8) & J1939_PGN_MAX;
|
|
/* set default message type */
|
|
skcb->addr.type = J1939_TP;
|
|
|
|
if (!j1939_address_is_valid(skcb->addr.sa)) {
|
|
netdev_err_once(priv->ndev, "%s: sa is broadcast address, ignoring!\n",
|
|
__func__);
|
|
goto done;
|
|
}
|
|
|
|
if (j1939_pgn_is_pdu1(skcb->addr.pgn)) {
|
|
/* Type 1: with destination address */
|
|
skcb->addr.da = skcb->addr.pgn;
|
|
/* normalize pgn: strip dst address */
|
|
skcb->addr.pgn &= 0x3ff00;
|
|
} else {
|
|
/* set broadcast address */
|
|
skcb->addr.da = J1939_NO_ADDR;
|
|
}
|
|
|
|
/* update localflags */
|
|
read_lock_bh(&priv->lock);
|
|
if (j1939_address_is_unicast(skcb->addr.sa) &&
|
|
priv->ents[skcb->addr.sa].nusers)
|
|
skcb->flags |= J1939_ECU_LOCAL_SRC;
|
|
if (j1939_address_is_unicast(skcb->addr.da) &&
|
|
priv->ents[skcb->addr.da].nusers)
|
|
skcb->flags |= J1939_ECU_LOCAL_DST;
|
|
read_unlock_bh(&priv->lock);
|
|
|
|
/* deliver into the j1939 stack ... */
|
|
j1939_ac_recv(priv, skb);
|
|
|
|
if (j1939_tp_recv(priv, skb))
|
|
/* this means the transport layer processed the message */
|
|
goto done;
|
|
|
|
j1939_simple_recv(priv, skb);
|
|
j1939_sk_recv(priv, skb);
|
|
done:
|
|
j1939_priv_put(priv);
|
|
kfree_skb(skb);
|
|
}
|
|
|
|
/* NETDEV MANAGEMENT */
|
|
|
|
/* values for can_rx_(un)register */
|
|
#define J1939_CAN_ID CAN_EFF_FLAG
|
|
#define J1939_CAN_MASK (CAN_EFF_FLAG | CAN_RTR_FLAG)
|
|
|
|
static DEFINE_MUTEX(j1939_netdev_lock);
|
|
|
|
static struct j1939_priv *j1939_priv_create(struct net_device *ndev)
|
|
{
|
|
struct j1939_priv *priv;
|
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
|
if (!priv)
|
|
return NULL;
|
|
|
|
rwlock_init(&priv->lock);
|
|
INIT_LIST_HEAD(&priv->ecus);
|
|
priv->ndev = ndev;
|
|
kref_init(&priv->kref);
|
|
kref_init(&priv->rx_kref);
|
|
dev_hold(ndev);
|
|
|
|
netdev_dbg(priv->ndev, "%s : 0x%p\n", __func__, priv);
|
|
|
|
return priv;
|
|
}
|
|
|
|
static inline void j1939_priv_set(struct net_device *ndev,
|
|
struct j1939_priv *priv)
|
|
{
|
|
struct can_ml_priv *can_ml = can_get_ml_priv(ndev);
|
|
|
|
can_ml->j1939_priv = priv;
|
|
}
|
|
|
|
static void __j1939_priv_release(struct kref *kref)
|
|
{
|
|
struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref);
|
|
struct net_device *ndev = priv->ndev;
|
|
|
|
netdev_dbg(priv->ndev, "%s: 0x%p\n", __func__, priv);
|
|
|
|
WARN_ON_ONCE(!list_empty(&priv->active_session_list));
|
|
WARN_ON_ONCE(!list_empty(&priv->ecus));
|
|
WARN_ON_ONCE(!list_empty(&priv->j1939_socks));
|
|
|
|
dev_put(ndev);
|
|
kfree(priv);
|
|
}
|
|
|
|
void j1939_priv_put(struct j1939_priv *priv)
|
|
{
|
|
kref_put(&priv->kref, __j1939_priv_release);
|
|
}
|
|
|
|
void j1939_priv_get(struct j1939_priv *priv)
|
|
{
|
|
kref_get(&priv->kref);
|
|
}
|
|
|
|
static int j1939_can_rx_register(struct j1939_priv *priv)
|
|
{
|
|
struct net_device *ndev = priv->ndev;
|
|
int ret;
|
|
|
|
j1939_priv_get(priv);
|
|
ret = can_rx_register(dev_net(ndev), ndev, J1939_CAN_ID, J1939_CAN_MASK,
|
|
j1939_can_recv, priv, "j1939", NULL);
|
|
if (ret < 0) {
|
|
j1939_priv_put(priv);
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void j1939_can_rx_unregister(struct j1939_priv *priv)
|
|
{
|
|
struct net_device *ndev = priv->ndev;
|
|
|
|
can_rx_unregister(dev_net(ndev), ndev, J1939_CAN_ID, J1939_CAN_MASK,
|
|
j1939_can_recv, priv);
|
|
|
|
/* The last reference of priv is dropped by the RCU deferred
|
|
* j1939_sk_sock_destruct() of the last socket, so we can
|
|
* safely drop this reference here.
|
|
*/
|
|
j1939_priv_put(priv);
|
|
}
|
|
|
|
static void __j1939_rx_release(struct kref *kref)
|
|
__releases(&j1939_netdev_lock)
|
|
{
|
|
struct j1939_priv *priv = container_of(kref, struct j1939_priv,
|
|
rx_kref);
|
|
|
|
j1939_can_rx_unregister(priv);
|
|
j1939_ecu_unmap_all(priv);
|
|
j1939_priv_set(priv->ndev, NULL);
|
|
mutex_unlock(&j1939_netdev_lock);
|
|
}
|
|
|
|
/* get pointer to priv without increasing ref counter */
|
|
static inline struct j1939_priv *j1939_ndev_to_priv(struct net_device *ndev)
|
|
{
|
|
struct can_ml_priv *can_ml = can_get_ml_priv(ndev);
|
|
|
|
return can_ml->j1939_priv;
|
|
}
|
|
|
|
static struct j1939_priv *j1939_priv_get_by_ndev_locked(struct net_device *ndev)
|
|
{
|
|
struct j1939_priv *priv;
|
|
|
|
lockdep_assert_held(&j1939_netdev_lock);
|
|
|
|
priv = j1939_ndev_to_priv(ndev);
|
|
if (priv)
|
|
j1939_priv_get(priv);
|
|
|
|
return priv;
|
|
}
|
|
|
|
static struct j1939_priv *j1939_priv_get_by_ndev(struct net_device *ndev)
|
|
{
|
|
struct j1939_priv *priv;
|
|
|
|
mutex_lock(&j1939_netdev_lock);
|
|
priv = j1939_priv_get_by_ndev_locked(ndev);
|
|
mutex_unlock(&j1939_netdev_lock);
|
|
|
|
return priv;
|
|
}
|
|
|
|
struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
|
|
{
|
|
struct j1939_priv *priv, *priv_new;
|
|
int ret;
|
|
|
|
mutex_lock(&j1939_netdev_lock);
|
|
priv = j1939_priv_get_by_ndev_locked(ndev);
|
|
if (priv) {
|
|
kref_get(&priv->rx_kref);
|
|
mutex_unlock(&j1939_netdev_lock);
|
|
return priv;
|
|
}
|
|
mutex_unlock(&j1939_netdev_lock);
|
|
|
|
priv = j1939_priv_create(ndev);
|
|
if (!priv)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
j1939_tp_init(priv);
|
|
spin_lock_init(&priv->j1939_socks_lock);
|
|
INIT_LIST_HEAD(&priv->j1939_socks);
|
|
|
|
mutex_lock(&j1939_netdev_lock);
|
|
priv_new = j1939_priv_get_by_ndev_locked(ndev);
|
|
if (priv_new) {
|
|
/* Someone was faster than us, use their priv and roll
|
|
* back our's.
|
|
*/
|
|
kref_get(&priv_new->rx_kref);
|
|
mutex_unlock(&j1939_netdev_lock);
|
|
dev_put(ndev);
|
|
kfree(priv);
|
|
return priv_new;
|
|
}
|
|
j1939_priv_set(ndev, priv);
|
|
|
|
ret = j1939_can_rx_register(priv);
|
|
if (ret < 0)
|
|
goto out_priv_put;
|
|
|
|
mutex_unlock(&j1939_netdev_lock);
|
|
return priv;
|
|
|
|
out_priv_put:
|
|
j1939_priv_set(ndev, NULL);
|
|
mutex_unlock(&j1939_netdev_lock);
|
|
|
|
dev_put(ndev);
|
|
kfree(priv);
|
|
|
|
return ERR_PTR(ret);
|
|
}
|
|
|
|
void j1939_netdev_stop(struct j1939_priv *priv)
|
|
{
|
|
kref_put_mutex(&priv->rx_kref, __j1939_rx_release, &j1939_netdev_lock);
|
|
j1939_priv_put(priv);
|
|
}
|
|
|
|
int j1939_send_one(struct j1939_priv *priv, struct sk_buff *skb)
|
|
{
|
|
int ret, dlc;
|
|
canid_t canid;
|
|
struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
|
|
struct can_frame *cf;
|
|
|
|
/* apply sanity checks */
|
|
if (j1939_pgn_is_pdu1(skcb->addr.pgn))
|
|
skcb->addr.pgn &= J1939_PGN_PDU1_MAX;
|
|
else
|
|
skcb->addr.pgn &= J1939_PGN_MAX;
|
|
|
|
if (skcb->priority > 7)
|
|
skcb->priority = 6;
|
|
|
|
ret = j1939_ac_fixup(priv, skb);
|
|
if (unlikely(ret))
|
|
goto failed;
|
|
dlc = skb->len;
|
|
|
|
/* re-claim the CAN_HDR from the SKB */
|
|
cf = skb_push(skb, J1939_CAN_HDR);
|
|
|
|
/* initialize header structure */
|
|
memset(cf, 0, J1939_CAN_HDR);
|
|
|
|
/* make it a full can frame again */
|
|
skb_put(skb, J1939_CAN_FTR + (8 - dlc));
|
|
|
|
canid = CAN_EFF_FLAG |
|
|
(skcb->priority << 26) |
|
|
(skcb->addr.pgn << 8) |
|
|
skcb->addr.sa;
|
|
if (j1939_pgn_is_pdu1(skcb->addr.pgn))
|
|
canid |= skcb->addr.da << 8;
|
|
|
|
cf->can_id = canid;
|
|
cf->len = dlc;
|
|
|
|
return can_send(skb, 1);
|
|
|
|
failed:
|
|
kfree_skb(skb);
|
|
return ret;
|
|
}
|
|
|
|
static int j1939_netdev_notify(struct notifier_block *nb,
|
|
unsigned long msg, void *data)
|
|
{
|
|
struct net_device *ndev = netdev_notifier_info_to_dev(data);
|
|
struct can_ml_priv *can_ml = can_get_ml_priv(ndev);
|
|
struct j1939_priv *priv;
|
|
|
|
if (!can_ml)
|
|
goto notify_done;
|
|
|
|
priv = j1939_priv_get_by_ndev(ndev);
|
|
if (!priv)
|
|
goto notify_done;
|
|
|
|
switch (msg) {
|
|
case NETDEV_DOWN:
|
|
j1939_cancel_active_session(priv, NULL);
|
|
j1939_sk_netdev_event_netdown(priv);
|
|
j1939_ecu_unmap_all(priv);
|
|
break;
|
|
}
|
|
|
|
j1939_priv_put(priv);
|
|
|
|
notify_done:
|
|
return NOTIFY_DONE;
|
|
}
|
|
|
|
static struct notifier_block j1939_netdev_notifier = {
|
|
.notifier_call = j1939_netdev_notify,
|
|
};
|
|
|
|
/* MODULE interface */
|
|
static __init int j1939_module_init(void)
|
|
{
|
|
int ret;
|
|
|
|
pr_info("can: SAE J1939\n");
|
|
|
|
ret = register_netdevice_notifier(&j1939_netdev_notifier);
|
|
if (ret)
|
|
goto fail_notifier;
|
|
|
|
ret = can_proto_register(&j1939_can_proto);
|
|
if (ret < 0) {
|
|
pr_err("can: registration of j1939 protocol failed\n");
|
|
goto fail_sk;
|
|
}
|
|
|
|
return 0;
|
|
|
|
fail_sk:
|
|
unregister_netdevice_notifier(&j1939_netdev_notifier);
|
|
fail_notifier:
|
|
return ret;
|
|
}
|
|
|
|
static __exit void j1939_module_exit(void)
|
|
{
|
|
can_proto_unregister(&j1939_can_proto);
|
|
|
|
unregister_netdevice_notifier(&j1939_netdev_notifier);
|
|
}
|
|
|
|
module_init(j1939_module_init);
|
|
module_exit(j1939_module_exit);
|