cred: setting and getting ids

This commit is contained in:
Matheus
2024-08-16 20:02:12 -03:00
parent 8045dd3de6
commit da9c40b054
13 changed files with 256 additions and 63 deletions
+3
View File
@@ -0,0 +1,3 @@
Password for the root user is: root
To start a graphical environment, run: startwm
+1 -1
View File
@@ -1,2 +1,2 @@
root::0:0:root:/root:/usr/bin/bash root:$6$Y60.qkhq.v3jWUVR$UyDvutrBYQNNhX1VvIrX2O7CcRPxuZ8EqyBktx7RG1nXzgXi7QHj3khzl14jxfKx45DpbVMIOXJziW2GqsOss.:0:0:root:/root:/usr/bin/bash
sshd::100:100:sshd privsep:/var/lib/ssh:/usr/bin/false sshd::100:100:sshd privsep:/var/lib/ssh:/usr/bin/false
+9 -1
View File
@@ -78,6 +78,10 @@ extern syscall_ppoll
extern syscall_pread extern syscall_pread
extern syscall_pwrite extern syscall_pwrite
extern syscall_mknodat extern syscall_mknodat
extern syscall_getresuid
extern syscall_getresgid
extern syscall_setresuid
extern syscall_setresgid
syscalltab: syscalltab:
dq syscall_print dq syscall_print
dq syscall_mmap dq syscall_mmap
@@ -158,7 +162,11 @@ dq syscall_ppoll
dq syscall_pread dq syscall_pread
dq syscall_pwrite dq syscall_pwrite
dq syscall_mknodat dq syscall_mknodat
syscallcount equ 79 dq syscall_getresuid
dq syscall_getresgid
dq syscall_setresuid
dq syscall_setresgid
syscallcount equ 83
section .text section .text
global arch_syscall_entry global arch_syscall_entry
; on entry: ; on entry:
+11 -3
View File
@@ -5,7 +5,7 @@
#ifdef SYSCALL_LOGGING #ifdef SYSCALL_LOGGING
#define SYSCALL_COUNT 79 #define SYSCALL_COUNT 83
#define LOGSTR(x) arch_e9_puts(x) #define LOGSTR(x) arch_e9_puts(x)
static char *name[] = { static char *name[] = {
@@ -87,7 +87,11 @@ static char *name[] = {
"ppoll", "ppoll",
"pread", "pread",
"pwrite", "pwrite",
"mknodat" "mknodat",
"getresuid",
"getresgid",
"setresuid",
"setresgid"
}; };
static char *args[] = { static char *args[] = {
@@ -169,7 +173,11 @@ static char *args[] = {
"fds %p nfds %d timeout %p sigmask %p", // ppoll "fds %p nfds %d timeout %p sigmask %p", // ppoll
"fd %d buffer %p count %lu offset %lu\n", // pread "fd %d buffer %p count %lu offset %lu\n", // pread
"fd %d buffer %p count %lu offset %lu\n", // pwrite "fd %d buffer %p count %lu offset %lu\n", // pwrite
"dirfd %d path %s mode %o dev %x\n" // mknodat "dirfd %d path %s mode %o dev %x\n", // mknodat
"uidp %p euidp %p suidp %p", // getresuid
"gidp %p egidp %p sgidp %p", // getresgid
"uid %d euid %d suid %d", // setresuid
"gid %d egid %d sgid %d" // setresgid
}; };
#endif #endif
+21
View File
@@ -0,0 +1,21 @@
#ifndef _CRED_H
#define _CRED_H
#define CRED_PERM_SUPERUSER (1l << 63)
#include <stdint.h>
typedef struct {
int uid, euid, suid;
int gid, egid, sgid;
uintmax_t allow;
uintmax_t deny;
} cred_t;
int cred_setuids(cred_t *cred, int uid, int euid, int suid);
int cred_setgids(cred_t *cred, int uid, int euid, int suid);
void cred_getuids(cred_t *cred, int *uidp, int *euidp, int *suidp);
void cred_getgids(cred_t *cred, int *gidp, int *egidp, int *sgidp);
#endif
+40
View File
@@ -0,0 +1,40 @@
#include <kernel/cred.h>
#include <errno.h>
int cred_setuids(cred_t *cred, int uid, int euid, int suid) {
if (uid < -1 || euid < -1 || suid < -1 || (uid == -1 && euid == -1 && suid == -1))
return EINVAL;
// TODO do permission check to see if its possible
cred->uid = uid;
cred->euid = euid;
cred->suid = suid;
return 0;
}
int cred_setgids(cred_t *cred, int gid, int egid, int sgid) {
if (gid < -1 || egid < -1 || sgid < -1 || (gid == -1 && egid == -1 && sgid == -1))
return EINVAL;
// TODO do permission check to see if its possible
cred->gid = gid;
cred->egid = egid;
cred->sgid = sgid;
return 0;
}
void cred_getuids(cred_t *cred, int *uidp, int *euidp, int *suidp) {
*uidp = cred->uid;
*euidp = cred->euid;
*suidp = cred->suid;
}
void cred_getgids(cred_t *cred, int *gidp, int *egidp, int *sgidp) {
*gidp = cred->gid;
*egidp = cred->egid;
*sgidp = cred->sgid;
}
+1
View File
@@ -44,6 +44,7 @@ syscallret_t syscall_fork(context_t *ctx) {
MUTEX_RELEASE(&proc->mutex); MUTEX_RELEASE(&proc->mutex);
nproc->umask = _cpu()->thread->proc->umask; nproc->umask = _cpu()->thread->proc->umask;
nproc->cred = _cpu()->thread->proc->cred;
nproc->root = sched_getroot(); nproc->root = sched_getroot();
nproc->cwd = sched_getcwd(); nproc->cwd = sched_getcwd();
nproc->threadlist = nthread; nproc->threadlist = nthread;
+29
View File
@@ -0,0 +1,29 @@
#include <kernel/abi.h>
#include <kernel/syscalls.h>
#include <kernel/cred.h>
syscallret_t syscall_getresgid(context_t *, gid_t *gidp, gid_t *egidp, gid_t *sgidp) {
syscallret_t ret = {
.ret = -1
};
int gid, egid, sgid;
cred_getgids(&_cpu()->thread->proc->cred, &gid, &egid, &sgid);
ret.errno = usercopy_touser(gidp, &gid, sizeof(gid));
if (ret.errno)
goto leave;
ret.errno = usercopy_touser(egidp, &egid, sizeof(egid));
if (ret.errno)
goto leave;
ret.errno = usercopy_touser(sgidp, &sgid, sizeof(sgid));
if (ret.errno)
goto leave;
ret.ret = 0;
leave:
return ret;
}
+29
View File
@@ -0,0 +1,29 @@
#include <kernel/abi.h>
#include <kernel/syscalls.h>
#include <kernel/cred.h>
syscallret_t syscall_getresuid(context_t *, uid_t *uidp, uid_t *euidp, uid_t *suidp) {
syscallret_t ret = {
.ret = -1
};
int uid, euid, suid;
cred_getuids(&_cpu()->thread->proc->cred, &uid, &euid, &suid);
ret.errno = usercopy_touser(uidp, &uid, sizeof(uid));
if (ret.errno)
goto leave;
ret.errno = usercopy_touser(euidp, &euid, sizeof(euid));
if (ret.errno)
goto leave;
ret.errno = usercopy_touser(suidp, &suid, sizeof(suid));
if (ret.errno)
goto leave;
ret.ret = 0;
leave:
return ret;
}
+12
View File
@@ -0,0 +1,12 @@
#include <kernel/abi.h>
#include <kernel/syscalls.h>
#include <kernel/cred.h>
syscallret_t syscall_setresgid(context_t *, gid_t gid, gid_t egid, gid_t sgid) {
syscallret_t ret;
ret.errno = cred_setgids(&_cpu()->thread->proc->cred, gid, egid, sgid);
ret.ret = ret.errno ? -1 : 0;
return ret;
}
+12
View File
@@ -0,0 +1,12 @@
#include <kernel/abi.h>
#include <kernel/syscalls.h>
#include <kernel/cred.h>
syscallret_t syscall_setresuid(context_t *, uid_t uid, uid_t euid, uid_t suid) {
syscallret_t ret;
ret.errno = cred_setuids(&_cpu()->thread->proc->cred, uid, euid, suid);
ret.ret = ret.errno ? -1 : 0;
return ret;
}
+74 -57
View File
@@ -1,5 +1,5 @@
diff --git mlibc-clean/abis/linux/in.h mlibc-workdir/abis/linux/in.h diff --git mlibc-clean/abis/linux/in.h mlibc-workdir/abis/linux/in.h
index eb7be7b..eb83b56 100644 index 87bc25f..d7370d0 100644
--- mlibc-clean/abis/linux/in.h --- mlibc-clean/abis/linux/in.h
+++ mlibc-workdir/abis/linux/in.h +++ mlibc-workdir/abis/linux/in.h
@@ -109,6 +109,9 @@ struct group_source_req { @@ -109,6 +109,9 @@ struct group_source_req {
@@ -101,10 +101,10 @@ index fc34a3b..b1a95a9 100644
#include <dlfcn.h> #include <dlfcn.h>
diff --git mlibc-clean/options/posix/generic/unistd-stubs.cpp mlibc-workdir/options/posix/generic/unistd-stubs.cpp diff --git mlibc-clean/options/posix/generic/unistd-stubs.cpp mlibc-workdir/options/posix/generic/unistd-stubs.cpp
index a40c6dc..acbe0ba 100644 index 16ab8b7..688908c 100644
--- mlibc-clean/options/posix/generic/unistd-stubs.cpp --- mlibc-clean/options/posix/generic/unistd-stubs.cpp
+++ mlibc-workdir/options/posix/generic/unistd-stubs.cpp +++ mlibc-workdir/options/posix/generic/unistd-stubs.cpp
@@ -1254,6 +1254,7 @@ void encrypt(char[64], int) { @@ -1257,6 +1257,7 @@ void encrypt(char[64], int) {
} }
#endif #endif
@@ -112,7 +112,7 @@ index a40c6dc..acbe0ba 100644
#if __MLIBC_BSD_OPTION #if __MLIBC_BSD_OPTION
void *sbrk(intptr_t increment) { void *sbrk(intptr_t increment) {
if(increment) { if(increment) {
@@ -1270,3 +1271,4 @@ void *sbrk(intptr_t increment) { @@ -1273,3 +1274,4 @@ void *sbrk(intptr_t increment) {
return out; return out;
} }
#endif #endif
@@ -207,10 +207,10 @@ index 0000000..e5b087a
+ +
diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp
new file mode 100644 new file mode 100644
index 0000000..6eb771e index 0000000..cfb7dfd
--- /dev/null --- /dev/null
+++ mlibc-workdir/sysdeps/astral/generic/generic.cpp +++ mlibc-workdir/sysdeps/astral/generic/generic.cpp
@@ -0,0 +1,957 @@ @@ -0,0 +1,970 @@
+#include <bits/ensure.h> +#include <bits/ensure.h>
+#include <mlibc/debug.hpp> +#include <mlibc/debug.hpp>
+#include <mlibc/all-sysdeps.hpp> +#include <mlibc/all-sysdeps.hpp>
@@ -227,11 +227,6 @@ index 0000000..6eb771e
+#include <unistd.h> +#include <unistd.h>
+#include <dirent.h> +#include <dirent.h>
+ +
+static int gid;
+static int egid;
+static int uid;
+static int euid;
+
+#ifndef MLIBC_BUILDING_RTLD +#ifndef MLIBC_BUILDING_RTLD
+// (s)brk is emulated in the libc rather than being +// (s)brk is emulated in the libc rather than being
+// an actual system call. +// an actual system call.
@@ -272,62 +267,80 @@ index 0000000..6eb771e
+#endif +#endif
+ +
+namespace mlibc { +namespace mlibc {
+ int sys_setuid(uid_t id) { + int sys_getsockopt(int fd, int layer, int number,
+ // TODO UNSTUB + void *__restrict buffer, socklen_t *__restrict size) {
+ uid = id; + // TODO unstub
+ mlibc::infoLogger() << "getsockopt: " << fd << " " << layer << " " << number << frg::endlog;
+ return 0; + return 0;
+ } + }
+ +
+ int sys_setgid(gid_t id) {
+ // TODO UNSTUB
+ gid = id;
+ return 0;
+ }
+
+ uid_t sys_getuid() {
+ // TODO UNSTUB
+ return uid;
+ }
+
+ gid_t sys_getgid() {
+ // TODO UNSTUB
+ return gid;
+ }
+
+ uid_t sys_geteuid() {
+ // TODO UNSTUB
+ return euid;
+ }
+
+ gid_t sys_getegid() {
+ // TODO UNSTUB
+ return egid;
+ }
+
+ int sys_setgroups(size_t size, const gid_t *list) { + int sys_setgroups(size_t size, const gid_t *list) {
+ // TODO unstub + // TODO unstub
+ return 0; + return 0;
+ } + }
+ +
+ int sys_setuid(uid_t id) {
+ return sys_setresuid(id, -1, -1);
+ }
+
+ int sys_setgid(gid_t id) {
+ return sys_setresgid(id, -1, -1);
+ }
+
+ int sys_seteuid(uid_t id) {
+ return sys_setresuid(-1, id, -1);
+ }
+
+ int sys_setegid(gid_t id) {
+ return sys_setresgid(-1, id, -1);
+ }
+
+ uid_t sys_getuid() {
+ uid_t r, e, s;
+ sys_getresuid(&r, &e, &s);
+
+ return r;
+ }
+
+ uid_t sys_geteuid() {
+ uid_t r, e, s;
+ sys_getresuid(&r, &e, &s);
+
+ return e;
+ }
+
+ gid_t sys_getgid() {
+ gid_t r, e, s;
+ sys_getresgid(&r, &e, &s);
+
+ return r;
+ }
+
+ gid_t sys_getegid() {
+ gid_t r, e, s;
+ sys_getresgid(&r, &e, &s);
+
+ return e;
+ }
+
+ int sys_setresuid(uid_t _ruid, uid_t _euid, uid_t _suid) { + int sys_setresuid(uid_t _ruid, uid_t _euid, uid_t _suid) {
+ // TODO unstub + long ret;
+ uid = _ruid; + return syscall(SYSCALL_SETRESUID, &ret, _ruid, _euid, _suid);
+ euid = _euid;
+ return 0;
+ } + }
+ +
+ int sys_setresgid(gid_t _rgid, gid_t _egid, gid_t _sgid) { + int sys_setresgid(gid_t _rgid, gid_t _egid, gid_t _sgid) {
+ // TODO unstub + long ret;
+ gid = _rgid; + return syscall(SYSCALL_SETRESGID, &ret, _rgid, _egid, _sgid);
+ egid = _egid;
+ return 0;
+ } + }
+ +
+ int sys_getsockopt(int fd, int layer, int number, + int sys_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid) {
+ void *__restrict buffer, socklen_t *__restrict size) { + long ret;
+ // TODO unstub + return syscall(SYSCALL_GETRESUID, &ret, (uint64_t)ruid, (uint64_t)euid, (uint64_t)suid);
+ mlibc::infoLogger() << "getsockopt: " << fd << " " << layer << " " << number << frg::endlog; + }
+ return 0; +
+ int sys_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid) {
+ long ret;
+ return syscall(SYSCALL_GETRESGID, &ret, (uint64_t)rgid, (uint64_t)egid, (uint64_t)sgid);
+ } + }
+ +
+ int sys_mknodat(int dirfd, const char *path, int mode, int dev) { + int sys_mknodat(int dirfd, const char *path, int mode, int dev) {
@@ -569,14 +582,14 @@ index 0000000..6eb771e
+ long ret; + long ret;
+ long error = syscall(SYSCALL_GETSID, &ret, pid); + long error = syscall(SYSCALL_GETSID, &ret, pid);
+ *pgid = ret; + *pgid = ret;
+ return 0; + return error;
+ } + }
+ +
+ int sys_getpgid(pid_t pid, pid_t *pgid) { + int sys_getpgid(pid_t pid, pid_t *pgid) {
+ long ret; + long ret;
+ long error = syscall(SYSCALL_GETPGID, &ret, pid); + long error = syscall(SYSCALL_GETPGID, &ret, pid);
+ *pgid = ret; + *pgid = ret;
+ return 0; + return error;
+ } + }
+ +
+ int sys_gethostname(char *buffer, size_t bufsize) { + int sys_gethostname(char *buffer, size_t bufsize) {
@@ -1187,10 +1200,10 @@ index 0000000..990eca0
+#endif +#endif
diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h
new file mode 100644 new file mode 100644
index 0000000..4a93549 index 0000000..6db5755
--- /dev/null --- /dev/null
+++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h +++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h
@@ -0,0 +1,102 @@ @@ -0,0 +1,106 @@
+#ifndef _SYSCALL_H_INCLUDE +#ifndef _SYSCALL_H_INCLUDE
+#define _SYSCALL_H_INCLUDE +#define _SYSCALL_H_INCLUDE
+ +
@@ -1273,6 +1286,10 @@ index 0000000..4a93549
+#define SYSCALL_PREAD 76 +#define SYSCALL_PREAD 76
+#define SYSCALL_PWRITE 77 +#define SYSCALL_PWRITE 77
+#define SYSCALL_MKNODAT 78 +#define SYSCALL_MKNODAT 78
+#define SYSCALL_GETRESUID 79
+#define SYSCALL_GETRESGID 80
+#define SYSCALL_SETRESUID 81
+#define SYSCALL_SETRESGID 82
+ +
+#include <stddef.h> +#include <stddef.h>
+#include <stdint.h> +#include <stdint.h>
+13
View File
@@ -1,3 +1,16 @@
diff --git shadow-clean/etc/login.defs shadow-workdir/etc/login.defs
index 33622c2..adc6025 100644
--- shadow-clean/etc/login.defs
+++ shadow-workdir/etc/login.defs
@@ -93,7 +93,7 @@ MOTD_FILE /etc/motd
#
# If defined, this file will be output before each login(1) prompt.
#
-#ISSUE_FILE /etc/issue
+ISSUE_FILE /etc/issue
#
# If defined, file which maps tty line to TERM environment parameter.
diff --git shadow-clean/lib/freezero.c shadow-workdir/lib/freezero.c diff --git shadow-clean/lib/freezero.c shadow-workdir/lib/freezero.c
index c565f43..a6adf98 100644 index c565f43..a6adf98 100644
--- shadow-clean/lib/freezero.c --- shadow-clean/lib/freezero.c