kernel+libc: Add set* and get* syscalls for UIDs and GIDs

This commit is contained in:
apio 2023-04-08 13:50:18 +02:00
parent f6f9254eb4
commit 3da1849c99
Signed by: apio
GPG Key ID: B8A7D06E42258954
7 changed files with 187 additions and 1 deletions

View File

@ -9,6 +9,9 @@ endfunction()
luna_app(init.cpp init) luna_app(init.cpp init)
luna_app(env.cpp env) luna_app(env.cpp env)
luna_app(su.cpp su)
target_link_libraries(su PRIVATE os)
luna_app(sh.cpp sh) luna_app(sh.cpp sh)
target_link_libraries(sh PRIVATE os) target_link_libraries(sh PRIVATE os)

32
apps/su.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <os/ArgumentParser.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv)
{
StringView id;
if (geteuid() != 0)
{
fprintf(stderr, "su must be run as root!\n");
return 1;
}
os::ArgumentParser parser;
parser.add_positional_argument(id, "id"_sv, true);
parser.parse(argc, argv);
int uid = atoi(id.chars());
if (uid == 0)
{
fprintf(stderr, "Already root!\n");
return 1;
}
setgid(uid);
setuid(uid);
execl("/bin/sh", "sh", NULL);
}

View File

@ -10,3 +10,83 @@ Result<u64> sys_getppid(Registers*, SyscallArgs)
{ {
return Scheduler::current()->parent_id; 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;
}

View File

@ -16,6 +16,8 @@ typedef __u64_t useconds_t;
typedef __i64_t off_t; typedef __i64_t off_t;
typedef __u64_t dev_t; typedef __u64_t dev_t;
typedef __u64_t ino_t; typedef __u64_t ino_t;
typedef __u32_t uid_t;
typedef __u32_t gid_t;
typedef off_t fpos_t; typedef off_t fpos_t;

View File

@ -28,6 +28,30 @@ extern "C"
/* Return the current process' parent process ID. */ /* Return the current process' parent process ID. */
pid_t getppid(void); pid_t getppid(void);
/* Return the current process' real user ID. */
uid_t getuid(void);
/* Return the current process' effective user ID. */
uid_t geteuid(void);
/* Return the current process' real group ID. */
gid_t getgid(void);
/* Return the current process' effective group ID. */
gid_t getegid(void);
/* Set the current process' user IDs. */
int setuid(uid_t uid);
/* Set the current process' effective user ID. */
int seteuid(uid_t uid);
/* Set the current process' group IDs. */
int setgid(gid_t gid);
/* Set the current process' effective group ID. */
int setegid(gid_t gid);
/* Replace the current process with another one. On success, does not return. */ /* Replace the current process with another one. On success, does not return. */
int execv(const char* path, char* const* argv); int execv(const char* path, char* const* argv);

View File

@ -31,6 +31,50 @@ extern "C"
return (pid_t)syscall(SYS_getppid); return (pid_t)syscall(SYS_getppid);
} }
uid_t getuid(void)
{
return (uid_t)syscall(SYS_getuid);
}
uid_t geteuid(void)
{
return (uid_t)syscall(SYS_geteuid);
}
gid_t getgid(void)
{
return (gid_t)syscall(SYS_getgid);
}
gid_t getegid(void)
{
return (gid_t)syscall(SYS_getegid);
}
int setuid(uid_t uid)
{
long rc = syscall(SYS_setuid, uid);
__errno_return(rc, int);
}
int seteuid(uid_t uid)
{
long rc = syscall(SYS_seteuid, uid);
__errno_return(rc, int);
}
int setgid(gid_t gid)
{
long rc = syscall(SYS_setgid, gid);
__errno_return(rc, int);
}
int setegid(gid_t gid)
{
long rc = syscall(SYS_setegid, gid);
__errno_return(rc, int);
}
int execv(const char* path, char* const* argv) int execv(const char* path, char* const* argv)
{ {
return execve(path, argv, environ); return execve(path, argv, environ);

View File

@ -2,7 +2,8 @@
#define enumerate_syscalls(_e) \ #define enumerate_syscalls(_e) \
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \ _e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \
_e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \
_e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid)
enum Syscalls enum Syscalls
{ {