Skip to content

Commit 03d4db8

Browse files
kernel/vtpm: add missing safety comments
Add SAFETY comments around FFI calls and some checks for return values. Signed-off-by: Stefano Garzarella <[email protected]>
1 parent 1dd6c6e commit 03d4db8

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

kernel/src/vtpm/tcgtpm/mod.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl TcgTpm {
4040
}
4141

4242
fn teardown(&self) -> Result<(), SvsmReqError> {
43+
// SAFETY: FFI call. Return value is checked.
4344
let result = unsafe { TPM_TearDown() };
4445
match result {
4546
0 => Ok(()),
@@ -51,6 +52,7 @@ impl TcgTpm {
5152
}
5253

5354
fn manufacture(&self, first_time: i32) -> Result<i32, SvsmReqError> {
55+
// SAFETY: FFI call. Parameter and return values are checked.
5456
let result = unsafe { TPM_Manufacture(first_time) };
5557
match result {
5658
// TPM manufactured successfully
@@ -96,6 +98,9 @@ impl TcgTpmSimulatorInterface for TcgTpm {
9698
let mut response_ffi_p = response_ffi.as_mut_ptr();
9799
let mut response_ffi_size = TPM_BUFFER_MAX_SIZE as u32;
98100

101+
// SAFETY: FFI calls. Parameters are checked. Both calls are void,
102+
// _plat__RunCommand() returns `response_ffi_size` value by reference
103+
// and it is validated.
99104
unsafe {
100105
_plat__LocalitySet(locality);
101106
_plat__RunCommand(
@@ -128,10 +133,20 @@ impl TcgTpmSimulatorInterface for TcgTpm {
128133
return Err(SvsmReqError::invalid_request());
129134
}
130135
if !only_reset {
131-
unsafe { _plat__Signal_PowerOn() };
136+
// SAFETY: FFI call. No parameter, return value is checked.
137+
let result = unsafe { _plat__Signal_PowerOn() };
138+
if result != 0 {
139+
log::error!("_plat__Signal_PowerOn failed rc={}", result);
140+
return Err(SvsmReqError::incomplete());
141+
}
132142
}
133143
// It calls TPM_init() within to indicate that a TPM2_Startup is required.
134-
unsafe { _plat__Signal_Reset() };
144+
// SAFETY: FFI call. No parameter, return value is checked.
145+
let result = unsafe { _plat__Signal_Reset() };
146+
if result != 0 {
147+
log::error!("_plat__Signal_Reset failed rc={}", result);
148+
return Err(SvsmReqError::incomplete());
149+
}
135150
self.is_powered_on = true;
136151

137152
Ok(())
@@ -141,6 +156,7 @@ impl TcgTpmSimulatorInterface for TcgTpm {
141156
if !self.is_powered_on {
142157
return Err(SvsmReqError::invalid_request());
143158
}
159+
// SAFETY: FFI call. No Parameters or return values.
144160
unsafe { _plat__SetNvAvail() };
145161

146162
Ok(())
@@ -162,10 +178,16 @@ impl VtpmInterface for TcgTpm {
162178
// 5. Power it on indicating it requires startup. By default, OVMF will start
163179
// and selftest it.
164180

165-
unsafe { _plat__NVEnable(VirtAddr::null().as_mut_ptr::<c_void>(), 0) };
181+
// SAFETY: FFI call. Parameters and return values are checked.
182+
let mut rc = unsafe { _plat__NVEnable(VirtAddr::null().as_mut_ptr::<c_void>(), 0) };
183+
if rc != 0 {
184+
log::error!("_plat__NVEnable failed rc={}", rc);
185+
return Err(SvsmReqError::incomplete());
186+
}
166187

167-
let mut rc = self.manufacture(1)?;
188+
rc = self.manufacture(1)?;
168189
if rc != 0 {
190+
// SAFETY: FFI call. Parameter checked, no return value.
169191
unsafe { _plat__NVDisable(1 as *mut c_void, 0) };
170192
return Err(SvsmReqError::incomplete());
171193
}

0 commit comments

Comments
 (0)