Re: usb: gadget: configfs: OS Extended Compatibility descriptors support

2015-02-13 Thread Andrzej Pietrasiewicz

W dniu 13.02.2015 o 09:06, Dan Carpenter pisze:

Hello Andrzej Pietrasiewicz,



Hello Dan,

Thank you for finding the problem.


The patch da4243145fb1: usb: gadget: configfs: OS Extended
Compatibility descriptors support from May 8, 2014, leads to the
following Smatch warning:

drivers/usb/gadget/configfs.c:1195 interf_grp_sub_compatible_id_store()
error: buffer overflow 'desc-ext_compat_id' 16 = 16



snip



Then we are putting the NULL terminator one space beyond the end of the
array.  -ext_compat_id is set in rndis_alloc_inst().

This is not a false postive, but I'm not positive how we should fix it.



I know how to fix it and will do it soon.

AP

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


re: usb: gadget: configfs: OS Extended Compatibility descriptors support

2015-02-13 Thread Dan Carpenter
Hello Andrzej Pietrasiewicz,

The patch da4243145fb1: usb: gadget: configfs: OS Extended
Compatibility descriptors support from May 8, 2014, leads to the
following Smatch warning:

drivers/usb/gadget/configfs.c:1195 interf_grp_sub_compatible_id_store()
error: buffer overflow 'desc-ext_compat_id' 16 = 16

drivers/usb/gadget/configfs.c
  1184  static ssize_t interf_grp_sub_compatible_id_store(struct usb_os_desc 
*desc,
  1185const char *page, 
size_t len)
  1186  {
  1187  int l;
  1188  
  1189  l = min_t(int, 8, len);

Let's assume l is 8.

  1190  if (page[l - 1] == '\n')
  1191  --l;
  1192  if (desc-opts_mutex)
  1193  mutex_lock(desc-opts_mutex);
  1194  memcpy(desc-ext_compat_id + 8, page, l);
  1195  desc-ext_compat_id[l + 8] = '\0';

Then we are putting the NULL terminator one space beyond the end of the
array.  -ext_compat_id is set in rndis_alloc_inst().

This is not a false postive, but I'm not positive how we should fix it.

  1196  
  1197  if (desc-opts_mutex)
  1198  mutex_unlock(desc-opts_mutex);
  1199  
  1200  return len;
  1201  }

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


[PATCHv2 6/8] usb: gadget: configfs: OS Extended Compatibility descriptors support

2014-05-08 Thread Andrzej Pietrasiewicz
Add handling of OS Extended Compatibility descriptors from configfs interface.
Hosts which expect the OS Descriptors ask only for configurations @ index 0,
but linux-based USB devices can provide more than one configuration.
This patch adds marking one of gadget's configurations the configuration
to be reported at index 0, regardless of the actual sequence of usb_add_config
invocations used for adding the configurations. The configuration is selected
by creating a symbolic link pointing to it from the os_desc directory
located at the top of a gadget's directory hierarchy.

One kind of OS Descriptors are Extended Compatibility Descriptors,
which need to be specified per interface. This patch adds interface.n
directory in function's configfs directory to represent each interface
defined by the function. Each interface's directory contains two attributes:
compatible_id and sub_compatible_id, which represent 8-byte
strings to be reported to the host as the Compatible ID and Sub Compatible
ID.

Signed-off-by: Andrzej Pietrasiewicz andrze...@samsung.com
---
 Documentation/ABI/testing/configfs-usb-gadget |  13 ++
 drivers/usb/gadget/configfs.c | 190 ++
 drivers/usb/gadget/configfs.h |  12 ++
 include/linux/usb/composite.h |   6 +
 4 files changed, 221 insertions(+)

diff --git a/Documentation/ABI/testing/configfs-usb-gadget 
b/Documentation/ABI/testing/configfs-usb-gadget
index 0e7b786..5c0b3e6 100644
--- a/Documentation/ABI/testing/configfs-usb-gadget
+++ b/Documentation/ABI/testing/configfs-usb-gadget
@@ -62,6 +62,19 @@ KernelVersion:   3.11
 Description:
This group contains functions available to this USB gadget.
 
+What:  /config/usb-gadget/gadget/functions/func.inst/interface.n
+Date:  May 2014
+KernelVersion: 3.16
+Description:
+   This group contains Feature Descriptors specific for one
+   gadget's USB interface or one interface group described
+   by an IAD.
+
+   The attributes:
+
+   compatible_id   - 8-byte string for Compatible ID
+   sub_compatible_id   - 8-byte string for Sub Compatible ID
+
 What:  /config/usb-gadget/gadget/strings
 Date:  Jun 2013
 KernelVersion: 3.11
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 4c781fc..1fd1e14 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -6,6 +6,7 @@
 #include linux/usb/composite.h
 #include linux/usb/gadget_configfs.h
 #include configfs.h
+#include u_f.h
 
 int check_user_usb_string(const char *name,
struct usb_gadget_strings *stringtab_dev)
@@ -872,10 +873,63 @@ static void os_desc_attr_release(struct config_item *item)
kfree(os_desc);
 }
 
+static int os_desc_link(struct config_item *os_desc_ci,
+   struct config_item *usb_cfg_ci)
+{
+   struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
+   struct gadget_info, os_desc_group);
+   struct usb_composite_dev *cdev = gi-cdev;
+   struct config_usb_cfg *c_target =
+   container_of(to_config_group(usb_cfg_ci),
+struct config_usb_cfg, group);
+   struct usb_configuration *c;
+   int ret;
+
+   mutex_lock(gi-lock);
+   list_for_each_entry(c, cdev-configs, list) {
+   if (c == c_target-c)
+   break;
+   }
+   if (c != c_target-c) {
+   ret = -EINVAL;
+   goto out;
+   }
+
+   if (cdev-os_desc_config) {
+   ret = -EBUSY;
+   goto out;
+   }
+
+   cdev-os_desc_config = c_target-c;
+   ret = 0;
+
+out:
+   mutex_unlock(gi-lock);
+   return ret;
+}
+
+static int os_desc_unlink(struct config_item *os_desc_ci,
+ struct config_item *usb_cfg_ci)
+{
+   struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
+   struct gadget_info, os_desc_group);
+   struct usb_composite_dev *cdev = gi-cdev;
+
+   mutex_lock(gi-lock);
+   if (gi-udc_name)
+   unregister_gadget(gi);
+   cdev-os_desc_config = NULL;
+   WARN_ON(gi-udc_name);
+   mutex_unlock(gi-lock);
+   return 0;
+}
+
 static struct configfs_item_operations os_desc_ops = {
.release= os_desc_attr_release,
.show_attribute = os_desc_attr_show,
.store_attribute= os_desc_attr_store,
+   .allow_link = os_desc_link,
+   .drop_link  = os_desc_unlink,
 };
 
 static struct config_item_type os_desc_type = {
@@ -884,6 +938,133 @@ static struct config_item_type os_desc_type = {
.ct_owner   = THIS_MODULE,
 };
 
+CONFIGFS_ATTR_STRUCT(usb_os_desc);
+CONFIGFS_ATTR_OPS(usb_os_desc);
+
+static struct configfs_item_operations 

[PATCH 6/8] usb: gadget: configfs: OS Extended Compatibility descriptors support

2014-05-06 Thread Andrzej Pietrasiewicz
Add handling of OS Extended Compatibility descriptors from configfs interface.
Hosts which expect the OS Descriptors ask only for configurations @ index 0,
but linux-based USB devices can provide more than one configuration.
This patch adds marking one of gadget's configurations the configuration
to be reported at index 0, regardless of the actual sequence of usb_add_config
invocations used for adding the configurations. The configuration is selected
by creating a symbolic link pointing to it from the os_desc directory
located at the top of a gadget's directory hierarchy.

One kind of OS Descriptors are Extended Compatibility Descriptors,
which need to be specified per interface. This patch adds interface.n
directory in function's configfs directory to represent each interface
defined by the function. Each interface's directory contains two attributes:
compatible_id and sub_compatible_id, which represent 8-byte
strings to be reported to the host as the Compatible ID and Sub Compatible
ID.

Signed-off-by: Andrzej Pietrasiewicz andrze...@samsung.com
---
 Documentation/ABI/testing/configfs-usb-gadget |  13 ++
 drivers/usb/gadget/configfs.c | 190 ++
 drivers/usb/gadget/configfs.h |  12 ++
 include/linux/usb/composite.h |   6 +
 4 files changed, 221 insertions(+)

diff --git a/Documentation/ABI/testing/configfs-usb-gadget 
b/Documentation/ABI/testing/configfs-usb-gadget
index 0e7b786..5c0b3e6 100644
--- a/Documentation/ABI/testing/configfs-usb-gadget
+++ b/Documentation/ABI/testing/configfs-usb-gadget
@@ -62,6 +62,19 @@ KernelVersion:   3.11
 Description:
This group contains functions available to this USB gadget.
 
+What:  /config/usb-gadget/gadget/functions/func.inst/interface.n
+Date:  May 2014
+KernelVersion: 3.16
+Description:
+   This group contains Feature Descriptors specific for one
+   gadget's USB interface or one interface group described
+   by an IAD.
+
+   The attributes:
+
+   compatible_id   - 8-byte string for Compatible ID
+   sub_compatible_id   - 8-byte string for Sub Compatible ID
+
 What:  /config/usb-gadget/gadget/strings
 Date:  Jun 2013
 KernelVersion: 3.11
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 4c781fc..1fd1e14 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -6,6 +6,7 @@
 #include linux/usb/composite.h
 #include linux/usb/gadget_configfs.h
 #include configfs.h
+#include u_f.h
 
 int check_user_usb_string(const char *name,
struct usb_gadget_strings *stringtab_dev)
@@ -872,10 +873,63 @@ static void os_desc_attr_release(struct config_item *item)
kfree(os_desc);
 }
 
+static int os_desc_link(struct config_item *os_desc_ci,
+   struct config_item *usb_cfg_ci)
+{
+   struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
+   struct gadget_info, os_desc_group);
+   struct usb_composite_dev *cdev = gi-cdev;
+   struct config_usb_cfg *c_target =
+   container_of(to_config_group(usb_cfg_ci),
+struct config_usb_cfg, group);
+   struct usb_configuration *c;
+   int ret;
+
+   mutex_lock(gi-lock);
+   list_for_each_entry(c, cdev-configs, list) {
+   if (c == c_target-c)
+   break;
+   }
+   if (c != c_target-c) {
+   ret = -EINVAL;
+   goto out;
+   }
+
+   if (cdev-os_desc_config) {
+   ret = -EBUSY;
+   goto out;
+   }
+
+   cdev-os_desc_config = c_target-c;
+   ret = 0;
+
+out:
+   mutex_unlock(gi-lock);
+   return ret;
+}
+
+static int os_desc_unlink(struct config_item *os_desc_ci,
+ struct config_item *usb_cfg_ci)
+{
+   struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
+   struct gadget_info, os_desc_group);
+   struct usb_composite_dev *cdev = gi-cdev;
+
+   mutex_lock(gi-lock);
+   if (gi-udc_name)
+   unregister_gadget(gi);
+   cdev-os_desc_config = NULL;
+   WARN_ON(gi-udc_name);
+   mutex_unlock(gi-lock);
+   return 0;
+}
+
 static struct configfs_item_operations os_desc_ops = {
.release= os_desc_attr_release,
.show_attribute = os_desc_attr_show,
.store_attribute= os_desc_attr_store,
+   .allow_link = os_desc_link,
+   .drop_link  = os_desc_unlink,
 };
 
 static struct config_item_type os_desc_type = {
@@ -884,6 +938,133 @@ static struct config_item_type os_desc_type = {
.ct_owner   = THIS_MODULE,
 };
 
+CONFIGFS_ATTR_STRUCT(usb_os_desc);
+CONFIGFS_ATTR_OPS(usb_os_desc);
+
+static struct configfs_item_operations 

[RFC 6/9] usb: gadget: configfs: OS Extended Compatibility descriptors support

2014-04-24 Thread Andrzej Pietrasiewicz
Add handling of OS Extended Compatibility descriptors from configfs interface.
Hosts which expect the OS Descriptors ask only for configurations @ index 0,
but linux-based USB devices can provide more than one configuration.
This patch adds marking one of gadget's configurations the configuration
to be reported at index 0, regardless of the actual sequence of usb_add_config
invocations used for adding the configurations. The configuration is selected
by creating a symbolic link pointing to it from the os_desc directory
located at the top of a gadget's directory hierarchy.

One kind of OS Descriptors are Extended Compatibility Descriptors,
which need to be specified per interface. This patch adds interface.n
directory in function's configfs directory to represent each interface
defined by the function. Each interface's directory contains two attributes:
compatible_id and sub_compatible_id, which represent 8-byte
strings to be reported to the host as the Compatible ID and Sub Compatible
ID.

Signed-off-by: Andrzej Pietrasiewicz andrze...@samsung.com
---
 Documentation/ABI/testing/configfs-usb-gadget |  13 ++
 drivers/usb/gadget/configfs.c | 190 ++
 drivers/usb/gadget/configfs.h |  12 ++
 include/linux/usb/composite.h |   6 +
 4 files changed, 221 insertions(+)

diff --git a/Documentation/ABI/testing/configfs-usb-gadget 
b/Documentation/ABI/testing/configfs-usb-gadget
index 0e7b786..5c0b3e6 100644
--- a/Documentation/ABI/testing/configfs-usb-gadget
+++ b/Documentation/ABI/testing/configfs-usb-gadget
@@ -62,6 +62,19 @@ KernelVersion:   3.11
 Description:
This group contains functions available to this USB gadget.
 
+What:  /config/usb-gadget/gadget/functions/func.inst/interface.n
+Date:  May 2014
+KernelVersion: 3.16
+Description:
+   This group contains Feature Descriptors specific for one
+   gadget's USB interface or one interface group described
+   by an IAD.
+
+   The attributes:
+
+   compatible_id   - 8-byte string for Compatible ID
+   sub_compatible_id   - 8-byte string for Sub Compatible ID
+
 What:  /config/usb-gadget/gadget/strings
 Date:  Jun 2013
 KernelVersion: 3.11
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index d37e60f..26c0870 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -6,6 +6,7 @@
 #include linux/usb/composite.h
 #include linux/usb/gadget_configfs.h
 #include configfs.h
+#include u_f.h
 
 int check_user_usb_string(const char *name,
struct usb_gadget_strings *stringtab_dev)
@@ -872,10 +873,63 @@ static void os_desc_attr_release(struct config_item *item)
kfree(os_desc);
 }
 
+static int os_desc_link(struct config_item *os_desc_ci,
+   struct config_item *usb_cfg_ci)
+{
+   struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
+   struct gadget_info, os_desc_group);
+   struct usb_composite_dev *cdev = gi-cdev;
+   struct config_usb_cfg *c_target =
+   container_of(to_config_group(usb_cfg_ci),
+struct config_usb_cfg, group);
+   struct usb_configuration *c;
+   int ret;
+
+   mutex_lock(gi-lock);
+   list_for_each_entry(c, cdev-configs, list) {
+   if (c == c_target-c)
+   break;
+   }
+   if (c != c_target-c) {
+   ret = -EINVAL;
+   goto out;
+   }
+
+   if (cdev-os_desc_config) {
+   ret = -EBUSY;
+   goto out;
+   }
+
+   cdev-os_desc_config = c_target-c;
+   ret = 0;
+
+out:
+   mutex_unlock(gi-lock);
+   return ret;
+}
+
+static int os_desc_unlink(struct config_item *os_desc_ci,
+ struct config_item *usb_cfg_ci)
+{
+   struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
+   struct gadget_info, os_desc_group);
+   struct usb_composite_dev *cdev = gi-cdev;
+
+   mutex_lock(gi-lock);
+   if (gi-udc_name)
+   unregister_gadget(gi);
+   cdev-os_desc_config = NULL;
+   WARN_ON(gi-udc_name);
+   mutex_unlock(gi-lock);
+   return 0;
+}
+
 static struct configfs_item_operations os_desc_ops = {
.release= os_desc_attr_release,
.show_attribute = os_desc_attr_show,
.store_attribute= os_desc_attr_store,
+   .allow_link = os_desc_link,
+   .drop_link  = os_desc_unlink,
 };
 
 static struct config_item_type os_desc_type = {
@@ -884,6 +938,133 @@ static struct config_item_type os_desc_type = {
.ct_owner   = THIS_MODULE,
 };
 
+CONFIGFS_ATTR_STRUCT(usb_os_desc);
+CONFIGFS_ATTR_OPS(usb_os_desc);
+
+static struct configfs_item_operations