1. 17 10月, 2017 1 次提交
    • H
      USB: devio: Revert "USB: devio: Don't corrupt user memory" · 845d584f
      Hans de Goede 提交于
      Taking the uurb->buffer_length userspace passes in as a maximum for the
      actual urbs transfer_buffer_length causes 2 serious issues:
      
      1) It breaks isochronous support for all userspace apps using libusb,
         as existing libusb versions pass in 0 for uurb->buffer_length,
         relying on the kernel using the lenghts of the usbdevfs_iso_packet_desc
         descriptors passed in added together as buffer length.
      
         This for example causes redirection of USB audio and Webcam's into
         virtual machines using qemu-kvm to no longer work. This is a userspace
         ABI break and as such must be reverted.
      
         Note that the original commit does not protect other users / the
         kernels memory, it only stops the userspace process making the call
         from shooting itself in the foot.
      
      2) It may cause the kernel to program host controllers to DMA over random
         memory. Just as the devio code used to only look at the iso_packet_desc
         lenghts, the host drivers do the same, relying on the submitter of the
         urbs to make sure the entire buffer is large enough and not checking
         transfer_buffer_length.
      
         But the "USB: devio: Don't corrupt user memory" commit now takes the
         userspace provided uurb->buffer_length for the buffer-size while copying
         over the user-provided iso_packet_desc lengths 1:1, allowing the user
         to specify a small buffer size while programming the host controller to
         dma a lot more data.
      
         (Atleast the ohci, uhci, xhci and fhci drivers do not check
          transfer_buffer_length for isoc transfers.)
      
      This reverts commit fa1ed74e ("USB: devio: Don't corrupt user memory")
      fixing both these issues.
      
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: stable@vger.kernel.org
      Signed-off-by: NHans de Goede <hdegoede@redhat.com>
      Acked-by: NAlan Stern <stern@rowland.harvard.edu>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      845d584f
  2. 25 9月, 2017 2 次提交
  3. 28 8月, 2017 2 次提交
    • A
      USB: core: constify vm_operations_struct · b64d47ae
      Arvind Yadav 提交于
      vm_operations_struct are not supposed to change at runtime.
      All functions working with const vm_operations_struct.
      So mark the non-const structs as const.
      Signed-off-by: NArvind Yadav <arvind.yadav.cs@gmail.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      b64d47ae
    • D
      USB: core: Avoid race of async_completed() w/ usbdev_release() · ed62ca2f
      Douglas Anderson 提交于
      While running reboot tests w/ a specific set of USB devices (and
      slub_debug enabled), I found that once every few hours my device would
      be crashed with a stack that looked like this:
      
      [   14.012445] BUG: spinlock bad magic on CPU#0, modprobe/2091
      [   14.012460]  lock: 0xffffffc0cb055978, .magic: ffffffc0, .owner: cryption contexts: %lu/%lu
      [   14.012460] /1025536097, .owner_cpu: 0
      [   14.012466] CPU: 0 PID: 2091 Comm: modprobe Not tainted 4.4.79 #352
      [   14.012468] Hardware name: Google Kevin (DT)
      [   14.012471] Call trace:
      [   14.012483] [<....>] dump_backtrace+0x0/0x160
      [   14.012487] [<....>] show_stack+0x20/0x28
      [   14.012494] [<....>] dump_stack+0xb4/0xf0
      [   14.012500] [<....>] spin_dump+0x8c/0x98
      [   14.012504] [<....>] spin_bug+0x30/0x3c
      [   14.012508] [<....>] do_raw_spin_lock+0x40/0x164
      [   14.012515] [<....>] _raw_spin_lock_irqsave+0x64/0x74
      [   14.012521] [<....>] __wake_up+0x2c/0x60
      [   14.012528] [<....>] async_completed+0x2d0/0x300
      [   14.012534] [<....>] __usb_hcd_giveback_urb+0xc4/0x138
      [   14.012538] [<....>] usb_hcd_giveback_urb+0x54/0xf0
      [   14.012544] [<....>] xhci_irq+0x1314/0x1348
      [   14.012548] [<....>] usb_hcd_irq+0x40/0x50
      [   14.012553] [<....>] handle_irq_event_percpu+0x1b4/0x3f0
      [   14.012556] [<....>] handle_irq_event+0x4c/0x7c
      [   14.012561] [<....>] handle_fasteoi_irq+0x158/0x1c8
      [   14.012564] [<....>] generic_handle_irq+0x30/0x44
      [   14.012568] [<....>] __handle_domain_irq+0x90/0xbc
      [   14.012572] [<....>] gic_handle_irq+0xcc/0x18c
      
      Investigation using kgdb() found that the wait queue that was passed
      into wake_up() had been freed (it was filled with slub_debug poison).
      
      I analyzed and instrumented the code and reproduced.  My current
      belief is that this is happening:
      
      1. async_completed() is called (from IRQ).  Moves "as" onto the
         completed list.
      2. On another CPU, proc_reapurbnonblock_compat() calls
         async_getcompleted().  Blocks on spinlock.
      3. async_completed() releases the lock; keeps running; gets blocked
         midway through wake_up().
      4. proc_reapurbnonblock_compat() => async_getcompleted() gets the
         lock; removes "as" from completed list and frees it.
      5. usbdev_release() is called.  Frees "ps".
      6. async_completed() finally continues running wake_up().  ...but
         wake_up() has a pointer to the freed "ps".
      
      The instrumentation that led me to believe this was based on adding
      some trace_printk() calls in a select few functions and then using
      kdb's "ftdump" at crash time.  The trace follows (NOTE: in the trace
      below I cheated a little bit and added a udelay(1000) in
      async_completed() after releasing the spinlock because I wanted it to
      trigger quicker):
      
      <...>-2104   0d.h2 13759034us!: async_completed at start: as=ffffffc0cc638200
      mtpd-2055    3.... 13759356us : async_getcompleted before spin_lock_irqsave
      mtpd-2055    3d..1 13759362us : async_getcompleted after list_del_init: as=ffffffc0cc638200
      mtpd-2055    3.... 13759371us+: proc_reapurbnonblock_compat: free_async(ffffffc0cc638200)
      mtpd-2055    3.... 13759422us+: async_getcompleted before spin_lock_irqsave
      mtpd-2055    3.... 13759479us : usbdev_release at start: ps=ffffffc0cc042080
      mtpd-2055    3.... 13759487us : async_getcompleted before spin_lock_irqsave
      mtpd-2055    3.... 13759497us!: usbdev_release after kfree(ps): ps=ffffffc0cc042080
      <...>-2104   0d.h2 13760294us : async_completed before wake_up(): as=ffffffc0cc638200
      
      To fix this problem we can just move the wake_up() under the ps->lock.
      There should be no issues there that I'm aware of.
      Signed-off-by: NDouglas Anderson <dianders@chromium.org>
      Acked-by: NAlan Stern <stern@rowland.harvard.edu>
      Cc: stable <stable@vger.kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      ed62ca2f
  4. 30 6月, 2017 1 次提交
  5. 13 6月, 2017 1 次提交
    • A
      USB: add usbfs ioctl to retrieve the connection speed · c01b244a
      Alan Stern 提交于
      The usbfs interface does not provide any way for the user to learn the
      speed at which a device is connected.  The current API includes a
      USBDEVFS_CONNECTINFO ioctl, but all it provides is the device's
      address and a one-bit value indicating whether the connection is low
      speed.  That may have sufficed in the era of USB-1.1, but it isn't
      good enough today.
      
      This patch introduces a new ioctl, USBDEVFS_GET_SPEED, which returns a
      numeric value indicating the speed of the connection: unknown, low,
      full, high, wireless, super, or super-plus.
      
      Similar information (not exactly the same) is available through sysfs,
      but it seems reasonable to provide the actual value in usbfs.
      Signed-off-by: NAlan Stern <stern@rowland.harvard.edu>
      Reported-by: NReinhard Huck <reinhard.huck@thesycon.de>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      c01b244a
  6. 17 5月, 2017 1 次提交
    • V
      USB: core: replace %p with %pK · 2f964780
      Vamsi Krishna Samavedam 提交于
      Format specifier %p can leak kernel addresses while not valuing the
      kptr_restrict system settings. When kptr_restrict is set to (1), kernel
      pointers printed using the %pK format specifier will be replaced with
      Zeros. Debugging Note : &pK prints only Zeros as address. If you need
      actual address information, write 0 to kptr_restrict.
      
      echo 0 > /proc/sys/kernel/kptr_restrict
      
      [Found by poking around in a random vendor kernel tree, it would be nice
      if someone would actually send these types of patches upstream - gkh]
      Signed-off-by: NVamsi Krishna Samavedam <vskrishn@codeaurora.org>
      Cc: stable <stable@vger.kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      2f964780
  7. 02 3月, 2017 1 次提交
  8. 28 2月, 2017 1 次提交
  9. 11 1月, 2017 1 次提交
  10. 28 9月, 2016 1 次提交
  11. 24 8月, 2016 1 次提交
    • A
      USB: avoid left shift by -1 · 53e5f36f
      Alan Stern 提交于
      UBSAN complains about a left shift by -1 in proc_do_submiturb().  This
      can occur when an URB is submitted for a bulk or control endpoint on
      a high-speed device, since the code doesn't bother to check the
      endpoint type; normally only interrupt or isochronous endpoints have
      a nonzero bInterval value.
      
      Aside from the fact that the operation is illegal, it shouldn't matter
      because the result isn't used.  Still, in theory it could cause a
      hardware exception or other problem, so we should work around it.
      This patch avoids doing the left shift unless the shift amount is >= 0.
      
      The same piece of code has another problem.  When checking the device
      speed (the exponential encoding for interrupt endpoints is used only
      by high-speed or faster devices), we need to look for speed >=
      USB_SPEED_SUPER as well as speed == USB_SPEED HIGH.  The patch adds
      this check.
      Signed-off-by: NAlan Stern <stern@rowland.harvard.edu>
      Reported-by: NVittorio Zecca <zeccav@gmail.com>
      Tested-by: NVittorio Zecca <zeccav@gmail.com>
      Suggested-by: NBjørn Mork <bjorn@mork.no>
      CC: <stable@vger.kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      53e5f36f
  12. 09 8月, 2016 2 次提交
    • A
      USB: remove race condition in usbfs/libusb when using reap-after-disconnect · 5cce4382
      Alan Stern 提交于
      Hans de Goede has reported a difficulty in the Linux port of libusb.
      When a device is removed, the poll() system call in usbfs starts
      returning POLLERR as soon as udev->state is set to
      USB_STATE_NOTATTACHED, but the outstanding URBs are not available for
      reaping until some time later (after usbdev_remove() has been called).
      This is awkward for libusb or other usbfs clients, although not an
      insuperable problem.
      
      At any rate, it's easy to change usbfs so that it returns POLLHUP as
      soon as the state becomes USB_STATE_NOTATTACHED but it doesn't return
      POLLERR until after the outstanding URBs have completed.  That's what
      this patch does; it uses the fact that ps->list is always on the
      dev->filelist list until usbdev_remove() takes it off, which happens
      after all the outstanding URBs have been cancelled.
      Signed-off-by: NAlan Stern <stern@rowland.harvard.edu>
      Reported-by: NHans de Goede <hdegoede@redhat.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      5cce4382
    • J
      usb: devio, do not warn when allocation fails · 70f7ca9a
      Jiri Slaby 提交于
      usbdev_mmap allocates a buffer. The size of the buffer is determined
      by a user. So with this code (no need to be root):
      
      	int fd = open("/dev/bus/usb/001/001", O_RDONLY);
      	mmap(NULL, 0x800000, PROT_READ, MAP_SHARED, fd, 0);
      
      we can see a warning:
      
      WARNING: CPU: 0 PID: 21771 at ../mm/page_alloc.c:3563 __alloc_pages_slowpath+0x1036/0x16e0()
      ...
      Call Trace:
       [<ffffffff8117a3ae>] ? warn_slowpath_null+0x2e/0x40
       [<ffffffff815178b6>] ? __alloc_pages_slowpath+0x1036/0x16e0
       [<ffffffff81516880>] ? warn_alloc_failed+0x250/0x250
       [<ffffffff8151226b>] ? get_page_from_freelist+0x75b/0x28b0
       [<ffffffff815184e3>] ? __alloc_pages_nodemask+0x583/0x6b0
       [<ffffffff81517f60>] ? __alloc_pages_slowpath+0x16e0/0x16e0
       [<ffffffff810565d4>] ? dma_generic_alloc_coherent+0x104/0x220
       [<ffffffffa0269e56>] ? hcd_buffer_alloc+0x1d6/0x3e0 [usbcore]
       [<ffffffffa0269c80>] ? hcd_buffer_destroy+0xa0/0xa0 [usbcore]
       [<ffffffffa0228f05>] ? usb_alloc_coherent+0x65/0x90 [usbcore]
       [<ffffffffa0275c05>] ? usbdev_mmap+0x1a5/0x770 [usbcore]
      ...
      
      Allocations like this one should be marked as __GFP_NOWARN. So do so.
      
      The size could be also clipped by something like:
      	if (size >= (1 << (MAX_ORDER + PAGE_SHIFT - 1)))
      		return -ENOMEM;
      But I think the overall limit of 16M (by usbfs_increase_memory_usage)
      is enough, so that we only silence the warning here.
      Signed-off-by: NJiri Slaby <jslaby@suse.cz>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Alan Stern <stern@rowland.harvard.edu>
      Cc: Steinar H. Gunderson <sesse@google.com>
      Cc: Markus Rechberger <mrechberger@gmail.com>
      Fixes: f7d34b44 (USB: Add support for usbfs zerocopy.)
      Cc: 4.6+ <stable@vger.kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      70f7ca9a
  13. 04 5月, 2016 1 次提交
  14. 29 4月, 2016 1 次提交
  15. 06 3月, 2016 1 次提交
    • R
      usb: devio: Add ioctl to disallow detaching kernel USB drivers. · d883f52e
      Reilly Grant 提交于
      The new USBDEVFS_DROP_PRIVILEGES ioctl allows a process to voluntarily
      relinquish the ability to issue other ioctls that may interfere with
      other processes and drivers that have claimed an interface on the
      device.
      
      This commit also includes a simple utility to be able to test the
      ioctl, located at Documentation/usb/usbdevfs-drop-permissions.c
      
      Example (with qemu-kvm's input device):
      
          $ lsusb
          ...
          Bus 001 Device 002: ID 0627:0001 Adomax Technology Co., Ltd
      
          $ usb-devices
          ...
          C:  #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
          I:  If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=00 Prot=02 Driver=usbhid
      
          $ sudo ./usbdevfs-drop-permissions /dev/bus/usb/001/002
          OK: privileges dropped!
          Available options:
          [0] Exit now
          [1] Reset device. Should fail if device is in use
          [2] Claim 4 interfaces. Should succeed where not in use
          [3] Narrow interface permission mask
          Which option shall I run?: 1
          ERROR: USBDEVFS_RESET failed! (1 - Operation not permitted)
          Which test shall I run next?: 2
          ERROR claiming if 0 (1 - Operation not permitted)
          ERROR claiming if 1 (1 - Operation not permitted)
          ERROR claiming if 2 (1 - Operation not permitted)
          ERROR claiming if 3 (1 - Operation not permitted)
          Which test shall I run next?: 0
      
      After unbinding usbhid:
      
          $ usb-devices
          ...
          I:  If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=00 Prot=02 Driver=(none)
      
          $ sudo ./usbdevfs-drop-permissions /dev/bus/usb/001/002
          ...
          Which option shall I run?: 2
          OK: claimed if 0
          ERROR claiming if 1 (1 - Operation not permitted)
          ERROR claiming if 2 (1 - Operation not permitted)
          ERROR claiming if 3 (1 - Operation not permitted)
          Which test shall I run next?: 1
          OK: USBDEVFS_RESET succeeded
          Which test shall I run next?: 0
      
      After unbinding usbhid and restricting the mask:
      
          $ sudo ./usbdevfs-drop-permissions /dev/bus/usb/001/002
          ...
          Which option shall I run?: 3
          Insert new mask: 0
          OK: privileges dropped!
          Which test shall I run next?: 2
          ERROR claiming if 0 (1 - Operation not permitted)
          ERROR claiming if 1 (1 - Operation not permitted)
          ERROR claiming if 2 (1 - Operation not permitted)
          ERROR claiming if 3 (1 - Operation not permitted)
      Signed-off-by: NReilly Grant <reillyg@chromium.org>
      Acked-by: NAlan Stern <stern@rowland.harvard.edu>
      Signed-off-by: NEmilio López <emilio.lopez@collabora.co.uk>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      d883f52e
  16. 15 2月, 2016 1 次提交
    • S
      USB: Add support for usbfs zerocopy. · f7d34b44
      Steinar H. Gunderson 提交于
      Add a new interface for userspace to preallocate memory that can be
      used with usbfs. This gives two primary benefits:
      
       - Zerocopy; data no longer needs to be copied between the userspace
         and the kernel, but can instead be read directly by the driver from
         userspace's buffers. This works for all kinds of transfers (even if
         nonsensical for control and interrupt transfers); isochronous also
         no longer need to memset() the buffer to zero to avoid leaking kernel data.
      
       - Once the buffers are allocated, USB transfers can no longer fail due to
         memory fragmentation; previously, long-running programs could run into
         problems finding a large enough contiguous memory chunk, especially on
         embedded systems or at high rates.
      
      Memory is allocated by using mmap() against the usbfs file descriptor,
      and similarly deallocated by munmap(). Once memory has been allocated,
      using it as pointers to a bulk or isochronous operation means you will
      automatically get zerocopy behavior. Note that this also means you cannot
      modify outgoing data until the transfer is complete. The same holds for
      data on the same cache lines as incoming data; DMA modifying them at the
      same time could lead to your changes being overwritten.
      
      There's a new capability USBDEVFS_CAP_MMAP that userspace can query to see
      if the running kernel supports this functionality, if just trying mmap() is
      not acceptable.
      
      Largely based on a patch by Markus Rechberger with some updates. The original
      patch can be found at:
      
        http://sundtek.de/support/devio_mmap_v0.4.diffSigned-off-by: NSteinar H. Gunderson <sesse@google.com>
      Signed-off-by: NMarkus Rechberger <mrechberger@gmail.com>
      Acked-by: NAlan Stern <stern@rowland.harvard.edu>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      f7d34b44
  17. 25 1月, 2016 3 次提交
  18. 23 12月, 2015 1 次提交
  19. 02 12月, 2015 2 次提交
    • A
      USB: add usbfs snooping for REAP and DISCARD · a016a816
      Alan Stern 提交于
      This patch improves the usbfs_snoop debugging facility by adding
      messages for a couple of significant events which, up to now, have not
      been logged.  The events are reaping and discarding (i.e.,
      cancelling) an URB.  The debugging messages include the userspace
      address of the URB being reaped or discarded.
      
      The reaping messages have to be added in four places, in order to
      handle blocking and non-blocking reaps in both normal and 32-bit
      compatibility mode.
      Signed-off-by: NAlan Stern <stern@rowland.harvard.edu>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      a016a816
    • A
      USB: limit usbfs snooping of URB contents · 0290cc9f
      Alan Stern 提交于
      The usbfs_snoop facility can be very useful for debugging problems
      involving usbfs.  However, it always prints out the entire contents of
      every URB.  When dealing with large quantities of data, this can be
      less than helpful.
      
      This patch ameliorates the situation by adding a module parameter to
      usbcore for controlling the maximum number of bytes to print when
      snooping an URB.  This makes debugging much easier.  For backward
      compatibility, the default value is set unreasonably high.
      Signed-off-by: NAlan Stern <stern@rowland.harvard.edu>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      0290cc9f
  20. 09 8月, 2015 1 次提交
  21. 06 8月, 2015 1 次提交
  22. 25 5月, 2015 1 次提交
  23. 10 4月, 2015 1 次提交
  24. 25 2月, 2015 1 次提交
  25. 01 2月, 2015 1 次提交
  26. 02 8月, 2014 1 次提交
  27. 11 3月, 2014 1 次提交
  28. 05 3月, 2014 5 次提交
  29. 25 2月, 2014 1 次提交
    • A
      USB: complain if userspace resets an active endpoint · f080a51b
      Alan Stern 提交于
      It is an error for a driver to call usb_clear_halt() or
      usb_reset_endpoint() while there are URBs queued for the endpoint,
      because the end result is not well defined.  At the time the endpoint
      gets reset, it may or may not be actively running.
      
      As far as I know, no kernel drivers do this.  But some userspace
      drivers do, and it seems like a good idea to bring this error to their
      attention.
      
      This patch adds a warning to the kernel log whenever a program invokes
      the USBDEVFS_CLEAR_HALT or USBDEVFS_RESETEP ioctls at an inappropriate
      time, and includes the name of the program.  This will make it clear
      that any subsequent errors are not due to the misbehavior of a kernel
      driver.
      Signed-off-by: NAlan Stern <stern@rowland.harvard.edu>
      Suggested-by: NBjørn Mork <bjorn@mork.no>
      CC: Stanislaw Gruszka <sgruszka@redhat.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      f080a51b
  30. 08 1月, 2014 1 次提交