Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow syscalls before libc is initialised, remove all existing early boot syscalls #2273

Merged
merged 9 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/kernel/memmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class Fixed_memory_range {
class Memory_map {
public:
using Key = uintptr_t;
using Map = std::map<Key, Fixed_memory_range>;
using Map = std::pmr::map<Key, Fixed_memory_range>;

/**
* Assign a fixed range of memory to a named purpose
Expand Down
4 changes: 0 additions & 4 deletions chainloader.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,4 @@ stdenv.mkDerivation rec {
pkgs.buildPackages.cmake
pkgs.buildPackages.nasm
];

buildInputs = [
pkgs.microsoft_gsl
];
}
2 changes: 1 addition & 1 deletion cmake/includeos.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ if (NOT ${PLATFORM} STREQUAL "userspace")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CAPABS} ${WARNS} -nostdlib -fno-omit-frame-pointer -c")
else()
# these kinda work with llvm
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CAPABS} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CAPABS} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c -fno-threadsafe-statics")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CAPABS} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c")
endif()
endif()
4 changes: 2 additions & 2 deletions cmake/library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ if (debug)
endif()

if (CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_CXX_FLAGS "-m32 -MMD ${CAPABS} ${WARNS} -nostdlib -fno-omit-frame-pointer -c -std=c++14 -D_LIBCPP_HAS_NO_THREADS=1")
set(CMAKE_CXX_FLAGS "-m32 -MMD ${CAPABS} ${WARNS} -nostdlib -fno-omit-frame-pointer -c -std=c++20 -D_LIBCPP_HAS_NO_THREADS=1")
set(CMAKE_C_FLAGS "-m32 -MMD ${CAPABS} ${WARNS} -nostdlib -fno-omit-frame-pointer -c")
else()
# these kinda work with llvm
set(CMAKE_CXX_FLAGS "-MMD ${CAPABS} ${OPTIMIZE} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c -std=c++14 -D_LIBCPP_HAS_NO_THREADS=1")
set(CMAKE_CXX_FLAGS "-MMD ${CAPABS} ${OPTIMIZE} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c -std=c++20 -fno-threadsafe-statics -D_LIBCPP_HAS_NO_THREADS=1")
set(CMAKE_C_FLAGS "-MMD ${CAPABS} ${OPTIMIZE} ${WARNS} -nostdlib -nostdlibinc -fno-omit-frame-pointer -c")
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/ist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static stack create_stack_virt(size_t size, const char* name)
}
static stack create_stack_simple(size_t size, const char* /*name*/)
{
auto* phys = (char*)memalign(4096, size);
auto* phys = (char*)kalloc_aligned(4096, size);
uintptr_t sp = (uintptr_t) phys + size - 8;
sp &= ~uintptr_t(0xf);
return {(void*) sp, phys};
Expand Down
3 changes: 1 addition & 2 deletions src/crt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
SET(SRCS
SET(SRCS
c_abi.c
ctype_b_loc.c
ctype_tolower_loc.c
string.c
quick_exit.cpp
cxx_abi.cpp
)
Expand Down
28 changes: 0 additions & 28 deletions src/crt/string.c

This file was deleted.

34 changes: 0 additions & 34 deletions src/crt/string.h

This file was deleted.

7 changes: 6 additions & 1 deletion src/include/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace kernel {
struct State {
bool running = true;
bool boot_sequence_passed = false;
bool libc_initialized = false;
bool libc_initialized = false; // Set when __libc_main returns
bool allow_syscalls = false; // Set before calling into libc
bool block_drivers_ready = false;
bool timestamps = false;
bool timestamps_ready = false;
Expand Down Expand Up @@ -62,6 +63,10 @@ namespace kernel {
return state().libc_initialized;
}

inline bool allow_syscalls() noexcept {
return state().allow_syscalls;
}

inline bool block_drivers_ready() noexcept {
return state().block_drivers_ready;
}
Expand Down
5 changes: 5 additions & 0 deletions src/kernel/heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,10 @@ void kernel::init_heap(uintptr_t free_mem_begin, uintptr_t memory_end) noexcept
auto brk_end = __init_brk(kernel::heap_begin(), __brk_max);
Expects(brk_end <= memory_end);
__init_mmap(brk_end, memory_end);

// Also set the PMR default allocator to use the same allocator as mmap
auto& alloc = os::mem::raw_allocator();
std::pmr::set_default_resource(&alloc);

__heap_ready = true;
}
4 changes: 2 additions & 2 deletions src/kernel/multiboot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void kernel::multiboot(uint32_t boot_addr)
if (info->flags & MULTIBOOT_INFO_CMDLINE) {
const auto* cmdline = (const char*) (uintptr_t) info->cmdline;
INFO2("* Booted with parameters @ %p: %s", cmdline, cmdline);
kernel::state().cmdline = strdup(cmdline);
kernel::state().cmdline = std::pmr::string(cmdline).data();
}

if (info->flags & MULTIBOOT_INFO_MEM_MAP) {
Expand Down Expand Up @@ -178,7 +178,7 @@ void kernel::multiboot(uint32_t boot_addr)
//os::mem::map_avail({map.addr, map.addr, {os::mem::Access::read | os::mem::Access::write}, map.len}, "Reserved (Multiboot)");
}
}
printf("\n");
INFO2("");
}

auto mods = os::modules();
Expand Down
7 changes: 5 additions & 2 deletions src/musl/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ inline constexpr auto& pr_param(std::ostream& out, L lhs, Args&&... rest){

template <typename Ret, typename ...Args>
inline void strace_print(const char* name, Ret ret, Args&&... args){
if (not kernel::state().libc_initialized)
return;

std::stringstream out;
out << name << "(";
Expand All @@ -74,6 +72,11 @@ inline void strace_print(const char* name, Ret ret, Args&&... args){
// strace, calling the syscall, recording return value and printing if enabled
template<typename Fn, typename ...Args>
inline auto strace(Fn func, [[maybe_unused]]const char* name, Args&&... args) {
if (!kernel::state().allow_syscalls) {
fprintf(stderr, "Syscalls not allowed here. Unexpected call to %s - terminating\n", name);
Expects(kernel::state().allow_syscalls);
}

auto ret = func(args...);

if constexpr (__strace)
Expand Down
5 changes: 5 additions & 0 deletions src/musl/stub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ inline void stubtrace_print(const char* name, R ret, Args&&... args) {
// calling the syscall, recording return value and only printing when strace is on
template<typename Fn, typename ...Args>
inline auto stubtrace(Fn func, const char* name[[maybe_unused]], Args&&... args) {
if (!kernel::state().allow_syscalls) {
fprintf(stderr, "Syscalls not allowed here. Unexpected call to %s (stub) - terminating\n", name);
Expects(kernel::state().allow_syscalls);
}

auto ret = func(args...);

if constexpr (__strace)
Expand Down
5 changes: 4 additions & 1 deletion src/platform/x86_nano/kernel_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
extern "C" {
void __init_sanity_checks();
uintptr_t _move_symbols(uintptr_t loc);
void _init_bss();
void _init_heap(uintptr_t);
void _init_elf_parser();
void _init_syscalls();
}

Expand Down Expand Up @@ -57,6 +57,9 @@ void kernel_start(uintptr_t magic, uintptr_t addr)
// Initialize heap
kernel::init_heap(free_mem_begin, mem_end);

// Get backtrace on nano too
_init_elf_parser();

// Initialize system calls
_init_syscalls();

Expand Down
7 changes: 4 additions & 3 deletions src/platform/x86_pc/init_libc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ namespace x86
int argc = 1;

// Env vars
argv[2] = strdup("LC_CTYPE=C");
argv[3] = strdup("LC_ALL=C");
argv[4] = strdup("USER=root");
argv[2] = std::pmr::string("LC_CTYPE=C").data();
argv[3] = std::pmr::string("LC_ALL=C").data();
argv[4] = std::pmr::string("USER=root").data();
argv[5] = 0x0;

// auxiliary vector
Expand Down Expand Up @@ -161,6 +161,7 @@ namespace x86

// GDB_ENTRY;
PRATTLE("* Starting libc initialization\n");
kernel::state().allow_syscalls = true;
__libc_start_main(kernel_main, argc, argv.data());
}
}
2 changes: 1 addition & 1 deletion test/kernel/integration/grub/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

# Create the GRUB image
subprocess.check_call(["bash",grubify,"kernel_grub.elf.bin"])
vm.boot(multiboot = False)
vm.boot(20, multiboot = False)