@@ -95,16 +95,20 @@ int32_t krun_set_root_disk(uint32_t ctx_id, const char *disk_path);
95
95
int32_t krun_set_data_disk (uint32_t ctx_id , const char * disk_path );
96
96
97
97
/**
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".
99
100
*
100
101
* This API is mutually exclusive with the deprecated krun_set_root_disk and
101
102
* krun_set_data_disk methods and must not be used together.
102
103
*
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
+ *
103
108
* Arguments:
104
109
* "ctx_id" - the configuration context ID.
105
110
* "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.
108
112
* "read_only" - whether the mount should be read-only. Required if the caller does not have
109
113
* write permissions (for disk images in /usr/share).
110
114
*
@@ -113,6 +117,59 @@ int32_t krun_set_data_disk(uint32_t ctx_id, const char *disk_path);
113
117
*/
114
118
int32_t krun_add_disk (uint32_t ctx_id , const char * block_id , const char * disk_path , bool read_only );
115
119
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
+
116
173
/**
117
174
* NO LONGER SUPPORTED. DO NOT USE.
118
175
*
0 commit comments