Skip to content

Commit 3553bbd

Browse files
committed
Add check for MTU=0
Adds a malloc function that wraps the get_mtu call and its size check before allocating, and adds checks for MTU size elsewhere. Signed-off-by: Trond Snekvik <trond@golioth.io>
1 parent 5ce2d8c commit 3553bbd

4 files changed

Lines changed: 37 additions & 4 deletions

File tree

gateway/src/bt/cert.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,15 @@ static int write_server_cert_characteristic(struct bt_conn *conn)
189189
struct golioth_node_info *node = get_node_info(conn);
190190
struct bt_gatt_write_params *params = &node->write_params;
191191
uint16_t server_cert_handle = node->attr_handles[GOLIOTH_GATT_ATTR_SERVER_CERT].value;
192-
size_t len = bt_gatt_get_mtu(conn) - BT_ATT_OVERHEAD;
192+
193+
size_t mtu = bt_gatt_get_mtu(conn);
194+
if (mtu < BT_ATT_OVERHEAD)
195+
{
196+
LOG_ERR("MTU too small: %d", mtu);
197+
return -EIO;
198+
}
199+
200+
size_t len = mtu - BT_ATT_OVERHEAD;
193201
enum golioth_ble_gatt_packetizer_result ret =
194202
golioth_ble_gatt_packetizer_get(node->packetizer, node->server_cert_scratch, &len);
195203

@@ -288,7 +296,7 @@ static void gateway_server_cert_write_start(struct bt_conn *conn)
288296
return;
289297
}
290298

291-
node->server_cert_scratch = malloc(bt_gatt_get_mtu(conn) - BT_ATT_OVERHEAD);
299+
node->server_cert_scratch = bt_gatt_mtu_malloc(conn);
292300
if (NULL == node->server_cert_scratch)
293301
{
294302
LOG_ERR("Could not allocate space for %s scratch buffer", "server cert");

gateway/src/bt/downlink.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ static int write_downlink_characteristic(struct bt_conn *conn)
5252
struct bt_gatt_write_params *params = &node->write_params;
5353
uint16_t downlink_handle = node->attr_handles[GOLIOTH_GATT_ATTR_DOWNLINK].value;
5454

55-
size_t len = bt_gatt_get_mtu(conn) - BT_ATT_OVERHEAD;
55+
size_t mtu = bt_gatt_get_mtu(conn);
56+
if (mtu < BT_ATT_OVERHEAD)
57+
{
58+
LOG_ERR("MTU too small: %d", mtu);
59+
return -EIO;
60+
}
61+
62+
size_t len = mtu - BT_ATT_OVERHEAD;
5663
enum golioth_ble_gatt_packetizer_result ret =
5764
golioth_ble_gatt_packetizer_get(node->packetizer, node->downlink_scratch, &len);
5865

@@ -154,7 +161,7 @@ struct downlink_context *gateway_downlink_start(struct bt_conn *conn)
154161
return NULL;
155162
}
156163

157-
node->downlink_scratch = malloc(bt_gatt_get_mtu(conn) - BT_ATT_OVERHEAD);
164+
node->downlink_scratch = bt_gatt_mtu_malloc(conn);
158165
if (NULL == node->downlink_scratch)
159166
{
160167
LOG_ERR("Could not allocate space for downlink scratch buffer");

gateway/src/bt/types.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma once
88

99
#include <stdint.h>
10+
#include <stdlib.h>
1011
#include <zephyr/bluetooth/gatt.h>
1112

1213
#define BT_ATT_OVERHEAD 3 /* opcode (1) + handle (2) */
@@ -46,3 +47,19 @@ struct golioth_node_info
4647
struct device_cert_context *device_cert_ctx;
4748
struct server_cert_context *server_cert_ctx;
4849
};
50+
51+
/**
52+
* Allocate a buffer the size of the current MTU for GATT operations.
53+
* @param conn Bluetooth connection
54+
* @return Pointer to allocated buffer or NULL on failure
55+
*/
56+
static inline void *bt_gatt_mtu_malloc(struct bt_conn *conn)
57+
{
58+
size_t mtu = bt_gatt_get_mtu(conn);
59+
if (mtu < BT_ATT_OVERHEAD)
60+
{
61+
return NULL;
62+
}
63+
64+
return malloc(mtu - BT_ATT_OVERHEAD);
65+
}

gateway/src/cert.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ int device_cert_finish(struct device_cert_context *context)
6868
status = golioth_gateway_device_cert_set(_client, context->buf, context->len, 5);
6969
if (status != GOLIOTH_OK)
7070
{
71+
LOG_ERR("Failed to finish device cert: %d", status);
7172
return -EIO;
7273
}
7374

0 commit comments

Comments
 (0)