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
|
//! This module handles "arbitration" of ATT packets, to determine whether they
//! should be handled by the primary stack or by the Rust stack
use pdl_runtime::Packet;
use std::sync::{Arc, Mutex};
use log::{error, trace, warn};
use std::sync::RwLock;
use crate::do_in_rust_thread;
use crate::packets::att;
use super::ffi::{InterceptAction, StoreCallbacksFromRust};
use super::ids::{AdvertiserId, TransportIndex};
use super::mtu::MtuEvent;
use super::opcode_types::{classify_opcode, OperationType};
use super::server::isolation_manager::IsolationManager;
static ARBITER: RwLock<Option<Arc<Mutex<IsolationManager>>>> = RwLock::new(None);
/// Initialize the Arbiter
pub fn initialize_arbiter() -> Arc<Mutex<IsolationManager>> {
let arbiter = Arc::new(Mutex::new(IsolationManager::new()));
let mut lock = ARBITER.write().unwrap();
assert!(lock.is_none(), "Rust stack should only start up once");
*lock = Some(arbiter.clone());
StoreCallbacksFromRust(
on_le_connect,
on_le_disconnect,
intercept_packet,
|tcb_idx| on_mtu_event(TransportIndex(tcb_idx), MtuEvent::OutgoingRequest),
|tcb_idx, mtu| on_mtu_event(TransportIndex(tcb_idx), MtuEvent::IncomingResponse(mtu)),
|tcb_idx, mtu| on_mtu_event(TransportIndex(tcb_idx), MtuEvent::IncomingRequest(mtu)),
);
arbiter
}
/// Clean the Arbiter
pub fn clean_arbiter() {
let mut lock = ARBITER.write().unwrap();
*lock = None
}
/// Acquire the mutex holding the Arbiter and provide a mutable reference to the
/// supplied closure
pub fn with_arbiter<T>(f: impl FnOnce(&mut IsolationManager) -> T) -> T {
f(ARBITER.read().unwrap().as_ref().expect("Rust stack is not started").lock().as_mut().unwrap())
}
/// Check if the Arbiter is initialized.
pub fn has_arbiter() -> bool {
ARBITER.read().unwrap().is_some()
}
/// Test to see if a buffer contains a valid ATT packet with an opcode we
/// are interested in intercepting (those intended for servers that are isolated)
fn try_parse_att_server_packet(
isolation_manager: &IsolationManager,
tcb_idx: TransportIndex,
packet: &[u8],
) -> Option<att::Att> {
isolation_manager.get_server_id(tcb_idx)?;
let att = att::Att::decode_full(packet).ok()?;
if att.opcode == att::AttOpcode::ExchangeMtuRequest {
// special case: this server opcode is handled by legacy stack, and we snoop
// on its handling, since the MTU is shared between the client + server
return None;
}
match classify_opcode(att.opcode) {
OperationType::Command | OperationType::Request | OperationType::Confirmation => Some(att),
_ => None,
}
}
fn on_le_connect(tcb_idx: u8, advertiser: u8) {
let tcb_idx = TransportIndex(tcb_idx);
let advertiser = AdvertiserId(advertiser);
let is_isolated = with_arbiter(|arbiter| arbiter.is_advertiser_isolated(advertiser));
if is_isolated {
do_in_rust_thread(move |modules| {
if let Err(err) = modules.gatt_module.on_le_connect(tcb_idx, Some(advertiser)) {
error!("{err:?}")
}
})
}
}
fn on_le_disconnect(tcb_idx: u8) {
// Events may be received after a FactoryReset
// is initiated for Bluetooth and the rust arbiter is taken
// down.
if !has_arbiter() {
warn!("arbiter is not yet initialized");
return;
}
let tcb_idx = TransportIndex(tcb_idx);
let was_isolated = with_arbiter(|arbiter| arbiter.is_connection_isolated(tcb_idx));
if was_isolated {
do_in_rust_thread(move |modules| {
if let Err(err) = modules.gatt_module.on_le_disconnect(tcb_idx) {
error!("{err:?}")
}
})
}
}
fn intercept_packet(tcb_idx: u8, packet: Vec<u8>) -> InterceptAction {
// Events may be received after a FactoryReset
// is initiated for Bluetooth and the rust arbiter is taken
// down.
if !has_arbiter() {
warn!("arbiter is not yet initialized");
return InterceptAction::Drop;
}
let tcb_idx = TransportIndex(tcb_idx);
if let Some(att) =
with_arbiter(|arbiter| try_parse_att_server_packet(arbiter, tcb_idx, &packet))
{
do_in_rust_thread(move |modules| {
trace!("pushing packet to GATT");
if let Some(bearer) = modules.gatt_module.get_bearer(tcb_idx) {
bearer.handle_packet(att)
} else {
error!("Bearer for {tcb_idx:?} not found");
}
});
InterceptAction::Drop
} else {
InterceptAction::Forward
}
}
fn on_mtu_event(tcb_idx: TransportIndex, event: MtuEvent) {
if with_arbiter(|arbiter| arbiter.is_connection_isolated(tcb_idx)) {
do_in_rust_thread(move |modules| {
let Some(bearer) = modules.gatt_module.get_bearer(tcb_idx) else {
error!("Bearer for {tcb_idx:?} not found");
return;
};
if let Err(err) = bearer.handle_mtu_event(event) {
error!("{err:?}")
}
});
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::gatt::ids::{AttHandle, ServerId};
use crate::packets::att;
const TCB_IDX: TransportIndex = TransportIndex(1);
const ADVERTISER_ID: AdvertiserId = AdvertiserId(3);
const SERVER_ID: ServerId = ServerId(4);
fn create_manager_with_isolated_connection(
tcb_idx: TransportIndex,
server_id: ServerId,
) -> IsolationManager {
let mut isolation_manager = IsolationManager::new();
isolation_manager.associate_server_with_advertiser(server_id, ADVERTISER_ID);
isolation_manager.on_le_connect(tcb_idx, Some(ADVERTISER_ID));
isolation_manager
}
#[test]
fn test_packet_capture_when_isolated() {
let isolation_manager = create_manager_with_isolated_connection(TCB_IDX, SERVER_ID);
let packet = att::AttReadRequest { attribute_handle: AttHandle(1).into() };
let out = try_parse_att_server_packet(
&isolation_manager,
TCB_IDX,
&packet.encode_to_vec().unwrap(),
);
assert!(out.is_some());
}
#[test]
fn test_packet_bypass_when_isolated() {
let isolation_manager = create_manager_with_isolated_connection(TCB_IDX, SERVER_ID);
let packet = att::AttErrorResponse {
opcode_in_error: att::AttOpcode::ReadResponse,
handle_in_error: AttHandle(1).into(),
error_code: att::AttErrorCode::InvalidHandle,
};
let out = try_parse_att_server_packet(
&isolation_manager,
TCB_IDX,
&packet.encode_to_vec().unwrap(),
);
assert!(out.is_none());
}
#[test]
fn test_mtu_bypass() {
let isolation_manager = create_manager_with_isolated_connection(TCB_IDX, SERVER_ID);
let packet = att::AttExchangeMtuRequest { mtu: 64 };
let out = try_parse_att_server_packet(
&isolation_manager,
TCB_IDX,
&packet.encode_to_vec().unwrap(),
);
assert!(out.is_none());
}
#[test]
fn test_packet_bypass_when_not_isolated() {
let isolation_manager = IsolationManager::new();
let packet = att::AttReadRequest { attribute_handle: AttHandle(1).into() };
let out = try_parse_att_server_packet(
&isolation_manager,
TCB_IDX,
&packet.encode_to_vec().unwrap(),
);
assert!(out.is_none());
}
}
|