Add {g,s}et{e,}{g,u}id(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-01-13 02:37:14 +01:00
parent 068c12be04
commit ade239f18b
18 changed files with 476 additions and 15 deletions

View File

@ -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 \

35
libc/getegid.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
getegid.cpp
Get effective group id.
*******************************************************************************/
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
DEFN_SYSCALL0(gid_t, sys_getegid, SYSCALL_GETEGID);
extern "C" gid_t getegid()
{
return sys_getegid();
}

35
libc/geteuid.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
geteuid.cpp
Get effective user id.
*******************************************************************************/
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
DEFN_SYSCALL0(uid_t, sys_geteuid, SYSCALL_GETEUID);
extern "C" uid_t geteuid()
{
return sys_geteuid();
}

35
libc/getgid.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
getgid.cpp
Get current group id.
*******************************************************************************/
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
DEFN_SYSCALL0(gid_t, sys_getgid, SYSCALL_GETGID);
extern "C" gid_t getgid()
{
return sys_getgid();
}

35
libc/getuid.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
getuid.cpp
Get current user id.
*******************************************************************************/
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
DEFN_SYSCALL0(uid_t, sys_getuid, SYSCALL_GETUID);
extern "C" uid_t getuid()
{
return sys_getuid();
}

View File

@ -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);

35
libc/setegid.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
setegid.cpp
Set effective group id.
*******************************************************************************/
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
DEFN_SYSCALL1(gid_t, sys_setegid, SYSCALL_GETEGID, gid_t);
extern "C" int setegid(gid_t egid)
{
return sys_setegid(egid);
}

35
libc/seteuid.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
seteuid.cpp
Set effective user id.
*******************************************************************************/
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
DEFN_SYSCALL1(uid_t, sys_seteuid, SYSCALL_GETEUID, uid_t);
extern "C" int seteuid(uid_t euid)
{
return sys_seteuid(euid);
}

35
libc/setgid.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
setgid.cpp
Set current group id.
*******************************************************************************/
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
DEFN_SYSCALL1(gid_t, sys_setgid, SYSCALL_GETGID, gid_t);
extern "C" int setgid(gid_t gid)
{
return sys_setgid(gid);
}

35
libc/setuid.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
setuid.cpp
Set user id.
*******************************************************************************/
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
DEFN_SYSCALL1(uid_t, sys_setuid, SYSCALL_GETUID, uid_t);
extern "C" int setuid(uid_t uid)
{
return sys_setuid(uid);
}

View File

@ -88,6 +88,7 @@ fsfunc.o \
fs/kram.o \
fs/user.o \
fs/util.o \
identity.o \
initrd.o \
inode.o \
interlock.o \

106
sortix/identity.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
identity.cpp
System calls for managing user and group identities.
*******************************************************************************/
#include <sortix/syscallnum.h>
#include <sortix/kernel/platform.h>
#include <sortix/kernel/kthread.h>
#include <sortix/kernel/syscall.h>
#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

36
sortix/identity.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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

View File

@ -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

View File

@ -25,22 +25,27 @@
#include <sortix/kernel/platform.h>
#include <sortix/kernel/ioctx.h>
#include <sortix/kernel/copy.h>
#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;
}

View File

@ -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();

View File

@ -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;

View File

@ -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;