Skip to content

Commit c93741e

Browse files
authored
Merge pull request #397 from jbaublitz/follow-up-fixes
Follow up fixes for reencryption API
2 parents 8bdc77b + f7886fc commit c93741e

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/luks2/reencrypt.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a> CryptLuks2ReencryptHandle<'a> {
111111
name: Option<&str>,
112112
passphrase: &[u8],
113113
keyslot_old: Option<c_uint>,
114-
keyslot_new: c_uint,
114+
keyslot_new: Option<c_uint>,
115115
cipher_and_mode: Option<(&str, &str)>,
116116
params: CryptParamsReencrypt,
117117
) -> Result<c_int, LibcryptErr> {
@@ -141,7 +141,7 @@ impl<'a> CryptLuks2ReencryptHandle<'a> {
141141
to_byte_ptr!(passphrase),
142142
passphrase.len(),
143143
keyslot_old.map(|k| k as c_int).unwrap_or(CRYPT_ANY_SLOT),
144-
keyslot_new as c_int,
144+
keyslot_new.map(|k| k as c_int).unwrap_or(CRYPT_ANY_SLOT),
145145
// NOTE: Must keep as_ref to avoid use after free error.
146146
cipher_cstring
147147
.as_ref()
@@ -163,20 +163,27 @@ impl<'a> CryptLuks2ReencryptHandle<'a> {
163163
name: Option<&str>,
164164
key_description: &str,
165165
keyslot_old: Option<c_uint>,
166-
keyslot_new: c_uint,
167-
cipher_and_mode: (&str, &str),
166+
keyslot_new: Option<c_uint>,
167+
cipher_and_mode: Option<(&str, &str)>,
168168
params: CryptParamsReencrypt,
169169
) -> Result<c_int, LibcryptErr> {
170170
let name_cstring = match name {
171171
Some(n) => Some(to_cstring!(n)?),
172172
None => None,
173173
};
174-
let (cipher, cipher_mode) = cipher_and_mode;
174+
let (cipher_cstring_err, cipher_mode_cstring_err) = cipher_and_mode
175+
.map(|(c, cm)| {
176+
(
177+
Some(to_cstring!(c)).transpose(),
178+
Some(to_cstring!(cm)).transpose(),
179+
)
180+
})
181+
.unwrap_or_else(|| (Ok(None), Ok(None)));
182+
let cipher_cstring = cipher_cstring_err?;
183+
let cipher_mode_cstring = cipher_mode_cstring_err?;
175184
let params_reencrypt: CryptParamsReencryptRef<'_> = (&params).try_into()?;
176185

177186
let description_cstring = to_cstring!(key_description)?;
178-
let cipher_cstring = to_cstring!(cipher)?;
179-
let cipher_mode_cstring = to_cstring!(cipher_mode)?;
180187
errno_int_success!(mutex!(
181188
libcryptsetup_rs_sys::crypt_reencrypt_init_by_keyring(
182189
self.reference.as_ptr(),
@@ -186,9 +193,17 @@ impl<'a> CryptLuks2ReencryptHandle<'a> {
186193
.unwrap_or(ptr::null()),
187194
description_cstring.as_ptr(),
188195
keyslot_old.map(|k| k as c_int).unwrap_or(CRYPT_ANY_SLOT),
189-
keyslot_new as c_int,
190-
cipher_cstring.as_ptr(),
191-
cipher_mode_cstring.as_ptr(),
196+
keyslot_new.map(|k| k as c_int).unwrap_or(CRYPT_ANY_SLOT),
197+
// NOTE: Must keep as_ref to avoid use after free error.
198+
cipher_cstring
199+
.as_ref()
200+
.map(|s| s.as_ptr())
201+
.unwrap_or_else(ptr::null),
202+
// NOTE: Must keep as_ref to avoid use after free error.
203+
cipher_mode_cstring
204+
.as_ref()
205+
.map(|s| s.as_ptr())
206+
.unwrap_or_else(ptr::null),
192207
params_reencrypt.as_ptr(),
193208
)
194209
))

0 commit comments

Comments
 (0)