Luna/kernel/src/sys/id.cpp

127 lines
2.7 KiB
C++
Raw Normal View History

2023-04-08 12:47:34 +00:00
#include "memory/MemoryManager.h"
2023-03-11 21:19:58 +00:00
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
Result<u64> sys_getpid(Registers*, SyscallArgs)
{
return Scheduler::current()->id;
}
Result<u64> sys_getppid(Registers*, SyscallArgs)
{
return Scheduler::current()->parent_id;
}
Result<u64> sys_getuid(Registers*, SyscallArgs)
{
return Scheduler::current()->auth.uid;
}
Result<u64> sys_geteuid(Registers*, SyscallArgs)
{
return Scheduler::current()->auth.euid;
}
Result<u64> sys_getgid(Registers*, SyscallArgs)
{
return Scheduler::current()->auth.gid;
}
Result<u64> sys_getegid(Registers*, SyscallArgs)
{
return Scheduler::current()->auth.egid;
}
Result<u64> sys_setuid(Registers*, SyscallArgs args)
{
u32 uid = (u32)args[0];
Credentials& auth = Scheduler::current()->auth;
if (auth.euid == 0)
{
auth.uid = auth.euid = auth.suid = uid;
return 0;
}
if (uid != auth.uid && uid != auth.suid) return err(EPERM);
auth.euid = uid;
return 0;
}
Result<u64> sys_seteuid(Registers*, SyscallArgs args)
{
u32 uid = (u32)args[0];
Credentials& auth = Scheduler::current()->auth;
if (auth.euid != 0 && uid != auth.uid && uid != auth.suid) return err(EPERM);
auth.euid = uid;
return 0;
}
Result<u64> sys_setgid(Registers*, SyscallArgs args)
{
u32 gid = (u32)args[0];
Credentials& auth = Scheduler::current()->auth;
if (auth.euid == 0)
{
auth.gid = auth.egid = auth.sgid = gid;
return 0;
}
if (gid != auth.gid && gid != auth.sgid) return err(EPERM);
auth.egid = gid;
return 0;
}
Result<u64> sys_setegid(Registers*, SyscallArgs args)
{
u32 gid = (u32)args[0];
Credentials& auth = Scheduler::current()->auth;
if (auth.euid != 0 && gid != auth.gid && gid != auth.sgid) return err(EPERM);
auth.egid = gid;
return 0;
}
2023-04-08 12:47:34 +00:00
Result<u64> sys_chmod(Registers*, SyscallArgs args)
{
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
mode_t mode = (mode_t)args[1];
Credentials& auth = Scheduler::current()->auth;
auto inode = TRY(VFS::resolve_path(path.chars(), auth, Scheduler::current()->current_directory));
2023-04-08 12:47:34 +00:00
if (auth.euid != 0 && auth.euid != inode->uid()) return err(EPERM);
TRY(inode->chmod(mode));
return 0;
}
Result<u64> sys_chown(Registers*, SyscallArgs args)
{
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
u32 uid = (u32)args[1];
u32 gid = (u32)args[2];
Credentials& auth = Scheduler::current()->auth;
auto inode = TRY(VFS::resolve_path(path.chars(), auth, Scheduler::current()->current_directory));
2023-04-08 12:47:34 +00:00
if (auth.euid != 0) return err(EPERM);
TRY(inode->chown(uid == (u32)-1 ? inode->uid() : uid, gid == (u32)-1 ? inode->gid() : gid));
return 0;
}