Skip to content

Commit a520e56

Browse files
committed
Disallow references to static mut
1 parent d295716 commit a520e56

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

ledger_device_sdk/src/nbgl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ trait SyncNBGL: Sized {
7979

8080
fn ux_sync_wait(&self, exit_on_apdu: bool) -> SyncNbgl {
8181
unsafe {
82-
if let Some(comm) = COMM_REF.as_mut() {
82+
if let Some(comm) = (*(&raw mut COMM_REF)).as_mut() {
8383
while !G_ENDED {
8484
let apdu_received = comm.next_event_ahead::<ApduHeader>();
8585
if exit_on_apdu && apdu_received {

ledger_device_sdk/src/nbgl/nbgl_home_and_settings.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ unsafe extern "C" fn settings_callback(token: c_int, _index: u8, _page: c_int) {
2020
_ => panic!("Invalid state."),
2121
}
2222

23-
if let Some(data) = NVM_REF.as_mut() {
23+
if let Some(data) = (*(&raw mut NVM_REF)).as_mut() {
2424
let mut switch_values: [u8; SETTINGS_SIZE] = *data.get_ref();
2525
if switch_values[setting_idx] == OFF_STATE {
2626
switch_values[setting_idx] = ON_STATE;
@@ -166,7 +166,7 @@ impl<'a> NbglHomeAndSettings {
166166
for (i, setting) in self.setting_contents.iter().enumerate() {
167167
SWITCH_ARRAY[i].text = setting[0].as_ptr();
168168
SWITCH_ARRAY[i].subText = setting[1].as_ptr();
169-
let state = if let Some(data) = NVM_REF.as_mut() {
169+
let state = if let Some(data) = (*(&raw mut NVM_REF)).as_mut() {
170170
data.get_ref()[i]
171171
} else {
172172
OFF_STATE
@@ -179,7 +179,7 @@ impl<'a> NbglHomeAndSettings {
179179
self.content = nbgl_content_t {
180180
content: nbgl_content_u {
181181
switchesList: nbgl_pageSwitchesList_s {
182-
switches: &SWITCH_ARRAY as *const nbgl_contentSwitch_t,
182+
switches: &raw const SWITCH_ARRAY as *const nbgl_contentSwitch_t,
183183
nbSwitches: self.nb_settings,
184184
},
185185
},
@@ -211,7 +211,7 @@ impl<'a> NbglHomeAndSettings {
211211
);
212212
match self.ux_sync_wait(true) {
213213
SyncNbgl::UxSyncRetApduReceived => {
214-
if let Some(comm) = COMM_REF.as_mut() {
214+
if let Some(comm) = (*(&raw mut COMM_REF)).as_mut() {
215215
if let Some(value) = comm.check_event() {
216216
return value;
217217
}
@@ -250,7 +250,7 @@ impl<'a> NbglHomeAndSettings {
250250
for (i, setting) in self.setting_contents.iter().enumerate() {
251251
SWITCH_ARRAY[i].text = setting[0].as_ptr();
252252
SWITCH_ARRAY[i].subText = setting[1].as_ptr();
253-
let state = if let Some(data) = NVM_REF.as_mut() {
253+
let state = if let Some(data) = (*(&raw mut NVM_REF)).as_mut() {
254254
data.get_ref()[i]
255255
} else {
256256
OFF_STATE
@@ -263,7 +263,7 @@ impl<'a> NbglHomeAndSettings {
263263
self.content = nbgl_content_t {
264264
content: nbgl_content_u {
265265
switchesList: nbgl_pageSwitchesList_s {
266-
switches: &SWITCH_ARRAY as *const nbgl_contentSwitch_t,
266+
switches: &raw const SWITCH_ARRAY as *const nbgl_contentSwitch_t,
267267
nbSwitches: self.nb_settings,
268268
},
269269
},

ledger_device_sdk/src/seph.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ pub fn handle_usb_event(event: u8) {
112112
match Events::from(event) {
113113
Events::USBEventReset => {
114114
unsafe {
115-
USBD_LL_SetSpeed(&mut USBD_Device, 1 /*USBD_SPEED_FULL*/);
116-
USBD_LL_Reset(&mut USBD_Device);
115+
USBD_LL_SetSpeed(&raw mut USBD_Device, 1 /*USBD_SPEED_FULL*/);
116+
USBD_LL_Reset(&raw mut USBD_Device);
117117

118118
if G_io_app.apdu_media != IO_APDU_MEDIA_NONE {
119119
return;
@@ -124,13 +124,13 @@ pub fn handle_usb_event(event: u8) {
124124
}
125125
}
126126
Events::USBEventSOF => unsafe {
127-
USBD_LL_SOF(&mut USBD_Device);
127+
USBD_LL_SOF(&raw mut USBD_Device);
128128
},
129129
Events::USBEventSuspend => unsafe {
130-
USBD_LL_Suspend(&mut USBD_Device);
130+
USBD_LL_Suspend(&raw mut USBD_Device);
131131
},
132132
Events::USBEventResume => unsafe {
133-
USBD_LL_Resume(&mut USBD_Device);
133+
USBD_LL_Resume(&raw mut USBD_Device);
134134
},
135135
_ => (),
136136
}
@@ -140,13 +140,13 @@ pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
140140
let endpoint = buffer[3] & 0x7f;
141141
match UsbEp::from(buffer[4]) {
142142
UsbEp::USBEpXFERSetup => unsafe {
143-
USBD_LL_SetupStage(&mut USBD_Device, &buffer[6]);
143+
USBD_LL_SetupStage(&raw mut USBD_Device, &buffer[6]);
144144
},
145145
UsbEp::USBEpXFERIn => {
146146
if (endpoint as u32) < IO_USB_MAX_ENDPOINTS {
147147
unsafe {
148148
G_io_app.usb_ep_timeouts[endpoint as usize].timeout = 0;
149-
USBD_LL_DataInStage(&mut USBD_Device, endpoint, &buffer[6]);
149+
USBD_LL_DataInStage(&raw mut USBD_Device, endpoint, &buffer[6]);
150150
}
151151
}
152152
}
@@ -158,7 +158,7 @@ pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
158158
buf: apdu_buffer.as_mut_ptr(),
159159
len: 260,
160160
};
161-
USBD_LL_DataOutStage(&mut USBD_Device, endpoint, &buffer[6], &mut apdu_buf);
161+
USBD_LL_DataOutStage(&raw mut USBD_Device, endpoint, &buffer[6], &mut apdu_buf);
162162
}
163163
}
164164
}
@@ -167,19 +167,21 @@ pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
167167
}
168168

169169
pub fn handle_capdu_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
170-
let io_app = unsafe { &mut G_io_app };
171-
if io_app.apdu_state == APDU_IDLE {
172-
let max = (apdu_buffer.len() - 3).min(buffer.len() - 3);
173-
let size = u16::from_be_bytes([buffer[1], buffer[2]]) as usize;
170+
let io_app = &raw mut G_io_app;
171+
unsafe {
172+
if (*io_app).apdu_state == APDU_IDLE {
173+
let max = (apdu_buffer.len() - 3).min(buffer.len() - 3);
174+
let size = u16::from_be_bytes([buffer[1], buffer[2]]) as usize;
174175

175-
io_app.apdu_media = IO_APDU_MEDIA_RAW;
176-
io_app.apdu_state = APDU_RAW;
176+
(*io_app).apdu_media = IO_APDU_MEDIA_RAW;
177+
(*io_app).apdu_state = APDU_RAW;
177178

178-
let len = size.min(max);
179+
let len = size.min(max);
179180

180-
io_app.apdu_length = len as u16;
181+
(*io_app).apdu_length = len as u16;
181182

182-
apdu_buffer[..len].copy_from_slice(&buffer[3..len + 3]);
183+
apdu_buffer[..len].copy_from_slice(&buffer[3..len + 3]);
184+
}
183185
}
184186
}
185187

ledger_secure_sdk_sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ critical-section = { version = "1.1.2", optional = true }
1818

1919
[features]
2020
heap = ["dep:embedded-alloc", "dep:critical-section"]
21+
ccid = []
2122

2223
[lints.rust.unexpected_cfgs]
2324
level = "warn"

ledger_secure_sdk_sys/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ unsafe impl critical_section::Impl for CriticalSection {
6565
extern "C" fn heap_init() {
6666
// HEAP_SIZE comes from heap_size.rs, which is defined via env var and build.rs
6767
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
68-
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
68+
unsafe { HEAP.init(&raw mut HEAP_MEM as usize, HEAP_SIZE) }
6969
}
7070

7171
#[no_mangle]

0 commit comments

Comments
 (0)