Skip to content

Commit

Permalink
fix(vfs): read_all
Browse files Browse the repository at this point in the history
  • Loading branch information
llenotre committed Aug 27, 2024
1 parent 1337edc commit 9286750
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
26 changes: 23 additions & 3 deletions kernel/src/file/vfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::{
use core::{
borrow::Borrow,
hash::{Hash, Hasher},
intrinsics::unlikely,
};
use node::Node;
use utils::{
Expand Down Expand Up @@ -126,27 +127,46 @@ impl Entry {
}

/// Reads the whole content of the file into a buffer.
///
/// **Caution**: the function reads until EOF, meaning the caller should not call this function
/// on an infinite file.
pub fn read_all(&self) -> EResult<Vec<u8>> {
const INCREMENT: usize = 512;
let len: usize = self
.node
.ops
.get_stat(&self.node.location)?
.size
.try_into()
.map_err(|_| errno!(EOVERFLOW))?;
let len = len
.checked_add(INCREMENT)
.ok_or_else(|| errno!(EOVERFLOW))?;
// Add more space to allow check for EOF
let mut buf = vec![0u8; len]?;
let mut off = 0;
// Stick to the file's size to have an upper bound
while off < len {
// Read until EOF
loop {
// If the size has been exceeded, resize the buffer
if off >= buf.len() {
let new_size = buf
.len()
.checked_add(INCREMENT)
.ok_or_else(|| errno!(EOVERFLOW))?;
buf.resize(new_size, 0)?;
}
let len =
self.node
.ops
.read_content(&self.node.location, off as _, &mut buf[off..])?;
// Reached EOF, stop here
if len == 0 {
break;
}
off += len;
}
// Adjust the size of the buffer
buf.truncate(off);
Ok(buf)
}

Expand Down Expand Up @@ -343,7 +363,7 @@ fn resolve_link(
symlink_rec: usize,
) -> EResult<Arc<Entry>> {
// If too many recursions occur, error
if symlink_rec + 1 > limits::SYMLOOP_MAX {
if unlikely(symlink_rec + 1 > limits::SYMLOOP_MAX) {
return Err(errno!(ELOOP));
}
// Read link
Expand Down
10 changes: 7 additions & 3 deletions kernel/src/process/exec/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
relocation::{ELF32Rel, ELF32Rela, Relocation, GOT_SYM},
ELF32ProgramHeader,
},
file::{path::Path, perm::AccessProfile, vfs},
file::{path::Path, perm::AccessProfile, vfs, FileType},
memory,
memory::vmem,
process,
Expand All @@ -39,6 +39,7 @@ use crate::{
use core::{
cmp::{max, min},
ffi::c_void,
intrinsics::unlikely,
iter,
mem::size_of,
num::NonZeroUsize,
Expand Down Expand Up @@ -278,8 +279,11 @@ fn build_auxiliary(
fn read_exec_file(file: &vfs::Entry, ap: &AccessProfile) -> EResult<Vec<u8>> {
// Check that the file can be executed by the user
let stat = file.get_stat()?;
if !ap.can_execute_file(&stat) {
return Err(errno!(ENOEXEC));
if unlikely(stat.get_type() != Some(FileType::Regular)) {
return Err(errno!(EACCES));
}
if unlikely(!ap.can_execute_file(&stat)) {
return Err(errno!(EACCES));
}
file.read_all()
}
Expand Down
4 changes: 0 additions & 4 deletions kernel/src/syscall/execve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ fn build_image(
argv: Vec<String>,
envp: Vec<String>,
) -> EResult<ProgramImage> {
let stat = file.get_stat()?;
if !path_resolution.access_profile.can_execute_file(&stat) {
return Err(errno!(EACCES));
}
let exec_info = ExecInfo {
path_resolution,
argv,
Expand Down

0 comments on commit 9286750

Please sign in to comment.