Hi everyone,
This patch introduces a new ioctl, USBDEVFS_DROP_PRIVILEGES,
to voluntarily forgo the ability to issue ioctls which may
interfere with other users of the USB device.
This feature allows a privileged process (in the case of Chrome OS,
permission_broker) to open a USB device node and then drop a number
of capabilities that are considered "privileged". These privileges
include the ability to reset the device if there are other users
(most notably a kernel driver) or to disconnect a kernel driver
from the device. The file descriptor can then be passed to an
unprivileged process.
This is useful for granting a process access to a device with
multiple functions. It won't be able to use its access to one
function to disrupt or take over control of another function.
This patch is currently being used in Chrome OS; I have updated it
to be in line with changes in v4.4-rc.
Cheers!
Emilio
Reilly Grant (1):
usb: devio: Add ioctl to disallow detaching kernel USB drivers.
drivers/usb/core/devio.c | 50 +++++++++++++++++++++++++++++++++++----
include/uapi/linux/usbdevice_fs.h | 1 +
2 files changed, 47 insertions(+), 4 deletions(-)
--
2.5.0
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Reviewed-by: Jorge Lucangeli Obes <redacted>
Reviewed-by: Kees Cook <redacted>
[emilio.lopez: fix build for v4.4-rc2 and adjust patch title prefix]
Signed-off-by: Emilio López <redacted>
---
drivers/usb/core/devio.c | 50 +++++++++++++++++++++++++++++++++++----
include/uapi/linux/usbdevice_fs.h | 1 +
2 files changed, 47 insertions(+), 4 deletions(-)
From: Peter Chen <hidden> Date: 2015-11-26 09:02:44
On Wed, Nov 25, 2015 at 12:45:34PM -0300, Emilio López wrote:
quoted hunk
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Reviewed-by: Jorge Lucangeli Obes <redacted>
Reviewed-by: Kees Cook <redacted>
[emilio.lopez: fix build for v4.4-rc2 and adjust patch title prefix]
Signed-off-by: Emilio López <redacted>
---
drivers/usb/core/devio.c | 50 +++++++++++++++++++++++++++++++++++----
include/uapi/linux/usbdevice_fs.h | 1 +
2 files changed, 47 insertions(+), 4 deletions(-)
@@ -1215,6 +1213,35 @@ static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg) static int proc_resetdevice(struct usb_dev_state *ps) {+ if (ps->privileges_dropped) {+ struct usb_host_config *actconfig = ps->dev->actconfig;++ /* Don't touch the device if any interfaces are claimed. It+ * could interfere with other drivers' operations and this+ * process has dropped its privileges to do such things.+ */+ if (actconfig) {+ int i;++ for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {+ if (usb_interface_claimed(+ actconfig->interface[i])) {+ dev_warn(&ps->dev->dev,+ "usbfs: interface %d claimed by"+ " %s while '%s'"+ " resets device\n",+ actconfig->interface[i]+ ->cur_altsetting+ ->desc.bInterfaceNumber,
@@ -2084,6 +2114,9 @@ static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg) sizeof(dc.driver)) == 0) return -EBUSY;+ if (ps->privileges_dropped)+ return -EACCES;+ dev_dbg(&intf->dev, "disconnect by usbfs\n"); usb_driver_release_interface(driver, intf); }
@@ -2130,6 +2163,12 @@ static int proc_free_streams(struct usb_dev_state *ps, void __user *arg) return r; }+static int proc_drop_privileges(struct usb_dev_state *ps)+{+ ps->privileges_dropped = true;+ return 0;+}+ /* * NOTE: All requests here that have interface numbers as parameters * are assuming that somehow the configuration has been prevented from
@@ -2318,6 +2357,9 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd, case USBDEVFS_FREE_STREAMS: ret = proc_free_streams(ps, p); break;+ case USBDEVFS_DROP_PRIVILEGES:+ ret = proc_drop_privileges(ps);+ break; } done:
--
2.5.0
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
From: Krzysztof Opasiak <hidden> Date: 2015-11-26 09:20:14
On 11/25/2015 04:45 PM, Emilio López wrote:
Hi everyone,
This patch introduces a new ioctl, USBDEVFS_DROP_PRIVILEGES,
to voluntarily forgo the ability to issue ioctls which may
interfere with other users of the USB device.
This feature allows a privileged process (in the case of Chrome OS,
permission_broker) to open a USB device node and then drop a number
of capabilities that are considered "privileged".
We had the same idea in Tizen but for now we didn't have time to
implement it.
These privileges
include the ability to reset the device if there are other users
(most notably a kernel driver) or to disconnect a kernel driver
from the device. The file descriptor can then be passed to an
unprivileged process.
And how about switching configuration? This can be also harmful even if
the are no other users for any interface in this configuration.
(Just imagine the situation in which only second config contains an HID
function and when app switch configuration it is activated without user
knowing about this;))
This is useful for granting a process access to a device with
multiple functions. It won't be able to use its access to one
function to disrupt or take over control of another function.
I run through your code and as far as I understand above is not exactly
true. Your patch allows only to prevent userspace from accessing
interfaces which has kernel drivers, there is no way to stop an
application from taking control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a kernel
driver but second and third doesn't. You have 2 apps. One should
communicate using second interface and another one third. But first app
is malicious and it claims all free interfaces of received device (your
patch doesn't prevent this). And when second app starts it is unable to
do anything with the device because all interfaces are taken. How would
you like to handle this?
Moreover I'm not convinced to this patch as it hardcodes the *policy* in
kernel code. Generally our approach (with passing fd from broker to
unprivileged process) was similar but we found out that if we would like
to do this correctly there is much more things to filter than in this
patch. We had two main ideas:
- implement some LSM hooks in ioctls() but this leads to a lot of
additional callbacks in lsm ops struct which even now is very big. But
as a benefit we would get a very flexible policy consistent with other
system policies
- split single usb device node into multiple files which could represent
single endpoins only for io and separate control file for privileged but
it's quite a lot of work and I don't know if any one is going to accept
such a change
Best regards,
--
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
From: Dan Carpenter <hidden> Date: 2015-11-26 09:21:09
On Thu, Nov 26, 2015 at 04:59:25PM +0800, Peter Chen wrote:
On Wed, Nov 25, 2015 at 12:45:34PM -0300, Emilio López wrote:
quoted
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Reviewed-by: Jorge Lucangeli Obes <redacted>
Reviewed-by: Kees Cook <redacted>
[emilio.lopez: fix build for v4.4-rc2 and adjust patch title prefix]
Signed-off-by: Emilio López <redacted>
---
drivers/usb/core/devio.c | 50 +++++++++++++++++++++++++++++++++++----
include/uapi/linux/usbdevice_fs.h | 1 +
2 files changed, 47 insertions(+), 4 deletions(-)
@@ -1215,6 +1213,35 @@ static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg) static int proc_resetdevice(struct usb_dev_state *ps) {+ if (ps->privileges_dropped) {+ struct usb_host_config *actconfig = ps->dev->actconfig;++ /* Don't touch the device if any interfaces are claimed. It+ * could interfere with other drivers' operations and this+ * process has dropped its privileges to do such things.+ */+ if (actconfig) {+ int i;++ for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {+ if (usb_interface_claimed(+ actconfig->interface[i])) {+ dev_warn(&ps->dev->dev,+ "usbfs: interface %d claimed by"+ " %s while '%s'"+ " resets device\n",+ actconfig->interface[i]+ ->cur_altsetting+ ->desc.bInterfaceNumber,
Yeah. Better to flip this around:
static int proc_resetdevice(struct usb_dev_state *ps)
{
struct usb_host_config *actconfig = ps->dev->actconfig;
struct usb_interface *interface;
int i;
if (ps->privileges_dropped && actconfig) {
for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
interface = actconfig->interface[i];
if (usb_interface_claimed(interface)) {
dev_warn(&ps->dev->dev,
"usbfs: interface %d claimed by %s while '%s' resets device\n",
interface->cur_altsetting->desc.bInterfaceNumber,
interface->dev.driver->name,
current->comm);
return -EACCES;
}
}
}
return usb_reset_device(ps->dev);
}
It still goes over the 80 character limit, but that's cleaner than
breaking up the variable.
regards,
dan carpenter
On Thu, Nov 26, 2015 at 10:19:29AM +0100, Krzysztof Opasiak wrote:
On 11/25/2015 04:45 PM, Emilio López wrote:
quoted
Hi everyone,
This patch introduces a new ioctl, USBDEVFS_DROP_PRIVILEGES,
to voluntarily forgo the ability to issue ioctls which may
interfere with other users of the USB device.
This feature allows a privileged process (in the case of Chrome OS,
permission_broker) to open a USB device node and then drop a number
of capabilities that are considered "privileged".
We had the same idea in Tizen but for now we didn't have time to implement
it.
These privileges
quoted
include the ability to reset the device if there are other users
(most notably a kernel driver) or to disconnect a kernel driver
from the device. The file descriptor can then be passed to an
unprivileged process.
And how about switching configuration? This can be also harmful even if the
are no other users for any interface in this configuration.
(Just imagine the situation in which only second config contains an HID
function and when app switch configuration it is activated without user
knowing about this;))
Adding this option might be nice.
quoted
This is useful for granting a process access to a device with
multiple functions. It won't be able to use its access to one
function to disrupt or take over control of another function.
I run through your code and as far as I understand above is not exactly
true. Your patch allows only to prevent userspace from accessing interfaces
which has kernel drivers, there is no way to stop an application from taking
control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a kernel
driver but second and third doesn't. You have 2 apps. One should communicate
using second interface and another one third. But first app is malicious and
it claims all free interfaces of received device (your patch doesn't prevent
this). And when second app starts it is unable to do anything with the
device because all interfaces are taken. How would you like to handle this?
You can't, and why would you ever want to, as you can't tell what an app
"should" or "should not" do. If you really care about this, then use a
LSM policy to prevent this.
Moreover I'm not convinced to this patch as it hardcodes the *policy* in
kernel code.
What policy is that?
Generally our approach (with passing fd from broker to
unprivileged process) was similar but we found out that if we would like to
do this correctly there is much more things to filter than in this patch. We
had two main ideas:
- implement some LSM hooks in ioctls() but this leads to a lot of additional
callbacks in lsm ops struct which even now is very big. But as a benefit we
would get a very flexible policy consistent with other system policies
- split single usb device node into multiple files which could represent
single endpoins only for io and separate control file for privileged but
it's quite a lot of work and I don't know if any one is going to accept such
a change
I've been asking for that for well over a decade, but no one ever did
the work. I think if you work through the options, it ends up not being
a viable solution...
thanks,
greg k-h
From: Krzysztof Opasiak <hidden> Date: 2015-11-27 08:44:52
On 11/26/2015 06:29 PM, Greg KH wrote:
On Thu, Nov 26, 2015 at 10:19:29AM +0100, Krzysztof Opasiak wrote:
quoted
On 11/25/2015 04:45 PM, Emilio López wrote:
quoted
Hi everyone,
This patch introduces a new ioctl, USBDEVFS_DROP_PRIVILEGES,
to voluntarily forgo the ability to issue ioctls which may
interfere with other users of the USB device.
This feature allows a privileged process (in the case of Chrome OS,
permission_broker) to open a USB device node and then drop a number
of capabilities that are considered "privileged".
We had the same idea in Tizen but for now we didn't have time to implement
it.
These privileges
quoted
include the ability to reset the device if there are other users
(most notably a kernel driver) or to disconnect a kernel driver
from the device. The file descriptor can then be passed to an
unprivileged process.
And how about switching configuration? This can be also harmful even if the
are no other users for any interface in this configuration.
(Just imagine the situation in which only second config contains an HID
function and when app switch configuration it is activated without user
knowing about this;))
Adding this option might be nice.
quoted
quoted
This is useful for granting a process access to a device with
multiple functions. It won't be able to use its access to one
function to disrupt or take over control of another function.
I run through your code and as far as I understand above is not exactly
true. Your patch allows only to prevent userspace from accessing interfaces
which has kernel drivers, there is no way to stop an application from taking
control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a kernel
driver but second and third doesn't. You have 2 apps. One should communicate
using second interface and another one third. But first app is malicious and
it claims all free interfaces of received device (your patch doesn't prevent
this). And when second app starts it is unable to do anything with the
device because all interfaces are taken. How would you like to handle this?
You can't, and why would you ever want to, as you can't tell what an app
"should" or "should not" do. If you really care about this, then use a
LSM policy to prevent this.
Well, an app can declare what it does and what it needs in it's manifest
file (or some equivalent of this) and the platform should ensure that
app can do only what it has declared.
I would really like to use LSM policy in here but currently it is
impossible as one device node represents whole device. Permissions (even
those from LSM) are being checked only on open() not on each ioctl() so
as far as I know there is nothing which prevents any owner of opened fd
to claim all available (not taken by someone else) interfaces and LSM
policy is unable to filter those calls (unless we add some LSM hooks
over there).
quoted
Moreover I'm not convinced to this patch as it hardcodes the *policy* in
kernel code.
What policy is that?
It's a policy which defines set of ioctls which cannot be issued in
"restricted mode".
quoted
Generally our approach (with passing fd from broker to
unprivileged process) was similar but we found out that if we would like to
do this correctly there is much more things to filter than in this patch. We
had two main ideas:
- implement some LSM hooks in ioctls() but this leads to a lot of additional
callbacks in lsm ops struct which even now is very big. But as a benefit we
would get a very flexible policy consistent with other system policies
- split single usb device node into multiple files which could represent
single endpoins only for io and separate control file for privileged but
it's quite a lot of work and I don't know if any one is going to accept such
a change
I've been asking for that for well over a decade, but no one ever did
the work. I think if you work through the options, it ends up not being
a viable solution...
I'm not surprised that no one ever did this as it looks like quite a lot
of work and current interface is still working;) Do you have some link
to a discussion or sth which shows why it's not a good solution?
--
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
On Fri, Nov 27, 2015 at 09:44:45AM +0100, Krzysztof Opasiak wrote:
On 11/26/2015 06:29 PM, Greg KH wrote:
quoted
On Thu, Nov 26, 2015 at 10:19:29AM +0100, Krzysztof Opasiak wrote:
quoted
On 11/25/2015 04:45 PM, Emilio López wrote:
quoted
Hi everyone,
This patch introduces a new ioctl, USBDEVFS_DROP_PRIVILEGES,
to voluntarily forgo the ability to issue ioctls which may
interfere with other users of the USB device.
This feature allows a privileged process (in the case of Chrome OS,
permission_broker) to open a USB device node and then drop a number
of capabilities that are considered "privileged".
We had the same idea in Tizen but for now we didn't have time to implement
it.
These privileges
quoted
include the ability to reset the device if there are other users
(most notably a kernel driver) or to disconnect a kernel driver
from the device. The file descriptor can then be passed to an
unprivileged process.
And how about switching configuration? This can be also harmful even if the
are no other users for any interface in this configuration.
(Just imagine the situation in which only second config contains an HID
function and when app switch configuration it is activated without user
knowing about this;))
Adding this option might be nice.
quoted
quoted
This is useful for granting a process access to a device with
multiple functions. It won't be able to use its access to one
function to disrupt or take over control of another function.
I run through your code and as far as I understand above is not exactly
true. Your patch allows only to prevent userspace from accessing interfaces
which has kernel drivers, there is no way to stop an application from taking
control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a kernel
driver but second and third doesn't. You have 2 apps. One should communicate
using second interface and another one third. But first app is malicious and
it claims all free interfaces of received device (your patch doesn't prevent
this). And when second app starts it is unable to do anything with the
device because all interfaces are taken. How would you like to handle this?
You can't, and why would you ever want to, as you can't tell what an app
"should" or "should not" do. If you really care about this, then use a
LSM policy to prevent this.
Well, an app can declare what it does and what it needs in it's manifest
file (or some equivalent of this) and the platform should ensure that app
can do only what it has declared.
"should"? Depending on what? :)
I would really like to use LSM policy in here but currently it is impossible
as one device node represents whole device. Permissions (even those from
LSM) are being checked only on open() not on each ioctl() so as far as I
know there is nothing which prevents any owner of opened fd to claim all
available (not taken by someone else) interfaces and LSM policy is unable to
filter those calls (unless we add some LSM hooks over there).
Yes, it's tough, I know, good luck.
Also deal with multiple devices, busses that are ordered differently
depending on the phase of the moon, and other fun things with dynamic
devices and ioctls. It's a loosing battle :)
quoted
quoted
Moreover I'm not convinced to this patch as it hardcodes the *policy* in
kernel code.
What policy is that?
It's a policy which defines set of ioctls which cannot be issued in
"restricted mode".
quoted
quoted
Generally our approach (with passing fd from broker to
unprivileged process) was similar but we found out that if we would like to
do this correctly there is much more things to filter than in this patch. We
had two main ideas:
- implement some LSM hooks in ioctls() but this leads to a lot of additional
callbacks in lsm ops struct which even now is very big. But as a benefit we
would get a very flexible policy consistent with other system policies
- split single usb device node into multiple files which could represent
single endpoins only for io and separate control file for privileged but
it's quite a lot of work and I don't know if any one is going to accept such
a change
I've been asking for that for well over a decade, but no one ever did
the work. I think if you work through the options, it ends up not being
a viable solution...
I'm not surprised that no one ever did this as it looks like quite a lot of
work and current interface is still working;) Do you have some link to a
discussion or sth which shows why it's not a good solution?
Dig through the archives, I think the last time this was brought up was
way before USB 3 came out. As for "not a good solution", you have to
map endpoints together somehow in some way in userspace, which gets
messy fast, and then you would have to somehow modify all userspace
programs to use the new model.
Good luck!
greg k-h
From: Oliver Neukum <oneukum@suse.com> Date: 2015-11-30 09:10:46
On Fri, 2015-11-27 at 18:39 -0800, Greg KH wrote:
Yes, it's tough, I know, good luck.
Also deal with multiple devices, busses that are ordered differently
depending on the phase of the moon, and other fun things with dynamic
devices and ioctls. It's a loosing battle :)
IMHO the fundamental problem is using one device node per device.
And I think it should be tackled. Yet I have no idea how to do
this in a compatible manner.
Regards
Oliver
From: Alan Stern <stern@rowland.harvard.edu> Date: 2015-11-30 16:16:10
On Fri, 27 Nov 2015, Krzysztof Opasiak wrote:
quoted
quoted
I run through your code and as far as I understand above is not exactly
true. Your patch allows only to prevent userspace from accessing interfaces
which has kernel drivers, there is no way to stop an application from taking
control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a kernel
driver but second and third doesn't. You have 2 apps. One should communicate
using second interface and another one third. But first app is malicious and
it claims all free interfaces of received device (your patch doesn't prevent
this). And when second app starts it is unable to do anything with the
device because all interfaces are taken. How would you like to handle this?
You can't, and why would you ever want to, as you can't tell what an app
"should" or "should not" do. If you really care about this, then use a
LSM policy to prevent this.
Well, an app can declare what it does and what it needs in it's manifest
file (or some equivalent of this) and the platform should ensure that
app can do only what it has declared.
I would really like to use LSM policy in here but currently it is
impossible as one device node represents whole device. Permissions (even
those from LSM) are being checked only on open() not on each ioctl() so
as far as I know there is nothing which prevents any owner of opened fd
to claim all available (not taken by someone else) interfaces and LSM
policy is unable to filter those calls (unless we add some LSM hooks
over there).
How about this approach? Once a process has dropped its usbfs
privileges, it's not allowed to claim any interfaces (either explicitly
or implicitly). Instead, it or some manager program must claim the
appropriate interfaces before dropping privileges.
Alan Stern
From: Krzysztof Opasiak <hidden> Date: 2015-11-30 17:12:29
On 11/30/2015 05:16 PM, Alan Stern wrote:
On Fri, 27 Nov 2015, Krzysztof Opasiak wrote:
quoted
quoted
quoted
I run through your code and as far as I understand above is not exactly
true. Your patch allows only to prevent userspace from accessing interfaces
which has kernel drivers, there is no way to stop an application from taking
control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a kernel
driver but second and third doesn't. You have 2 apps. One should communicate
using second interface and another one third. But first app is malicious and
it claims all free interfaces of received device (your patch doesn't prevent
this). And when second app starts it is unable to do anything with the
device because all interfaces are taken. How would you like to handle this?
You can't, and why would you ever want to, as you can't tell what an app
"should" or "should not" do. If you really care about this, then use a
LSM policy to prevent this.
Well, an app can declare what it does and what it needs in it's manifest
file (or some equivalent of this) and the platform should ensure that
app can do only what it has declared.
I would really like to use LSM policy in here but currently it is
impossible as one device node represents whole device. Permissions (even
those from LSM) are being checked only on open() not on each ioctl() so
as far as I know there is nothing which prevents any owner of opened fd
to claim all available (not taken by someone else) interfaces and LSM
policy is unable to filter those calls (unless we add some LSM hooks
over there).
How about this approach? Once a process has dropped its usbfs
privileges, it's not allowed to claim any interfaces (either explicitly
or implicitly). Instead, it or some manager program must claim the
appropriate interfaces before dropping privileges.
I agree that restricting interface claiming only to privileged process
is a good idea. Unfortunately this generates a problem when program
needs more than one interface (like in cdc - data + control for
example). We need to declare both of them in first call to "usb-manager"
or reopen the dev node at second call and claim all interfaces claimed
using this fd till now and claim one more and then drop privileges and
send a new fd.
Maybe better option would be to add optional argument to claim interface
ioctl() and allow to claim interface for other fd than the current one?
So "usb-manager" could have fd with full control and claim interfaces
for apps which have fds with restricted privileges.
Best regards,
--
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
On Mon, Nov 30, 2015 at 06:12:22PM +0100, Krzysztof Opasiak wrote:
On 11/30/2015 05:16 PM, Alan Stern wrote:
quoted
On Fri, 27 Nov 2015, Krzysztof Opasiak wrote:
quoted
quoted
quoted
I run through your code and as far as I understand above is not exactly
true. Your patch allows only to prevent userspace from accessing interfaces
which has kernel drivers, there is no way to stop an application from taking
control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a kernel
driver but second and third doesn't. You have 2 apps. One should communicate
using second interface and another one third. But first app is malicious and
it claims all free interfaces of received device (your patch doesn't prevent
this). And when second app starts it is unable to do anything with the
device because all interfaces are taken. How would you like to handle this?
You can't, and why would you ever want to, as you can't tell what an app
"should" or "should not" do. If you really care about this, then use a
LSM policy to prevent this.
Well, an app can declare what it does and what it needs in it's manifest
file (or some equivalent of this) and the platform should ensure that
app can do only what it has declared.
I would really like to use LSM policy in here but currently it is
impossible as one device node represents whole device. Permissions (even
those from LSM) are being checked only on open() not on each ioctl() so
as far as I know there is nothing which prevents any owner of opened fd
to claim all available (not taken by someone else) interfaces and LSM
policy is unable to filter those calls (unless we add some LSM hooks
over there).
How about this approach? Once a process has dropped its usbfs
privileges, it's not allowed to claim any interfaces (either explicitly
or implicitly). Instead, it or some manager program must claim the
appropriate interfaces before dropping privileges.
I agree that restricting interface claiming only to privileged process is a
good idea. Unfortunately this generates a problem when program needs more
than one interface (like in cdc - data + control for example). We need to
declare both of them in first call to "usb-manager" or reopen the dev node
at second call and claim all interfaces claimed using this fd till now and
claim one more and then drop privileges and send a new fd.
Have you seen such a device that is controlled this way in userspace?
Don't over-engineer something that is probably pretty rare...
thanks,
greg k-h
From: Krzysztof Opasiak <hidden> Date: 2015-11-30 18:49:05
On 11/30/2015 06:20 PM, Greg KH wrote:
On Mon, Nov 30, 2015 at 06:12:22PM +0100, Krzysztof Opasiak wrote:
quoted
On 11/30/2015 05:16 PM, Alan Stern wrote:
quoted
On Fri, 27 Nov 2015, Krzysztof Opasiak wrote:
quoted
quoted
quoted
I run through your code and as far as I understand above is not exactly
true. Your patch allows only to prevent userspace from accessing interfaces
which has kernel drivers, there is no way to stop an application from taking
control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a kernel
driver but second and third doesn't. You have 2 apps. One should communicate
using second interface and another one third. But first app is malicious and
it claims all free interfaces of received device (your patch doesn't prevent
this). And when second app starts it is unable to do anything with the
device because all interfaces are taken. How would you like to handle this?
You can't, and why would you ever want to, as you can't tell what an app
"should" or "should not" do. If you really care about this, then use a
LSM policy to prevent this.
Well, an app can declare what it does and what it needs in it's manifest
file (or some equivalent of this) and the platform should ensure that
app can do only what it has declared.
I would really like to use LSM policy in here but currently it is
impossible as one device node represents whole device. Permissions (even
those from LSM) are being checked only on open() not on each ioctl() so
as far as I know there is nothing which prevents any owner of opened fd
to claim all available (not taken by someone else) interfaces and LSM
policy is unable to filter those calls (unless we add some LSM hooks
over there).
How about this approach? Once a process has dropped its usbfs
privileges, it's not allowed to claim any interfaces (either explicitly
or implicitly). Instead, it or some manager program must claim the
appropriate interfaces before dropping privileges.
I agree that restricting interface claiming only to privileged process is a
good idea. Unfortunately this generates a problem when program needs more
than one interface (like in cdc - data + control for example). We need to
declare both of them in first call to "usb-manager" or reopen the dev node
at second call and claim all interfaces claimed using this fd till now and
claim one more and then drop privileges and send a new fd.
Have you seen such a device that is controlled this way in userspace?
Don't over-engineer something that is probably pretty rare...
Yes I have seen such devices (not cdc of course) and they were driven
using libusb (vendor specific service + "driver" to bypass publishing
protocol code due to kernel's GPL). I have even seen an android app
written in java which claims and uses multiple interfaces using
android's USB API, so it's real;)
--
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
Hi,
I'm reviving this thread as there's no consensus so far, and I'd like to
see this supported in some way in mainline.
El 30/11/15 a las 15:48, Krzysztof Opasiak escribió:
On 11/30/2015 06:20 PM, Greg KH wrote:
quoted
On Mon, Nov 30, 2015 at 06:12:22PM +0100, Krzysztof Opasiak wrote:
quoted
On 11/30/2015 05:16 PM, Alan Stern wrote:
quoted
On Fri, 27 Nov 2015, Krzysztof Opasiak wrote:
quoted
quoted
quoted
I run through your code and as far as I understand above is not
exactly
true. Your patch allows only to prevent userspace from accessing
interfaces
which has kernel drivers, there is no way to stop an application
from taking
control over all free interfaces.
Let's say that your device has 3 interfaces. First of them has a
kernel
driver but second and third doesn't. You have 2 apps. One should
communicate
using second interface and another one third. But first app is
malicious and
it claims all free interfaces of received device (your patch
doesn't prevent
this). And when second app starts it is unable to do anything
with the
device because all interfaces are taken. How would you like to
handle this?
You can't, and why would you ever want to, as you can't tell what
an app
"should" or "should not" do. If you really care about this, then
use a
LSM policy to prevent this.
Well, an app can declare what it does and what it needs in it's
manifest
file (or some equivalent of this) and the platform should ensure that
app can do only what it has declared.
I would really like to use LSM policy in here but currently it is
impossible as one device node represents whole device. Permissions
(even
those from LSM) are being checked only on open() not on each
ioctl() so
as far as I know there is nothing which prevents any owner of
opened fd
to claim all available (not taken by someone else) interfaces and LSM
policy is unable to filter those calls (unless we add some LSM hooks
over there).
How about this approach? Once a process has dropped its usbfs
privileges, it's not allowed to claim any interfaces (either explicitly
or implicitly). Instead, it or some manager program must claim the
appropriate interfaces before dropping privileges.
I agree that restricting interface claiming only to privileged
process is a
good idea. Unfortunately this generates a problem when program needs
more
than one interface (like in cdc - data + control for example). We
need to
declare both of them in first call to "usb-manager" or reopen the dev
node
at second call and claim all interfaces claimed using this fd till
now and
claim one more and then drop privileges and send a new fd.
I talked with Reilly some time back and they proposed using an extra
parameter in the ioctl. This parameter would be a mask that specifies
which interfaces the unprivileged process is allowed to claim (but
doesn't necessarily have to do so). Providing a mask of ~0 would retain
the current patch behaviour, and allow existing programs using
out-of-tree implementations to continue working with little changes.
Now, if this were to be used on a system that knew better, via an app
manifest or similar, the manager process would generate a suitable mask
based on the interfaces required by the unprivileged process and use it
as a parameter when dropping privileges. This way, the unprivileged
process would be unable to claim interfaces it is not supposed to claim,
while allowing it the liberty to do as it wishes with the interfaces it
is allowed to use.
Krzysztof et al, would you find such an interface suitable? If you think
it could be a way forward I'll gladly write the code and send a new patch.
Cheers,
Emilio
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Signed-off-by: Emilio López <emilio.lopez-ZGY8ohtN/8pPYcu2f3hruQ@public.gmane.org>
---
I guess code talks more than words :) This patch is only build-tested,
and is meant to showcase the approach. The basic idea is to allow
limiting the interface claims via an extra parameter to resolve
Krzysztof's worries.
A longer paragraph explaining the idea can be seen at
http://www.spinics.net/lists/linux-usb/msg135271.html
Cheers!
Emilio
Changes in v2:
- Added a parameter to the ioctl, a mask of interfaces an unprivileged
process is allowed to claim.
- Drop Tested-by's
drivers/usb/core/devio.c | 65 ++++++++++++++++++++++++++++++++++++---
include/uapi/linux/usbdevice_fs.h | 5 +++
2 files changed, 66 insertions(+), 4 deletions(-)
@@ -1215,6 +1222,27 @@ static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)staticintproc_resetdevice(structusb_dev_state*ps){+structusb_host_config*actconfig=ps->dev->actconfig;+structusb_interface*interface;+inti,number;++/* Don't touch the device if any interfaces are claimed. It+*couldinterferewithotherdrivers'operationsandthis+*processhasdroppeditsprivilegestodosuchthings.+*/+if(ps->privileges_dropped&&actconfig){+for(i=0;i<actconfig->desc.bNumInterfaces;++i){+interface=actconfig->interface[i];+number=interface->cur_altsetting->desc.bInterfaceNumber;+if(usb_interface_claimed(interface)){+dev_warn(&ps->dev->dev,+"usbfs: interface %d claimed by %s while '%s' resets device\n",+number,interface->dev.driver->name,current->comm);+return-EACCES;+}+}+}+returnusb_reset_device(ps->dev);}
@@ -2130,6 +2164,26 @@ static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)returnr;}+staticintproc_drop_privileges(structusb_dev_state*ps,void__user*arg)+{+structusbdevfs_drop_privsdata;++if(copy_from_user(&data,arg,sizeof(data)))+return-EFAULT;++/* This is a one way operation. Once privileges were dropped,+*youcannotdoitagain(Otherwiseunprivilegedprocesses+*wouldbeabletochangetheirallowedinterfacesmask)+*/+if(ps->privileges_dropped)+return-EACCES;++ps->interface_allowed_mask=data.interface_allowed_mask;+ps->privileges_dropped=true;++return0;+}+/**NOTE:Allrequestsherethathaveinterfacenumbersasparameters*areassumingthatsomehowtheconfigurationhasbeenpreventedfrom
@@ -2318,6 +2372,9 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,caseUSBDEVFS_FREE_STREAMS:ret=proc_free_streams(ps,p);break;+caseUSBDEVFS_DROP_PRIVILEGES:+ret=proc_drop_privileges(ps,p);+break;}done:
From: Alan Stern <stern@rowland.harvard.edu> Date: 2016-01-22 16:10:47
On Thu, 21 Jan 2016, Emilio López wrote:
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Signed-off-by: Emilio López <emilio.lopez-ZGY8ohtN/8pPYcu2f3hruQ@public.gmane.org>
static int proc_resetdevice(struct usb_dev_state *ps)
{
+ struct usb_host_config *actconfig = ps->dev->actconfig;
+ struct usb_interface *interface;
+ int i, number;
+
+ /* Don't touch the device if any interfaces are claimed. It
+ * could interfere with other drivers' operations and this
+ * process has dropped its privileges to do such things.
+ */
This comment should be rephrased. It should say something like:
"Don't allow if the process has dropped its privilege to do such
things and any of the interfaces are claimed."
You also might consider allowing the reset if the interfaces are
claimed only by the current process (or more precisely, by ps).
+static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
+{
+ struct usbdevfs_drop_privs data;
+
+ if (copy_from_user(&data, arg, sizeof(data)))
+ return -EFAULT;
+
+ /* This is a one way operation. Once privileges were dropped,
+ * you cannot do it again (Otherwise unprivileged processes
+ * would be able to change their allowed interfaces mask)
+ */
If you're going to keep a mask of claimable interfaces then there's no
reason this has to be a one-time operation. Processes should always be
allowed to shrink the mask, just not to grow it.
Alan Stern
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Alan,
El 22/01/16 a las 13:10, Alan Stern escribió:
On Thu, 21 Jan 2016, Emilio López wrote:
quoted
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Signed-off-by: Emilio López <redacted>
quoted
static int proc_resetdevice(struct usb_dev_state *ps)
{
+ struct usb_host_config *actconfig = ps->dev->actconfig;
+ struct usb_interface *interface;
+ int i, number;
+
+ /* Don't touch the device if any interfaces are claimed. It
+ * could interfere with other drivers' operations and this
+ * process has dropped its privileges to do such things.
+ */
This comment should be rephrased. It should say something like:
"Don't allow if the process has dropped its privilege to do such
things and any of the interfaces are claimed."
I have replaced it with the following now
/* Don't allow a device reset if the process has dropped the
* privilege to do such things and any of the interfaces are
* currently claimed.
*/
You also might consider allowing the reset if the interfaces are
claimed only by the current process (or more precisely, by ps).
quoted
+static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
+{
+ struct usbdevfs_drop_privs data;
+
+ if (copy_from_user(&data, arg, sizeof(data)))
+ return -EFAULT;
+
+ /* This is a one way operation. Once privileges were dropped,
+ * you cannot do it again (Otherwise unprivileged processes
+ * would be able to change their allowed interfaces mask)
+ */
If you're going to keep a mask of claimable interfaces then there's no
reason this has to be a one-time operation. Processes should always be
allowed to shrink the mask, just not to grow it.
Good point, I've changed this to look like the following
/* This is an one way operation. Once privileges are
* dropped, you cannot regain them. You may however reissue
* this ioctl to shrink the allowed interfaces mask.
*/
if (ps->privileges_dropped)
ps->interface_allowed_mask &= data.interface_allowed_mask;
else
ps->interface_allowed_mask = data.interface_allowed_mask;
ps->privileges_dropped = true;
Or maybe I could change the default mask to ~0 and simplify this a bit, hm.
Thank you for the review!
Emilio
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Signed-off-by: Emilio López <emilio.lopez-ZGY8ohtN/8pPYcu2f3hruQ@public.gmane.org>
---
Changes in v3:
- Switch ioctl to use a __u32 given the iface qty is capped at 32
- Reword comments as requested by Alan
- Allow callers to shrink the allowed interfaces mask
Changes in v2:
- Added a parameter to the ioctl, a mask of interfaces an unprivileged
process is allowed to claim.
- Drop Tested-by's
drivers/usb/core/devio.c | 59 ++++++++++++++++++++++++++++++++++++---
include/uapi/linux/usbdevice_fs.h | 1 +
2 files changed, 56 insertions(+), 4 deletions(-)
@@ -1198,6 +1202,27 @@ static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)staticintproc_resetdevice(structusb_dev_state*ps){+structusb_host_config*actconfig=ps->dev->actconfig;+structusb_interface*interface;+inti,number;++/* Don't allow a device reset if the process has dropped the+*privilegetodosuchthingsandanyoftheinterfacesare+*currentlyclaimed.+*/+if(ps->privileges_dropped&&actconfig){+for(i=0;i<actconfig->desc.bNumInterfaces;++i){+interface=actconfig->interface[i];+number=interface->cur_altsetting->desc.bInterfaceNumber;+if(usb_interface_claimed(interface)){+dev_warn(&ps->dev->dev,+"usbfs: interface %d claimed by %s while '%s' resets device\n",+number,interface->dev.driver->name,current->comm);+return-EACCES;+}+}+}+returnusb_reset_device(ps->dev);}
@@ -2123,6 +2154,23 @@ static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)returnr;}+staticintproc_drop_privileges(structusb_dev_state*ps,void__user*arg)+{+u32data;++if(copy_from_user(&data,arg,sizeof(data)))+return-EFAULT;++/* This is an one way operation. Once privileges are+*dropped,youcannotregainthem.Youmayhoweverreissue+*thisioctltoshrinktheallowedinterfacesmask.+*/+ps->interface_allowed_mask&=data;+ps->privileges_dropped=true;++return0;+}+/**NOTE:Allrequestsherethathaveinterfacenumbersasparameters*areassumingthatsomehowtheconfigurationhasbeenpreventedfrom
@@ -2311,6 +2359,9 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,caseUSBDEVFS_FREE_STREAMS:ret=proc_free_streams(ps,p);break;+caseUSBDEVFS_DROP_PRIVILEGES:+ret=proc_drop_privileges(ps,p);+break;}done:
On Thu, Feb 04, 2016 at 12:20:57AM -0300, Emilio López wrote:
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Signed-off-by: Emilio López <emilio.lopez-ZGY8ohtN/8pPYcu2f3hruQ@public.gmane.org>
---
Changes in v3:
- Switch ioctl to use a __u32 given the iface qty is capped at 32
- Reword comments as requested by Alan
- Allow callers to shrink the allowed interfaces mask
No documentation? Proof it works? Test scripts?
This isn't a trivial feature to add, how do we know it is correct?
thanks,
greg k-h
From: Alan Stern <stern@rowland.harvard.edu> Date: 2016-02-04 16:28:03
On Thu, 4 Feb 2016, Emilio López wrote:
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Signed-off-by: Emilio López <redacted>
---
Changes in v3:
- Switch ioctl to use a __u32 given the iface qty is capped at 32
- Reword comments as requested by Alan
- Allow callers to shrink the allowed interfaces mask
quoted hunk
@@ -624,6 +626,10 @@ static int claimintf(struct usb_dev_state *ps, unsigned int ifnum) if (test_bit(ifnum, &ps->ifclaimed)) return 0;+ if (ps->privileges_dropped+ && !test_bit(ifnum, &ps->interface_allowed_mask))
Continuation lines in this file are indented by 2 tab stops, not 1
space.
quoted hunk
@@ -1198,6 +1202,27 @@ static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg) static int proc_resetdevice(struct usb_dev_state *ps) {+ struct usb_host_config *actconfig = ps->dev->actconfig;+ struct usb_interface *interface;+ int i, number;++ /* Don't allow a device reset if the process has dropped the+ * privilege to do such things and any of the interfaces are+ * currently claimed.+ */+ if (ps->privileges_dropped && actconfig) {+ for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {+ interface = actconfig->interface[i];+ number = interface->cur_altsetting->desc.bInterfaceNumber;+ if (usb_interface_claimed(interface)) {
The test should be:
if (usb_interface_claimed(interface) &&
!test_bit(number, &ps->ifclaimed)) {
We don't want to prevent people from resetting a device merely because
they have claimed an interface. Only if someone else has claimed one.
Hello Alan,
El 04/02/16 a las 13:27, Alan Stern escribió:
On Thu, 4 Feb 2016, Emilio López wrote:
quoted
From: Reilly Grant <redacted>
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.
Signed-off-by: Reilly Grant <redacted>
Signed-off-by: Emilio López <emilio.lopez-ZGY8ohtN/8pPYcu2f3hruQ@public.gmane.org>
---
Changes in v3:
- Switch ioctl to use a __u32 given the iface qty is capped at 32
- Reword comments as requested by Alan
- Allow callers to shrink the allowed interfaces mask
quoted
@@ -624,6 +626,10 @@ static int claimintf(struct usb_dev_state *ps, unsigned int ifnum) if (test_bit(ifnum, &ps->ifclaimed)) return 0;+ if (ps->privileges_dropped+ && !test_bit(ifnum, &ps->interface_allowed_mask))
Continuation lines in this file are indented by 2 tab stops, not 1
space.
Ok, I'll change it.
quoted
@@ -1198,6 +1202,27 @@ static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg) static int proc_resetdevice(struct usb_dev_state *ps) {+ struct usb_host_config *actconfig = ps->dev->actconfig;+ struct usb_interface *interface;+ int i, number;++ /* Don't allow a device reset if the process has dropped the+ * privilege to do such things and any of the interfaces are+ * currently claimed.+ */+ if (ps->privileges_dropped && actconfig) {+ for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {+ interface = actconfig->interface[i];+ number = interface->cur_altsetting->desc.bInterfaceNumber;+ if (usb_interface_claimed(interface)) {
The test should be:
if (usb_interface_claimed(interface) &&
!test_bit(number, &ps->ifclaimed)) {
We don't want to prevent people from resetting a device merely because
they have claimed an interface. Only if someone else has claimed one.
From: Reilly Grant <redacted>
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: Reilly Grant <redacted>
Signed-off-by: Emilio López <emilio.lopez-ZGY8ohtN/8pPYcu2f3hruQ@public.gmane.org>
---
Changes in v4:
- IOR -> IOW
- Explain the new ioctl on usb Docbook
- Small program to be able to test the new functionality
- New capability flag
- Allow people to reset devices if they are the only ones
claiming interfaces, as suggested by Alan
Changes in v3:
- Switch ioctl to use a __u32 given the iface qty is capped at 32
- Reword comments as requested by Alan
- Allow callers to shrink the allowed interfaces mask
Changes in v2:
- Added a parameter to the ioctl, a mask of interfaces an unprivileged
process is allowed to claim.
- Drop Tested-by's
Documentation/DocBook/usb.tmpl | 12 +++
Documentation/usb/usbdevfs-drop-permissions.c | 120 ++++++++++++++++++++++++++
drivers/usb/core/devio.c | 63 ++++++++++++--
include/uapi/linux/usbdevice_fs.h | 2 +
4 files changed, 192 insertions(+), 5 deletions(-)
create mode 100644 Documentation/usb/usbdevfs-drop-permissions.c
@@ -732,6 +732,18 @@ usbdev_ioctl (int fd, int ifno, unsigned request, void *param) or SET_INTERFACE. </para></warning></listitem></varlistentry>+ <varlistentry><term>USBDEVFS_DROP_PRIVILEGES</term>+ <listitem><para>This is used to relinquish the ability+ to do certain operations which are considered to be+ privileged on a usbfs file descriptor.+ This includes claiming arbitrary interfaces, resetting+ a device on which there are currently claimed interfaces+ from other users, and issuing USBDEVFS_IOCTL calls.+ The ioctl parameter is a 32 bit mask of interfaces+ the user is allowed to claim on this file descriptor.+ You may issue this ioctl more than one time to narrow+ said mask.+ </para></listitem></varlistentry> </variablelist> </sect2>
@@ -0,0 +1,120 @@+#include<sys/ioctl.h>+#include<sys/types.h>+#include<sys/stat.h>+#include<fcntl.h>+#include<stdio.h>+#include<errno.h>+#include<string.h>+#include<inttypes.h>+#include<unistd.h>++#include<linux/usbdevice_fs.h>++/* For building without an updated set of headers */+#ifndef USBDEVFS_DROP_PRIVILEGES+#define USBDEVFS_DROP_PRIVILEGES _IOW('U', 30, __u32)+#define USBDEVFS_CAP_DROP_PRIVILEGES 0x20+#endif++voiddrop_privileges(intfd,uint32_tmask)+{+intres;++res=ioctl(fd,USBDEVFS_DROP_PRIVILEGES,&mask);+if(res)+printf("ERROR: USBDEVFS_DROP_PRIVILEGES returned %d\n",res);+else+printf("OK: privileges dropped!\n");+}++voidreset_device(intfd)+{+intres;++res=ioctl(fd,USBDEVFS_RESET);+if(!res)+printf("OK: USBDEVFS_RESET succeeded\n");+else+printf("ERROR: reset failed! (%d - %s)\n",+-res,strerror(-res));+}++voidclaim_some_intf(intfd)+{+inti,res;++for(i=0;i<4;i++){+res=ioctl(fd,USBDEVFS_CLAIMINTERFACE,&i);+if(!res)+printf("OK: claimed if %d\n",i);+else+printf("ERROR claiming if %d (%d - %s)\n",+i,-res,strerror(-res));+}+}++intmain(intargc,char*argv[])+{+uint32_tmask,caps;+intc,fd;++fd=open(argv[1],O_RDWR);+if(fd<0){+printf("Failed to open file\n");+gotoerr_fd;+}++/*+*checkifdroppingprivilegesissupported,+*bailonsystemswherethecapabilityisnotpresent+*/+ioctl(fd,USBDEVFS_GET_CAPABILITIES,&caps);+if(!(caps&USBDEVFS_CAP_DROP_PRIVILEGES)){+printf("DROP_PRIVILEGES not supported\n");+gotoerr;+}++/*+*Dropprivilegesbutkeeptheabilitytoclaimall+*freeinterfaces(i.e.,thosenotusedbykerneldrivers)+*/+drop_privileges(fd,-1U);++printf("Available options:\n"+"[0] Exit now\n"+"[1] Reset device. Should fail if device is in use\n"+"[2] Claim 4 interfaces. Should succeed where not in use\n"+"[3] Narrow interface permission mask\n"+"Which option shall I run?: ");++while(scanf("%d",&c)==1){+switch(c){+case0:+gotoexit;+case1:+reset_device(fd);+break;+case2:+claim_some_intf(fd);+break;+case3:+printf("Insert new mask: ");+scanf("%x",&mask);+drop_privileges(fd,mask);+break;+default:+printf("I don't recognize that\n");+}++printf("Which test shall I run next?: ");+}++exit:+close(fd);+return0;++err:+close(fd);+err_fd:+return1;+}
@@ -1198,6 +1202,28 @@ static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)staticintproc_resetdevice(structusb_dev_state*ps){+structusb_host_config*actconfig=ps->dev->actconfig;+structusb_interface*interface;+inti,number;++/* Don't allow a device reset if the process has dropped the+*privilegetodosuchthingsandanyoftheinterfacesare+*currentlyclaimed.+*/+if(ps->privileges_dropped&&actconfig){+for(i=0;i<actconfig->desc.bNumInterfaces;++i){+interface=actconfig->interface[i];+number=interface->cur_altsetting->desc.bInterfaceNumber;+if(usb_interface_claimed(interface)&&+!test_bit(number,&ps->ifclaimed)){+dev_warn(&ps->dev->dev,+"usbfs: interface %d claimed by %s while '%s' resets device\n",+number,interface->dev.driver->name,current->comm);+return-EACCES;+}+}+}+returnusb_reset_device(ps->dev);}
@@ -2123,6 +2156,23 @@ static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)returnr;}+staticintproc_drop_privileges(structusb_dev_state*ps,void__user*arg)+{+u32data;++if(copy_from_user(&data,arg,sizeof(data)))+return-EFAULT;++/* This is an one way operation. Once privileges are+*dropped,youcannotregainthem.Youmayhoweverreissue+*thisioctltoshrinktheallowedinterfacesmask.+*/+ps->interface_allowed_mask&=data;+ps->privileges_dropped=true;++return0;+}+/**NOTE:Allrequestsherethathaveinterfacenumbersasparameters*areassumingthatsomehowtheconfigurationhasbeenpreventedfrom
@@ -2311,6 +2361,9 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,caseUSBDEVFS_FREE_STREAMS:ret=proc_free_streams(ps,p);break;+caseUSBDEVFS_DROP_PRIVILEGES:+ret=proc_drop_privileges(ps,p);+break;}done:
From: Alan Stern <stern@rowland.harvard.edu> Date: 2016-02-18 18:44:49
On Sun, 14 Feb 2016, Emilio López wrote:
From: Reilly Grant <redacted>
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: Reilly Grant <redacted>
Signed-off-by: Emilio López <emilio.lopez-ZGY8ohtN/8pPYcu2f3hruQ@public.gmane.org>
Acked-by: Alan Stern <stern-nwvwT67g6+6dFdvTe/nMLpVzexx5G7lz@public.gmane.org>