Compare commits

..

3 Commits

Author SHA1 Message Date
77ebdda2e0
libos: Add Process::spawn()
All checks were successful
continuous-integration/drone/push Build is passing
2023-08-07 22:49:12 +02:00
097353e779
kernel: Properly check memory bounds while touching user memory
Before this patch, one byte of each page was being accessed without checking the page's permissions.
2023-08-07 22:49:00 +02:00
10c892d606
kernel: Allow null envp in execve()
As far as I know, this is not standard, but I'm doing this as a convenience for programs using exec() right after clearenv().
2023-08-07 22:48:21 +02:00
4 changed files with 90 additions and 3 deletions

View File

@ -562,7 +562,7 @@ namespace MemoryManager
while (size--)
{
// Crossed a page boundary, gotta check the page tables again before touching any memory!!
if (user_ptr % ARCH_PAGE_SIZE)
if ((user_ptr % ARCH_PAGE_SIZE) == 0)
{
if (!validate_page_access(user_ptr, MMU::ReadWrite | MMU::User)) return false;
}
@ -590,7 +590,7 @@ namespace MemoryManager
while (size--)
{
// Crossed a page boundary, gotta check the page tables again before touching any memory!!
if (user_ptr % ARCH_PAGE_SIZE)
if ((user_ptr % ARCH_PAGE_SIZE) == 0)
{
if (!validate_page_access(user_ptr, MMU::User)) return false;
}

View File

@ -55,7 +55,8 @@ Result<u64> sys_execve(Registers* regs, SyscallArgs args)
{
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
auto argv = TRY(copy_string_vector_from_userspace(args[1]));
auto envp = TRY(copy_string_vector_from_userspace(args[2]));
Vector<String> envp;
if (args[2]) envp = TRY(copy_string_vector_from_userspace(args[2]));
if ((calculate_userspace_stack_size(argv) + calculate_userspace_stack_size(envp)) > MAX_ARGV_STACK_SIZE)
return err(E2BIG);

View File

@ -59,6 +59,37 @@ namespace os
*/
static Result<void> exec(StringView path, Slice<String> args, Slice<String> env, bool search_in_path = true);
/**
* @brief Spawn a new process from an executable file.
*
* @param path The new executable's path.
* @param args The argument list to pass to the new process.
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
* @return Result<pid_t> An error, or the process ID of the new process.
*/
static Result<pid_t> spawn(StringView path, Slice<String> args, bool search_in_path = true);
/**
* @brief Spawn a new process from an executable file.
*
* @param path The new executable's path.
* @param args The argument list to pass to the new process.
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
* @return Result<pid_t> An error, or the process ID of the new process.
*/
static Result<pid_t> spawn(StringView path, Slice<StringView> args, bool search_in_path = true);
/**
* @brief Spawn a new process from an executable file.
*
* @param path The new executable's path.
* @param args The argument list to pass to the new process.
* @param env The environment to pass to the new process, instead of the current environment.
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
* @return Result<pid_t> An error, or the process ID of the new process.
*/
static Result<pid_t> spawn(StringView path, Slice<String> args, Slice<String> env, bool search_in_path = true);
// To use as the child argument to wait() to wait for any child.
static constexpr pid_t ANY_CHILD = -1;

View File

@ -8,11 +8,14 @@
*/
#include <errno.h>
#include <luna/DebugLog.h>
#include <os/Process.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
extern char** environ;
namespace os
{
Result<pid_t> Process::fork()
@ -64,6 +67,58 @@ namespace os
return err(errno);
}
static Result<pid_t> do_spawn(const char* path, char** argv, char** envp, bool use_path)
{
pid_t child = fork();
if (child < 0) return err(errno);
if (child == 0)
{
if (use_path) execvpe(path, argv, envp);
else
execve(path, argv, envp);
_exit(127);
}
return child;
}
Result<pid_t> Process::spawn(StringView path, Slice<String> args, bool search_in_path)
{
Vector<const char*> argv;
for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); }
TRY(argv.try_append(nullptr));
return do_spawn(path.chars(), const_cast<char**>(argv.data()), environ, search_in_path);
}
Result<pid_t> Process::spawn(StringView path, Slice<StringView> args, bool search_in_path)
{
Vector<const char*> argv;
for (const auto& arg : args)
{
TRY(argv.try_append(arg.chars()));
dbgln("os::Process:spawn() adding new argument: %s", arg.chars());
}
TRY(argv.try_append(nullptr));
dbgln("os::Process:spawn() invoking do_spawn(): path=%s, argv=%p, envp=%p", path.chars(),
const_cast<char**>(argv.data()), environ);
return do_spawn(path.chars(), const_cast<char**>(argv.data()), environ, search_in_path);
}
Result<pid_t> Process::spawn(StringView path, Slice<String> args, Slice<String> env, bool search_in_path)
{
Vector<const char*> argv;
for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); }
TRY(argv.try_append(nullptr));
Vector<const char*> envp;
for (const auto& arg : env) { TRY(envp.try_append(arg.chars())); }
TRY(envp.try_append(nullptr));
return do_spawn(path.chars(), const_cast<char**>(argv.data()), const_cast<char**>(envp.data()), search_in_path);
}
Result<pid_t> Process::wait(pid_t child, int* status, int options)
{
long rc = syscall(SYS_waitpid, child, status, options);