Skip to content

Commit 27a6c26

Browse files
committed
bluetooth: cgms: racp: validate write length
Ensure incoming RACP characteristic writes are rejected with proper ATT error codes before reaching internal processing the CGMS subsystem. Signed-off-by: Łukasz Duda <lukasz.duda@nordicsemi.no> (cherry picked from commit 0684d60)
1 parent 9eb0315 commit 27a6c26

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

subsys/bluetooth/services/cgms/cgms.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ static ssize_t racp_on_receive(struct bt_conn *conn,
269269
{
270270
int rc;
271271

272+
if (offset != 0) {
273+
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
274+
}
275+
276+
if (len > CGMS_RACP_MAX_LENGTH) {
277+
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
278+
}
279+
272280
rc = cgms_racp_recv_request(conn, buf, len);
273281
if (rc < 0) {
274282
LOG_WRN("Internal Error during RACP Handling: %d", rc);

subsys/bluetooth/services/cgms/cgms_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
extern "C" {
1616
#endif
1717

18+
/* Maximum byte length of a RACP characteristic write. */
19+
#define CGMS_RACP_MAX_LENGTH 20
20+
1821
/* Continuous Glucose Monitoring feature */
1922
enum cgms_feat {
2023
CGMS_FEAT_CALIBRATION_SUPPORTED = BIT(0),

subsys/bluetooth/services/cgms/cgms_racp.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
LOG_MODULE_DECLARE(cgms, CONFIG_BT_CGMS_LOG_LEVEL);
1717

18-
#define CGMS_RACP_LENGTH 20
19-
2018
#define RACP_Q_STACK_SIZE 2048
2119
#define RACP_Q_PRIORITY 1
2220
K_THREAD_STACK_DEFINE(racp_q_stack_area, RACP_Q_STACK_SIZE);
@@ -79,7 +77,7 @@ struct racp_task {
7977
struct k_work item;
8078
struct bt_conn *peer;
8179
struct net_buf_simple req;
82-
uint8_t req_buf[CGMS_RACP_LENGTH];
80+
uint8_t req_buf[CGMS_RACP_MAX_LENGTH];
8381
};
8482

8583
static struct cgms_meas_db_entry cgms_meas_db_entry_pool[RECORD_NUM];
@@ -105,7 +103,7 @@ static struct cgms_meas_db_entry *entry_alloc(void)
105103

106104
static int generic_handler(struct bt_conn *peer, uint8_t opcode, uint8_t response_code)
107105
{
108-
NET_BUF_SIMPLE_DEFINE(rsp, CGMS_RACP_LENGTH);
106+
NET_BUF_SIMPLE_DEFINE(rsp, CGMS_RACP_MAX_LENGTH);
109107

110108
net_buf_simple_add_u8(&rsp, RACP_OPCODE_RESPONSE_CODE);
111109
net_buf_simple_add_u8(&rsp, RACP_OPERATOR_NULL);
@@ -273,7 +271,7 @@ static int report_num_recs_all_handler(struct bt_conn *peer)
273271
uint16_t count = 0;
274272
sys_snode_t *record_node;
275273

276-
NET_BUF_SIMPLE_DEFINE(rsp, CGMS_RACP_LENGTH);
274+
NET_BUF_SIMPLE_DEFINE(rsp, CGMS_RACP_MAX_LENGTH);
277275

278276
if (!sys_slist_is_empty(&database)) {
279277
SYS_SLIST_FOR_EACH_NODE(&database, record_node) {
@@ -297,7 +295,7 @@ static int report_num_recs_greater_or_equal_handler(struct bt_conn *peer,
297295
struct cgms_meas_db_entry *entry;
298296
sys_snode_t *record_node;
299297

300-
NET_BUF_SIMPLE_DEFINE(rsp, CGMS_RACP_LENGTH);
298+
NET_BUF_SIMPLE_DEFINE(rsp, CGMS_RACP_MAX_LENGTH);
301299

302300
if (operand->len < 3) {
303301
return generic_handler(peer, RACP_OPCODE_REPORT_RECS,
@@ -434,6 +432,10 @@ int cgms_racp_recv_request(struct bt_conn *peer, const uint8_t *req_data, uint16
434432
}
435433

436434
/* For other requests, prepare the data and submit to workqueue. */
435+
if (req_len > sizeof(report_record_task.req_buf)) {
436+
return -EMSGSIZE;
437+
}
438+
437439
report_record_task.peer = peer;
438440
memcpy(report_record_task.req_buf, req_data, req_len);
439441
net_buf_simple_init_with_data(&report_record_task.req, report_record_task.req_buf, req_len);

0 commit comments

Comments
 (0)