Skip to content

Commit 936fd2c

Browse files
committed
Merge tag 'linux-can-fixes-for-6.5-20230717' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can
Marc Kleine-Budde says: ==================== pull-request: can 2023-07-17 The 1st patch is by Ziyang Xuan and fixes a possible memory leak in the receiver handling in the CAN RAW protocol. YueHaibing contributes a use after free in bcm_proc_show() of the Broad Cast Manager (BCM) CAN protocol. The next 2 patches are by me and fix a possible null pointer dereference in the RX path of the gs_usb driver with activated hardware timestamps and the candlelight firmware. The last patch is by Fedor Ross, Marek Vasut and me and targets the mcp251xfd driver. The polling timeout of __mcp251xfd_chip_set_mode() is increased to fix bus joining on busy CAN buses and very low bit rate. * tag 'linux-can-fixes-for-6.5-20230717' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can: can: mcp251xfd: __mcp251xfd_chip_set_mode(): increase poll timeout can: gs_usb: fix time stamp counter initialization can: gs_usb: gs_can_open(): improve error handling can: bcm: Fix UAF in bcm_proc_show() can: raw: fix receiver memory leak ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 195e903 + 9efa1a5 commit 936fd2c

File tree

5 files changed

+113
-97
lines changed

5 files changed

+113
-97
lines changed

drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ static int
227227
__mcp251xfd_chip_set_mode(const struct mcp251xfd_priv *priv,
228228
const u8 mode_req, bool nowait)
229229
{
230+
const struct can_bittiming *bt = &priv->can.bittiming;
231+
unsigned long timeout_us = MCP251XFD_POLL_TIMEOUT_US;
230232
u32 con = 0, con_reqop, osc = 0;
231233
u8 mode;
232234
int err;
@@ -246,12 +248,16 @@ __mcp251xfd_chip_set_mode(const struct mcp251xfd_priv *priv,
246248
if (mode_req == MCP251XFD_REG_CON_MODE_SLEEP || nowait)
247249
return 0;
248250

251+
if (bt->bitrate)
252+
timeout_us = max_t(unsigned long, timeout_us,
253+
MCP251XFD_FRAME_LEN_MAX_BITS * USEC_PER_SEC /
254+
bt->bitrate);
255+
249256
err = regmap_read_poll_timeout(priv->map_reg, MCP251XFD_REG_CON, con,
250257
!mcp251xfd_reg_invalid(con) &&
251258
FIELD_GET(MCP251XFD_REG_CON_OPMOD_MASK,
252259
con) == mode_req,
253-
MCP251XFD_POLL_SLEEP_US,
254-
MCP251XFD_POLL_TIMEOUT_US);
260+
MCP251XFD_POLL_SLEEP_US, timeout_us);
255261
if (err != -ETIMEDOUT && err != -EBADMSG)
256262
return err;
257263

drivers/net/can/spi/mcp251xfd/mcp251xfd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ static_assert(MCP251XFD_TIMESTAMP_WORK_DELAY_SEC <
387387
#define MCP251XFD_OSC_STAB_TIMEOUT_US (10 * MCP251XFD_OSC_STAB_SLEEP_US)
388388
#define MCP251XFD_POLL_SLEEP_US (10)
389389
#define MCP251XFD_POLL_TIMEOUT_US (USEC_PER_MSEC)
390+
#define MCP251XFD_FRAME_LEN_MAX_BITS (736)
390391

391392
/* Misc */
392393
#define MCP251XFD_NAPI_WEIGHT 32

drivers/net/can/usb/gs_usb.c

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,6 @@ struct gs_can {
303303
struct can_bittiming_const bt_const, data_bt_const;
304304
unsigned int channel; /* channel number */
305305

306-
/* time counter for hardware timestamps */
307-
struct cyclecounter cc;
308-
struct timecounter tc;
309-
spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
310-
struct delayed_work timestamp;
311-
312306
u32 feature;
313307
unsigned int hf_size_tx;
314308

@@ -325,6 +319,13 @@ struct gs_usb {
325319
struct gs_can *canch[GS_MAX_INTF];
326320
struct usb_anchor rx_submitted;
327321
struct usb_device *udev;
322+
323+
/* time counter for hardware timestamps */
324+
struct cyclecounter cc;
325+
struct timecounter tc;
326+
spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
327+
struct delayed_work timestamp;
328+
328329
unsigned int hf_size_rx;
329330
u8 active_channels;
330331
};
@@ -388,15 +389,15 @@ static int gs_cmd_reset(struct gs_can *dev)
388389
GFP_KERNEL);
389390
}
390391

391-
static inline int gs_usb_get_timestamp(const struct gs_can *dev,
392+
static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
392393
u32 *timestamp_p)
393394
{
394395
__le32 timestamp;
395396
int rc;
396397

397-
rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_TIMESTAMP,
398+
rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP,
398399
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
399-
dev->channel, 0,
400+
0, 0,
400401
&timestamp, sizeof(timestamp),
401402
USB_CTRL_GET_TIMEOUT,
402403
GFP_KERNEL);
@@ -410,73 +411,74 @@ static inline int gs_usb_get_timestamp(const struct gs_can *dev,
410411

411412
static u64 gs_usb_timestamp_read(const struct cyclecounter *cc) __must_hold(&dev->tc_lock)
412413
{
413-
struct gs_can *dev = container_of(cc, struct gs_can, cc);
414+
struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
414415
u32 timestamp = 0;
415416
int err;
416417

417-
lockdep_assert_held(&dev->tc_lock);
418+
lockdep_assert_held(&parent->tc_lock);
418419

419420
/* drop lock for synchronous USB transfer */
420-
spin_unlock_bh(&dev->tc_lock);
421-
err = gs_usb_get_timestamp(dev, &timestamp);
422-
spin_lock_bh(&dev->tc_lock);
421+
spin_unlock_bh(&parent->tc_lock);
422+
err = gs_usb_get_timestamp(parent, &timestamp);
423+
spin_lock_bh(&parent->tc_lock);
423424
if (err)
424-
netdev_err(dev->netdev,
425-
"Error %d while reading timestamp. HW timestamps may be inaccurate.",
426-
err);
425+
dev_err(&parent->udev->dev,
426+
"Error %d while reading timestamp. HW timestamps may be inaccurate.",
427+
err);
427428

428429
return timestamp;
429430
}
430431

431432
static void gs_usb_timestamp_work(struct work_struct *work)
432433
{
433434
struct delayed_work *delayed_work = to_delayed_work(work);
434-
struct gs_can *dev;
435+
struct gs_usb *parent;
435436

436-
dev = container_of(delayed_work, struct gs_can, timestamp);
437-
spin_lock_bh(&dev->tc_lock);
438-
timecounter_read(&dev->tc);
439-
spin_unlock_bh(&dev->tc_lock);
437+
parent = container_of(delayed_work, struct gs_usb, timestamp);
438+
spin_lock_bh(&parent->tc_lock);
439+
timecounter_read(&parent->tc);
440+
spin_unlock_bh(&parent->tc_lock);
440441

441-
schedule_delayed_work(&dev->timestamp,
442+
schedule_delayed_work(&parent->timestamp,
442443
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
443444
}
444445

445446
static void gs_usb_skb_set_timestamp(struct gs_can *dev,
446447
struct sk_buff *skb, u32 timestamp)
447448
{
448449
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
450+
struct gs_usb *parent = dev->parent;
449451
u64 ns;
450452

451-
spin_lock_bh(&dev->tc_lock);
452-
ns = timecounter_cyc2time(&dev->tc, timestamp);
453-
spin_unlock_bh(&dev->tc_lock);
453+
spin_lock_bh(&parent->tc_lock);
454+
ns = timecounter_cyc2time(&parent->tc, timestamp);
455+
spin_unlock_bh(&parent->tc_lock);
454456

455457
hwtstamps->hwtstamp = ns_to_ktime(ns);
456458
}
457459

458-
static void gs_usb_timestamp_init(struct gs_can *dev)
460+
static void gs_usb_timestamp_init(struct gs_usb *parent)
459461
{
460-
struct cyclecounter *cc = &dev->cc;
462+
struct cyclecounter *cc = &parent->cc;
461463

462464
cc->read = gs_usb_timestamp_read;
463465
cc->mask = CYCLECOUNTER_MASK(32);
464466
cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
465467
cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
466468

467-
spin_lock_init(&dev->tc_lock);
468-
spin_lock_bh(&dev->tc_lock);
469-
timecounter_init(&dev->tc, &dev->cc, ktime_get_real_ns());
470-
spin_unlock_bh(&dev->tc_lock);
469+
spin_lock_init(&parent->tc_lock);
470+
spin_lock_bh(&parent->tc_lock);
471+
timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
472+
spin_unlock_bh(&parent->tc_lock);
471473

472-
INIT_DELAYED_WORK(&dev->timestamp, gs_usb_timestamp_work);
473-
schedule_delayed_work(&dev->timestamp,
474+
INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
475+
schedule_delayed_work(&parent->timestamp,
474476
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
475477
}
476478

477-
static void gs_usb_timestamp_stop(struct gs_can *dev)
479+
static void gs_usb_timestamp_stop(struct gs_usb *parent)
478480
{
479-
cancel_delayed_work_sync(&dev->timestamp);
481+
cancel_delayed_work_sync(&parent->timestamp);
480482
}
481483

482484
static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
@@ -560,6 +562,9 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
560562
if (!netif_device_present(netdev))
561563
return;
562564

565+
if (!netif_running(netdev))
566+
goto resubmit_urb;
567+
563568
if (hf->echo_id == -1) { /* normal rx */
564569
if (hf->flags & GS_CAN_FLAG_FD) {
565570
skb = alloc_canfd_skb(dev->netdev, &cfd);
@@ -833,6 +838,7 @@ static int gs_can_open(struct net_device *netdev)
833838
.mode = cpu_to_le32(GS_CAN_MODE_START),
834839
};
835840
struct gs_host_frame *hf;
841+
struct urb *urb = NULL;
836842
u32 ctrlmode;
837843
u32 flags = 0;
838844
int rc, i;
@@ -855,23 +861,27 @@ static int gs_can_open(struct net_device *netdev)
855861
}
856862

857863
if (!parent->active_channels) {
864+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
865+
gs_usb_timestamp_init(parent);
866+
858867
for (i = 0; i < GS_MAX_RX_URBS; i++) {
859-
struct urb *urb;
860868
u8 *buf;
861869

862870
/* alloc rx urb */
863871
urb = usb_alloc_urb(0, GFP_KERNEL);
864-
if (!urb)
865-
return -ENOMEM;
872+
if (!urb) {
873+
rc = -ENOMEM;
874+
goto out_usb_kill_anchored_urbs;
875+
}
866876

867877
/* alloc rx buffer */
868878
buf = kmalloc(dev->parent->hf_size_rx,
869879
GFP_KERNEL);
870880
if (!buf) {
871881
netdev_err(netdev,
872882
"No memory left for USB buffer\n");
873-
usb_free_urb(urb);
874-
return -ENOMEM;
883+
rc = -ENOMEM;
884+
goto out_usb_free_urb;
875885
}
876886

877887
/* fill, anchor, and submit rx urb */
@@ -894,9 +904,7 @@ static int gs_can_open(struct net_device *netdev)
894904
netdev_err(netdev,
895905
"usb_submit failed (err=%d)\n", rc);
896906

897-
usb_unanchor_urb(urb);
898-
usb_free_urb(urb);
899-
break;
907+
goto out_usb_unanchor_urb;
900908
}
901909

902910
/* Drop reference,
@@ -926,13 +934,9 @@ static int gs_can_open(struct net_device *netdev)
926934
flags |= GS_CAN_MODE_FD;
927935

928936
/* if hardware supports timestamps, enable it */
929-
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
937+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
930938
flags |= GS_CAN_MODE_HW_TIMESTAMP;
931939

932-
/* start polling timestamp */
933-
gs_usb_timestamp_init(dev);
934-
}
935-
936940
/* finally start device */
937941
dev->can.state = CAN_STATE_ERROR_ACTIVE;
938942
dm.flags = cpu_to_le32(flags);
@@ -942,17 +946,32 @@ static int gs_can_open(struct net_device *netdev)
942946
GFP_KERNEL);
943947
if (rc) {
944948
netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
945-
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
946-
gs_usb_timestamp_stop(dev);
947949
dev->can.state = CAN_STATE_STOPPED;
948-
return rc;
950+
951+
goto out_usb_kill_anchored_urbs;
949952
}
950953

951954
parent->active_channels++;
952955
if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
953956
netif_start_queue(netdev);
954957

955958
return 0;
959+
960+
out_usb_unanchor_urb:
961+
usb_unanchor_urb(urb);
962+
out_usb_free_urb:
963+
usb_free_urb(urb);
964+
out_usb_kill_anchored_urbs:
965+
if (!parent->active_channels) {
966+
usb_kill_anchored_urbs(&dev->tx_submitted);
967+
968+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
969+
gs_usb_timestamp_stop(parent);
970+
}
971+
972+
close_candev(netdev);
973+
974+
return rc;
956975
}
957976

958977
static int gs_usb_get_state(const struct net_device *netdev,
@@ -998,14 +1017,13 @@ static int gs_can_close(struct net_device *netdev)
9981017

9991018
netif_stop_queue(netdev);
10001019

1001-
/* stop polling timestamp */
1002-
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1003-
gs_usb_timestamp_stop(dev);
1004-
10051020
/* Stop polling */
10061021
parent->active_channels--;
10071022
if (!parent->active_channels) {
10081023
usb_kill_anchored_urbs(&parent->rx_submitted);
1024+
1025+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1026+
gs_usb_timestamp_stop(parent);
10091027
}
10101028

10111029
/* Stop sending URBs */

net/can/bcm.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,12 @@ static int bcm_release(struct socket *sock)
15261526

15271527
lock_sock(sk);
15281528

1529+
#if IS_ENABLED(CONFIG_PROC_FS)
1530+
/* remove procfs entry */
1531+
if (net->can.bcmproc_dir && bo->bcm_proc_read)
1532+
remove_proc_entry(bo->procname, net->can.bcmproc_dir);
1533+
#endif /* CONFIG_PROC_FS */
1534+
15291535
list_for_each_entry_safe(op, next, &bo->tx_ops, list)
15301536
bcm_remove_op(op);
15311537

@@ -1561,12 +1567,6 @@ static int bcm_release(struct socket *sock)
15611567
list_for_each_entry_safe(op, next, &bo->rx_ops, list)
15621568
bcm_remove_op(op);
15631569

1564-
#if IS_ENABLED(CONFIG_PROC_FS)
1565-
/* remove procfs entry */
1566-
if (net->can.bcmproc_dir && bo->bcm_proc_read)
1567-
remove_proc_entry(bo->procname, net->can.bcmproc_dir);
1568-
#endif /* CONFIG_PROC_FS */
1569-
15701570
/* remove device reference */
15711571
if (bo->bound) {
15721572
bo->bound = 0;

0 commit comments

Comments
 (0)