[PATCH v2 5/5] usb: Add usb interface authorization: SysFS part of usb interface authorization.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To allow (1) or deny (0) interfaces it is needed to write to the mask.
As default each bit has the initial value of the default authorization bit.
The value would showed or have to be written in hexadecimal format.
Entry: /sys/bus/usb/devices/*-*/interface_authorization_mask

Example: Only the interfaces 0 and 2 from device at 3-2 should allowed, the others should be denied.
echo 5 > /sys/bus/usb/devices/3-2/interface_authorization_mask

Signed-off-by: Stefan Koch <skoch@xxxxxxx>
---
 drivers/usb/core/sysfs.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 70 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index d269738..afa0799e 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -622,7 +622,6 @@ usb_descriptor_attr(bDeviceProtocol, "%02x\n");
 usb_descriptor_attr(bNumConfigurations, "%d\n");
 usb_descriptor_attr(bMaxPacketSize0, "%d\n");
 
-
 /* show if the device is authorized (1) or not (0) */
 static ssize_t authorized_show(struct device *dev,
 			       struct device_attribute *attr, char *buf)
@@ -655,6 +654,74 @@ static ssize_t authorized_store(struct device *dev,
 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
 				  authorized_show, authorized_store);
 
+/*
+ * show authorization status of usb interface as bitmask
+ * 1 is authorized, 0 is not authorized
+ *
+ * example: 0b00000101 interfaces 0 and 2 are authorized
+ *          the others are not authorized
+ */
+static ssize_t interface_authorization_mask_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct usb_device
+	*usb_dev = to_usb_device(dev);
+	__u32 val = 0;
+	unsigned i, n;
+
+	if (!usb_dev->actconfig)
+		return -ENODEV;
+
+	/* number of device's interfaces */
+	n = usb_dev->actconfig->desc.bNumInterfaces;
+	for (i = 0; i < n; i++)
+		val |= usb_dev->actconfig->interface[i]->authorized << i;
+
+	return sprintf(buf, "%02x\n", val);
+}
+
+/*
+ * authorize or deauthorize an usb interface per bitmask
+ * 1 is to authorize, 0 is not to authorize
+ * example: 0b00000101 authorizes interface 0 and 2
+ *          and deauthorizes all other interfaces
+ */
+static ssize_t interface_authorization_mask_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct usb_device
+	*usb_dev = to_usb_device(dev);
+	int rc = 0;
+	u32 val = 0;
+
+	if (!usb_dev->actconfig)
+		return -ENODEV;
+	else if (sscanf(buf, "%x\n", &val) != 1)
+		return -EINVAL;
+
+	rc = usb_device_set_mask(dev, val);
+
+	return rc < 0 ? rc : count;
+}
+static DEVICE_ATTR_RW(interface_authorization_mask);
+
+/*
+ * show authorization change status of usb interfaces bitmask
+ * if 1 the mask has modified if 0 is was not modified
+ *
+ * note: would be set after mask_store was called,
+ *       also if the mask has the same value as the old
+ */
+static ssize_t interface_authorization_mask_changed_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct usb_device
+	*usb_dev = to_usb_device(dev);
+
+	return sprintf(buf, "%u\n", usb_dev->mask_changed);
+}
+static DEVICE_ATTR_RO(interface_authorization_mask_changed);
+
 /* "Safely remove a device" */
 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
 			    const char *buf, size_t count)
@@ -703,6 +770,8 @@ static struct attribute *dev_attrs[] = {
 	&dev_attr_quirks.attr,
 	&dev_attr_avoid_reset_quirk.attr,
 	&dev_attr_authorized.attr,
+	&dev_attr_interface_authorization_mask.attr,
+	&dev_attr_interface_authorization_mask_changed.attr,
 	&dev_attr_remove.attr,
 	&dev_attr_removable.attr,
 	&dev_attr_ltm_capable.attr,
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux