From a5f7a5bb3b7f28ba7e4fa246775b29a0e5537255 Mon Sep 17 00:00:00 2001 From: Jinke Han Date: Tue, 8 Sep 2026 15:37:42 +0800 Subject: x86/kprobes: Fix crash when probing CS CALL instructions When using eBPF to probe CS CALL instructions within a function, a crash can be triggered. The eBPF tool probes offset 257 of the __hrtimer_run_queues() function: <__hrtimer_run_queues+249>: nopl 0x0(%rax,%rax,1) <__hrtimer_run_queues+254>: mov %r14,%rdi <__hrtimer_run_queues+257>: cs call <__x86_indirect_thunk_r12> <__hrtimer_run_queues+263>: mov %eax,%r12d <__hrtimer_run_queues+266>: xchg %ax,%ax <__hrtimer_run_queues+268>: mov %r13,%rdi Which triggers this crash: BUG: unable to handle page fault for address: 00000000000f41c9 #PF: supervisor write access in kernel mode #PF: error_code(0x0002) - not-present page PGD 0 P4D 0 Oops: 0002 [#1] SMP NOPTI CPU: 1 PID: 0 Comm: swapper/1 Kdump: loaded Tainted: P RIP: 0010:__hrtimer_run_queues+0x106/0x230 Note that __hrtimer_run_queues+0x106 is __hrtimer_run_queues+262, which is at the 6th byte of the above CS CALL instruction. Since the CS CALL instruction occupies 6 bytes, the exception occurred in the middle of that call instruction. The root cause is that when using eBPF tools to probe in the middle of a function, a kprobe with INT3 is used as the underlying implementation. During single-step emulation of the original CALL instruction, int3_emulate_call() assumes that the probed CALL instruction is 5 bytes long. However, the actual CS-prefixed CALL instruction occupies 6 bytes, so it constructs an incorrect exception return address. When the CPU returns from the kprobe handler, the next instruction to be executed is at the address of the last byte of that CS CALL instruction. Coincidentally, starting from that address, the CPU fetches and decodes a completely different instruction, which ultimately triggers a kernel crash. Fix the issue by using the actual instruction length obtained from the instruction decoder when constructing the exception return address, rather than relying on the hardcoded CALL_INSN_SIZE macro. [ mingo: Refined the changelog ] Fixes: 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step") Suggested-by: Masami Hiramatsu (Google) Signed-off-by: Jinke Han Signed-off-by: Ingo Molnar Reviewed-by: Masami Hiramatsu (Google) Acked-by: Yafang Shao Acked-by: Borislav Petkov Cc: Peter Zijlstra Link: https://patch.msgid.link/20260908073742.GA10517@didi-ThinkCentre-M920t-N000 --- drivers/usb/serial/bus.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 drivers/usb/serial/bus.c (limited to 'drivers/usb/serial/bus.c') diff --git a/drivers/usb/serial/bus.c b/drivers/usb/serial/bus.c new file mode 100644 index 000000000..9e2a18c0b --- /dev/null +++ b/drivers/usb/serial/bus.c @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * USB Serial Converter Bus specific functions + * + * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com) + */ + +#include +#include +#include +#include +#include +#include +#include + +static int usb_serial_device_match(struct device *dev, + const struct device_driver *drv) +{ + const struct usb_serial_port *port = to_usb_serial_port(dev); + const struct usb_serial_driver *driver = to_usb_serial_driver(drv); + + /* + * drivers are already assigned to ports in serial_probe so it's + * a simple check here. + */ + if (driver == port->serial->type) + return 1; + + return 0; +} + +static int usb_serial_device_probe(struct device *dev) +{ + struct usb_serial_port *port = to_usb_serial_port(dev); + struct usb_serial_driver *driver; + struct device *tty_dev; + int retval = 0; + int minor; + + /* make sure suspend/resume doesn't race against port_probe */ + retval = usb_autopm_get_interface(port->serial->interface); + if (retval) + return retval; + + driver = port->serial->type; + if (driver->port_probe) { + retval = driver->port_probe(port); + if (retval) + goto err_autopm_put; + } + + minor = port->minor; + tty_dev = tty_port_register_device(&port->port, usb_serial_tty_driver, + minor, dev); + if (IS_ERR(tty_dev)) { + retval = PTR_ERR(tty_dev); + goto err_port_remove; + } + + usb_autopm_put_interface(port->serial->interface); + + dev_info(&port->serial->dev->dev, + "%s converter now attached to ttyUSB%d\n", + driver->description, minor); + + return 0; + +err_port_remove: + if (driver->port_remove) + driver->port_remove(port); +err_autopm_put: + usb_autopm_put_interface(port->serial->interface); + + return retval; +} + +static void usb_serial_device_remove(struct device *dev) +{ + struct usb_serial_port *port = to_usb_serial_port(dev); + struct usb_serial_driver *driver; + int minor; + int autopm_err; + + /* + * Make sure suspend/resume doesn't race against port_remove. + * + * Note that no further runtime PM callbacks will be made if + * autopm_get fails. + */ + autopm_err = usb_autopm_get_interface(port->serial->interface); + + minor = port->minor; + tty_unregister_device(usb_serial_tty_driver, minor); + + driver = port->serial->type; + if (driver->port_remove) + driver->port_remove(port); + + dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", + driver->description, minor); + + if (!autopm_err) + usb_autopm_put_interface(port->serial->interface); +} + +static ssize_t new_id_store(struct device_driver *driver, + const char *buf, size_t count) +{ + struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); + ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, + driver, buf, count); + + if (retval >= 0 && usb_drv->usb_driver != NULL) + retval = usb_store_new_id(&usb_drv->usb_driver->dynids, + usb_drv->usb_driver->id_table, + &usb_drv->usb_driver->driver, + buf, count); + return retval; +} + +static ssize_t new_id_show(struct device_driver *driver, char *buf) +{ + struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); + + return usb_show_dynids(&usb_drv->dynids, buf); +} +static DRIVER_ATTR_RW(new_id); + +static struct attribute *usb_serial_drv_attrs[] = { + &driver_attr_new_id.attr, + NULL, +}; +ATTRIBUTE_GROUPS(usb_serial_drv); + +static void free_dynids(struct usb_serial_driver *drv) +{ + struct usb_dynid *dynid, *n; + + guard(mutex)(&usb_dynids_lock); + list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { + list_del(&dynid->node); + kfree(dynid); + } +} + +const struct bus_type usb_serial_bus_type = { + .name = "usb-serial", + .match = usb_serial_device_match, + .probe = usb_serial_device_probe, + .remove = usb_serial_device_remove, + .drv_groups = usb_serial_drv_groups, +}; + +int usb_serial_bus_register(struct usb_serial_driver *driver) +{ + int retval; + + driver->driver.bus = &usb_serial_bus_type; + INIT_LIST_HEAD(&driver->dynids.list); + + retval = driver_register(&driver->driver); + + return retval; +} + +void usb_serial_bus_deregister(struct usb_serial_driver *driver) +{ + free_dynids(driver); + driver_unregister(&driver->driver); +} + -- cgit v1.3.1