-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
345 lines (320 loc) · 11.8 KB
/
lib.rs
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
pub mod lkl;
pub use lkl::syscall_wrappers::*;
use nix::unistd::close;
use std::fs::File;
use std::os::unix::io::{IntoRawFd, RawFd};
/// construct this with LklSetup::new()
pub struct LklSetup {
pub disk: lkl_disk,
partition: u32,
disk_id: u32,
pub mount_point: String,
file: RawFd,
}
impl LklSetup {
/// new parses the settings and initializes the kernel
pub fn new(arg: LklSetupArgs) -> Result<LklSetup, &'static str> {
let file = match File::options().read(true).write(true).open(arg.filename) {
Err(e) => {
panic!("Error opening {:}", e);
}
Ok(k) => k.into_raw_fd(),
};
let mut disk = lkl_disk {
dev: 0,
fd: file as i32,
ops: 0,
};
let boot_arg = match arg.boot_settings {
Some(k) => to_cstr(&k)
.expect("boot_settings formats null bytes wrong")
.as_ptr()
.cast(),
None => to_cstr("mem=128M loglevel=8\0").unwrap().as_ptr().cast(),
};
let disk_id;
fn on_panic() {
println!("an oopsie happened");
}
unsafe {
lkl_host_ops.panic = match arg.on_panic {
Some(k) => (k as *const fn()) as c_ulong,
None => (on_panic as *const fn()) as c_ulong,
};
if arg.print.is_some() {
lkl_host_ops.print = (arg.print.unwrap() as *const fn()) as c_ulong;
}
lkl_init(&lkl_host_ops);
disk_id = lkl_disk_add(&mut disk) as u32;
lkl_start_kernel(&lkl_host_ops, boot_arg);
}
if (disk_id as i32) < 0 {
eprintln!("Error adding disk:");
let _reason = match strerror(&(disk_id as i32)) {
Ok(k) => {
eprintln!("{:}", k);
k
}
Err(_) => {
eprintln!("Unparseable error string");
"unknown string"
}
};
unsafe {
lkl_sys_halt();
}
return Err("Couldn't add disk");
}
let partition = arg.partition_num.unwrap_or(0);
let fs_type = &arg.filesystem_type.unwrap_or("ext4".to_string()).to_owned()[..];
let default_options = match fs_type {
"ext4" => "errors=remount-ro\0",
"btrfs" => "thread_pool=1\0",
"gfs2" => "acl\0",
"reiserfs" => "acl,user_xattr\0",
&_ => "\0",
};
let mut fs_type_with_nul = String::from(fs_type);
fs_type_with_nul.push_str("\0");
let mount_options = arg
.filesystem_options
.unwrap_or(default_options.to_string());
let msize: u32 = 100;
let mut mpoint = vec![0u8; msize as usize];
let ret;
unsafe {
ret = lkl_mount_dev(
disk_id,
partition,
to_cstr(&fs_type_with_nul)
.expect("filesystem has incorrect nulls")
.as_ptr()
.cast(),
0,
to_cstr(&mount_options)
.expect("mount options has incorrect nulls")
.as_ptr()
.cast(),
mpoint.as_mut_ptr().cast(),
msize,
) as i32;
}
if ret < 0 {
eprintln!("Error lkl_mount_dev:");
print_error(&ret);
unsafe {
lkl_sys_halt();
}
return Err("lkl_mount_dev failed");
}
let mount_point = String::from_utf8(mpoint).unwrap();
println!("[*] Filesystem mounted at {:}", mount_point);
let full = String::from(mount_point);
// removing trailing null bytes except leave one
let mount_point = &full[0..full.find("\0").unwrap_or(0) + 1];
let mut params = [ptr::null::<c_ulong>(); 5];
params[0] = to_cstr(&mount_point)
.expect("mount point has null")
.as_ptr()
.cast::<c_ulong>();
let r;
unsafe {
r = lkl_syscall(
__lkl__NR_chdir as i64,
ptr::addr_of_mut!(params).cast::<c_long>(),
);
}
if r < 0 {
return Err("Can't chdir to moint point corrupted filesystem");
}
// return string without null byte
let mount_point = &mount_point[0..mount_point.len() - 1];
Ok(LklSetup {
disk: disk,
partition: partition,
disk_id: disk_id,
mount_point: mount_point.to_owned(),
file: file,
})
}
}
/// Unmounts the disk for LKL then removes it and stops the kernel
impl Drop for LklSetup {
fn drop(&mut self) {
close(self.file).unwrap();
unsafe {
lkl_umount_dev(self.disk_id, self.partition, 0, 1000) as i32;
lkl_disk_remove(self.disk);
lkl_sys_halt();
}
}
}
/// Due to ownership automatically deallocating fds out of scope the file descriptor for
/// the disk image needs to be passed in.
/// Next is the boot settings which can be the amount of memory and log level
/// (i.e. mem=128M loglevel=8
/// on_panic is the function that runs when there is a panic and print replaces printk
pub struct LklSetupArgs {
pub filename: String,
pub boot_settings: Option<String>,
pub partition_num: Option<u32>,
pub filesystem_type: Option<String>,
pub filesystem_options: Option<String>,
pub on_panic: Option<fn()>,
pub print: Option<fn()>,
}
fn setup_test() -> LklSetup {
// pass in the file descriptor so the library can read and write
// the changes to disk:
LklSetup::new(LklSetupArgs {
filename: String::from("ext4-00.img"),
boot_settings: None,
partition_num: None,
filesystem_type: Some("ext4".to_string()),
filesystem_options: None,
on_panic: None,
print: None,
})
.unwrap()
}
#[cfg(test)]
mod tests {
use crate::*;
use more_asserts as ma;
use std::mem;
#[test]
fn separate_later() {
let server = setup_test();
const BUF_LEN: usize = 26;
const MSG: &str = "that's what i call riddim\0";
let mut mpoint = server.mount_point.clone();
mpoint.push_str("/test591\0");
let filename = to_cstr(&mpoint).unwrap();
// open a file in the mounted filesystem - make sure to use null bytes to terminate CStrings
let mut r = lkl_sys_open(&filename, LKL_O_RDWR | LKL_O_CREAT, 0o755);
ma::assert_ge!(r, 0);
let fd = r as i32;
let buf = MSG.as_bytes();
r = lkl_sys_write(fd, buf, BUF_LEN);
assert_eq!(r as usize, BUF_LEN);
r = lkl_sys_close(fd);
assert_eq!(r, 0);
let mut read_buf = [0 as u8; BUF_LEN];
let readfd = lkl_sys_open(&filename, LKL_O_RDONLY, 0) as i32;
ma::assert_ge!(r, 0);
// reading back our message from the file we wrote to:
r = lkl_sys_read(readfd, &mut read_buf, BUF_LEN);
assert_eq!(r as usize, BUF_LEN);
assert_eq!(MSG, String::from_utf8(read_buf.to_vec()).unwrap());
let mut stat = stat {
..Default::default()
};
let r = lkl_sys_fstat(readfd, &mut stat);
const S_IFREG: u64 = 0o0100000;
const S_IFMT: u64 = 0o0170000;
const S_IFDIR: u64 = 0o0040000;
// check if it is a regular file
assert_eq!((stat.st_mode & S_IFMT), S_IFREG);
// confirm it is not a directory
assert_ne!((stat.st_mode & S_IFMT), S_IFDIR);
ma::assert_ge!(r, 0);
let mut ruid = 12;
let mut euid = 12;
let mut suid = 12;
// get real, effective, and saved user IDs
lkl_sys_getresuid(&mut ruid, &mut euid, &mut suid);
assert_ne!(suid, 12);
assert_eq!(stat.st_uid, ruid);
let mut rgid = 12;
let mut egid = 12;
let mut sgid = 12;
lkl_sys_getresgid(&mut rgid, &mut egid, &mut sgid);
assert_ne!(sgid, 12);
assert_eq!(stat.st_gid, rgid);
assert_eq!(egid, 0);
assert_eq!(euid, 0);
const SEEK_SET: u32 = 0;
let s_offset: u32 = 3;
// seek should return offset if successful
let r = lkl_sys_lseek(readfd, s_offset, SEEK_SET) as u32;
assert_eq!(r, s_offset);
/*mmap always fails??
const PAGE_SIZE: usize = 0x1000;
const PROT_READ: i32 = 1;
const MAP_SHARED: i32 = 4;
let mut page = [0; PAGE_SIZE];
// this works in C fine but no setup of mmap works for some reason
// idk whether to use &mut [u8] or u64 for the address
let ptr = lkl_sys_mmap(
0x5b0000, 0x1000, 0x1|0x2, 0x10|0x20|0x02,-1, 0);
print_error(&(ptr as i32));
ma::assert_ge!(ptr, 0);*/
let mut dirp = dirent64 {
..Default::default()
};
let r = lkl_sys_getdents64(readfd, &mut dirp, mem::size_of::<dirent64>());
// not a directory
assert_eq!(r, -20);
let r = lkl_sys_close(readfd);
assert_eq!(r, 0);
let mut dirpath = server.mount_point.clone();
dirpath.push_str("/smh\0");
let dirpath = to_cstr(&dirpath).expect("invalid string for directory name");
let _fd = lkl_sys_open(&dirpath, LKL_O_DIRECTORY | LKL_O_RDONLY, 0) as i32;
let r = lkl_sys_mkdir(&dirpath, 0o755);
assert_eq!(r, 0);
let dirfd = lkl_sys_open(dirpath, LKL_O_DIRECTORY | LKL_O_RDONLY, 0) as i32;
ma::assert_ge!(dirfd, 0);
let r = lkl_sys_getdents64(dirfd, &mut dirp, mem::size_of::<dirent64>());
ma::assert_ge!(r, 0);
// pwrite64 test
let mut dirpath = server.mount_point.clone();
let mut mtpoint = dirpath.clone();
dirpath.push_str("/smh/lmao\0");
let dirpath = to_cstr(&dirpath).unwrap();
let writefd = lkl_sys_open(&dirpath, LKL_O_WRONLY | LKL_O_CREAT, 0o755) as i32;
let r = lkl_sys_pwrite64(writefd, buf, BUF_LEN, 0);
assert_eq!(r as usize, BUF_LEN);
lkl_sys_close(writefd);
// pread64 test
const OFFSET: usize = 5;
let mut read_buf = [0 as u8; BUF_LEN - OFFSET];
let readfd = lkl_sys_open(&dirpath, LKL_O_RDONLY, 0) as i32;
let r = lkl_sys_pread64(readfd, &mut read_buf, BUF_LEN - OFFSET, OFFSET as u64);
assert_eq!(r as usize, BUF_LEN - OFFSET);
//println!();
let read_str = String::from_utf8(read_buf.to_vec()).unwrap();
assert_eq!(String::from(&MSG[OFFSET..]), read_str);
mtpoint.push_str("/smh/blue\0");
let new_name = to_cstr(&mtpoint).unwrap();
let r = lkl_sys_rename(&dirpath, &new_name);
assert_eq!(r, 0);
let old_name_fd = lkl_sys_open(&dirpath, LKL_O_RDONLY, 0) as i32;
ma::assert_lt!(old_name_fd, 0);
let new_fd = lkl_sys_open(&new_name, LKL_O_RDONLY, 0) as i32;
ma::assert_ge!(new_fd, 0);
let r = lkl_sys_close(new_fd);
assert_eq!(r, 0);
let mut mntpt = server.mount_point.clone();
mntpt.push_str("\0");
let mntfd =
lkl_sys_open(to_cstr(&mntpt).unwrap(), LKL_O_RDONLY | LKL_O_DIRECTORY, 0) as i32;
let r = lkl_sys_unlinkat(mntfd, to_cstr("test591\0").unwrap(), 0);
//print_error(&(r as i32));
assert_eq!(r, 0);
let r = lkl_sys_unlinkat(dirfd, to_cstr("blue\0").unwrap(), 0);
assert_eq!(r, 0);
let r = lkl_sys_rmdir(to_cstr("smh\0").unwrap());
assert_eq!(r, 0);
let r = lkl_sys_fsync(readfd);
assert_eq!(r, 0);
let r = lkl_sys_fdatasync(dirfd);
assert_eq!(r, 0);
let r = lkl_sys_syncfs(readfd);
assert_eq!(r, 0);
let r = lkl_sys_close(readfd);
assert_eq!(r, 0);
let r = lkl_sys_close(dirfd);
assert_eq!(r, 0);
}
}