Skip to content

Commit 097ed18

Browse files
lylezhu2012henrikbrixandersen
authored andcommitted
Bluetooth: SDP: Fix stack override issue
Check the remaining space of the local variable `filter` to avoid stack override issue. Signed-off-by: Lyle Zhu <[email protected]> (cherry picked from commit f3a1cf2)
1 parent f5f9872 commit 097ed18

File tree

1 file changed

+16
-8
lines changed
  • subsys/bluetooth/host

1 file changed

+16
-8
lines changed

subsys/bluetooth/host/sdp.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct select_attrs_data {
115115
uint16_t max_att_len;
116116
uint16_t att_list_len;
117117
uint8_t cont_state_size;
118-
uint8_t num_filters;
118+
size_t num_filters;
119119
bool new_service;
120120
};
121121

@@ -814,7 +814,7 @@ static uint8_t select_attrs(struct bt_sdp_attribute *attr, uint8_t att_idx,
814814
struct select_attrs_data *sad = user_data;
815815
uint16_t att_id_lower, att_id_upper, att_id_cur, space;
816816
uint32_t attr_size, seq_size;
817-
uint8_t idx_filter;
817+
size_t idx_filter;
818818

819819
for (idx_filter = 0U; idx_filter < sad->num_filters; idx_filter++) {
820820

@@ -939,7 +939,7 @@ static uint8_t select_attrs(struct bt_sdp_attribute *attr, uint8_t att_idx,
939939
* @return len Length of the attribute list created
940940
*/
941941
static uint16_t create_attr_list(struct bt_sdp *sdp, struct bt_sdp_record *record,
942-
uint32_t *filter, uint8_t num_filters,
942+
uint32_t *filter, size_t num_filters,
943943
uint16_t max_att_len, uint8_t cont_state_size,
944944
uint8_t next_att, struct search_state *state,
945945
struct net_buf *rsp_buf)
@@ -978,12 +978,13 @@ static uint16_t create_attr_list(struct bt_sdp *sdp, struct bt_sdp_record *recor
978978
* IDs, the lower 2 bytes contain the ID and the upper 2 bytes are set to
979979
* 0xFFFF. For attribute ranges, the lower 2bytes indicate the start ID and
980980
* the upper 2bytes indicate the end ID
981+
* @param max_filters Max element slots of filter to be filled in
981982
* @param num_filters No. of filter elements filled in (to be returned)
982983
*
983984
* @return 0 for success, or relevant error code
984985
*/
985986
static uint16_t get_att_search_list(struct net_buf *buf, uint32_t *filter,
986-
uint8_t *num_filters)
987+
size_t max_filters, size_t *num_filters)
987988
{
988989
struct bt_sdp_data_elem data_elem;
989990
uint16_t res;
@@ -998,6 +999,11 @@ static uint16_t get_att_search_list(struct net_buf *buf, uint32_t *filter,
998999
size = data_elem.data_size;
9991000

10001001
while (size) {
1002+
if (*num_filters >= max_filters) {
1003+
LOG_WRN("Exceeded maximum array length %u of %p", max_filters, filter);
1004+
return 0;
1005+
}
1006+
10011007
res = parse_data_elem(buf, &data_elem);
10021008
if (res) {
10031009
return res;
@@ -1075,7 +1081,8 @@ static uint16_t sdp_svc_att_req(struct bt_sdp *sdp, struct net_buf *buf,
10751081
struct net_buf *rsp_buf;
10761082
uint32_t svc_rec_hdl;
10771083
uint16_t max_att_len, res, att_list_len;
1078-
uint8_t num_filters, cont_state_size, next_att = 0U;
1084+
size_t num_filters;
1085+
uint8_t cont_state_size, next_att = 0U;
10791086

10801087
if (buf->len < 6) {
10811088
LOG_WRN("Malformed packet");
@@ -1086,7 +1093,7 @@ static uint16_t sdp_svc_att_req(struct bt_sdp *sdp, struct net_buf *buf,
10861093
max_att_len = net_buf_pull_be16(buf);
10871094

10881095
/* Set up the filters */
1089-
res = get_att_search_list(buf, filter, &num_filters);
1096+
res = get_att_search_list(buf, filter, ARRAY_SIZE(filter), &num_filters);
10901097
if (res) {
10911098
/* Error in parsing */
10921099
return res;
@@ -1191,7 +1198,8 @@ static uint16_t sdp_svc_search_att_req(struct bt_sdp *sdp, struct net_buf *buf,
11911198
struct bt_sdp_att_rsp *rsp;
11921199
struct bt_sdp_data_elem_seq *seq = NULL;
11931200
uint16_t max_att_len, res, att_list_len = 0U;
1194-
uint8_t num_filters, cont_state_size, next_svc = 0U, next_att = 0U;
1201+
size_t num_filters;
1202+
uint8_t cont_state_size, next_svc = 0U, next_att = 0U;
11951203
bool dry_run = false;
11961204

11971205
res = find_services(buf, matching_recs);
@@ -1207,7 +1215,7 @@ static uint16_t sdp_svc_search_att_req(struct bt_sdp *sdp, struct net_buf *buf,
12071215
max_att_len = net_buf_pull_be16(buf);
12081216

12091217
/* Set up the filters */
1210-
res = get_att_search_list(buf, filter, &num_filters);
1218+
res = get_att_search_list(buf, filter, ARRAY_SIZE(filter), &num_filters);
12111219

12121220
if (res) {
12131221
/* Error in parsing */

0 commit comments

Comments
 (0)