diff mbox series

usb: gadget: f_fs: fix fortify warning

Message ID 20231212112923.61799-1-dmantipov@yandex.ru (mailing list archive)
State Superseded
Headers show
Series usb: gadget: f_fs: fix fortify warning | expand

Commit Message

Dmitry Antipov Dec. 12, 2023, 11:29 a.m. UTC
When compiling with gcc version 14.0.0 20231206 (experimental)
and CONFIG_FORTIFY_SOURCE=y, I've noticed the following warning:

...
In function 'fortify_memcpy_chk',
    inlined from '__ffs_func_bind_do_os_desc' at drivers/usb/gadget/function/f_fs.c:2934:3:
./include/linux/fortify-string.h:588:25: warning: call to '__read_overflow2_field'
declared with attribute warning: detected read beyond size of field (2nd parameter);
maybe use struct_group()? [-Wattribute-warning]
  588 |                         __read_overflow2_field(q_size_field, size);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This call to 'memcpy()' is interpreted as an attempt to copy both
'CompatibleID' and 'SubCompatibleID' of 'struct usb_ext_compat_desc'
from an address of the first one, which causes an overread warning.
Since we actually want to copy both of them at once, use the
convenient 'struct_group()' and 'sizeof_field()' here.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 drivers/usb/gadget/function/f_fs.c  | 5 ++---
 include/uapi/linux/usb/functionfs.h | 6 ++++--
 2 files changed, 6 insertions(+), 5 deletions(-)

Comments

kernel test robot Dec. 13, 2023, 8:09 a.m. UTC | #1
Hi Dmitry,

kernel test robot noticed the following build errors:

[auto build test ERROR on usb/usb-testing]
[also build test ERROR on usb/usb-next usb/usb-linus linus/master v6.7-rc5 next-20231213]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Dmitry-Antipov/usb-gadget-f_fs-fix-fortify-warning/20231212-193946
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20231212112923.61799-1-dmantipov%40yandex.ru
patch subject: [PATCH] usb: gadget: f_fs: fix fortify warning
config: x86_64-randconfig-001-20231213 (https://download.01.org/0day-ci/archive/20231213/202312131514.x4csSoVu-lkp@intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231213/202312131514.x4csSoVu-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312131514.x4csSoVu-lkp@intel.com/

All errors (new ones prefixed by >>):

   In file included from <command-line>:32:
>> ./usr/include/linux/usb/functionfs.h:76:2: error: expected specifier-qualifier-list before 'struct_group'
      76 |  struct_group(IDs,
         |  ^~~~~~~~~~~~
kernel test robot Dec. 13, 2023, 9:03 p.m. UTC | #2
Hi Dmitry,

kernel test robot noticed the following build errors:

[auto build test ERROR on usb/usb-testing]
[also build test ERROR on usb/usb-next usb/usb-linus linus/master v6.7-rc5 next-20231213]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Dmitry-Antipov/usb-gadget-f_fs-fix-fortify-warning/20231212-193946
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20231212112923.61799-1-dmantipov%40yandex.ru
patch subject: [PATCH] usb: gadget: f_fs: fix fortify warning
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20231214/202312140411.KJ3yiKt5-lkp@intel.com/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231214/202312140411.KJ3yiKt5-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312140411.KJ3yiKt5-lkp@intel.com/

All errors (new ones prefixed by >>):

   In file included from <built-in>:1:
>> ./usr/include/linux/usb/functionfs.h:76:2: error: type name requires a specifier or qualifier
           struct_group(IDs,
           ^
>> ./usr/include/linux/usb/functionfs.h:77:3: error: unexpected type name '__u8': expected identifier
                   __u8    CompatibleID[8];
                   ^
>> ./usr/include/linux/usb/functionfs.h:77:8: error: expected ')'
                   __u8    CompatibleID[8];
                           ^
   ./usr/include/linux/usb/functionfs.h:76:14: note: to match this '('
           struct_group(IDs,
                       ^
>> ./usr/include/linux/usb/functionfs.h:76:15: error: a parameter list without types is only allowed in a function definition
           struct_group(IDs,
                        ^
   ./usr/include/linux/usb/functionfs.h:79:2: error: type name requires a specifier or qualifier
           );
           ^
>> ./usr/include/linux/usb/functionfs.h:79:2: error: expected member name or ';' after declaration specifiers
>> ./usr/include/linux/usb/functionfs.h:78:27: error: expected ';' at end of declaration list
                   __u8    SubCompatibleID[8];
                                              ^
                                              ;
   7 errors generated.
diff mbox series

Patch

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index efe3e3b85769..dafedc33928d 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -2931,9 +2931,8 @@  static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
 
 		t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
 		t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
-		memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
-		       ARRAY_SIZE(desc->CompatibleID) +
-		       ARRAY_SIZE(desc->SubCompatibleID));
+		memcpy(t->os_desc->ext_compat_id, &desc->IDs,
+		       sizeof_field(struct usb_ext_compat_desc, IDs));
 		length = sizeof(*desc);
 	}
 		break;
diff --git a/include/uapi/linux/usb/functionfs.h b/include/uapi/linux/usb/functionfs.h
index d77ee6b65328..afb97d6d950e 100644
--- a/include/uapi/linux/usb/functionfs.h
+++ b/include/uapi/linux/usb/functionfs.h
@@ -73,8 +73,10 @@  struct usb_os_desc_header {
 struct usb_ext_compat_desc {
 	__u8	bFirstInterfaceNumber;
 	__u8	Reserved1;
-	__u8	CompatibleID[8];
-	__u8	SubCompatibleID[8];
+	struct_group(IDs,
+		__u8	CompatibleID[8];
+		__u8	SubCompatibleID[8];
+	);
 	__u8	Reserved2[6];
 };