[BlueZ v2 02/20] attrib/gatt: Guard against possible integer overflow

Bastien Nocera
2024-05-10T12:14:02Z

Error: INTEGER_OVERFLOW (CWE-190): [#def30]
bluez-5.75/attrib/gatt.c:1016:2: known_value_assign: "last" = "65535", its value is now 65535.
bluez-5.75/attrib/gatt.c:1087:2: overflow_const: Expression "dd->start", which is equal to 65536, where "last + 1" is known to be equal to 65536, overflows the type that receives it, an unsigned integer 16 bits wide.
1085| }
1086|
1087|-> dd->start = last + 1;
1088|
1089| if (last < dd->end && !uuid_found) {
---
attrib/gatt.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/attrib/gatt.c b/attrib/gatt.c
index b496dd1ebd95..3cedae9d167a 100644
--- a/attrib/gatt.c
+++ b/attrib/gatt.c
@@ -1076,10 +1076,12 @@ static void desc_discovered_cb(guint8 status, const guint8 *ipdu,
att_data_list_free(list);

/*
- * If last handle is lower from previous start handle then it is smth
- * wrong. Let's stop search, otherwise we might enter infinite loop.
+ * If last handle is lower from previous start handle or if iterating
+ * to the next handle from the last possible offset would overflow, then
+ * something is wrong. Let's stop search, otherwise we might enter
+ * infinite loop.
*/
- if (last < dd->start) {
+ if (last < dd->start || last == G_MAXUINT16) {
err = ATT_ECODE_UNLIKELY;
goto done;
}
--
2.44.0