Skip to content

Commit fd3f965

Browse files
committed
bt: check BT MTU before subtracting ATT overhead
Returned MTU can be 0 in case BT (or only ATT) connection is no longer active. In that case subtracting would overflow unsigned number. This fixes issue detected by Coverity Scan. Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
1 parent c753cdf commit fd3f965

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

lib/bt/downlink.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ struct pouch_gateway_downlink_context *pouch_gateway_downlink_start(struct bt_co
199199
return NULL;
200200
}
201201

202-
size_t mtu = bt_gatt_get_mtu(conn) - POUCH_GATEWAY_BT_ATT_OVERHEAD;
202+
size_t mtu = bt_gatt_get_mtu(conn);
203+
if (mtu < POUCH_GATEWAY_BT_ATT_OVERHEAD)
204+
{
205+
LOG_ERR("MTU too small");
206+
return NULL;
207+
}
208+
mtu -= POUCH_GATEWAY_BT_ATT_OVERHEAD;
203209

204210
node->downlink_ctx = pouch_gateway_downlink_open(downlink_data_available, conn);
205211
if (NULL == node->downlink_ctx)

lib/bt/server_cert.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,15 @@ void pouch_gateway_server_cert_write(struct bt_conn *conn)
220220
return;
221221
}
222222

223-
size_t mtu = bt_gatt_get_mtu(conn) - POUCH_GATEWAY_BT_ATT_OVERHEAD;
223+
size_t mtu = bt_gatt_get_mtu(conn);
224+
if (mtu < POUCH_GATEWAY_BT_ATT_OVERHEAD)
225+
{
226+
LOG_ERR("MTU too small");
227+
server_cert_cleanup(conn);
228+
pouch_gateway_bt_finished(conn);
229+
return;
230+
}
231+
mtu -= POUCH_GATEWAY_BT_ATT_OVERHEAD;
224232

225233
node->server_cert_sender = pouch_gatt_sender_create(node->packetizer, send_data_cb, conn, mtu);
226234
if (NULL == node->server_cert_sender)

0 commit comments

Comments
 (0)