shared/gatt-server: Fix heap overflow when appending prepare writes
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Tue, 8 Jun 2021 23:46:49 +0000 (16:46 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 11 Mar 2022 13:38:38 +0000 (19:08 +0530)
The code shall check if the prepare writes would append more the
allowed maximum attribute length.

Fixes https://github.com/bluez/bluez/security/advisories/GHSA-479m-xcq5-9g2q

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/shared/gatt-server.c

index 22f7ef3..0135b5c 100644 (file)
@@ -792,6 +792,20 @@ static uint8_t authorize_req(struct bt_gatt_server *server,
                                                server->authorize_data);
 }
 
+static uint8_t check_length(uint16_t length, uint16_t offset)
+{
+       if (length > BT_ATT_MAX_VALUE_LEN)
+               return BT_ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LEN;
+
+       if (offset > BT_ATT_MAX_VALUE_LEN)
+               return BT_ATT_ERROR_INVALID_OFFSET;
+
+       if (length + offset > BT_ATT_MAX_VALUE_LEN)
+               return BT_ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LEN;
+
+       return 0;
+}
+
 static void write_cb(struct bt_att_chan *chan, uint8_t opcode, const void *pdu,
                                        uint16_t length, void *user_data)
 {
@@ -822,6 +836,10 @@ static void write_cb(struct bt_att_chan *chan, uint8_t opcode, const void *pdu,
                                (opcode == BT_ATT_OP_WRITE_REQ) ? "Req" : "Cmd",
                                handle);
 
+       ecode = check_length(length, 0);
+       if (ecode)
+               goto error;
+
        ecode = check_permissions(server, attr, BT_ATT_PERM_WRITE_MASK);
        if (ecode)
                goto error;
@@ -1325,6 +1343,10 @@ static void prep_write_cb(struct bt_att_chan *chan, uint8_t opcode,
        util_debug(server->debug_callback, server->debug_data,
                                "Prep Write Req - handle: 0x%04x", handle);
 
+       ecode = check_length(length, offset);
+       if (ecode)
+               goto error;
+
        ecode = check_permissions(server, attr, BT_ATT_PERM_WRITE_MASK);
        if (ecode)
                goto error;