Skip to content

Commit

Permalink
libusb0/1: Simplify range checks in nut API nut_libusb_get_string()
Browse files Browse the repository at this point in the history
The string index is a number between 1 and 255, simply because it is the
lower byte of wValue in the GET_DESCRIPTOR request. String index 0 is
used for retrieving the langid array, so it is not valid for this
function.

There is no reason to impose a limit to the length of the buffer that
the user is lending us, certainly not above the range of the involved
types, so just a minimal sanity check is enough. Since in the minimal
case we would return an empty zero-terminated string, it must at least
have room for this.

Signed-off-by: Tormod Volden <[email protected]>
  • Loading branch information
tormodvolden committed Aug 28, 2024
1 parent f3ebe44 commit 03c1ea7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 36 deletions.
19 changes: 2 additions & 17 deletions drivers/libusb0.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,29 +863,14 @@ static int libusb_get_string(
{
int ret;

#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) )
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-unsigned-zero-compare"
#endif
/*
* usb.h:int nut_usb_get_string(usb_dev_handle *dev, int index,
* usb.h- char *buf, size_t buflen);
*/
if (!udev
|| StringIdx < 0 || (uintmax_t)StringIdx > INT_MAX
|| buflen < 0 || (uintmax_t)buflen > (uintmax_t)SIZE_MAX
|| StringIdx < 1 || StringIdx > 255
|| buflen < 1
) {
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) )
# pragma GCC diagnostic pop
#endif
return -1;
}

Expand Down
20 changes: 1 addition & 19 deletions drivers/libusb1.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,25 +985,7 @@ static int nut_libusb_get_string(
{
int ret;

#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) )
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-unsigned-zero-compare"
#endif
if (!udev
|| StringIdx < 0 || (uintmax_t)StringIdx > UINT8_MAX
|| buflen < 0 || (uintmax_t)buflen > (uintmax_t)INT_MAX
) {
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) )
# pragma GCC diagnostic pop
#endif
if (!udev || StringIdx < 1 || buflen < 1) {
return -1;
}

Expand Down

0 comments on commit 03c1ea7

Please sign in to comment.