Skip to content

Commit

Permalink
feat(isolation): move flags, use POSIX errors
Browse files Browse the repository at this point in the history
Depending on the error, we return EINVAL (like in the kernel itself)
when O_DIRECTORY is used together with O_CREAT, EIO when the kernel
requests to open a file that does not have a UTF-8 filename, and
-ENOENT when the file is not present in the file map.

The sysopen flags were moved to uhyve-interface for now.
  • Loading branch information
n0toose committed Nov 25, 2024
1 parent 42e1818 commit 0cf67ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
14 changes: 0 additions & 14 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,3 @@ pub const UHYVE_QUEUE_SIZE: usize = 8;
pub const UHYVE_IRQ_NET: u32 = 11;

pub const GUEST_PAGE_SIZE: u64 = 0x200000; /* 2 MB pages in guest */

// File operations supported by Hermit and Uhyve
pub const O_RDONLY: i32 = 0o0000;
pub const O_WRONLY: i32 = 0o0001;
pub const O_RDWR: i32 = 0o0002;
pub const O_CREAT: i32 = 0o0100;
pub const O_EXCL: i32 = 0o0200;
pub const O_TRUNC: i32 = 0o1000;
pub const O_APPEND: i32 = 0o2000;
pub const O_DIRECT: i32 = 0o40000;
pub const O_DIRECTORY: i32 = 0o200000;

pub const ALLOWED_OPEN_FLAGS: i32 =
O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_TRUNC | O_APPEND | O_DIRECT | O_DIRECTORY;
11 changes: 6 additions & 5 deletions src/hypercall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use std::{
os::unix::ffi::OsStrExt,
};

use libc::{EINVAL, EIO, ENOENT};
use tempfile::TempDir;
use uhyve_interface::{parameters::*, GuestPhysAddr, Hypercall, HypercallAddress, MAX_ARGC_ENVC};

use crate::{
consts::{ALLOWED_OPEN_FLAGS, BOOT_PML4, O_CREAT, O_DIRECTORY, O_EXCL},
consts::BOOT_PML4,
isolation::UhyveFileMap,
mem::{MemoryError, MmapMemory},
virt_to_phys,
Expand Down Expand Up @@ -97,11 +98,11 @@ pub fn unlink(mem: &MmapMemory, sysunlink: &mut UnlinkParams, file_map: &mut Uhy
}
} else {
error!("The kernel requested to unlink() an unknown path ({guest_path}): Rejecting...");
sysunlink.ret = -1;
sysunlink.ret = -ENOENT;
}
} else {
error!("The kernel requested to open() a path that is not valid UTF-8. Rejecting...");
sysunlink.ret = -1;
sysunlink.ret = -EIO;
}
}

Expand Down Expand Up @@ -142,7 +143,7 @@ pub fn open(
// See: https://github.com/hermit-os/kernel/commit/71bc629
if (flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT) {
error!("An open() call used O_DIRECTORY and O_CREAT at the same time. Aborting...");
sysopen.ret = -1
sysopen.ret = -EINVAL
}

// Existing files that already exist should be in the file map, not here.
Expand All @@ -162,7 +163,7 @@ pub fn open(
}
} else {
error!("The kernel requested to open() a path that is not valid UTF-8. Rejecting...");
sysopen.ret = -1;
sysopen.ret = -EINVAL;
}
}

Expand Down
14 changes: 14 additions & 0 deletions uhyve-interface/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,17 @@ pub struct SerialWriteBufferParams {
pub buf: GuestPhysAddr,
pub len: usize,
}

// File operations supported by Hermit and Uhyve
pub const O_RDONLY: i32 = 0o0000;
pub const O_WRONLY: i32 = 0o0001;
pub const O_RDWR: i32 = 0o0002;
pub const O_CREAT: i32 = 0o0100;
pub const O_EXCL: i32 = 0o0200;
pub const O_TRUNC: i32 = 0o1000;
pub const O_APPEND: i32 = 0o2000;
pub const O_DIRECT: i32 = 0o40000;
pub const O_DIRECTORY: i32 = 0o200000;

pub const ALLOWED_OPEN_FLAGS: i32 =
O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_TRUNC | O_APPEND | O_DIRECT | O_DIRECTORY;

0 comments on commit 0cf67ba

Please sign in to comment.