Skip to content

Commit e7a5397

Browse files
committed
Add krun_add_disk2 API
Adds the `krun_add_disk2` API that requires the user to specify the format of the disk image they're providing. The following formats are supported: - KRUN_DISK_FORMAT_RAW - KRUN_DISK_FORMAT_QCOW2 Signed-off-by: Jake Correnti <[email protected]>
1 parent 5176a82 commit e7a5397

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

include/libkrun.h

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,20 @@ int32_t krun_set_root_disk(uint32_t ctx_id, const char *disk_path);
9595
int32_t krun_set_data_disk(uint32_t ctx_id, const char *disk_path);
9696

9797
/**
98-
* Adds a disk image to be used as a general partition for the microVM.
98+
* Adds a disk image to be used as a general partition for the microVM. The only supported image
99+
* format is "raw".
99100
*
100101
* This API is mutually exclusive with the deprecated krun_set_root_disk and
101102
* krun_set_data_disk methods and must not be used together.
102103
*
104+
* This function deliberately only handles images in the Raw format, because it doesn't allow
105+
* specifying an image format, and probing an image's format is dangerous. For more information,
106+
* see the security note on `krun_add_disk2`, which allows opening non-Raw images.
107+
*
103108
* Arguments:
104109
* "ctx_id" - the configuration context ID.
105110
* "block_id" - a null-terminated string representing the partition.
106-
* "disk_path" - a null-terminated string representing the path leading to the disk image that
107-
* contains the root file-system.
111+
* "disk_path" - a null-terminated string representing the path leading to the disk image.
108112
* "read_only" - whether the mount should be read-only. Required if the caller does not have
109113
* write permissions (for disk images in /usr/share).
110114
*
@@ -113,6 +117,59 @@ int32_t krun_set_data_disk(uint32_t ctx_id, const char *disk_path);
113117
*/
114118
int32_t krun_add_disk(uint32_t ctx_id, const char *block_id, const char *disk_path, bool read_only);
115119

120+
/* Supported disk image formats */
121+
#define KRUN_DISK_FORMAT_RAW 0
122+
#define KRUN_DISK_FORMAT_QCOW2 1
123+
/**
124+
* Adds a disk image to be used as a general partition for the microVM. The supported
125+
* image formats are: "raw" and "qcow2".
126+
*
127+
* This API is mutually exclusive with the deprecated krun_set_root_disk and
128+
* krun_set_data_disk methods and must not be used together.
129+
*
130+
* SECURITY NOTE:
131+
* Non-Raw images can reference other files, which libkrun will automatically open, and to which the
132+
* guest will have access. Libkrun should therefore never be asked to open an image in a non-Raw
133+
* format when it doesn't come from a fully trustworthy source.
134+
*
135+
* Consequently, probing an image's format is quite dangerous and to be avoided if at all possible,
136+
* which is why libkrun provides no facilities for doing so. If it's not clear what format an image
137+
* has, it may also not be clear whether it can be trusted to not reference files to which the guest
138+
* shouldn't have access.
139+
*
140+
* If probing absolutely can't be avoided, it must only be done on images that are fully trusted, i.e.
141+
* before a potentially untrusted guest had write access to it. Specifically, consider that a guest has
142+
* full access to all of a Raw image, and can therefore turn it into a file in an arbitrary format, for
143+
* example, into a Qcow2 image, referencing and granting a malicious guest access to arbitrary files.
144+
* To hand a Raw image to an untrusted and potentially malicious guest, and then to re-probe it after
145+
* the guest was able to write to it (when it can no longer be trusted), would therefore be a severe
146+
* security vulnerability.
147+
*
148+
* Therefore, after having probed a yet fully trusted image once, the result must be remembered so the
149+
* image will from then on always be opened in the format that was detected originally. When adhering
150+
* to this, a guest can write anything they want to a Raw image, it's always going to be opened as a
151+
* Raw image, preventing the security vulnerability outlined above.
152+
*
153+
* However, if at all possible, the image format should be explicitly selected based on knowledge
154+
* obtained separately from the pure image data, for example by the user.
155+
*
156+
* Arguments:
157+
* "ctx_id" - the configuration context ID.
158+
* "block_id" - a null-terminated string representing the partition.
159+
* "disk_path" - a null-terminated string representing the path leading to the disk image.
160+
* "disk_format" - the disk image format (i.e. KRUN_DISK_FORMAT_{RAW, QCOW2})
161+
* "read_only" - whether the mount should be read-only. Required if the caller does not have
162+
* write permissions (for disk images in /usr/share).
163+
*
164+
* Returns:
165+
* Zero on success or a negative error number on failure.
166+
*/
167+
int32_t krun_add_disk2(uint32_t ctx_id,
168+
const char *block_id,
169+
const char *disk_path,
170+
uint32_t disk_format,
171+
bool read_only);
172+
116173
/**
117174
* NO LONGER SUPPORTED. DO NOT USE.
118175
*

src/libkrun/src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,53 @@ pub unsafe extern "C" fn krun_add_disk(
534534
KRUN_SUCCESS
535535
}
536536

537+
#[allow(clippy::missing_safety_doc)]
538+
#[no_mangle]
539+
#[cfg(feature = "blk")]
540+
pub unsafe extern "C" fn krun_add_disk2(
541+
ctx_id: u32,
542+
c_block_id: *const c_char,
543+
c_disk_path: *const c_char,
544+
disk_format: u32,
545+
read_only: bool,
546+
) -> i32 {
547+
let disk_path = match CStr::from_ptr(c_disk_path).to_str() {
548+
Ok(disk) => disk,
549+
Err(_) => return -libc::EINVAL,
550+
};
551+
552+
let block_id = match CStr::from_ptr(c_block_id).to_str() {
553+
Ok(block_id) => block_id,
554+
Err(_) => return -libc::EINVAL,
555+
};
556+
557+
let format = match disk_format {
558+
0 => ImageType::Raw,
559+
1 => ImageType::Qcow2,
560+
_ => {
561+
// Do not continue if the user cannot specify a valid disk format
562+
return -libc::EINVAL;
563+
}
564+
};
565+
566+
match CTX_MAP.lock().unwrap().entry(ctx_id) {
567+
Entry::Occupied(mut ctx_cfg) => {
568+
let cfg = ctx_cfg.get_mut();
569+
let block_device_config = BlockDeviceConfig {
570+
block_id: block_id.to_string(),
571+
cache_type: CacheType::Writeback,
572+
disk_image_path: disk_path.to_string(),
573+
disk_image_format: format,
574+
is_disk_read_only: read_only,
575+
};
576+
cfg.add_block_cfg(block_device_config);
577+
}
578+
Entry::Vacant(_) => return -libc::ENOENT,
579+
}
580+
581+
KRUN_SUCCESS
582+
}
583+
537584
#[allow(clippy::missing_safety_doc)]
538585
#[no_mangle]
539586
#[cfg(feature = "blk")]

0 commit comments

Comments
 (0)