The IDR API is deprecated in favour of XArray, see
Documentation/core-api/idr.rst. Convert the
minor table accordingly.
XA_LIMIT is inclusive, so the upper bound becomes
GB_NUM_MINORS - 1 in alloc_minor(). xa_alloc()
returns -EBUSY instead of -ENOSPC when full;
adjust the check in gb_uart_probe().
table_lock is kept: it prevents the entry from
being freed, while get_gb_by_minor() uses it,
whereas the XArray lock only protects the array
itself.
Signed-off-by: Philipp Prell <philipp.prell(a)icloud.com>
---
Compile-tested only (W=1, C=1); I do not have the Greybus hardware.
An AI assistant (Claude) was used to review my changes and to point
out bugs in earlier drafts and to help with the wording of the commit message.
The code changes were written by me.
drivers/staging/greybus/uart.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/drivers/staging/greybus/uart.c b/drivers/staging/greybus/uart.c
index e1b73db6a17c..ff33991562f5 100644
--- a/drivers/staging/greybus/uart.c
+++ b/drivers/staging/greybus/uart.c
@@ -22,7 +22,7 @@
#include <linux/serial.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
-#include <linux/idr.h>
+#include <linux/xarray.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/kfifo.h>
@@ -66,7 +66,7 @@ struct gb_tty {
};
static struct tty_driver *gb_tty_driver;
-static DEFINE_IDR(tty_minors);
+static DEFINE_XARRAY_ALLOC(tty_minors);
static DEFINE_MUTEX(table_lock);
static int gb_uart_receive_data_handler(struct gb_operation *op)
@@ -341,7 +341,7 @@ static struct gb_tty *get_gb_by_minor(unsigned int minor)
struct gb_tty *gb_tty;
mutex_lock(&table_lock);
- gb_tty = idr_find(&tty_minors, minor);
+ gb_tty = xa_load(&tty_minors, minor);
if (gb_tty) {
mutex_lock(&gb_tty->mutex);
if (gb_tty->disconnected) {
@@ -358,13 +358,18 @@ static struct gb_tty *get_gb_by_minor(unsigned int minor)
static int alloc_minor(struct gb_tty *gb_tty)
{
- int minor;
+ u32 minor;
+ int ret;
mutex_lock(&table_lock);
- minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
+ ret = xa_alloc(&tty_minors, &minor, gb_tty,
+ XA_LIMIT(0, GB_NUM_MINORS - 1), GFP_KERNEL);
mutex_unlock(&table_lock);
- if (minor >= 0)
- gb_tty->minor = minor;
+
+ if (ret)
+ return ret;
+
+ gb_tty->minor = minor;
return minor;
}
@@ -374,7 +379,7 @@ static void release_minor(struct gb_tty *gb_tty)
gb_tty->minor = 0; /* Maybe should use an invalid value instead */
mutex_lock(&table_lock);
- idr_remove(&tty_minors, minor);
+ xa_erase(&tty_minors, minor);
mutex_unlock(&table_lock);
}
@@ -860,7 +865,7 @@ static int gb_uart_probe(struct gbphy_device *gbphy_dev,
minor = alloc_minor(gb_tty);
if (minor < 0) {
- if (minor == -ENOSPC) {
+ if (minor == -EBUSY) {
dev_err(&gbphy_dev->dev,
"no more free minor numbers\n");
retval = -ENODEV;
@@ -991,7 +996,7 @@ static void gb_tty_exit(void)
{
tty_unregister_driver(gb_tty_driver);
tty_driver_kref_put(gb_tty_driver);
- idr_destroy(&tty_minors);
+ xa_destroy(&tty_minors);
}
static const struct gbphy_device_id gb_uart_id_table[] = {
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.56.0
Copied name for certain functions can be 32 bytes, but destination is
also only 32 bytes. After adding prefix, name may be truncated, causing
compiler warning from -Wformat-truncation.
drivers/staging/greybus/audio_topology.c: In function ‘gbaudio_tplg_process_kcontrols’:
drivers/staging/greybus/audio_topology.c:1172:65: warning: ‘%s’ directive output may be truncated writing up to 31 bytes into a region of size between 17 and 27 [-Wformat-truncation=]
drivers/staging/greybus/audio_topology.c: In function ‘gbaudio_tplg_create_widget’:
drivers/staging/greybus/audio_topology.c:1090:51: warning: ‘%s’ directive output may be truncated writing up to 31 bytes into a region of size between 17 and 27 [-Wformat-truncation=]
Modify the temporary name buffer size in
gbaudio_tplg_process_kcontrols() and gbaudio_tplg_create_widget() to
match the maximum size that can be copied back to the name field. Add
a bitmask to the ID put in the prefix to assure values 0-63 as stated
in "include/linux/greybus/greybus_protocols.h" for struct
gb_audio_control. The compiler expects 2 digits max when bitmask is
presnet, allowing one more character in controls compared to widgets.
For the widgets, name may only be 24 bytes max for IDs 0-255. For the
controls, name may only be 25 bytes max for IDs 0-63. IDs with less
digits get truncated regardless.
Compile tested only.
Signed-off-by: Garrett Floyd <garrettfloyd345(a)hotmail.com>
---
drivers/staging/greybus/audio_topology.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/drivers/staging/greybus/audio_topology.c b/drivers/staging/greybus/audio_topology.c
index 76146f91cddc..1a6ef1bdf170 100644
--- a/drivers/staging/greybus/audio_topology.c
+++ b/drivers/staging/greybus/audio_topology.c
@@ -1018,7 +1018,8 @@ static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
struct gb_audio_control *curr;
struct gbaudio_control *control, *_control;
size_t size;
- char temp_name[NAME_SIZE];
+ /* Subtracting 3 for "GB ", 3 for ID 0-255, 1 for final space */
+ char temp_name[NAME_SIZE - 7];
ret = gbaudio_validate_kcontrol_count(w);
if (ret) {
@@ -1087,7 +1088,8 @@ static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
/* Prefix dev_id to widget control_name */
strscpy(temp_name, w->name, sizeof(temp_name));
- snprintf(w->name, sizeof(w->name), "GB %d %s", module->dev_id, temp_name);
+ snprintf(w->name, sizeof(w->name), "GB %u %s", module->dev_id,
+ temp_name);
switch (w->type) {
case snd_soc_dapm_spk:
@@ -1143,7 +1145,8 @@ static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
struct gb_audio_control *curr;
struct gbaudio_control *control, *_control;
size_t size;
- char temp_name[NAME_SIZE];
+ /* Subtracting 3 for "GB ", 2 for ID 0-63, 1 for final space */
+ char temp_name[NAME_SIZE - 6];
size = sizeof(struct snd_kcontrol_new) * module->num_controls;
dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
@@ -1169,7 +1172,8 @@ static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
control->id = curr->id;
/* Prefix dev_id to widget_name */
strscpy(temp_name, curr->name, sizeof(temp_name));
- snprintf(curr->name, sizeof(curr->name), "GB %d %s", module->dev_id,
+ /* bitmask enforces values between 0-63 */
+ snprintf(curr->name, sizeof(curr->name), "GB %u %s", module->dev_id & 0x3f,
temp_name);
control->name = curr->name;
if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
--
2.55.0
Hello,
syzbot found the following issue on:
HEAD commit: 6628e7d4c6b2 Linux 7.4-rc1
git tree: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
console output: https://syzkaller.appspot.com/x/log.txt?x=109ac1a1580000
kernel config: https://syzkaller.appspot.com/x/.config?x=f9bf69220d319004
dashboard link: https://syzkaller.appspot.com/bug?extid=0b456664585fa81a47f6
compiler: gcc (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=149ac1a1580000
C reproducer: https://syzkaller.appspot.com/x/repro.c?x=150c9f8d580000
Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/d415be35d54e/disk-6628e7d4.raw…
vmlinux: https://storage.googleapis.com/syzbot-assets/69e49385122b/vmlinux-6628e7d4.…
kernel image: https://storage.googleapis.com/syzbot-assets/51be81fff25f/bzImage-6628e7d4.…
AI-generated root cause summary:
In gb_connection_recv() (drivers/greybus/operation.c), the kernel verifies that
the incoming buffer length is at least sizeof(struct gb_operation_msg_hdr), but
fails to validate that the message header's claimed size (header.size, parsed as
msg_size) is also at least sizeof(struct gb_operation_msg_hdr). When msg_size <
sizeof(struct gb_operation_msg_hdr), gb_operation_create_incoming() subtracts
the header size from msg_size, causing an unsigned integer underflow when
calculating request_size. In gb_operation_message_alloc(), the payload size and
header size wrap back around and kzalloc(msg_size) allocates an undersized
buffer smaller than the header, leading to out-of-bounds reads when reading
request_header->operation_id or a ZERO_SIZE_PTR dereference panic when msg_size
is zero.
full analysis: https://syzkaller.appspot.com/x/ai_root_cause?x=0b456664585fa81a47f6
IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+0b456664585fa81a47f6(a)syzkaller.appspotmail.com
greybus 1-svc: unexpected request 0x7f received (state 0)
==================================================================
BUG: KASAN: slab-out-of-bounds in gb_operation_response_alloc drivers/greybus/operation.c:490 [inline]
BUG: KASAN: slab-out-of-bounds in gb_operation_response_send drivers/greybus/operation.c:832 [inline]
BUG: KASAN: slab-out-of-bounds in gb_operation_request_handle drivers/greybus/operation.c:252 [inline]
BUG: KASAN: slab-out-of-bounds in gb_operation_work+0x8be/0x990 drivers/greybus/operation.c:280
Read of size 2 at addr ffff888105f0a642 by task kworker/u8:2/38
CPU: 1 UID: 0 PID: 38 Comm: kworker/u8:2 Not tainted syzkaller #0 PREEMPT(lazy)
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/15/2026
Workqueue: greybus1:0 gb_operation_work
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:94 [inline]
dump_stack_lvl+0x100/0x190 lib/dump_stack.c:120
print_address_description mm/kasan/report.c:378 [inline]
print_report+0x13d/0x4b0 mm/kasan/report.c:482
kasan_report+0xdf/0x1c0 mm/kasan/report.c:595
gb_operation_response_alloc drivers/greybus/operation.c:490 [inline]
gb_operation_response_send drivers/greybus/operation.c:832 [inline]
gb_operation_request_handle drivers/greybus/operation.c:252 [inline]
gb_operation_work+0x8be/0x990 drivers/greybus/operation.c:280
process_one_work+0xac7/0x1b10 kernel/workqueue.c:3495
process_scheduled_works kernel/workqueue.c:3578 [inline]
worker_thread+0x5ef/0xe50 kernel/workqueue.c:3659
kthread+0x373/0x450 kernel/kthread.c:436
ret_from_fork+0x69f/0xc90 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
</TASK>
Allocated by task 28:
kasan_save_stack+0x30/0x50 mm/kasan/common.c:57
kasan_save_track+0x14/0x30 mm/kasan/common.c:78
poison_kmalloc_redzone mm/kasan/common.c:409 [inline]
__kasan_kmalloc+0x8f/0xa0 mm/kasan/common.c:426
kasan_kmalloc include/linux/kasan.h:263 [inline]
__do_kmalloc_node mm/slub.c:5531 [inline]
__kmalloc_noprof+0x315/0x7e0 mm/slub.c:5556
_kmalloc_noprof include/linux/slab.h:995 [inline]
_kzalloc_noprof include/linux/slab.h:1312 [inline]
gb_operation_message_alloc+0x9b/0x2c0 drivers/greybus/operation.c:380
gb_operation_create_common+0x9f/0x710 drivers/greybus/operation.c:532
gb_operation_create_incoming drivers/greybus/operation.c:644 [inline]
gb_connection_recv_request drivers/greybus/operation.c:926 [inline]
gb_connection_recv+0x1dc/0x1050 drivers/greybus/operation.c:1063
greybus_data_rcvd+0x244/0x600 drivers/greybus/connection.c:89
cport_in_callback+0x1fb/0x280 drivers/greybus/es2.c:840
__usb_hcd_giveback_urb+0x38e/0x610 drivers/usb/core/hcd.c:1657
usb_hcd_giveback_urb+0x3ca/0x4a0 drivers/usb/core/hcd.c:1741
dummy_timer+0xdb5/0x3840 drivers/usb/gadget/udc/dummy_hcd.c:2051
__run_hrtimer kernel/time/hrtimer.c:2079 [inline]
__hrtimer_run_queues+0x448/0x9a0 kernel/time/hrtimer.c:2136
hrtimer_run_softirq+0x1ca/0x360 kernel/time/hrtimer.c:2153
handle_softirqs+0x209/0xa00 kernel/softirq.c:669
__do_softirq kernel/softirq.c:703 [inline]
invoke_softirq kernel/softirq.c:542 [inline]
__irq_exit_rcu+0x17a/0x220 kernel/softirq.c:786
irq_exit_rcu+0x9/0x30 kernel/softirq.c:809
instr_sysvec_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1062 [inline]
sysvec_apic_timer_interrupt+0x8f/0xb0 arch/x86/kernel/apic/apic.c:1062
asm_sysvec_apic_timer_interrupt+0x1a/0x20 arch/x86/include/asm/idtentry.h:674
The buggy address belongs to the object at ffff888105f0a640
which belongs to the cache kmalloc-8 of size 8
The buggy address is located 2 bytes inside of
allocated 3-byte region [ffff888105f0a640, ffff888105f0a643)
The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x105f0a
flags: 0x200000000000000(node=0|zone=2)
page_type: f5(slab)
raw: 0200000000000000 ffff888100041500 dead000000000100 dead000000000122
raw: 0000000000000000 0000000000800080 00000000f5000000 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 0, migratetype Unmovable, gfp_mask 0xd2c00(GFP_NOIO|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 1, tgid 1 (swapper/0), ts 6490716744
set_page_owner include/linux/page_owner.h:33 [inline]
post_alloc_hook+0xfd/0x120 mm/page_alloc.c:1856
prep_new_page mm/page_alloc.c:1864 [inline]
get_page_from_freelist+0xf4b/0x3440 mm/page_alloc.c:3935
__alloc_frozen_pages_noprof+0x299/0x2d50 mm/page_alloc.c:5444
alloc_slab_page mm/slub.c:3384 [inline]
allocate_slab mm/slub.c:3499 [inline]
new_slab+0xb1/0x6d0 mm/slub.c:3550
refill_objects+0xe3/0x410 mm/slub.c:7539
refill_sheaf mm/slub.c:2917 [inline]
__pcs_replace_empty_main+0x35e/0x650 mm/slub.c:4811
alloc_from_pcs mm/slub.c:4887 [inline]
slab_alloc_node mm/slub.c:5021 [inline]
__do_kmalloc_node mm/slub.c:5530 [inline]
__kmalloc_node_noprof+0x634/0x820 mm/slub.c:5543
_kmalloc_array_node_noprof include/linux/slab.h:1297 [inline]
__blk_mq_realloc_hw_ctxs+0x579/0x820 block/blk-mq.c:4588
blk_mq_realloc_hw_ctxs block/blk-mq.c:4652 [inline]
blk_mq_init_allocated_queue+0x36c/0x1500 block/blk-mq.c:4694
blk_mq_alloc_queue+0x1bd/0x290 block/blk-mq.c:4464
scsi_alloc_sdev+0x972/0xe90 drivers/scsi/scsi_scan.c:339
scsi_probe_and_add_lun+0x523/0x720 drivers/scsi/scsi_scan.c:1227
__scsi_scan_target+0x188/0x420 drivers/scsi/scsi_scan.c:1803
scsi_scan_channel drivers/scsi/scsi_scan.c:1893 [inline]
scsi_scan_channel+0x142/0x1e0 drivers/scsi/scsi_scan.c:1868
scsi_scan_host_selected+0x2b3/0x380 drivers/scsi/scsi_scan.c:1922
do_scsi_scan_host+0x1f4/0x270 drivers/scsi/scsi_scan.c:2055
page_owner free stack trace missing
Memory state around the buggy address:
ffff888105f0a500: 06 fc fc fc 00 fc fc fc 00 fc fc fc 00 fc fc fc
ffff888105f0a580: fa fc fc fc 06 fc fc fc 06 fc fc fc 06 fc fc fc
>ffff888105f0a600: 06 fc fc fc 00 fc fc fc 03 fc fc fc fa fc fc fc
^
ffff888105f0a680: fa fc fc fc 00 fc fc fc fa fc fc fc 00 fc fc fc
ffff888105f0a700: fa fc fc fc 05 fc fc fc 05 fc fc fc 05 fc fc fc
==================================================================
---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkaller(a)googlegroups.com.
syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title
If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)
If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report
If you want to undo deduplication, reply with:
#syz undup
The following has been changed:
- introduce mutex to C serdev tty port controller
- introduce a new C serdev API to fix race conditions and simplify rust
code. Should also be useful on the C side.
- fix race conditions
- simplify rust code
Signed-off-by: Markus Probst <markus.probst(a)posteo.de>
---
Changes in v6:
- fix issues reported by Sashiko:
- fix compile error on 3. patch
- rebase onto v7.4-rc2
- use dev_WARN instead of WARN_ON (WARN intentionally kept)
- Link to v5: https://patch.msgid.link/20261022-rust_serdev_probe_refactor-v5-0-a27d862a4…
Changes in v5:
- fix issues reported by Sashiko:
- fix access to serport->tty outside of locked mutex
- only lock receive_lock in ttyport_open for set_bit
- Link to v4: https://patch.msgid.link/20261016-rust_serdev_probe_refactor-v4-0-46832e034…
Changes in v4:
- fix issues reported by Sashiko:
- add receive_lock to prevent deadlocks
- Link to v3: https://patch.msgid.link/20260930-rust_serdev_probe_refactor-v3-0-d1b6e7713…
Changes in v3:
- add mutex
- Link to v2: https://patch.msgid.link/20260920-rust_serdev_probe_refactor-v2-0-43b855162…
Changes in v2:
- fix missing memory barriers in serdev apis
- use `tty_buffer_restart_work` instead of `tty_flip_buffer_push`
- use `tty_buffer_flush_work` instead of exclusively locking the mutex
- add `#[inline]` to `PrivateData::driver_data`
- remove patches for mutable references
- Link to v1: https://patch.msgid.link/20260906-rust_serdev_probe_refactor-v1-0-69cdae007…
---
Markus Probst (4):
tty: serdev: Add mutex lock
tty: serdev: Export functions to pause receive_buf callback calls
rust: serdev: Replace `active` mutex with receive pause
rust: serdev: Simplify callbacks
drivers/tty/serdev/core.c | 56 +++++++++++-
drivers/tty/serdev/serdev-ttyport.c | 80 +++++++++++++++--
include/linux/serdev.h | 8 +-
rust/kernel/serdev.rs | 167 ++++++++++++------------------------
4 files changed, 186 insertions(+), 125 deletions(-)
---
base-commit: fd9230005e85472b81c1c7c16afe93dd878996b1
change-id: 20260905-rust_serdev_probe_refactor-0e2b044354a7
In gb_connection_recv(), the size of the received data is checked
against sizeof(struct gb_operation_msg_hdr) and against the message
size encoded in the header (msg_size), but msg_size itself is never
checked against the header size.
If a peer sends a message whose header claims a size smaller than
the header itself (e.g. header.size == 3), the two existing checks
both pass (size >= sizeof(header) and size >= msg_size), and the
message is dispatched with the undersized msg_size.
In gb_operation_create_incoming(), this leads to an unsigned
integer underflow in:
request_size = size - sizeof(struct gb_operation_msg_hdr);
The underflowed request_size then wraps back around in
gb_operation_message_alloc():
message_size = payload_size + sizeof(*header);
causing kzalloc() to allocate a buffer that is too small to hold
the message header. When the response is later prepared in
gb_operation_response_alloc(), reading
request_header->operation_id reads past the end of the undersized
allocation, triggering a KASAN slab-out-of-bounds report. When
header.size is zero, kzalloc(0) returns ZERO_SIZE_PTR and the
same read dereferences an invalid address.
Combine the two existing checks into one that also verifies
msg_size >= sizeof(header), so that the "Caller has made sure we
at least have a message header" assumption in
gb_operation_create_incoming() actually holds.
Suggested-by: Dan Carpenter <dan(a)pereq.eu>
Link: https://lore.kernel.org/all/sashiko-outbox-210716@kernel.org/T/
Fixes: d90c25b0a279 ("greybus: let operation layer examine incoming data")
Reported-by: syzbot+0b456664585fa81a47f6(a)syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=0b456664585fa81a47f6
Signed-off-by: Shunbo Shi <shishunbo(a)cmss.chinamobile.com>
---
drivers/greybus/operation.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/greybus/operation.c b/drivers/greybus/operation.c
index 7e12ffb2dd60..dcb38dc29cff 100644
--- a/drivers/greybus/operation.c
+++ b/drivers/greybus/operation.c
@@ -1047,9 +1047,9 @@ void gb_connection_recv(struct gb_connection *connection,
/* Use memcpy as data may be unaligned */
memcpy(&header, data, sizeof(header));
msg_size = le16_to_cpu(header.size);
- if (size < msg_size) {
+ if (msg_size < sizeof(header) || msg_size > size) {
dev_err_ratelimited(dev,
- "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
+ "%s: incomplete or invalid message 0x%04x of type 0x%02x received (%zu < %zu)\n",
connection->name,
le16_to_cpu(header.operation_id),
header.type, size, msg_size);
--
2.53.0