1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
extern crate vm_memory;
use std::fmt;
use std::io::{Read, Seek};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use vm_memory::ByteValued;
use vm_memory::{Address, Bytes, GuestAddress, GuestMemory, GuestUsize};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub use crate::loader_gen::bootparam;
pub use crate::cmdline::Cmdline;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod x86_64;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub use x86_64::*;
#[cfg(target_arch = "aarch64")]
mod aarch64;
#[cfg(target_arch = "aarch64")]
pub use aarch64::*;
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
Bzimage(bzimage::Error),
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
Elf(elf::Error),
#[cfg(all(feature = "pe", target_arch = "aarch64"))]
Pe(pe::Error),
InvalidCommandLine,
CommandLineCopy,
CommandLineOverflow,
InvalidKernelStartAddress,
MemoryOverflow,
}
pub type Result<T> = std::result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let desc = match self {
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
Error::Bzimage(ref _e) => "failed to load bzImage kernel image",
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
Error::Elf(ref _e) => "failed to load ELF kernel image",
#[cfg(all(feature = "pe", target_arch = "aarch64"))]
Error::Pe(ref _e) => "failed to load PE kernel image",
Error::InvalidCommandLine => "invalid command line provided",
Error::CommandLineCopy => "failed writing command line to guest memory",
Error::CommandLineOverflow => "command line overflowed guest memory",
Error::InvalidKernelStartAddress => "invalid kernel start address",
Error::MemoryOverflow => "memory to load kernel image is not enough",
};
write!(f, "Kernel Loader: {}", desc)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
Error::Bzimage(ref e) => Some(e),
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
Error::Elf(ref e) => Some(e),
#[cfg(all(feature = "pe", target_arch = "aarch64"))]
Error::Pe(ref e) => Some(e),
Error::InvalidCommandLine => None,
Error::CommandLineCopy => None,
Error::CommandLineOverflow => None,
Error::InvalidKernelStartAddress => None,
Error::MemoryOverflow => None,
}
}
}
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
impl From<elf::Error> for Error {
fn from(err: elf::Error) -> Self {
Error::Elf(err)
}
}
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
impl From<bzimage::Error> for Error {
fn from(err: bzimage::Error) -> Self {
Error::Bzimage(err)
}
}
#[cfg(all(feature = "pe", target_arch = "aarch64"))]
impl From<pe::Error> for Error {
fn from(err: pe::Error) -> Self {
Error::Pe(err)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct KernelLoaderResult {
pub kernel_load: GuestAddress,
pub kernel_end: GuestUsize,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub setup_header: Option<bootparam::setup_header>,
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
pub pvh_boot_cap: elf::PvhBootCapability,
}
pub trait KernelLoader {
fn load<F, M: GuestMemory>(
guest_mem: &M,
kernel_offset: Option<GuestAddress>,
kernel_image: &mut F,
highmem_start_address: Option<GuestAddress>,
) -> Result<KernelLoaderResult>
where
F: Read + Seek;
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
unsafe impl ByteValued for bootparam::setup_header {}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
unsafe impl ByteValued for bootparam::boot_params {}
pub fn load_cmdline<M: GuestMemory>(
guest_mem: &M,
guest_addr: GuestAddress,
cmdline: &Cmdline,
) -> Result<()> {
let cmdline_string = cmdline
.as_cstring()
.map_err(|_| Error::InvalidCommandLine)?;
let cmdline_bytes = cmdline_string.as_bytes_with_nul();
let end = guest_addr
.checked_add((cmdline_bytes.len() - 1) as u64)
.ok_or(Error::CommandLineOverflow)?;
if end > guest_mem.last_addr() {
return Err(Error::CommandLineOverflow);
}
guest_mem
.write_slice(cmdline_bytes, guest_addr)
.map_err(|_| Error::CommandLineCopy)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use vm_memory::{Address, GuestAddress};
type GuestMemoryMmap = vm_memory::GuestMemoryMmap<()>;
const MEM_SIZE: u64 = 0x100_0000;
fn create_guest_mem() -> GuestMemoryMmap {
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
}
#[test]
fn test_cmdline_overflow() {
let gm = create_guest_mem();
let mut cl = Cmdline::new(10);
cl.insert_str("12345").unwrap();
let cmdline_address = GuestAddress(u64::MAX - 5);
assert_eq!(
Err(Error::CommandLineOverflow),
load_cmdline(&gm, cmdline_address, &cl)
);
let cmdline_address = GuestAddress(MEM_SIZE - 5);
assert_eq!(
Err(Error::CommandLineOverflow),
load_cmdline(&gm, cmdline_address, &cl)
);
let cmdline_address = GuestAddress(MEM_SIZE - 6);
assert!(load_cmdline(&gm, cmdline_address, &cl).is_ok());
}
#[test]
fn test_cmdline_write_end_regresion() {
let gm = create_guest_mem();
let mut cmdline_address = GuestAddress(45);
let sample_buf = &[1; 100];
gm.write(sample_buf, cmdline_address).unwrap();
let mut cl = Cmdline::new(10);
load_cmdline(&gm, cmdline_address, &cl).unwrap();
let val: u8 = gm.read_obj(cmdline_address).unwrap();
assert_eq!(val, b'\0');
cl.insert_str("123").unwrap();
load_cmdline(&gm, cmdline_address, &cl).unwrap();
let val: u8 = gm.read_obj(cmdline_address).unwrap();
assert_eq!(val, b'1');
cmdline_address = cmdline_address.unchecked_add(1);
let val: u8 = gm.read_obj(cmdline_address).unwrap();
assert_eq!(val, b'2');
cmdline_address = cmdline_address.unchecked_add(1);
let val: u8 = gm.read_obj(cmdline_address).unwrap();
assert_eq!(val, b'3');
cmdline_address = cmdline_address.unchecked_add(1);
let val: u8 = gm.read_obj(cmdline_address).unwrap();
assert_eq!(val, b'\0');
}
}