Skip to content

Commit 57c59dc

Browse files
blenessyslp
authored andcommitted
Introduce the krun_set_data_disk API.
This API adds the possibility to introduce a second block device, to a TEE. It is assumed that the root disk contains a symmetric key (secret), and code to encrypt/decrypt the data disk before use. The recommended setup is to included cryptsetup on the root disk and use that to safely access the data disk. Signed-off-by: Zalan Blenessy <[email protected]>
1 parent 7412f15 commit 57c59dc

File tree

7 files changed

+87
-14
lines changed

7 files changed

+87
-14
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ LIBRARY_HEADER = include/libkrun.h
22
INIT_BINARY = init/init
33

44
ABI_VERSION=1
5-
FULL_VERSION=1.4.10
5+
FULL_VERSION=1.5.0
66

77
ifeq ($(SEV),1)
88
VARIANT = -sev

examples/launch-tee.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ int main(int argc, char *const argv[])
3838
int err;
3939
int i;
4040

41-
if (argc != 3) {
41+
if (argc < 3 || argc > 4) {
4242
printf("Invalid arguments\n");
43-
printf("Usage: %s DISK_IMAGE TEE_CONFIG_FILE\n", argv[0]);
43+
printf("Usage: %s ROOT_DISK_IMAGE TEE_CONFIG_FILE [DATA_DISK_IMAGE]\n", argv[0]);
4444
return -1;
4545
}
4646

@@ -74,6 +74,15 @@ int main(int argc, char *const argv[])
7474
return -1;
7575
}
7676

77+
// Use the third (optional) command line argument as the disk image containing a data disk.
78+
if (argc > 3) {
79+
if (err = krun_set_data_disk(ctx_id, argv[3])) {
80+
errno = -err;
81+
perror("Error configuring data disk image");
82+
return -1;
83+
}
84+
}
85+
7786
if (getcwd(&current_path[0], MAX_PATH) == NULL) {
7887
errno = -err;
7988
perror("Error getting current directory");

include/libkrun.h

+14
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ int32_t krun_set_root(uint32_t ctx_id, const char *root_path);
7575
*/
7676
int32_t krun_set_root_disk(uint32_t ctx_id, const char *disk_path);
7777

78+
/*
79+
* Sets the path to the disk image that contains the file-system to be used as a data partition for the microVM.
80+
* The only supported image format is "raw". Only available in libkrun-SEV.
81+
*
82+
* Arguments:
83+
* "ctx_id" - the configuration context ID.
84+
* "disk_path" - a null-terminated string representing the path leading to the disk image that
85+
* contains the root file-system.
86+
*
87+
* Returns:
88+
* Zero on success or a negative error number on failure.
89+
*/
90+
int32_t krun_set_data_disk(uint32_t ctx_id, const char *disk_path);
91+
7892
/*
7993
* Configures the mapped volumes for the microVM. Only supported on macOS, on Linux use
8094
* user_namespaces and bind-mounts instead. Not available in libkrun-SEV.

src/libkrun/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libkrun"
3-
version = "1.4.10"
3+
version = "1.5.0"
44
authors = ["Sergio Lopez <[email protected]>"]
55
edition = "2021"
66
build = "build.rs"

src/libkrun/src/lib.rs

+58-8
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ struct ContextConfig {
5555
#[cfg(not(feature = "tee"))]
5656
fs_cfg: Option<FsDeviceConfig>,
5757
#[cfg(feature = "tee")]
58-
block_cfg: Option<BlockDeviceConfig>,
58+
root_block_cfg: Option<BlockDeviceConfig>,
59+
#[cfg(feature = "tee")]
60+
data_block_cfg: Option<BlockDeviceConfig>,
5961
port_map: Option<HashMap<u16, u16>>,
6062
#[cfg(feature = "tee")]
6163
tee_config_file: Option<PathBuf>,
@@ -128,13 +130,23 @@ impl ContextConfig {
128130
}
129131

130132
#[cfg(feature = "tee")]
131-
fn set_block_cfg(&mut self, block_cfg: BlockDeviceConfig) {
132-
self.block_cfg = Some(block_cfg);
133+
fn set_root_block_cfg(&mut self, block_cfg: BlockDeviceConfig) {
134+
self.root_block_cfg = Some(block_cfg);
135+
}
136+
137+
#[cfg(feature = "tee")]
138+
fn get_root_block_cfg(&self) -> Option<BlockDeviceConfig> {
139+
self.root_block_cfg.clone()
140+
}
141+
142+
#[cfg(feature = "tee")]
143+
fn set_data_block_cfg(&mut self, block_cfg: BlockDeviceConfig) {
144+
self.data_block_cfg = Some(block_cfg);
133145
}
134146

135147
#[cfg(feature = "tee")]
136-
fn get_block_cfg(&self) -> Option<BlockDeviceConfig> {
137-
self.block_cfg.clone()
148+
fn get_data_block_cfg(&self) -> Option<BlockDeviceConfig> {
149+
self.data_block_cfg.clone()
138150
}
139151

140152
fn set_port_map(&mut self, port_map: HashMap<u16, u16>) {
@@ -397,7 +409,37 @@ pub unsafe extern "C" fn krun_set_root_disk(ctx_id: u32, c_disk_path: *const c_c
397409
is_disk_read_only: false,
398410
is_disk_root: true,
399411
};
400-
cfg.set_block_cfg(block_device_config);
412+
cfg.set_root_block_cfg(block_device_config);
413+
}
414+
Entry::Vacant(_) => return -libc::ENOENT,
415+
}
416+
417+
KRUN_SUCCESS
418+
}
419+
420+
#[allow(clippy::missing_safety_doc)]
421+
#[no_mangle]
422+
#[cfg(feature = "tee")]
423+
pub unsafe extern "C" fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_char) -> i32 {
424+
let disk_path = match CStr::from_ptr(c_disk_path).to_str() {
425+
Ok(disk) => disk,
426+
Err(_) => return -libc::EINVAL,
427+
};
428+
429+
//let fs_id = "/dev/root".to_string();
430+
//let shared_dir = root_path.to_string();
431+
432+
match CTX_MAP.lock().unwrap().entry(ctx_id) {
433+
Entry::Occupied(mut ctx_cfg) => {
434+
let cfg = ctx_cfg.get_mut();
435+
let block_device_config = BlockDeviceConfig {
436+
block_id: "data".to_string(),
437+
cache_type: CacheType::Writeback,
438+
disk_image_path: disk_path.to_string(),
439+
is_disk_read_only: false,
440+
is_disk_root: false,
441+
};
442+
cfg.set_data_block_cfg(block_device_config);
401443
}
402444
Entry::Vacant(_) => return -libc::ENOENT,
403445
}
@@ -660,8 +702,16 @@ pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {
660702
}
661703

662704
#[cfg(feature = "tee")]
663-
if let Some(block_cfg) = ctx_cfg.get_block_cfg() {
664-
if ctx_cfg.vmr.set_block_device(block_cfg).is_err() {
705+
if let Some(block_cfg) = ctx_cfg.get_root_block_cfg() {
706+
if ctx_cfg.vmr.add_block_device(block_cfg).is_err() {
707+
error!("Error configuring virtio-blk");
708+
return -libc::EINVAL;
709+
}
710+
}
711+
712+
#[cfg(feature = "tee")]
713+
if let Some(block_cfg) = ctx_cfg.get_data_block_cfg() {
714+
if ctx_cfg.vmr.add_block_device(block_cfg).is_err() {
665715
error!("Error configuring virtio-blk");
666716
return -libc::EINVAL;
667717
}

src/vmm/src/resources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl VmResources {
229229
}
230230

231231
#[cfg(feature = "tee")]
232-
pub fn set_block_device(&mut self, config: BlockDeviceConfig) -> Result<BlockConfigError> {
232+
pub fn add_block_device(&mut self, config: BlockDeviceConfig) -> Result<BlockConfigError> {
233233
self.block.insert(config)
234234
}
235235

0 commit comments

Comments
 (0)