diff --git a/libc/Makefile b/libc/Makefile index 2e14782e..92941640 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -188,11 +188,15 @@ ftruncate.o \ getc.o \ getcwd.o \ getdtablesize.o \ +getegid.o \ +geteuid.o \ +getgid.o \ getpagesize.o \ getpid.o \ getppid.o \ gettermmode.o \ gettimeofday.o \ +getuid.o \ init.o \ ioleast.o \ isatty.o \ @@ -232,9 +236,13 @@ rmdir.o \ sbrk.o \ scanf.o \ select.o \ +setegid.o \ +seteuid.o \ +setgid.o \ setjmp.o \ setlocale.o \ settermmode.o \ +setuid.o \ sfork.o \ SIG_DFL.o \ SIG_ERR.o \ diff --git a/libc/getegid.cpp b/libc/getegid.cpp new file mode 100644 index 00000000..f41cc53e --- /dev/null +++ b/libc/getegid.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + getegid.cpp + Get effective group id. + +*******************************************************************************/ + +#include +#include + +#include + +DEFN_SYSCALL0(gid_t, sys_getegid, SYSCALL_GETEGID); + +extern "C" gid_t getegid() +{ + return sys_getegid(); +} diff --git a/libc/geteuid.cpp b/libc/geteuid.cpp new file mode 100644 index 00000000..3cad8706 --- /dev/null +++ b/libc/geteuid.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + geteuid.cpp + Get effective user id. + +*******************************************************************************/ + +#include +#include + +#include + +DEFN_SYSCALL0(uid_t, sys_geteuid, SYSCALL_GETEUID); + +extern "C" uid_t geteuid() +{ + return sys_geteuid(); +} diff --git a/libc/getgid.cpp b/libc/getgid.cpp new file mode 100644 index 00000000..24505ab6 --- /dev/null +++ b/libc/getgid.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + getgid.cpp + Get current group id. + +*******************************************************************************/ + +#include +#include + +#include + +DEFN_SYSCALL0(gid_t, sys_getgid, SYSCALL_GETGID); + +extern "C" gid_t getgid() +{ + return sys_getgid(); +} diff --git a/libc/getuid.cpp b/libc/getuid.cpp new file mode 100644 index 00000000..6a532e54 --- /dev/null +++ b/libc/getuid.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + getuid.cpp + Get current user id. + +*******************************************************************************/ + +#include +#include + +#include + +DEFN_SYSCALL0(uid_t, sys_getuid, SYSCALL_GETUID); + +extern "C" uid_t getuid() +{ + return sys_getuid(); +} diff --git a/libc/include/unistd.h b/libc/include/unistd.h index ff7b84f8..a2f1e13c 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -93,9 +93,6 @@ void encrypt(char [64], int); int fdatasync(int); int fexecve(int, char* const [], char* const []); long fpathconf(int, int); -gid_t getegid(void); -uid_t geteuid(void); -gid_t getgid(void); int getgroups(int, gid_t []); long gethostid(void); int gethostname(char*, size_t); @@ -105,19 +102,14 @@ int getopt(int, char* const [], const char*); pid_t getpgid(pid_t); pid_t getpgrp(void); pid_t getsid(pid_t); -uid_t getuid(void); int lockf(int, int, off_t); int nice(int); long pathconf(const char*, int); int pause(void); -int setegid(gid_t); -int seteuid(uid_t); -int setgid(gid_t); int setpgid(pid_t, pid_t); int setregid(gid_t, gid_t); int setreuid(uid_t, uid_t); pid_t setsid(void); -int setuid(uid_t); void swab(const void* restrict, void* restrict, ssize_t); int symlink(const char*, const char*); int symlinkat(const char*, int, const char*); @@ -158,8 +150,12 @@ int fsync(int); int ftruncate(int, off_t); char* getcwd(char*, size_t); char* get_current_dir_name(void); +gid_t getegid(void); +uid_t geteuid(void); pid_t getpid(void); pid_t getppid(void); +uid_t getuid(void); +gid_t getgid(void); int isatty(int); int lchown(const char*, uid_t, gid_t); int link(const char*, const char*); @@ -172,6 +168,10 @@ ssize_t readlink(const char* restrict, char* restrict, size_t); ssize_t readlinkat(int, const char* restrict, char* restrict, size_t); ssize_t read(int, void*, size_t); int rmdir(const char*); +int setegid(gid_t); +int seteuid(uid_t); +int setgid(gid_t); +int setuid(uid_t); unsigned sleep(unsigned); int truncate(const char*, off_t); int truncateat(int dirfd, const char*, off_t); diff --git a/libc/setegid.cpp b/libc/setegid.cpp new file mode 100644 index 00000000..3d06fdd1 --- /dev/null +++ b/libc/setegid.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + setegid.cpp + Set effective group id. + +*******************************************************************************/ + +#include +#include + +#include + +DEFN_SYSCALL1(gid_t, sys_setegid, SYSCALL_GETEGID, gid_t); + +extern "C" int setegid(gid_t egid) +{ + return sys_setegid(egid); +} diff --git a/libc/seteuid.cpp b/libc/seteuid.cpp new file mode 100644 index 00000000..08b0f1a4 --- /dev/null +++ b/libc/seteuid.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + seteuid.cpp + Set effective user id. + +*******************************************************************************/ + +#include +#include + +#include + +DEFN_SYSCALL1(uid_t, sys_seteuid, SYSCALL_GETEUID, uid_t); + +extern "C" int seteuid(uid_t euid) +{ + return sys_seteuid(euid); +} diff --git a/libc/setgid.cpp b/libc/setgid.cpp new file mode 100644 index 00000000..1ad3f351 --- /dev/null +++ b/libc/setgid.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + setgid.cpp + Set current group id. + +*******************************************************************************/ + +#include +#include + +#include + +DEFN_SYSCALL1(gid_t, sys_setgid, SYSCALL_GETGID, gid_t); + +extern "C" int setgid(gid_t gid) +{ + return sys_setgid(gid); +} diff --git a/libc/setuid.cpp b/libc/setuid.cpp new file mode 100644 index 00000000..ff1d369e --- /dev/null +++ b/libc/setuid.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + setuid.cpp + Set user id. + +*******************************************************************************/ + +#include +#include + +#include + +DEFN_SYSCALL1(uid_t, sys_setuid, SYSCALL_GETUID, uid_t); + +extern "C" int setuid(uid_t uid) +{ + return sys_setuid(uid); +} diff --git a/sortix/Makefile b/sortix/Makefile index c285f3cc..ea6eab43 100644 --- a/sortix/Makefile +++ b/sortix/Makefile @@ -88,6 +88,7 @@ fsfunc.o \ fs/kram.o \ fs/user.o \ fs/util.o \ +identity.o \ initrd.o \ inode.o \ interlock.o \ diff --git a/sortix/identity.cpp b/sortix/identity.cpp new file mode 100644 index 00000000..5ec11063 --- /dev/null +++ b/sortix/identity.cpp @@ -0,0 +1,106 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of Sortix. + + Sortix is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + Sortix is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . + + identity.cpp + System calls for managing user and group identities. + +*******************************************************************************/ + +#include + +#include +#include +#include + +#include "process.h" +#include "identity.h" + +namespace Sortix { +namespace Identity { + +static uid_t sys_getuid() +{ + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + return process->uid; +} + +static int sys_setuid(uid_t uid) +{ + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + return process->uid = uid, 0; +} + +static gid_t sys_getgid() +{ + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + return process->gid; +} + +static int sys_setgid(gid_t gid) +{ + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + return process->gid = gid, 0; +} + +static uid_t sys_geteuid() +{ + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + return process->euid; +} + +static int sys_seteuid(uid_t euid) +{ + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + return process->euid = euid, 0; +} + +static gid_t sys_getegid() +{ + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + return process->egid; +} + +static int sys_setegid(gid_t egid) +{ + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + return process->egid = egid, 0; +} + +void Init() +{ + Syscall::Register(SYSCALL_GETUID, (void*) sys_getuid); + Syscall::Register(SYSCALL_GETGID, (void*) sys_getgid); + Syscall::Register(SYSCALL_SETUID, (void*) sys_setuid); + Syscall::Register(SYSCALL_SETGID, (void*) sys_setgid); + Syscall::Register(SYSCALL_GETEUID, (void*) sys_geteuid); + Syscall::Register(SYSCALL_GETEGID, (void*) sys_getegid); + Syscall::Register(SYSCALL_SETEUID, (void*) sys_seteuid); + Syscall::Register(SYSCALL_SETEGID, (void*) sys_setegid); +} + +} // namespace Identity +} // namespace Sortix diff --git a/sortix/identity.h b/sortix/identity.h new file mode 100644 index 00000000..64da213c --- /dev/null +++ b/sortix/identity.h @@ -0,0 +1,36 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of Sortix. + + Sortix is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + Sortix is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . + + identity.h + System calls for managing user and group identities. + +*******************************************************************************/ + +#ifndef SORTIX_IDENTITY_H +#define SORTIX_IDENTITY_H + +namespace Sortix { +namespace Identity { + +void Init(); + +} // namespace Identity +} // namespace Sortix + +#endif diff --git a/sortix/include/sortix/syscallnum.h b/sortix/include/sortix/syscallnum.h index 42e4a9c7..5871a162 100644 --- a/sortix/include/sortix/syscallnum.h +++ b/sortix/include/sortix/syscallnum.h @@ -97,6 +97,14 @@ #define SYSCALL_RENAMEAT 73 #define SYSCALL_READLINKAT 74 #define SYSCALL_FSYNC 75 -#define SYSCALL_MAX_NUM 76 /* index of highest constant + 1 */ +#define SYSCALL_GETUID 76 +#define SYSCALL_GETGID 77 +#define SYSCALL_SETUID 78 +#define SYSCALL_SETGID 79 +#define SYSCALL_GETEUID 80 +#define SYSCALL_GETEGID 81 +#define SYSCALL_SETEUID 82 +#define SYSCALL_SETEGID 83 +#define SYSCALL_MAX_NUM 84 /* index of highest constant + 1 */ #endif diff --git a/sortix/ioctx.cpp b/sortix/ioctx.cpp index 408e1377..92691f97 100644 --- a/sortix/ioctx.cpp +++ b/sortix/ioctx.cpp @@ -25,22 +25,27 @@ #include #include #include + #include "process.h" namespace Sortix { void SetupUserIOCtx(ioctx_t* ctx) { - ctx->uid = ctx->auth_uid = CurrentProcess()->uid; - ctx->gid = ctx->auth_gid = CurrentProcess()->gid; + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + ctx->uid = ctx->auth_uid = process->uid; + ctx->gid = ctx->auth_gid = process->gid; ctx->copy_to_dest = CopyToUser; ctx->copy_from_src = CopyFromUser; } void SetupKernelIOCtx(ioctx_t* ctx) { - ctx->uid = ctx->auth_uid = CurrentProcess()->uid; - ctx->gid = ctx->auth_gid = CurrentProcess()->gid; + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + ctx->uid = ctx->auth_uid = process->uid; + ctx->gid = ctx->auth_gid = process->gid; ctx->copy_to_dest = CopyToKernel; ctx->copy_from_src = CopyFromKernel; } diff --git a/sortix/kernel.cpp b/sortix/kernel.cpp index 2786996e..7ffbc35e 100644 --- a/sortix/kernel.cpp +++ b/sortix/kernel.cpp @@ -72,6 +72,7 @@ #include "serialterminal.h" #include "textterminal.h" #include "elf.h" +#include "identity.h" #include "initrd.h" #include "vga.h" #include "bga.h" @@ -408,6 +409,9 @@ static void BootThread(void* /*user*/) // Initialize the sound driver. Sound::Init(); + // Initialize the identity system calls. + Identity::Init(); + // Initialize the IO system. IO::Init(); diff --git a/sortix/process.cpp b/sortix/process.cpp index c8cfb538..6805dcbf 100644 --- a/sortix/process.cpp +++ b/sortix/process.cpp @@ -122,9 +122,12 @@ namespace Sortix firstthread = NULL; threadlock = KTHREAD_MUTEX_INITIALIZER; ptrlock = KTHREAD_MUTEX_INITIALIZER; + idlock = KTHREAD_MUTEX_INITIALIZER; mmapfrom = 0x80000000UL; exitstatus = -1; pid = AllocatePID(); + uid = euid = 0; + gid = egid = 0; Put(this); } @@ -571,6 +574,13 @@ namespace Sortix clone->mtable = mtable; kthread_mutex_unlock(&ptrlock); + kthread_mutex_lock(&idlock); + clone->uid = uid; + clone->gid = gid; + clone->euid = euid; + clone->egid = egid; + kthread_mutex_unlock(&idlock); + if ( !(clone->program_image_path = String::Clone(program_image_path)) ) failure = false; diff --git a/sortix/process.h b/sortix/process.h index a6027ea8..e4381be4 100644 --- a/sortix/process.h +++ b/sortix/process.h @@ -83,8 +83,11 @@ namespace Sortix char* program_image_path; addr_t addrspace; pid_t pid; - uid_t uid; - gid_t gid; + + public: + kthread_mutex_t idlock; + uid_t uid, euid; + gid_t gid, egid; private: kthread_mutex_t ptrlock;