usbip: make vhci_hcd.* objects independent of vhci_hcd.0
Subsystems (2)
cover usbip: make vhci_hcd.* objects independent of vhci_hcd.0
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
So, we got the idea that in order to enforce that remote usbip devices are only visible inside the container where they were imported, we could conveniently mount-bind inside every container just one of the vhci_hcd directories below /sys/devices/platform. So that it is as if every container had a vhci_hcd just for itself (and also, we restrict access to the matching USB ports in cgroups).
Unfortunately, all the vhci_hcd.* devices are controlled through attributes in vhci_hcd.0 making our approach fail and so... well, that is what this patch series changes. It makes every vhci_hcd device controllable through attributes inside its own sysfs directory.
The first patch, does that in the kernel, and the second and third patches change user space, adapting the libusbip and the usbip tools code respectively.
Then there is a fourth patch, that allows to create much more USB hubs per machine. It was limited to 64 and we are running thousands of containers (every one requiring a hub) per host.
These changes are not completely backward compatible. In the sysfs side, besides moving around the attribute files, now the port numbers go from 0 to CONFIG_USBIP_VHCI_HC_PORTS * 2 - 1 and are reused for every vhci_hcd device. I could have maintained the absolute numeration but I think reusing the numbers is a better and simpler approach.
I have considered that until very recently, support for vhci_hcd devices above the .0 was broken in the uspip tools (see 1ac7c8a78be85f84b019d3d2742d1a9f07255cc5 "usbip: fix usbip attach to find a port that matches the requested speed") and that has been the place where I have set the bar for backward compatibility: usage of the tools must remain unchanged for accessing "vhci_hcd.0", but may require changes otherwise. The same is true for the library functions which now provides new functions for selecting the target vhci_hcd device. The old functions now always target "vhci_hcd.0".
So, for instance, now, "usbip port" by default only shows "vhci_hcd.0" ports but "usbip port -a" shows all of them, and, for instance, "usbip port -i 4", shows the ports in "vhci_hcd.4".
Cheers.
PATCH 1/4 usbip: make vhci_hcd.* objects independent of vhci_hcd.0
From: Salvador Fandiño <[email protected]>
The usbip VHCI layer supports multiple "vhci_hcd" devices, every one emulating both a high speed and a super speed USB hub. These devices are exposed in sysfs as "vhci_hcd.0", "vhci_hcd.1", etc.
But instead of controlling then by attributes inside their respective directories, all of then were controlled through "vhci_hcd.0" where the attributes "attach" and "detach" were used to connect and disconnect respectively remote USB devices to any of the virtual hub ports. Also port status was show but a series of "status.X" attributes, all inside "vhci_hcd.0".
Besides the rather unusuality of this approach, it precludes users from doing interesting things, as for instance, restricting the access to vhci_hcd devices independently.
This patch adds "attach", "detach", "status" and "nports" attributes inside every "vhci_hcd" object, besides "vhci_hcd.0" making then independent.
Attribute "debug", allowing to control when debug messages for the usbip subsystems are generated is, as before, only attached to "vhci_hcd.0".
--- drivers/usb/usbip/vhci.h | 21 ++- drivers/usb/usbip/vhci_hcd.c | 25 ++-- drivers/usb/usbip/vhci_sysfs.c | 311 +++++++++++++---------------------------- 3 files changed, 132 insertions(+), 225 deletions(-)
drivers/usb/usbip/vhci.h +18 -3
@@ -88,7 +88,14 @@ enum hub_speed {#define VHCI_NR_HCS 1#endif -#define MAX_STATUS_NAME 16 +struct vhci_attrs { +struct dev_ext_attribute dev_attr_status; +struct dev_ext_attribute dev_attr_attach; +struct dev_ext_attribute dev_attr_detach; +struct dev_ext_attribute dev_attr_nports; + +struct attribute_group attribute_group; +};struct vhci {spinlock_t lock;
@@ -97,6 +104,8 @@ struct vhci {struct vhci_hcd *vhci_hcd_hs;struct vhci_hcd *vhci_hcd_ss; + +struct vhci_attrs *attrs; };/* for usb_hcd.hcd_priv[0] */
@@ -126,8 +135,8 @@ extern struct attribute_group vhci_attr_group;void rh_port_connect (struct vhci_device *vdev,enum usb_device_speed speed);/* vhci_sysfs.c */ -int vhci_init_attr_group (void ); -void vhci_finish_attr_group (void ); +int vhci_init_attr_group (struct vhci_hcd *vhci_hcd,int id); +void vhci_finish_attr_group (struct vhci_hcd *vhci_hcd);/* vhci_rx.c */ struct urb *pickup_urb_and_free_priv (struct vhci_device *vdev,__u32 seqnum);
@@ -171,4 +180,10 @@ static inline struct vhci_hcd *vdev_to_vhci_hcd(struct vhci_device *vdev) return container_of((void *)(vdev - vdev->rhport), struct vhci_hcd, vdev); } +static inline struct vhci *device_attribute_to_vhci ( +struct device_attribute *attr) +{ +return (struct vhci *)((struct dev_ext_attribute *)attr)->var; +} +#endif /* __USBIP_VHCI_H */
drivers/usb/usbip/vhci_hcd.c +14 -11
@@ -1110,13 +1110,14 @@ static int vhci_setup(struct usb_hcd *hcd)static int vhci_start (struct usb_hcd *hcd) {struct vhci_hcd *vhci_hcd =hcd_to_vhci_hcd (hcd); +struct vhci *vhci = vhci_hcd->vhci;int id, rhport;int err; -usbip_dbg_vhci_hc ("enter vhci_start\n" ); +usbip_dbg_vhci_hc ("enter vhci_start for %s\n" ,hcd_name (hcd));if (usb_hcd_is_primary_hcd (hcd)) -spin_lock_init (&vhci_hcd->vhci->lock); +spin_lock_init (&vhci->lock);/* initialize private data of usb_hcd */
@@ -1143,16 +1144,17 @@ static int vhci_start(struct usb_hcd *hcd) }/* vhci_hcd is now ready to be controlled through sysfs */ -if (id ==0 &&usb_hcd_is_primary_hcd (hcd)) { - err =vhci_init_attr_group (); +if (usb_hcd_is_primary_hcd (hcd)) { + err =vhci_init_attr_group (vhci_hcd, id);if (err) {pr_err ("init attr group\n" );return err; } - err =sysfs_create_group (&hcd_dev (hcd)->kobj, &vhci_attr_group); + err =sysfs_create_group (&hcd_dev (hcd)->kobj, + &vhci->attrs->attribute_group);if (err) {pr_err ("create sysfs files\n" ); -vhci_finish_attr_group (); +vhci_finish_attr_group (vhci_hcd);return err; }pr_info ("created sysfs %s\n" ,hcd_name (hcd));
@@ -1164,15 +1166,16 @@ static int vhci_start(struct usb_hcd *hcd)static void vhci_stop (struct usb_hcd *hcd) {struct vhci_hcd *vhci_hcd =hcd_to_vhci_hcd (hcd); -int id, rhport; +struct vhci *vhci = vhci_hcd->vhci; +int rhport;usbip_dbg_vhci_hc ("stop VHCI controller\n" );/* 1. remove the userland interface of vhci_hcd */ - id =hcd_name_to_id (hcd_name (hcd)); -if (id ==0 &&usb_hcd_is_primary_hcd (hcd)) { -sysfs_remove_group (&hcd_dev (hcd)->kobj, &vhci_attr_group); -vhci_finish_attr_group (); +if (usb_hcd_is_primary_hcd (hcd)) { +sysfs_remove_group (&hcd_dev (hcd)->kobj, + &vhci->attrs->attribute_group); +vhci_finish_attr_group (vhci_hcd); }/* 2. shutdown all the ports of vhci_hcd */
drivers/usb/usbip/vhci_sysfs.c +100 -211
440 lines not on this page · raw message
PATCH 2/4 libusbip: use per vhci_hcd controller attributes
In reply to Salvador FandinoFrom: Salvador Fandiño <[email protected]>
Now every vhci_hcd device is controlled by its own sysfs attributes (before all of them were controlled by vhci_hcd.0 attributes). This patch addapts libusbip to this new interface.
Before this patch the library did not provide any mean to access a specific vhci_hcd device. In order to keep backward compatibility while at the same time allowing to access vhci_hcd devices independently the following approach has been taken:
New functions accepting a vhci_hcd path or an index have been added ("usbip_vhci_driver_open_path" and "usbip_vhci_driver_open_ix" respectively). The old function "usbip_vhci_driver_open" can only be used to open "vhci_hcd.0"). That will make old programs fail to use any other vhci_hcd device, but as that functionality was broken in the kernel anyway until very recently (see 1ac7c8a78be85f84b019d3d2742d1a9f07255cc5) we consider it a good compromise.
--- tools/usb/usbip/libsrc/vhci_driver.c | 105 +++++++++++++++++++++-------------- tools/usb/usbip/libsrc/vhci_driver.h | 12 +++- 2 files changed, 74 insertions(+), 43 deletions(-)
tools/usb/usbip/libsrc/vhci_driver.c +64 -41
@@ -114,13 +114,16 @@ static int refresh_imported_device_list(void)char status[MAX_STATUS_NAME +1 ] ="status" ;int i, ret; -for (i =0 ; i < vhci_driver->ncontrollers; i++) { +for (i =0 ; ; i++) {if (i >0 )snprintf (status,sizeof (status),"status.%d" , i); attr_status =udev_device_get_sysattr_value (vhci_driver->hc_device, status);if (!attr_status) { +if (i >0 ) +break ; +err ("udev_device_get_sysattr_value failed" );return -1 ; }
@@ -148,33 +151,6 @@ static int get_nports(void)return (int )strtoul (attr_nports,NULL ,10 ); } -static int vhci_hcd_filter (const struct dirent *dirent) -{ -return strcmp (dirent->d_name,"vhci_hcd" ) >=0 ; -} - -static int get_ncontrollers (void ) -{ -struct dirent **namelist; -struct udev_device *platform; -int n; - - platform =udev_device_get_parent (vhci_driver->hc_device); -if (platform ==NULL ) -return -1 ; - - n =scandir (udev_device_get_syspath (platform), &namelist, vhci_hcd_filter,NULL ); -if (n <0 ) -err ("scandir failed" ); -else { -for (int i =0 ; i < n; i++) -free (namelist[i]); -free (namelist); - } - -return n; -} -/* * Read the given port's record. *
@@ -199,7 +175,8 @@ static int read_record(int rhport, char *host, unsigned long host_len,if (!buffer)return -1 ; -snprintf (path,PATH_MAX ,VHCI_STATE_PATH "/port%d" , rhport); +snprintf (path,PATH_MAX ,VHCI_STATE_PATH "/port%d-%d" , + vhci_driver->ix, rhport); file =fopen (path,"r" );if (!file) {
@@ -238,9 +215,18 @@ static int read_record(int rhport, char *host, unsigned long host_len,return 0 ; } +static int vhci_path_to_ix (const char *path) +{ +int i =strlen (path); + +while (--i >=0 &&isdigit (path[i])) + ; +return atoi (path + i +1 ); +} +/* ---------------------------------------------------------------------- */ -int usbip_vhci_driver_open (void ) +int usbip_vhci_driver_open_path (const char *path) { udev_context =udev_new ();if (!udev_context) {
@@ -252,14 +238,15 @@ int usbip_vhci_driver_open(void)/* will be freed in usbip_driver_close() */ vhci_driver->hc_device = -udev_device_new_from_subsystem_sysname (udev_context, -USBIP_VHCI_BUS_TYPE , -USBIP_VHCI_DEVICE_NAME ); +udev_device_new_from_syspath (udev_context, path); +if (!vhci_driver->hc_device) {err ("udev_device_new_from_subsystem_sysname failed" ); goto err; } + vhci_driver->ix =vhci_path_to_ix (path); + vhci_driver->nports =get_nports ();dbg ("available ports: %d" , vhci_driver->nports);
@@ -271,14 +258,6 @@ int usbip_vhci_driver_open(void) goto err; } - vhci_driver->ncontrollers =get_ncontrollers (); -dbg ("available controllers: %d" , vhci_driver->ncontrollers); - -if (vhci_driver->ncontrollers <=0 ) { -err ("no available usb controllers" ); - goto err; - } -if (refresh_imported_device_list ()) goto err;
@@ -297,6 +276,45 @@ int usbip_vhci_driver_open(void)return -1 ; } +int usbip_vhci_driver_open (void ) +{ +return usbip_vhci_driver_open_ix (0 ); +} + +int usbip_vhci_driver_open_ix (int vhci_ix) +{ +struct udev *udev_context; +struct udev_device *dev =NULL ; +char vhci_name[PATH_MAX +1 ]; +int len, rc =-1 ; + + len =snprintf (vhci_name,PATH_MAX , +"%s.%d" ,USBIP_VHCI_DEVICE_NAME_PREFIX , vhci_ix); +if (len >=PATH_MAX ) { +err ("vhci device name is too long" ); +return -1 ; + } + + udev_context =udev_new (); +if (!udev_context) { +err ("udev_new failed" ); +return -1 ; + } + + dev =udev_device_new_from_subsystem_sysname (udev_context, +USBIP_VHCI_BUS_TYPE , + vhci_name); +if (!dev) { +err ("udev_device_from_subsystem_sysname failed to open %s" , + vhci_name); + }else { + rc =usbip_vhci_driver_open_path (udev_device_get_syspath (dev)); +udev_device_unref (dev); + } + +udev_unref (udev_context); +return rc; +}void usbip_vhci_driver_close (void ) {
@@ -459,3 +477,8 @@ int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)return 0 ; } + +int usbip_vhci_driver_ix (void ) +{ +return vhci_driver->ix; +}
tools/usb/usbip/libsrc/vhci_driver.h +10 -2
@@ -12,7 +12,9 @@#include "usbip_common.h" #define USBIP_VHCI_BUS_TYPE "platform" -#define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0" +#define USBIP_VHCI_DEVICE_NAME_PREFIX "vhci_hcd" +#define USBIP_VHCI_DEVICE_NAME_0 (USBIP_VHCI_DEVICE_NAME_PREFIX ".0") +#define USBIP_VHCI_DEVICE_NAME_PATTERN (USBIP_VHCI_DEVICE_NAME_PREFIX ".*")#define MAXNPORT 128enum hub_speed {
@@ -39,7 +41,7 @@ struct usbip_vhci_driver {/* /sys/devices/platform/vhci_hcd */ struct udev_device *hc_device; -int ncontrollers; +int ix;int nports;struct usbip_imported_device idev[MAXNPORT ]; };
@@ -47,7 +49,11 @@ struct usbip_vhci_driver {extern struct usbip_vhci_driver *vhci_driver; +/* will be removed */ int usbip_vhci_driver_open (void ); + +int usbip_vhci_driver_open_path (const char *); +int usbip_vhci_driver_open_ix (int vhci_ix);void usbip_vhci_driver_close (void );int usbip_vhci_refresh_device_list (void );
@@ -65,4 +71,6 @@ int usbip_vhci_detach_device(uint8_t port);int usbip_vhci_imported_device_dump (struct usbip_imported_device *idev); +int usbip_vhci_driver_ix (void ); +#endif /* __VHCI_DRIVER_H */
PATCH 3/4 usbip tools: use per vhci_hcd controller attributes
In reply to Salvador FandinoFrom: Salvador Fandiño <[email protected]>
Now every vhci_hcd device is controlled by its own sysfs attributes (before all of them were controlled by vhci_hcd.0 attributes). This patch addapts the usbip tools to use the new interface through libusbip.
The user visible changes are as follows:
- The files containing port information are named as "port$ix-$port" (they were "port$port" before), as port numbers are local to the vhci_hcd device now.
- Command "usbip port" by default only list ports attached to "vhci_hcd.0", the new flag "--vhci-ix" can be used to list other device. The flag "--all" is also available for listing all the devices (it has not been made the default because it inserts per vhci_hcd device headers, changing the output format).
--- tools/usb/usbip/src/Makefile.am | 3 +- tools/usb/usbip/src/usbip_attach.c | 101 ++++++++++++++++-------- tools/usb/usbip/src/usbip_detach.c | 57 ++++++++------ tools/usb/usbip/src/usbip_enumerate.c | 55 +++++++++++++ tools/usb/usbip/src/usbip_enumerate.h | 25 ++++++ tools/usb/usbip/src/usbip_port.c | 143 +++++++++++++++++++++++++++++----- tools/usb/usbip/src/utils.c | 16 ++++ tools/usb/usbip/src/utils.h | 1 + 8 files changed, 322 insertions(+), 79 deletions(-) create mode 100644 tools/usb/usbip/src/usbip_enumerate.c create mode 100644 tools/usb/usbip/src/usbip_enumerate.h
tools/usb/usbip/src/Makefile.am +2 -1
@@ -7,6 +7,7 @@ sbin_PROGRAMS := usbip usbipd usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \ usbip_attach.c usbip_detach.c usbip_list.c \ - usbip_bind.c usbip_unbind.c usbip_port.c + usbip_bind.c usbip_unbind.c usbip_port.c \ + usbip_enumerate.c usbipd_SOURCES := usbip_network.h usbipd.c usbip_network.c
tools/usb/usbip/src/usbip_attach.c +68 -33
162 lines not on this page · raw message
tools/usb/usbip/src/usbip_detach.c +32 -25
109 lines not on this page · raw message
tools/usb/usbip/src/usbip_enumerate.c new file +55 -0
55 lines not on this page · raw message
tools/usb/usbip/src/usbip_enumerate.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2018 Qindel Formacion y Servicios SL +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __USBIP_ENUMERATE_H +#define __USBIP_ENUMERATE_H + +#include <libudev.h> + +struct udev_enumerate *vhci_enumerate (void ); + +#endif /* __USBIP_ENUMERATE_H */
tools/usb/usbip/src/usbip_port.c +123 -20
177 lines not on this page · raw message
tools/usb/usbip/src/utils.c +16 -0
@@ -19,6 +19,8 @@#include <errno.h> #include <stdio.h> #include <string.h> +#include <stdlib.h> +#include <ctype.h> #include "usbip_common.h" #include "utils.h"
@@ -53,3 +55,17 @@ int modify_match_busid(char *busid, int add)return 0 ; } + +int atoi_with_check (const char *str,int *pi) +{ +ssize_t len =strlen (str); + +for (ssize_t i =0 ; i < len; i++) { +if (!isdigit (str[i])) { +err ("%s is not a number" , str); +return -1 ; + } + } + *pi =atoi (str); +return 0 ; +}
tools/usb/usbip/src/utils.h +1 -0
@@ -20,6 +20,7 @@#define __UTILS_Hint modify_match_busid (char *busid,int add); +int atoi_with_check (const char *str,int *pi);#endif /* __UTILS_H */
PATCH 4/4 config: make USB_MAXBUS configurable and adjust VHCI_NR_HCS top limit
In reply to Salvador FandinoFrom: Salvador Fandiño <[email protected]>
The maximum number of USB host controllers supported by the kernel had a hard-coded limit of 64. In some scenarios that limit may be not enough. For instance, in my particular case, I have systems with thousands of containers running and would like to provide a VHCI (USBIP host controller) device to every one of them but can't because of this limit.
This patch adds a new configuration entry for USB_MAXBUS and also increases the top limit for USBIP_VHCI_NR_HCS (number of VHCI controllers) accordingly.
--- drivers/usb/Kconfig | 9 +++++++++ drivers/usb/core/hcd.c | 2 +- drivers/usb/usbip/Kconfig | 5 ++++- 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/usb/Kconfig +9 -0
@@ -72,6 +72,15 @@ config USB To compile this driver as a module, choose M here: the module will be called usbcore. +config USB_MAXBUS + int "Maximum number of USB host controllers" + range 1 4096 + default 64 + ---help--- + This defines the number of USB host controllers that would be + available, both physical and virtual (for instance, those + used by USB/IP). + config USB_PCI bool "PCI based USB host interface" depends on PCI
drivers/usb/core/hcd.c +1 -1
@@ -83,7 +83,7 @@ DEFINE_IDR (usb_bus_idr);EXPORT_SYMBOL_GPL (usb_bus_idr);/* used when allocating bus numbers */ -#define USB_MAXBUS 64 +#define USB_MAXBUS CONFIG_USB_MAXBUS/* used when updating list of hcds */ DEFINE_MUTEX (usb_bus_idr_lock);/* exported only for usbfs */
drivers/usb/usbip/Kconfig +4 -1
@@ -37,7 +37,7 @@ config USBIP_VHCI_HC_PORTS config USBIP_VHCI_NR_HCS int "Number of USB/IP virtual host controllers" - range 1 128 + range 1 2048 default 1 depends on USBIP_VHCI_HCD ---help---
@@ -46,6 +46,9 @@ config USBIP_VHCI_NR_HCS virtual host controllers as if adding physical host controllers. + Note that the number of host controllers is also limited by + USB_MAXBUS. + config USBIP_HOST tristate "Host driver" depends on USBIP_CORE && USB
Shuah Khan
maintainerQuoted 60 lines · Salvador Fandino
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
So, we got the idea that in order to enforce that remote usbip devices are only visible inside the container where they were imported, we could conveniently mount-bind inside every container just one of the
vhci_hcddirectories below/sys/devices/platform. So that it is as if every container had avhci_hcdjust for itself (and also, we restrict access to the matching USB ports in cgroups).Unfortunately, all the vhci_hcd.* devices are controlled through attributes in vhci_hcd.0 making our approach fail and so... well, that is what this patch series changes. It makes every
vhci_hcddevice controllable through attributes inside its own sysfs directory.The first patch, does that in the kernel, and the second and third patches change user space, adapting the libusbip and the usbip tools code respectively.
Then there is a fourth patch, that allows to create much more USB hubs per machine. It was limited to 64 and we are running thousands of containers (every one requiring a hub) per host.
These changes are not completely backward compatible. In the sysfs side, besides moving around the attribute files, now the port numbers go from 0 to
CONFIG_USBIP_VHCI_HC_PORTS* 2 - 1 and are reused for everyvhci_hcddevice. I could have maintained the absolute numeration but I think reusing the numbers is a better and simpler approach.I have considered that until very recently, support for
vhci_hcddevices above the .0 was broken in the uspip tools (see1ac7c8a78be85f84b019d3d2742d1a9f07255cc5"usbip: fix usbip attach to find a port that matches the requested speed") and that has been the place where I have set the bar for backward compatibility: usage of the tools must remain unchanged for accessing "vhci_hcd.0", but may require changes otherwise. The same is true for the library functions which now provides new functions for selecting the targetvhci_hcddevice. The old functions now always target "vhci_hcd.0".
Quoted 6 lines
So, for instance, now, "usbip port" by default only shows "vhci_hcd.0" ports but "usbip port -a" shows all of them, and, for instance, "usbip port -i 4", shows the ports in "vhci_hcd.4".
Cheers.
I will take a look at these in the next week or so.
thanks,
-- Shuah
Shuah Khan
maintainerHi Salvador,
Quoted 20 lines · Salvador Fandino
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
Quoted 3 lines
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
Did you explore a solution to add a mechanism for access control to usbip?
Quoted 20 lines
So, we got the idea that in order to enforce that remote usbip devices are only visible inside the container where they were imported, we could conveniently mount-bind inside every container just one of the
vhci_hcddirectories below/sys/devices/platform. So that it is as if every container had avhci_hcdjust for itself (and also, we restrict access to the matching USB ports in cgroups).Unfortunately, all the vhci_hcd.* devices are controlled through attributes in vhci_hcd.0 making our approach fail and so... well, that is what this patch series changes. It makes every
vhci_hcddevice controllable through attributes inside its own sysfs directory.> The first patch, does that in the kernel, and the second and third patches change user space, adapting the libusbip and the usbip tools code respectively.Then there is a fourth patch, that allows to create much more USB hubs per machine. It was limited to 64 and we are running thousands of containers (every one requiring a hub) per host.
Quoted 5 lines
These changes are not completely backward compatible. In the sysfs side, besides moving around the attribute files, now the port numbers go from 0 to
CONFIG_USBIP_VHCI_HC_PORTS* 2 - 1 and are reused for everyvhci_hcddevice. I could have maintained the absolute numeration but I think reusing the numbers is a better and simpler approach.
Not being able to maintain backwards compatibility is an issue. This is a considerable change to the user interface.
Quoted 11 lines
I have considered that until very recently, support for
vhci_hcddevices above the .0 was broken in the uspip tools (see1ac7c8a78be85f84b019d3d2742d1a9f07255cc5"usbip: fix usbip attach to find a port that matches the requested speed") and that has been the place where I have set the bar for backward compatibility: usage of the tools must remain unchanged for accessing "vhci_hcd.0", but may require changes otherwise. The same is true for the library functions which now provides new functions for selecting the targetvhci_hcddevice. The old functions now always target "vhci_hcd.0".
Quoted 4 lines
So, for instance, now, "usbip port" by default only shows "vhci_hcd.0" ports but "usbip port -a" shows all of them, and, for instance, "usbip port -i 4", shows the ports in "vhci_hcd.4".
I am going to play with these patches and how extensive the changes are for users. In the meantime, maybe you can explore alternatives that don't break backwards compatibility.
thanks,
-- Shuah
Quoted 23 lines · Shuah Khan
Hi Salvador,
Quoted 20 lines · Salvador Fandino
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
Quoted 6 lines
Quoted 3 lines
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
Did you explore a solution to add a mechanism for access control to usbip?
Could you elaborate on that?
For "usbip", do you mean the user space tools? If that is the case, I don't think it would be enough. My aim is to limit vhci usage from containers and I have no control about what runs inside the containers. So, a mangled usbip tool-set could be used by a malicious user to circumvent any access control set there.
IMO, there is no other choice but to control access to VHCI at the kernel level.
Quoted 21 lines
Quoted 21 lines
So, we got the idea that in order to enforce that remote usbip devices are only visible inside the container where they were imported, we could conveniently mount-bind inside every container just one of the
vhci_hcddirectories below/sys/devices/platform. So that it is as if every container had avhci_hcdjust for itself (and also, we restrict access to the matching USB ports in cgroups).Unfortunately, all the vhci_hcd.* devices are controlled through attributes in vhci_hcd.0 making our approach fail and so... well, that is what this patch series changes. It makes every
vhci_hcddevice controllable through attributes inside its own sysfs directory.> The first patch, does that in the kernel, and the second and third patches change user space, adapting the libusbip and the usbip tools code respectively.Then there is a fourth patch, that allows to create much more USB hubs per machine. It was limited to 64 and we are running thousands of containers (every one requiring a hub) per host.
Quoted 8 lines
Quoted 5 lines
These changes are not completely backward compatible. In the sysfs side, besides moving around the attribute files, now the port numbers go from 0 to
CONFIG_USBIP_VHCI_HC_PORTS* 2 - 1 and are reused for everyvhci_hcddevice. I could have maintained the absolute numeration but I think reusing the numbers is a better and simpler approach.Not being able to maintain backwards compatibility is an issue. This is a considerable change to the user interface.
Well, it is true that it is a considerable change to the user interface breaking backward compatibility, but as I had already stated, that interface was broken until a couple of months ago, when my coworker, Juan Zea, reported it and nobody had noticed it before. So, I don't think we are going to affect too many people.
Note also that the user interface does not change when only vhci_hcd.0 is used.
Regards
Quoted 24 lines · Salvador Fandiño
Quoted 23 lines · Shuah Khan
Hi Salvador,
Quoted 20 lines · Salvador Fandino
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
In this use-case:
- does a container act as usbip client and attach devices from their host?
- do containers attach remote devices from other systems?
Is the core of the problem really that any remote system can import without a provision for being able to restrict export to a set of remote machines? If so, this is a generic problem even without containers and I would like to solve this with a generic solution that works in all cases, not just for containers.
The approach in this patch series appears to solve the problem just for containers.
Quoted 9 lines
Quoted 3 lines
Did you explore a solution to add a mechanism for access control to usbip?
Could you elaborate on that?
For "usbip", do you mean the user space tools?
If that is the case, I don't think it would be enough.
My aim is to limit vhci usage from containers and I have no control about what runs inside the containers. So, a mangled usbip tool-set could > > be used by a malicious user to circumvent any access control set there.>
I mean the driver. There might be changes necessary in the user-space as well depending on how the access controls are implemented. I am not proposing implementing access controls in the user-space.
Quoted 2 lines
IMO, there is no other choice but to control access to VHCI at the kernel level.
Probably. Please give as many details as possible on your environment for me to make a call on if this problem can be solved in a different way.
thanks,
-- Shuah
Quoted 25 lines · Shuah Khan
Quoted 24 lines · Salvador Fandiño
Quoted 23 lines · Shuah Khan
Hi Salvador,
Quoted 20 lines · Salvador Fandino
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
In this use-case:
- does a container act as usbip client and attach devices from their host?
- do containers attach remote devices from other systems?
In my particular case devices are imported from remote machines. But well, the thing is that I don't care where the connections come from, they could even be devices emulated in user space.
Quoted 5 lines
Is the core of the problem really that any remote system can import without a provision for being able to restrict export to a set of remote machines? If so, this is a generic problem even without containers and I would like to solve this with a generic solution that works in all cases, not just for containers.
No, that is a different issue. You are talking about controlling which devices can be connected, from which hosts, etc. That is an interesting problem but not the one I am trying to tackle here.
I don't mind which every user does inside its container as far as it does not interfere which other users. In practice that means:
1- Not being able to attach/detach devices in other containers 2- Not being able to list devices attached in other containers 3- Not being able to access devices attached in other containers.
Point 3 is already enforceable using the devices hierarchy in cgroups. For points 1 and 2, my proposition is making every vhci_hcd device have its own fully independent sysfs directory (instead of all of them going through vhci_hcd.0) so that they can be selectively exposed with rw permissions inside the containers.
Quoted 15 lines
The approach in this patch series appears to solve the problem just for containers.
Quoted 7 lines
Quoted 2 lines
Did you explore a solution to add a mechanism for access control to usbip?
Could you elaborate on that?
For "usbip", do you mean the user space tools?
If that is the case, I don't think it would be enough.
My aim is to limit vhci usage from containers and I have no control about what runs inside the containers. So, a mangled usbip tool-set could > > be used by a malicious user to circumvent any access control set there.>I mean the driver. There might be changes necessary in the user-space as well depending on how the access controls are implemented. I am not proposing implementing access controls in the user-space.
Quoted 5 lines
Quoted 2 lines
IMO, there is no other choice but to control access to VHCI at the kernel level.
Probably. Please give as many details as possible on your environment for me to make a call on if this problem can be solved in a different way.
In our particular real life application, we are targeting the kernel interface directly, we don't use the usbip tools at all. It is that way because we have our own* transport layer, authentication and authorization mechanisms. And once all the handshaking is done we end with a socket we can directly pass to the kernel in order to get it attached to a vhci_hcd port. We don't like having an extra application listening on some TCP port which can be accessed by third parties on the client side either.
The imported USB devices used are mostly devices which do not require kernel modules and that are accessed though libusb by the applications (i.e., id card readers, barcode scanners, signing pads, etc.).
* Just in case you want to know, USBIP data goes through a channel in a nx (https://github.com/ArcticaProject/nx-libs) connection running over a websocket over TLS. Authentication is performed by the broker (a proxy which knows where a user containers are running). Authorization is performed following policies configured by the administrator (currently it is just an all or nothing policy: USPIP is allowed or not) by the control application at container creation time.
Quoted 36 lines · Salvador Fandiño
Quoted 25 lines · Shuah Khan
Quoted 24 lines · Salvador Fandiño
Quoted 23 lines · Shuah Khan
Hi Salvador,
Quoted 20 lines · Salvador Fandino
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
In this use-case:
- does a container act as usbip client and attach devices from their host?
- do containers attach remote devices from other systems?
Quoted 9 lines
In my particular case devices are imported from remote machines. But well, the thing is that I don't care where the connections come from, they could even be devices emulated in user space.
Quoted 5 lines
Is the core of the problem really that any remote system can import without a provision for being able to restrict export to a set of remote machines? If so, this is a generic problem even without containers and I would like to solve this with a generic solution that works in all cases, not just for containers.
No, that is a different issue. You are talking about controlling which devices can be connected, from which hosts, etc. That is an interesting problem but not the one I am trying to tackle here.
Not entirely. These problems are linked if you use usbip driver and usbip tools. USBIp driver is intended to be used in conjunction with the usbip tools.
Quoted 3 lines
I don't mind which every user does inside its container as far as it does not interfere which other users. In practice that means:
1- Not being able to attach/detach devices in other container
How do container attach/detach in other containers in your setup?
Quoted 1 line
2- Not being able to list devices attached in other containers
How do container list devices in other containers in your setup?
Quoted 21 lines
3- Not being able to access devices attached in other containers.
Point 3 is already enforceable using the devices hierarchy in cgroups. For points 1 and 2, my proposition is making every
vhci_hcddevice have its own fully independent sysfs directory (instead of all of them going through vhci_hcd.0) so that they can be selectively exposed with rw permissions inside the containers.Quoted 15 lines
The approach in this patch series appears to solve the problem just for containers.
Quoted 7 lines
Quoted 2 lines
Did you explore a solution to add a mechanism for access control to usbip?
Could you elaborate on that?
For "usbip", do you mean the user space tools?
If that is the case, I don't think it would be enough.
My aim is to limit vhci usage from containers and I have no control about what runs inside the containers. So, a mangled usbip tool-set could > > be used by a malicious user to circumvent any access control set there.>I mean the driver. There might be changes necessary in the user-space as well depending on how the access controls are implemented. I am not proposing implementing access controls in the user-space.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
IMO, there is no other choice but to control access to VHCI at the kernel level.
Probably. Please give as many details as possible on your environment for me to make a call on if this problem can be solved in a different way.
In our particular real life application, we are targeting the kernel interface directly, we don't use the usbip tools at all. It is that way because we have our own* transport layer, authentication and authorization mechanisms. And once all the handshaking is done we end with a socket we can directly pass to the kernel in order to get it attached to a
vhci_hcdport.
How do you do that? Can you elaborate on how do you pass the socket to the USBIP host? The way you are using is unsupported and just not the way it is designed to be used.
We don't like having an extra application listening on some TCP port which can be accessed by third parties on the client side either.
Quoted 1 line
USBIP is a server/client protocol is intended to work that way. You can specify a port to use.
Quoted 1 line
The imported USB devices used are mostly devices which do not require kernel modules and that are accessed though libusb by the applications (i.e., id card readers, barcode scanners, signing pads, etc.).
This is just not they was USBIP driver in the kernel is intended to be used. I am beginning to think that USBIP isn't the right solution for your application. You are talking about not using the protocol the way it is designed and finding custom ways to use it.
I am all for making the USBIP more secure for container environment by adding features to restrict:
- Remote machines that can import (attach) - this can be per device.
- Make sure detach is done only by the remote that imported the device
- Restrict listing of imported devices to the remote that imported them
- Enhance current version match to a stricter version match and add checksum match between kernel and user-space.
Let me know if you would like to explore the above options that are generic as opposed to custom solution based on a setup that doesn't use the USBIP driver the way it is designed to work.
thanks,
-- Shuah
Quoted 43 lines · Shuah Khan
Quoted 42 lines · Salvador Fandiño
Quoted 25 lines · Shuah Khan
Quoted 24 lines · Salvador Fandiño
Quoted 23 lines · Shuah Khan
Hi Salvador,
Quoted 20 lines · Salvador Fandino
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
In this use-case:
- does a container act as usbip client and attach devices from their host?
- do containers attach remote devices from other systems?In my particular case devices are imported from remote machines. But well, the thing is that I don't care where the connections come from, they could even be devices emulated in user space.
Quoted 5 lines
Is the core of the problem really that any remote system can import without a provision for being able to restrict export to a set of remote machines? If so, this is a generic problem even without containers and I would like to solve this with a generic solution that works in all cases, not just for containers.
Quoted 5 lines
Quoted 2 lines
No, that is a different issue. You are talking about controlling which devices can be connected, from which hosts, etc. That is an interesting problem but not the one I am trying to tackle here.
Not entirely. These problems are linked if you use usbip driver and usbip tools. USBIp driver is intended to be used in conjunction with the usbip tools.
I will come back to that later.
Quoted 4 lines
Quoted 3 lines
I don't mind which every user does inside its container as far as it does not interfere which other users. In practice that means:
1- Not being able to attach/detach devices in other container
How do container attach/detach in other containers in your setup?
(let's for a moment forget I am using my own user-space USBIP layer, because it doesn't do anything different than the USBIP tools other than not using TCP sockets as the transport layer)
The sysfs is bind-mounted inside the container. I have also enabled access to USBIP devices through the cgroups device hierarchy.
In that scenario, I can just use the "usbip" tool to attach remote USBIP devices.
Limiting what devices can be attached or detached inside the "usbip" tool is not enough because, in general, the container's root can install any software he likes inside the container including for instance, a mangled "usbip" tool with any don't-touch-others-usbip-devices policy we could implement there disabled.
Quoted 2 lines
Quoted 1 line
2- Not being able to list devices attached in other containers
How do container list devices in other containers in your setup?
Which the current implementation, running "usbip port" inside the container shows all the devices attached from the host or from any container.
The information is available from the "status*" entries in vhci_hcd.0, so, again, even if some see-only-the-usbip-devices-attached-from-this-container policy where enforced by the usbip tool, the container's root would still be able to read the status files directly and see all the devices connected host-wise.
In the end, that means that any enforcement of container-to-container or container-to-host USBIP access restriction must be made at the kernel level and that's just what my proposed patch allows to do: It makes the vhci_hcd devices fully independent. Every one having its own attach, detach and status objects. In that way, every container can be limited to accessing just one of them (well, or some subset) by selectively hidden vhci_hcd.* entries on the container sysfs when it is started. Then, from inside the container it is impossible to attach/detach devices in other containers vhci_hcd or to list them.
Coming back to where you said "Not entirely. These problems are linked if you use usbip driver and usbip tools. USBIp driver is intended to be used in conjunction with the usbip tools."
Well, as I have already explained, the usbip user space tools can not be used to limit access from the container to the host or to other containers because the container's root may mangle it in order to override any protection. So, I don't see how using or not using the usbip has any importance here.
Quoted 22 lines
Quoted 22 lines
3- Not being able to access devices attached in other containers.
Point 3 is already enforceable using the devices hierarchy in cgroups. For points 1 and 2, my proposition is making every
vhci_hcddevice have its own fully independent sysfs directory (instead of all of them going through vhci_hcd.0) so that they can be selectively exposed with rw permissions inside the containers.Quoted 15 lines
The approach in this patch series appears to solve the problem just for containers.
Quoted 7 lines
Quoted 2 lines
Did you explore a solution to add a mechanism for access control to usbip?
Could you elaborate on that?
For "usbip", do you mean the user space tools?
If that is the case, I don't think it would be enough.
My aim is to limit vhci usage from containers and I have no control about what runs inside the containers. So, a mangled usbip tool-set could > > be used by a malicious user to circumvent any access control set there.>I mean the driver. There might be changes necessary in the user-space as well depending on how the access controls are implemented. I am not proposing implementing access controls in the user-space.
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
IMO, there is no other choice but to control access to VHCI at the kernel level.
Probably. Please give as many details as possible on your environment for me to make a call on if this problem can be solved in a different way.
In our particular real life application, we are targeting the kernel interface directly, we don't use the usbip tools at all. It is that way because we have our own* transport layer, authentication and authorization mechanisms. And once all the handshaking is done we end with a socket we can directly pass to the kernel in order to get it attached to a
vhci_hcdport.How do you do that? Can you elaborate on how do you pass the socket to the USBIP host?
My userspace application creates a socketpair and passes one side to the kernel writing to the "attach" file. It does exactly the same the usbip tool does, the only difference is that instead of a TCP socket it uses a UNIX one.
Quoted 2 lines
The way you are using is unsupported and just not the way it is designed
to be used.
Yea, that's the magic of Open Source. You intended your application to be used some way, somebody comes with a slightly different use case and submits a patch so that his use case is also supported...
In any case, the discussion here should not be affected by if I am using the standard user space tools or some custom ones. It is only related to how to effectively allow compartmentalization of the USBIP devices in a secure manner.
Quoted 29 lines
We don't like having an extra application listening on some TCP port which can be accessed by third parties on the client side either. USBIP is a server/client protocol is intended to work that way. You can specify
a port to use.Quoted 1 line
The imported USB devices used are mostly devices which do not require kernel modules and that are accessed though libusb by the applications (i.e., id card readers, barcode scanners, signing pads, etc.).
This is just not they was USBIP driver in the kernel is intended to be used. I am beginning to think that USBIP isn't the right solution for your application. You are talking about not using the protocol the way it is designed and finding custom ways to use it.
I am all for making the USBIP more secure for container environment by adding features to restrict:
- Remote machines that can import (attach) - this can be per device.
- Make sure detach is done only by the remote that imported the device
- Restrict listing of imported devices to the remote that imported them
- Enhance current version match to a stricter version match and add checksum match between kernel and user-space.Let me know if you would like to explore the above options that are generic as opposed to custom solution based on a setup that doesn't use the USBIP driver the way it is designed to work.
thanks,
-- Shuah
Ping!
In March, I submitted a patch-set to get USBIP to play nicely inside containers, but the discussion died after a couple of mails, I got abducted into some unrelated project and mostly forgot about it...
But now, I have some time available to put into this and would like to push it further.
So, can we pick the discussion up again?
Regards
Quoted 157 lines · Salvador Fandiño
Quoted 43 lines · Shuah Khan
Quoted 42 lines · Salvador Fandiño
Quoted 25 lines · Shuah Khan
Quoted 24 lines · Salvador Fandiño
Quoted 23 lines · Shuah Khan
Hi Salvador,
Quoted 20 lines · Salvador Fandino
Let me start by explaining the problem that have motivated me to write this patches:
I work on the QVD, a virtual desktop platform for Linux. This software runs Linux desktops (i.e. XFCE, KDE) and their applications inside LXC containers, and makes then available through the network to remote users.
Supporting USB devices is a common feature customers have been requesting us for a long time (in order to use, for instance, remote signature pads, bar-code scanners, fingerprint readers, etc.). So, we have been working on that feature using the USB/IP layer on the kernel.
Connecting and disconnecting devices and transferring data works seamless for the devices listed above. But we also want to make the usbip operations private to the container where they are run. For instance, it is unacceptable for our product, that one user could list the devices connected by other users or access them.
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
We can control how can access every device using cgroups once those are attached, but the usbip layer is not providing any mechanism for controlling who can attach, detach or list the devices.
In this use-case:
- does a container act as usbip client and attach devices from their host?
- do containers attach remote devices from other systems?In my particular case devices are imported from remote machines. But well, the thing is that I don't care where the connections come from, they could even be devices emulated in user space.
Quoted 5 lines
Is the core of the problem really that any remote system can import without a provision for being able to restrict export to a set of remote machines? If so, this is a generic problem even without containers and I would like to solve this with a generic solution that works in all cases, not just for containers.
Quoted 5 lines
Quoted 2 lines
No, that is a different issue. You are talking about controlling which devices can be connected, from which hosts, etc. That is an interesting problem but not the one I am trying to tackle here.
Not entirely. These problems are linked if you use usbip driver and usbip tools. USBIp driver is intended to be used in conjunction with the usbip tools.
I will come back to that later.
Quoted 4 lines
Quoted 3 lines
I don't mind which every user does inside its container as far as it does not interfere which other users. In practice that means:
1- Not being able to attach/detach devices in other container
How do container attach/detach in other containers in your setup?
(let's for a moment forget I am using my own user-space USBIP layer, because it doesn't do anything different than the USBIP tools other than not using TCP sockets as the transport layer)
The sysfs is bind-mounted inside the container. I have also enabled access to USBIP devices through the cgroups device hierarchy.
In that scenario, I can just use the "usbip" tool to attach remote USBIP devices.
Limiting what devices can be attached or detached inside the "usbip" tool is not enough because, in general, the container's root can install any software he likes inside the container including for instance, a mangled "usbip" tool with any don't-touch-others-usbip-devices policy we could implement there disabled.
Quoted 2 lines
Quoted 1 line
2- Not being able to list devices attached in other containers
How do container list devices in other containers in your setup?
Which the current implementation, running "usbip port" inside the container shows all the devices attached from the host or from any container.
The information is available from the "status*" entries in vhci_hcd.0, so, again, even if some see-only-the-usbip-devices-attached-from-this-container policy where enforced by the usbip tool, the container's root would still be able to read the status files directly and see all the devices connected host-wise.
In the end, that means that any enforcement of container-to-container or container-to-host USBIP access restriction must be made at the kernel level and that's just what my proposed patch allows to do: It makes the
vhci_hcddevices fully independent. Every one having its own attach, detach and status objects. In that way, every container can be limited to accessing just one of them (well, or some subset) by selectively hidden vhci_hcd.* entries on the container sysfs when it is started. Then, from inside the container it is impossible to attach/detach devices in other containersvhci_hcdor to list them.Coming back to where you said "Not entirely. These problems are linked if you use usbip driver and usbip tools. USBIp driver is intended to be used in conjunction with the usbip tools."
Well, as I have already explained, the usbip user space tools can not be used to limit access from the container to the host or to other containers because the container's root may mangle it in order to override any protection. So, I don't see how using or not using the usbip has any importance here.
Quoted 22 lines
Quoted 22 lines
3- Not being able to access devices attached in other containers.
Point 3 is already enforceable using the devices hierarchy in cgroups. For points 1 and 2, my proposition is making every
vhci_hcddevice have its own fully independent sysfs directory (instead of all of them going through vhci_hcd.0) so that they can be selectively exposed with rw permissions inside the containers.Quoted 15 lines
The approach in this patch series appears to solve the problem just for containers.
Quoted 7 lines
Quoted 2 lines
Did you explore a solution to add a mechanism for access control to usbip?
Could you elaborate on that?
For "usbip", do you mean the user space tools?
If that is the case, I don't think it would be enough.
My aim is to limit vhci usage from containers and I have no control about what runs inside the containers. So, a mangled usbip tool-set could > > be used by a malicious user to circumvent any access control set there.>I mean the driver. There might be changes necessary in the user-space as well depending on how the access controls are implemented. I am not proposing implementing access controls in the user-space.
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
IMO, there is no other choice but to control access to VHCI at the kernel level.
Probably. Please give as many details as possible on your environment for me to make a call on if this problem can be solved in a different way.
In our particular real life application, we are targeting the kernel interface directly, we don't use the usbip tools at all. It is that way because we have our own* transport layer, authentication and authorization mechanisms. And once all the handshaking is done we end with a socket we can directly pass to the kernel in order to get it attached to a
vhci_hcdport.How do you do that? Can you elaborate on how do you pass the socket to the USBIP host?
My userspace application creates a socketpair and passes one side to the kernel writing to the "attach" file. It does exactly the same the usbip tool does, the only difference is that instead of a TCP socket it uses a UNIX one.
Quoted 2 lines
The way you are using is unsupported and just not the way it is designed to be used.
Yea, that's the magic of Open Source. You intended your application to be used some way, somebody comes with a slightly different use case and submits a patch so that his use case is also supported...
In any case, the discussion here should not be affected by if I am using the standard user space tools or some custom ones. It is only related to how to effectively allow compartmentalization of the USBIP devices in a secure manner.
Quoted 30 lines
We don't like having an extra application listening on some TCP port which can be accessed by third parties on the client side either. USBIP is a server/client protocol is intended to work that way. You can specify
a port to use.Quoted 1 line
The imported USB devices used are mostly devices which do not require kernel modules and that are accessed though libusb by the applications (i.e., id card readers, barcode scanners, signing pads, etc.).
This is just not they was USBIP driver in the kernel is intended to be used. I am beginning to think that USBIP isn't the right solution for your application. You are talking about not using the protocol the way it is designed and finding custom ways to use it.
I am all for making the USBIP more secure for container environment by adding features to restrict:
- Remote machines that can import (attach) - this can be per device.
- Make sure detach is done only by the remote that imported the device
- Restrict listing of imported devices to the remote that imported them
- Enhance current version match to a stricter version match and add checksum
match between kernel and user-space.Let me know if you would like to explore the above options that are generic as opposed to custom solution based on a setup that doesn't use the USBIP driver the way it is designed to work.
thanks,
-- Shuah