Skip to content

Commit aa8141d

Browse files
larsclausengregkh
authored andcommitted
usb: gadget: ffs: Execute copy_to_user() with USER_DS set
[ Upstream commit 4058ebf ] When using a AIO read() operation on the function FS gadget driver a URB is submitted asynchronously and on URB completion the received data is copied to the userspace buffer associated with the read operation. This is done from a kernel worker thread invoking copy_to_user() (through copy_to_iter()). And while the user space process memory is made available to the kernel thread using use_mm(), some architecture require in addition to this that the operation runs with USER_DS set. Otherwise the userspace memory access will fail. For example on ARM64 with Privileged Access Never (PAN) and User Access Override (UAO) enabled the following crash occurs. Internal error: Accessing user space memory with fs=KERNEL_DS: 9600004f [#1] SMP Modules linked in: CPU: 2 PID: 1636 Comm: kworker/2:1 Not tainted 4.9.0-04081-g8ab2dfb-dirty #487 Hardware name: ZynqMP ZCU102 Rev1.0 (DT) Workqueue: events ffs_user_copy_worker task: ffffffc87afc8080 task.stack: ffffffc87a00c000 PC is at __arch_copy_to_user+0x190/0x220 LR is at copy_to_iter+0x78/0x3c8 [...] [<ffffff800847b790>] __arch_copy_to_user+0x190/0x220 [<ffffff80086f25d8>] ffs_user_copy_worker+0x70/0x130 [<ffffff80080b8c64>] process_one_work+0x1dc/0x460 [<ffffff80080b8f38>] worker_thread+0x50/0x4b0 [<ffffff80080bf5a0>] kthread+0xd8/0xf0 [<ffffff8008083680>] ret_from_fork+0x10/0x50 Address this by placing a set_fs(USER_DS) before of the copy operation and revert it again once the copy operation has finished. This patch is analogous to commit d7ffde3 ("vhost: use USER_DS in vhost_worker thread") which addresses the same underlying issue. Signed-off-by: Lars-Peter Clausen <[email protected]> Signed-off-by: Felipe Balbi <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3fb5102 commit aa8141d

File tree

1 file changed

+4
-0
lines changed
  • drivers/usb/gadget/function

1 file changed

+4
-0
lines changed

drivers/usb/gadget/function/f_fs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,15 @@ static void ffs_user_copy_worker(struct work_struct *work)
649649
bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
650650

651651
if (io_data->read && ret > 0) {
652+
mm_segment_t oldfs = get_fs();
653+
654+
set_fs(USER_DS);
652655
use_mm(io_data->mm);
653656
ret = copy_to_iter(io_data->buf, ret, &io_data->data);
654657
if (ret != io_data->req->actual && iov_iter_count(&io_data->data))
655658
ret = -EFAULT;
656659
unuse_mm(io_data->mm);
660+
set_fs(oldfs);
657661
}
658662

659663
io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);

0 commit comments

Comments
 (0)