1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <base/location.h>
#include <bluetooth/log.h>
#include <fuzzer/FuzzedDataProvider.h>
#include <gmock/gmock.h>
#include <cstdint>
#include <string>
#include <vector>
#include "btif/include/stack_manager_t.h"
#include "common/message_loop_thread.h"
#include "hal/snoop_logger.h"
#include "hci/controller_interface_mock.h"
#include "osi/include/allocator.h"
#include "stack/btm/btm_int_types.h"
#include "stack/include/bt_psm_types.h"
#include "stack/include/l2cap_acl_interface.h"
#include "stack/include/l2cap_controller_interface.h"
#include "stack/include/l2cap_hci_link_interface.h"
#include "stack/include/l2cap_interface.h"
#include "stack/include/l2cap_module.h"
#include "stack/include/l2cdefs.h"
#include "stack/l2cap/l2c_int.h"
#include "test/fake/fake_osi.h"
#include "test/mock/mock_main_shim_entry.h"
#include "test/mock/mock_stack_acl.h"
#include "test/mock/mock_stack_btm_devctl.h"
using bluetooth::Uuid;
using testing::Return;
using namespace bluetooth;
// Verify the passed data is readable
static void ConsumeData(const uint8_t* data, size_t size) {
volatile uint8_t checksum = 0;
for (size_t i = 0; i < size; i++) {
checksum ^= data[i];
}
}
tBTM_CB btm_cb;
bt_status_t do_in_main_thread(base::OnceCallback<void()>) {
// this is not properly mocked, so we use abort to catch if this is used in
// any test cases
abort();
}
bt_status_t do_in_main_thread_delayed(base::OnceCallback<void()>, std::chrono::microseconds) {
// this is not properly mocked, so we use abort to catch if this is used in
// any test cases
abort();
}
bluetooth::common::MessageLoopThread* get_main_thread() { return nullptr; }
bluetooth::common::PostableContext* get_main() { return nullptr; }
namespace bluetooth {
namespace os {
uint32_t GetSystemPropertyUint32Base(const std::string& /*property*/, uint32_t default_value,
int /*base*/) {
return default_value;
}
uint32_t GetSystemPropertyUint32(const std::string& /*property*/, uint32_t default_value) {
return default_value;
}
} // namespace os
namespace hal {
class SnoopLogger;
const std::string SnoopLogger::kBtSnoopLogModeFiltered = "filtered";
std::string SnoopLogger::GetCurrentSnoopMode() { return "filtered"; }
void SnoopLogger::AcceptlistL2capChannel(uint16_t, uint16_t, uint16_t) {}
void SnoopLogger::AddA2dpMediaChannel(uint16_t, uint16_t, uint16_t) {}
void SnoopLogger::AddRfcommL2capChannel(uint16_t, uint16_t, uint16_t) {}
void SnoopLogger::ClearL2capAcceptlist(uint16_t, uint16_t, uint16_t) {}
void SnoopLogger::RemoveA2dpMediaChannel(uint16_t, uint16_t) {}
void SnoopLogger::SetL2capChannelClose(uint16_t, uint16_t, uint16_t) {}
void SnoopLogger::SetL2capChannelOpen(uint16_t, uint16_t, uint16_t, uint16_t, bool) {}
} // namespace hal
} // namespace bluetooth
namespace connection_manager {
bool direct_connect_add(uint8_t /* id */, const RawAddress& /* bd_addr */,
tBLE_ADDR_TYPE /* addr_type */) {
return true;
}
} // namespace connection_manager
namespace {
class FakeBtStack {
public:
FakeBtStack() {
test::mock::stack_acl::acl_send_data_packet_br_edr.body = [](const RawAddress& /*bd_addr*/,
BT_HDR* hdr) {
ConsumeData((const uint8_t*)hdr, hdr->offset + hdr->len);
osi_free(hdr);
};
test::mock::stack_acl::acl_send_data_packet_ble.body = [](const RawAddress& /*bd_addr*/,
BT_HDR* hdr) {
ConsumeData((const uint8_t*)hdr, hdr->offset + hdr->len);
osi_free(hdr);
};
GetInterfaceToProfiles()->profileSpecific_HACK->GetHearingAidDeviceCount = []() { return 1; };
bluetooth::hci::testing::mock_controller_ =
std::make_unique<bluetooth::hci::testing::MockControllerInterface>();
ON_CALL(*bluetooth::hci::testing::mock_controller_, GetLeSuggestedDefaultDataLength)
.WillByDefault(Return(512));
bluetooth::hci::LeBufferSize iso_size;
iso_size.le_data_packet_length_ = 512;
iso_size.total_num_le_packets_ = 6;
ON_CALL(*bluetooth::hci::testing::mock_controller_, GetControllerIsoBufferSize)
.WillByDefault(Return(iso_size));
bluetooth::hci::LeBufferSize le_size;
le_size.le_data_packet_length_ = 512;
le_size.total_num_le_packets_ = 6;
ON_CALL(*bluetooth::hci::testing::mock_controller_, GetLeBufferSize)
.WillByDefault(Return(le_size));
ON_CALL(*bluetooth::hci::testing::mock_controller_, SupportsBle).WillByDefault(Return(true));
ON_CALL(*bluetooth::hci::testing::mock_controller_, GetAclPacketLength)
.WillByDefault(Return(512));
}
~FakeBtStack() {
test::mock::stack_acl::acl_send_data_packet_br_edr = {};
test::mock::stack_acl::acl_send_data_packet_ble = {};
bluetooth::hci::testing::mock_controller_.reset();
}
};
class Fakes {
public:
test::fake::FakeOsi fake_osi;
FakeBtStack fake_stack;
};
} // namespace
constexpr uint8_t kAttAddr[] = {0x11, 0x78, 0x78, 0x78, 0x78, 0x78};
constexpr uint16_t kAttHndl = 0x0111;
constexpr uint8_t kEattAddr[] = {0x22, 0x78, 0x78, 0x78, 0x78, 0x78};
constexpr uint8_t kSmpBrAddr[] = {0x33, 0x78, 0x78, 0x78, 0x78, 0x78};
constexpr uint16_t kSmpBrHndl = 0x0222;
constexpr uint16_t kNumClassicAclBuffer = 100;
constexpr uint16_t kNumLeAclBuffer = 100;
static void Fuzz(const uint8_t* data, size_t size) {
memset(&btm_cb, 0, sizeof(btm_cb));
l2c_init();
l2c_link_init(kNumClassicAclBuffer);
l2c_link_process_ble_num_bufs(kNumLeAclBuffer);
tL2CAP_FIXED_CHNL_REG reg = {
.pL2CA_FixedConn_Cb = [](uint16_t, const RawAddress&, bool, uint16_t, tBT_TRANSPORT) {},
.pL2CA_FixedData_Cb =
[](uint16_t, const RawAddress&, BT_HDR* hdr) {
ConsumeData((const uint8_t*)hdr, hdr->offset + hdr->len);
},
.pL2CA_FixedCong_Cb = [](const RawAddress&, bool) {},
.default_idle_tout = 1000,
};
tL2CAP_APPL_INFO appl_info = {
.pL2CA_ConnectInd_Cb = [](const RawAddress&, uint16_t, uint16_t, uint8_t) {},
.pL2CA_ConnectCfm_Cb = [](uint16_t, tL2CAP_CONN) {},
.pL2CA_ConfigInd_Cb = [](uint16_t, tL2CAP_CFG_INFO*) {},
.pL2CA_ConfigCfm_Cb = [](uint16_t, uint16_t, tL2CAP_CFG_INFO*) {},
.pL2CA_DisconnectInd_Cb = [](uint16_t, bool) {},
.pL2CA_DisconnectCfm_Cb = [](uint16_t, uint16_t) {},
.pL2CA_DataInd_Cb =
[](uint16_t, BT_HDR* hdr) {
ConsumeData((const uint8_t*)hdr, hdr->offset + hdr->len);
},
.pL2CA_CongestionStatus_Cb = [](uint16_t, bool) {},
.pL2CA_TxComplete_Cb = [](uint16_t, uint16_t) {},
.pL2CA_Error_Cb = [](uint16_t, uint16_t) {},
.pL2CA_CreditBasedConnectInd_Cb = [](const RawAddress&, std::vector<uint16_t>&, uint16_t,
uint16_t, uint8_t) {},
.pL2CA_CreditBasedConnectCfm_Cb = [](const RawAddress&, uint16_t, uint16_t,
tL2CAP_LE_RESULT_CODE) {},
.pL2CA_CreditBasedReconfigCompleted_Cb = [](const RawAddress&, uint16_t, bool,
tL2CAP_LE_CFG_INFO*) {},
.pL2CA_CreditBasedCollisionInd_Cb = [](const RawAddress&) {},
};
log::assert_that(stack::l2cap::get_interface().L2CA_RegisterWithSecurity(
BT_PSM_ATT, appl_info, false, nullptr, L2CAP_MTU_SIZE, 0, BTM_SEC_NONE),
"assert failed: L2CA_RegisterWithSecurity(BT_PSM_ATT, appl_info, "
"false, nullptr, L2CAP_MTU_SIZE, 0, BTM_SEC_NONE)");
log::assert_that(stack::l2cap::get_interface().L2CA_RegisterLECoc(BT_PSM_EATT, appl_info,
BTM_SEC_NONE, {}),
"assert failed: L2CA_RegisterLECoc(BT_PSM_EATT, appl_info, "
"BTM_SEC_NONE, {{}})");
log::assert_that(stack::l2cap::get_interface().L2CA_RegisterFixedChannel(L2CAP_ATT_CID, ®),
"assert failed: L2CA_RegisterFixedChannel(L2CAP_ATT_CID, ®)");
log::assert_that(stack::l2cap::get_interface().L2CA_ConnectFixedChnl(L2CAP_ATT_CID, kAttAddr),
"assert failed: L2CA_ConnectFixedChnl(L2CAP_ATT_CID, kAttAddr)");
log::assert_that(
l2cble_conn_comp(kAttHndl, HCI_ROLE_CENTRAL, kAttAddr, BLE_ADDR_PUBLIC, 100, 100, 100),
"assert failed: l2cble_conn_comp(kAttHndl, HCI_ROLE_CENTRAL, kAttAddr, "
"BLE_ADDR_PUBLIC, 100, 100, 100)");
log::assert_that(stack::l2cap::get_interface().L2CA_RegisterFixedChannel(L2CAP_SMP_BR_CID, ®),
"assert failed: L2CA_RegisterFixedChannel(L2CAP_SMP_BR_CID, ®)");
log::assert_that(
stack::l2cap::get_interface().L2CA_ConnectFixedChnl(L2CAP_SMP_BR_CID, kSmpBrAddr),
"assert failed: L2CA_ConnectFixedChnl(L2CAP_SMP_BR_CID, kSmpBrAddr)");
l2c_link_hci_conn_comp(HCI_SUCCESS, kSmpBrHndl, kSmpBrAddr);
auto att_cid = stack::l2cap::get_interface().L2CA_ConnectReq(BT_PSM_ATT, kAttAddr);
log::assert_that(att_cid != 0, "assert failed: att_cid != 0");
tL2CAP_LE_CFG_INFO cfg;
auto eatt_cid =
stack::l2cap::get_interface().L2CA_ConnectLECocReq(BT_PSM_EATT, kEattAddr, &cfg, 0);
log::assert_that(eatt_cid != 0, "assert failed: eatt_cid != 0");
FuzzedDataProvider fdp(data, size);
// Feeding input packets
constexpr uint16_t kMinPacketSize = 4 + L2CAP_PKT_OVERHEAD;
constexpr uint16_t kMaxPacketSize = 1024;
for (;;) {
auto size = fdp.ConsumeIntegralInRange<uint16_t>(kMinPacketSize, kMaxPacketSize);
auto bytes = fdp.ConsumeBytes<uint8_t>(size);
if (bytes.size() < kMinPacketSize) {
break;
}
BT_HDR* hdr = (BT_HDR*)osi_calloc(sizeof(BT_HDR) + bytes.size());
hdr->len = bytes.size();
std::copy(bytes.cbegin(), bytes.cend(), hdr->data);
l2c_rcv_acl_data(hdr);
}
(void)stack::l2cap::get_interface().L2CA_DisconnectReq(att_cid);
(void)stack::l2cap::get_interface().L2CA_DisconnectLECocReq(eatt_cid);
(void)stack::l2cap::get_interface().L2CA_RemoveFixedChnl(L2CAP_SMP_BR_CID, kSmpBrAddr);
l2c_link_hci_disc_comp(kSmpBrHndl, HCI_SUCCESS);
(void)stack::l2cap::get_interface().L2CA_RemoveFixedChnl(L2CAP_ATT_CID, kAttAddr);
l2c_link_hci_disc_comp(kAttHndl, HCI_SUCCESS);
l2cu_device_reset();
l2c_free();
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
auto fakes = std::make_unique<Fakes>();
Fuzz(Data, Size);
return 0;
}
|