Compare commits
11 Commits
8ed0ff1381
...
1c4f1ab867
Author | SHA1 | Date | |
---|---|---|---|
1c4f1ab867 | |||
2269ec267c | |||
b1729689df | |||
77d331b258 | |||
c312d81de4 | |||
26b20938de | |||
0115cce750 | |||
6ddfc5ee52 | |||
16dc227a05 | |||
3effe8b004 | |||
b2f321c0b8 |
@ -1,4 +1,4 @@
|
||||
APPS := init sym sh crash uname uptime hello ps ls args cat
|
||||
APPS := init sym sh crash uname uptime hello ps ls args cat stat
|
||||
|
||||
APPS_DIR := $(LUNA_ROOT)/apps
|
||||
APPS_SRC := $(APPS_DIR)/src
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <luna.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
@ -6,10 +7,16 @@
|
||||
|
||||
void show_motd()
|
||||
{
|
||||
FILE* fp = fopen("/etc/motd", "r");
|
||||
int fd = open("/etc/motd", O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
{
|
||||
if (errno != ENOENT) { perror("open"); }
|
||||
return;
|
||||
}
|
||||
FILE* fp = fdopen(fd, "r");
|
||||
if (!fp)
|
||||
{
|
||||
if (errno != ENOENT) { perror("fopen"); }
|
||||
perror("fopen");
|
||||
return;
|
||||
}
|
||||
|
||||
|
55
apps/src/stat.c
Normal file
55
apps/src/stat.c
Normal file
@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
const char* mode_to_string(mode_t mode)
|
||||
{
|
||||
static char mode_string[12];
|
||||
|
||||
char mode_set[12] = {'s', 'g', 'r', 'w', 'x', 'r', 'w', 'x', 'r', 'w', 'x', 0};
|
||||
mode_t mode_val[12] = {S_ISUID, S_ISGID, S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP,
|
||||
S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH, S_IFMT};
|
||||
|
||||
for (int i = 0; i < 12; i++)
|
||||
{
|
||||
if (mode & mode_val[i]) mode_string[i] = mode_set[i];
|
||||
else
|
||||
mode_string[i] = '-';
|
||||
}
|
||||
|
||||
return mode_string;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc == 1)
|
||||
{
|
||||
fprintf(stderr, "Usage: stat [file]\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
struct stat st;
|
||||
if (stat(argv[1], &st) < 0)
|
||||
{
|
||||
perror("stat");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("Type: ");
|
||||
|
||||
switch (st.st_mode & S_IFMT)
|
||||
{
|
||||
case S_IFREG: puts("Regular file"); break;
|
||||
case S_IFDIR: puts("Directory"); break;
|
||||
case S_IFCHR: puts("Character device"); break;
|
||||
default: puts("Unknown"); break;
|
||||
}
|
||||
|
||||
printf("Length: %ld\n", st.st_size);
|
||||
printf("Inode: %ld\n", st.st_ino);
|
||||
printf("UID: %d\n", st.st_uid);
|
||||
printf("GID: %d\n", st.st_gid);
|
||||
printf("Mode: %s\n", mode_to_string(st.st_mode));
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef long ssize_t;
|
||||
|
||||
@ -19,7 +20,7 @@ namespace VFS
|
||||
typedef ssize_t (*node_read)(Node*, size_t, size_t, char*);
|
||||
typedef ssize_t (*node_write)(Node*, size_t, size_t, const char*);
|
||||
typedef Node* (*node_finddir)(Node*, const char*);
|
||||
typedef int (*node_mkdir)(Node*, const char*);
|
||||
typedef int (*node_mkdir)(Node*, const char*, mode_t);
|
||||
typedef int (*node_block)(Node*);
|
||||
typedef Node* (*node_readdir)(Node*, long);
|
||||
|
||||
@ -38,6 +39,9 @@ namespace VFS
|
||||
node_block block_func;
|
||||
int tty = 0;
|
||||
Node* link;
|
||||
int uid;
|
||||
int gid;
|
||||
mode_t mode;
|
||||
};
|
||||
|
||||
ssize_t read(Node* node, size_t offset, size_t length, char* buffer);
|
||||
@ -45,6 +49,9 @@ namespace VFS
|
||||
int mkdir(const char* path, const char* name);
|
||||
int mkdir(const char* pathname);
|
||||
|
||||
int do_mkdir(const char* path, const char* name, int uid, int gid, mode_t mode);
|
||||
int do_mkdir(const char* pathname, int uid, int gid, mode_t mode);
|
||||
|
||||
int would_block(Node* node);
|
||||
|
||||
void mount_root(Node* root);
|
||||
@ -61,4 +68,11 @@ namespace VFS
|
||||
Node* root();
|
||||
|
||||
Node* readdir(Node* dir, long offset);
|
||||
|
||||
bool can_execute(Node* node, int uid, int gid);
|
||||
bool can_read(Node* node, int uid, int gid);
|
||||
bool can_write(Node* node, int uid, int gid);
|
||||
|
||||
bool is_setuid(Node* node);
|
||||
bool is_setgid(Node* node);
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
#define EBADF 9
|
||||
#define EAGAIN 11
|
||||
#define ENOMEM 12
|
||||
#define EACCES 13
|
||||
#define EFAULT 14
|
||||
#define EEXIST 17
|
||||
#define ENOTDIR 20
|
||||
|
@ -28,6 +28,8 @@
|
||||
#define SYS_getdents 22
|
||||
#define SYS_stat 23
|
||||
#define SYS_dup2 24
|
||||
#define SYS_setuid 25
|
||||
#define SYS_setgid 26
|
||||
|
||||
struct stat;
|
||||
struct pstat;
|
||||
@ -54,7 +56,7 @@ void sys_execv(Context* context, const char* pathname, char** argv);
|
||||
void sys_fcntl(Context* context, int fd, int command, uintptr_t arg);
|
||||
void sys_mprotect(Context* context, void* address, size_t size, int prot);
|
||||
void sys_clock(Context* context);
|
||||
void sys_mkdir(Context* context, const char* filename);
|
||||
void sys_mkdir(Context* context, const char* filename, mode_t mode);
|
||||
void sys_fork(Context* context);
|
||||
void sys_waitpid(Context* context, long pid, int* wstatus, int options);
|
||||
void sys_access(Context* context, const char* path, int amode);
|
||||
@ -62,4 +64,6 @@ void sys_fstat(Context* context, int fd, struct stat* buf);
|
||||
void sys_pstat(Context* context, long pid, struct pstat* buf);
|
||||
void sys_getdents(Context* context, int fd, struct luna_dirent* buf, size_t count);
|
||||
void sys_stat(Context* context, const char* path, struct stat* buf);
|
||||
void sys_dup2(Context* context, int fd, int fd2);
|
||||
void sys_dup2(Context* context, int fd, int fd2);
|
||||
void sys_setuid(Context* context, int new_uid, int new_euid);
|
||||
void sys_setgid(Context* context, int new_gid, int new_egid);
|
@ -36,6 +36,11 @@ struct Task
|
||||
|
||||
int64_t task_time = 0;
|
||||
|
||||
int uid;
|
||||
int euid;
|
||||
int gid;
|
||||
int egid;
|
||||
|
||||
Task* next_task = nullptr;
|
||||
Task* prev_task = nullptr;
|
||||
|
||||
@ -98,6 +103,8 @@ struct Task
|
||||
|
||||
Descriptor* descriptor_from_fd(int fd, int& error);
|
||||
|
||||
bool is_superuser();
|
||||
|
||||
private:
|
||||
void resume_read();
|
||||
void resume_wait();
|
||||
|
@ -157,7 +157,43 @@ int VFS::mkdir(const char* path, const char* name)
|
||||
kwarnln("Already exists");
|
||||
return -EEXIST;
|
||||
}
|
||||
return node->mkdir_func(node, name);
|
||||
return node->mkdir_func(node, name, 0755);
|
||||
}
|
||||
|
||||
int VFS::do_mkdir(const char* path, const char* name, int uid, int gid, mode_t mode)
|
||||
{
|
||||
Node* node = resolve_path(path, vfs_root);
|
||||
if (!node)
|
||||
{
|
||||
kwarnln("Attempting to mkdir in %s, which does not exist", path);
|
||||
return -ENOENT;
|
||||
}
|
||||
if (node->type != VFS_DIRECTORY)
|
||||
{
|
||||
kwarnln("Attempting to mkdir in %s, which is not a directory!!", path);
|
||||
return -ENOTDIR;
|
||||
}
|
||||
if (!node->mkdir_func)
|
||||
{
|
||||
kwarnln("Chosen node does not support mkdir()");
|
||||
return -ENOTSUP;
|
||||
}
|
||||
if (!node->find_func)
|
||||
{
|
||||
kwarnln("Chosen node does not support finddir()");
|
||||
return -ENOTSUP;
|
||||
}
|
||||
if (node->find_func(node, name) != nullptr)
|
||||
{
|
||||
kwarnln("Already exists");
|
||||
return -EEXIST;
|
||||
}
|
||||
if (!can_write(node, uid, gid))
|
||||
{
|
||||
kwarnln("Not enough permissions");
|
||||
return -EACCES;
|
||||
}
|
||||
return node->mkdir_func(node, name, mode);
|
||||
}
|
||||
|
||||
int VFS::mkdir(const char* pathname)
|
||||
@ -178,6 +214,24 @@ int VFS::mkdir(const char* pathname)
|
||||
return result;
|
||||
}
|
||||
|
||||
int VFS::do_mkdir(const char* pathname, int uid, int gid, mode_t mode)
|
||||
{
|
||||
char* bstr = strdup(pathname);
|
||||
char* dstr = strdup(pathname);
|
||||
|
||||
char* base = basename(bstr);
|
||||
char* dir = dirname(dstr);
|
||||
|
||||
kdbgln("mkdir(): creating %s in directory %s", base, dir);
|
||||
|
||||
int result = do_mkdir(dir, base, uid, gid, mode);
|
||||
|
||||
kfree(bstr);
|
||||
kfree(dstr);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool VFS::exists(const char* pathname)
|
||||
{
|
||||
return resolve_path(pathname) != nullptr;
|
||||
@ -208,4 +262,35 @@ VFS::Node* VFS::readdir(VFS::Node* dir, long offset)
|
||||
if (!dir) return 0;
|
||||
if (!dir->readdir_func) return 0;
|
||||
return dir->readdir_func(dir, offset);
|
||||
}
|
||||
|
||||
bool VFS::can_execute(VFS::Node* node, int uid, int gid)
|
||||
{
|
||||
if (uid == node->uid) return node->mode & 0100;
|
||||
if (gid == node->gid) return node->mode & 0010;
|
||||
return node->mode & 0001;
|
||||
}
|
||||
|
||||
bool VFS::can_write(VFS::Node* node, int uid, int gid)
|
||||
{
|
||||
if (uid == node->uid) return node->mode & 0200;
|
||||
if (gid == node->gid) return node->mode & 0020;
|
||||
return node->mode & 0002;
|
||||
}
|
||||
|
||||
bool VFS::can_read(VFS::Node* node, int uid, int gid)
|
||||
{
|
||||
if (uid == node->uid) return node->mode & 0400;
|
||||
if (gid == node->gid) return node->mode & 0040;
|
||||
return node->mode & 0004;
|
||||
}
|
||||
|
||||
bool VFS::is_setuid(VFS::Node* node)
|
||||
{
|
||||
return node->mode & 04000;
|
||||
}
|
||||
|
||||
bool VFS::is_setgid(VFS::Node* node)
|
||||
{
|
||||
return node->mode & 02000;
|
||||
}
|
@ -14,6 +14,8 @@ VFS::Node* ConsoleDevice::create_new(const char* devname)
|
||||
dev->type = VFS_DEVICE;
|
||||
dev->flags = 0;
|
||||
dev->tty = 1;
|
||||
dev->uid = dev->gid = 0;
|
||||
dev->mode = 0222;
|
||||
strncpy(dev->name, devname, sizeof(dev->name));
|
||||
return dev;
|
||||
}
|
||||
|
@ -25,6 +25,8 @@ VFS::Node* DeviceFS::get()
|
||||
devfs_root->type = VFS_DIRECTORY;
|
||||
devfs_root->find_func = DeviceFS::finddir;
|
||||
devfs_root->readdir_func = DeviceFS::readdir;
|
||||
devfs_root->mode = 0755;
|
||||
devfs_root->uid = devfs_root->gid = 0;
|
||||
strncpy(devfs_root->name, "dev", sizeof(devfs_root->name));
|
||||
|
||||
devfs_files[devfs_file_count++] = VersionDevice::create_new("version");
|
||||
|
@ -26,6 +26,8 @@ VFS::Node* KeyboardDevice::create_new(const char* devname)
|
||||
dev->type = VFS_DEVICE;
|
||||
dev->flags = 0;
|
||||
dev->tty = 1;
|
||||
dev->uid = dev->gid = 0;
|
||||
dev->mode = 0444;
|
||||
strncpy(dev->name, devname, sizeof(dev->name));
|
||||
return dev;
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ VFS::Node* NullDevice::create_new(const char* devname)
|
||||
dev->length = 0;
|
||||
dev->type = VFS_DEVICE;
|
||||
dev->flags = 0;
|
||||
dev->uid = dev->gid = 0;
|
||||
dev->mode = 0666;
|
||||
strncpy(dev->name, devname, sizeof(dev->name));
|
||||
return dev;
|
||||
}
|
||||
|
@ -14,6 +14,8 @@ VFS::Node* RandomDevice::create_new(const char* devname)
|
||||
dev->length = 0;
|
||||
dev->type = VFS_DEVICE;
|
||||
dev->flags = 0;
|
||||
dev->uid = dev->gid = 0;
|
||||
dev->mode = 0444;
|
||||
strncpy(dev->name, devname, sizeof(dev->name));
|
||||
return dev;
|
||||
}
|
||||
|
@ -13,6 +13,8 @@ VFS::Node* SerialDevice::create_new(const char* devname)
|
||||
dev->length = 0;
|
||||
dev->type = VFS_DEVICE;
|
||||
dev->flags = 0;
|
||||
dev->uid = dev->gid = 0;
|
||||
dev->mode = 0200;
|
||||
strncpy(dev->name, devname, sizeof(dev->name));
|
||||
return dev;
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ VFS::Node* UptimeDevice::create_new(const char* devname)
|
||||
dev->length = 0;
|
||||
dev->type = VFS_DEVICE;
|
||||
dev->flags = 0;
|
||||
dev->uid = dev->gid = 0;
|
||||
dev->mode = 0444;
|
||||
strncpy(dev->name, devname, sizeof(dev->name));
|
||||
return dev;
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ VFS::Node* VersionDevice::create_new(const char* devname)
|
||||
dev->length = strlen(moon_version()) + 5;
|
||||
dev->type = VFS_DEVICE;
|
||||
dev->flags = 0;
|
||||
dev->uid = dev->gid = 0;
|
||||
dev->mode = 0444;
|
||||
strncpy(dev->name, devname, sizeof(dev->name));
|
||||
return dev;
|
||||
}
|
||||
|
@ -195,7 +195,7 @@ VFS::Node* initrd_read_dir(VFS::Node* node, long offset)
|
||||
return dir.files[offset];
|
||||
}
|
||||
|
||||
int initrd_mkdir(VFS::Node* node, const char* name) // FIXME: Return proper error numbers.
|
||||
int initrd_mkdir(VFS::Node* node, const char* name, mode_t mode) // FIXME: Return proper error numbers.
|
||||
{
|
||||
if (total_dirs >= 32)
|
||||
{
|
||||
@ -225,6 +225,8 @@ int initrd_mkdir(VFS::Node* node, const char* name) // FIXME: Return proper erro
|
||||
new_node.mkdir_func = initrd_mkdir;
|
||||
new_node.length = 0;
|
||||
new_node.type = VFS_DIRECTORY;
|
||||
new_node.mode = mode;
|
||||
new_node.uid = new_node.gid = 0;
|
||||
strncpy(new_node.name, name, sizeof(new_node.name));
|
||||
InitRD::Directory dir;
|
||||
strncpy(dir.name, name, sizeof(dir.name));
|
||||
@ -282,6 +284,8 @@ static bool initrd_register_dir(InitRD::Directory& dir, uint64_t inode)
|
||||
node.mkdir_func = initrd_mkdir;
|
||||
node.readdir_func = initrd_read_dir;
|
||||
node.length = 0;
|
||||
node.mode = 0755;
|
||||
node.uid = node.gid = 0;
|
||||
strncpy(node.name, buffer, sizeof(node.name));
|
||||
strncpy(dir.name, buffer, sizeof(dir.name));
|
||||
|
||||
@ -341,6 +345,8 @@ static bool initrd_register_file(InitRD::File& f, uint64_t inode)
|
||||
node.read_func = initrd_read;
|
||||
node.length = f.size;
|
||||
node.type = VFS_FILE;
|
||||
node.mode = 0555;
|
||||
node.uid = node.gid = 0;
|
||||
strncpy(node.name, buffer, sizeof(node.name));
|
||||
strncpy(f.name, buffer, sizeof(f.name));
|
||||
|
||||
@ -383,6 +389,8 @@ static void initrd_initialize_root()
|
||||
initrd_root.length = 0;
|
||||
initrd_root.inode = 0;
|
||||
initrd_root.type |= VFS_DIRECTORY;
|
||||
initrd_root.mode = 0755;
|
||||
initrd_root.uid = initrd_root.gid = 0;
|
||||
InitRD::Directory& root = dirs[0];
|
||||
total_dirs++;
|
||||
strncpy(initrd_root.name, "initrd", sizeof(initrd_root.name));
|
||||
|
@ -27,16 +27,19 @@ void Syscall::entry(Context* context)
|
||||
case SYS_fcntl: sys_fcntl(context, (int)context->rdi, (int)context->rsi, context->rdx); break;
|
||||
case SYS_mprotect: sys_mprotect(context, (void*)context->rdi, context->rsi, (int)context->rdx); break;
|
||||
case SYS_clock: sys_clock(context); break;
|
||||
case SYS_mkdir: sys_mkdir(context, (const char*)context->rdi); break;
|
||||
case SYS_mkdir: sys_mkdir(context, (const char*)context->rdi, (mode_t)context->rsi); break;
|
||||
case SYS_fork: sys_fork(context); break;
|
||||
case SYS_waitpid: sys_waitpid(context, (long)context->rdi, (int*)context->rsi, (int)context->rdx); break;
|
||||
case SYS_access: sys_access(context, (const char*)context->rdi, (int)context->rsi); break;
|
||||
case SYS_fstat: sys_fstat(context, (int)context->rdi, (struct stat*)context->rsi); break;
|
||||
case SYS_stat: sys_stat(context, (const char*)context->rdi, (struct stat*)context->rsi); break;
|
||||
case SYS_pstat: sys_pstat(context, (long)context->rdi, (struct pstat*)context->rsi); break;
|
||||
case SYS_getdents:
|
||||
sys_getdents(context, (int)context->rdi, (struct luna_dirent*)context->rsi, (size_t)context->rdx);
|
||||
break;
|
||||
case SYS_dup2: sys_dup2(context, (int)context->rdi, (int)context->rsi); break;
|
||||
case SYS_setuid: sys_setuid(context, (int)context->rdi, (int)context->rsi); break;
|
||||
case SYS_setgid: sys_setgid(context, (int)context->rdi, (int)context->rsi); break;
|
||||
default: context->rax = -ENOSYS; break;
|
||||
}
|
||||
VMM::exit_syscall_context();
|
||||
|
@ -45,6 +45,11 @@ void sys_fork(Context* context)
|
||||
|
||||
child->ppid = parent->id;
|
||||
|
||||
child->uid = parent->uid;
|
||||
child->euid = parent->euid;
|
||||
child->gid = parent->gid;
|
||||
child->egid = parent->egid;
|
||||
|
||||
child->regs.rax = 0;
|
||||
context->rax = child->id;
|
||||
|
||||
@ -92,6 +97,13 @@ void sys_execv(Context* context, const char* pathname, char** argv)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!VFS::can_execute(program, Scheduler::current_task()->euid, Scheduler::current_task()->egid))
|
||||
{
|
||||
kfree(kpathname);
|
||||
context->rax = -EACCES;
|
||||
return;
|
||||
}
|
||||
|
||||
long memusage;
|
||||
if ((memusage = ELFLoader::check_elf_image(program)) < 0)
|
||||
{
|
||||
@ -211,6 +223,9 @@ void sys_execv(Context* context, const char* pathname, char** argv)
|
||||
ASSERT(image); // If check_elf_image succeeded, load_elf_from_vfs MUST succeed, unless something has gone terribly
|
||||
// wrong.
|
||||
|
||||
if (VFS::is_setuid(program)) task->uid = program->uid;
|
||||
if (VFS::is_setgid(program)) task->gid = program->gid;
|
||||
|
||||
strlcpy(task->name, kpathname, sizeof(task->name));
|
||||
|
||||
Scheduler::reset_task(task, image);
|
||||
|
@ -3,6 +3,10 @@
|
||||
|
||||
#define ID_PID 0
|
||||
#define ID_PPID 1
|
||||
#define ID_UID 2
|
||||
#define ID_EUID 3
|
||||
#define ID_GID 4
|
||||
#define ID_EGID 5
|
||||
|
||||
void sys_getprocid(Context* context, int field)
|
||||
{
|
||||
@ -16,9 +20,77 @@ void sys_getprocid(Context* context, int field)
|
||||
context->rax = Scheduler::current_task()->ppid;
|
||||
return;
|
||||
}
|
||||
else if (field == ID_UID)
|
||||
{
|
||||
context->rax = Scheduler::current_task()->uid;
|
||||
return;
|
||||
}
|
||||
else if (field == ID_EUID)
|
||||
{
|
||||
context->rax = Scheduler::current_task()->euid;
|
||||
return;
|
||||
}
|
||||
else if (field == ID_GID)
|
||||
{
|
||||
context->rax = Scheduler::current_task()->gid;
|
||||
return;
|
||||
}
|
||||
else if (field == ID_EGID)
|
||||
{
|
||||
context->rax = Scheduler::current_task()->egid;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
context->rax = -EINVAL;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void sys_setuid(Context* context, int new_uid, int new_euid)
|
||||
{
|
||||
Task* current_task = Scheduler::current_task();
|
||||
|
||||
if (!current_task->is_superuser())
|
||||
{
|
||||
if (new_uid != current_task->uid && new_uid != current_task->euid)
|
||||
{
|
||||
context->rax = -EPERM;
|
||||
return;
|
||||
}
|
||||
if (new_euid != current_task->euid && new_euid != current_task->uid)
|
||||
{
|
||||
context->rax = -EPERM;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
current_task->uid = new_uid;
|
||||
current_task->euid = new_euid;
|
||||
|
||||
context->rax = 0;
|
||||
}
|
||||
|
||||
void sys_setgid(Context* context, int new_gid, int new_egid)
|
||||
{
|
||||
Task* current_task = Scheduler::current_task();
|
||||
|
||||
if (!current_task->is_superuser())
|
||||
{
|
||||
if (new_gid != current_task->gid && new_gid != current_task->egid)
|
||||
{
|
||||
context->rax = -EPERM;
|
||||
return;
|
||||
}
|
||||
if (new_egid != current_task->egid && new_egid != current_task->gid)
|
||||
{
|
||||
context->rax = -EPERM;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
current_task->gid = new_gid;
|
||||
current_task->egid = new_egid;
|
||||
|
||||
context->rax = 0;
|
||||
}
|
@ -5,7 +5,6 @@
|
||||
#include "sys/UserMemory.h"
|
||||
#include "thread/Scheduler.h"
|
||||
|
||||
typedef unsigned long off_t;
|
||||
typedef unsigned short mode_t;
|
||||
typedef unsigned long ino_t;
|
||||
|
||||
@ -15,6 +14,8 @@ struct stat // FIXME: This struct is quite stubbed out.
|
||||
mode_t st_mode;
|
||||
off_t st_size;
|
||||
int st_dev; // FIXME: Implement this.
|
||||
uid_t st_uid;
|
||||
gid_t st_gid;
|
||||
};
|
||||
|
||||
void do_stat(Context* context, VFS::Node* node, struct stat* buf)
|
||||
@ -26,8 +27,10 @@ void do_stat(Context* context, VFS::Node* node, struct stat* buf)
|
||||
return;
|
||||
}
|
||||
kstat->st_ino = node->inode;
|
||||
kstat->st_mode = (mode_t)node->type;
|
||||
kstat->st_mode = node->mode | ((1 << (node->type)) * 010000);
|
||||
kstat->st_size = node->length;
|
||||
kstat->st_uid = node->uid;
|
||||
kstat->st_gid = node->gid;
|
||||
release_user_ref(kstat);
|
||||
context->rax = 0;
|
||||
}
|
||||
|
@ -207,6 +207,24 @@ void sys_open(Context* context, const char* filename, int flags, mode_t) // FIXM
|
||||
return;
|
||||
}
|
||||
|
||||
if (can_read && !VFS::can_read(node, current_task->euid, current_task->egid))
|
||||
{
|
||||
kwarnln("open failed because process with uid %d and gid %d couldn't open file %s with mode %d for reading",
|
||||
current_task->euid, current_task->egid, kfilename, node->mode);
|
||||
kfree(kfilename);
|
||||
context->rax = -EACCES;
|
||||
return;
|
||||
}
|
||||
|
||||
if (can_write && !VFS::can_write(node, current_task->euid, current_task->egid))
|
||||
{
|
||||
kwarnln("open failed because process with uid %d and gid %d couldn't open file %s with mode %d for writing",
|
||||
current_task->euid, current_task->egid, kfilename, node->mode);
|
||||
kfree(kfilename);
|
||||
context->rax = -EACCES;
|
||||
return;
|
||||
}
|
||||
|
||||
bool able_to_block = (flags & OPEN_NONBLOCK) == 0;
|
||||
bool close_on_exec = (flags & OPEN_CLOEXEC) > 0;
|
||||
|
||||
@ -304,7 +322,7 @@ void sys_close(Context* context, int fd)
|
||||
return;
|
||||
}
|
||||
|
||||
void sys_mkdir(Context* context, const char* filename)
|
||||
void sys_mkdir(Context* context, const char* filename, mode_t mode)
|
||||
{
|
||||
char* kfilename = strdup_from_user(filename);
|
||||
if (!kfilename)
|
||||
@ -313,7 +331,9 @@ void sys_mkdir(Context* context, const char* filename)
|
||||
return;
|
||||
}
|
||||
|
||||
int rc = VFS::mkdir(kfilename);
|
||||
Task* current_task = Scheduler::current_task();
|
||||
|
||||
int rc = VFS::do_mkdir(kfilename, current_task->euid, current_task->egid, mode);
|
||||
|
||||
kfree(kfilename);
|
||||
|
||||
|
@ -115,6 +115,7 @@ void Scheduler::add_kernel_task(const char* taskname, void (*task)(void))
|
||||
new_task->user_task = false;
|
||||
new_task->id = free_pid++;
|
||||
new_task->ppid = 0;
|
||||
new_task->uid = new_task->euid = new_task->gid = new_task->egid = 0;
|
||||
new_task->regs.rip = (uint64_t)task;
|
||||
new_task->allocated_stack =
|
||||
(uint64_t)MemoryManager::get_pages(TASK_PAGES_IN_STACK); // 16 KB is enough for everyone, right?
|
||||
@ -159,6 +160,7 @@ long Scheduler::load_user_task(const char* filename)
|
||||
memset(&new_task->regs, 0, sizeof(Context));
|
||||
new_task->id = free_pid++;
|
||||
new_task->ppid = 0;
|
||||
new_task->uid = new_task->euid = new_task->gid = new_task->egid = 0;
|
||||
if (!new_task->allocator.init())
|
||||
{
|
||||
delete new_task;
|
||||
|
@ -125,4 +125,9 @@ Descriptor* Task::descriptor_from_fd(int fd, int& error)
|
||||
return nullptr;
|
||||
}
|
||||
return &files[fd];
|
||||
}
|
||||
|
||||
bool Task::is_superuser()
|
||||
{
|
||||
return euid == 0;
|
||||
}
|
@ -3,5 +3,9 @@
|
||||
|
||||
#define ID_PID 0
|
||||
#define ID_PPID 1
|
||||
#define ID_UID 2
|
||||
#define ID_EUID 3
|
||||
#define ID_GID 4
|
||||
#define ID_EGID 5
|
||||
|
||||
#endif
|
@ -5,4 +5,6 @@
|
||||
#define __VFS_DIRECTORY 0x1
|
||||
#define __VFS_DEVICE 0x2
|
||||
|
||||
#define __VFS_TO_IFMT(type) ((1 << type) * 010000)
|
||||
|
||||
#endif
|
@ -10,18 +10,49 @@ struct stat // FIXME: This struct is quite stubbed out.
|
||||
mode_t st_mode;
|
||||
off_t st_size;
|
||||
int st_dev; // Not implemented.
|
||||
uid_t st_uid;
|
||||
gid_t st_gid;
|
||||
};
|
||||
|
||||
#define S_ISDIR(mode) (((mode)&0xf) == __VFS_DIRECTORY)
|
||||
#define S_ISREG(mode) (((mode)&0xf) == __VFS_FILE)
|
||||
#define S_ISCHR(mode) (((mode)&0xf) == __VFS_DEVICE)
|
||||
/* Type of file. */
|
||||
#define S_IFMT 070000
|
||||
/* Directory. */
|
||||
#define S_IFDIR __VFS_TO_IFMT(__VFS_DIRECTORY)
|
||||
/* Regular file. */
|
||||
#define S_IFREG __VFS_TO_IFMT(__VFS_FILE)
|
||||
/* Character device. */
|
||||
#define S_IFCHR __VFS_TO_IFMT(__VFS_DEVICE)
|
||||
|
||||
#define __S_IFCMP(mode, value) (mode & S_IFMT) == value
|
||||
|
||||
/* Is it a directory? */
|
||||
#define S_ISDIR(mode) (__S_IFCMP((mode), S_IFDIR))
|
||||
/* Is it a regular file? */
|
||||
#define S_ISREG(mode) (__S_IFCMP((mode), S_IFREG))
|
||||
/* Is it a character device? */
|
||||
#define S_ISCHR(mode) (__S_IFCMP((mode), S_IFCHR))
|
||||
|
||||
#define S_IRWXU 0700
|
||||
#define S_IRUSR 0400
|
||||
#define S_IWUSR 0200
|
||||
#define S_IXUSR 0100
|
||||
#define S_IRWXG 070
|
||||
#define S_IRGRP 040
|
||||
#define S_IWGRP 020
|
||||
#define S_IXGRP 010
|
||||
#define S_IRWXO 07
|
||||
#define S_IROTH 04
|
||||
#define S_IWOTH 02
|
||||
#define S_IXOTH 01
|
||||
#define S_ISUID 04000
|
||||
#define S_ISGID 02000
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* Creates a new directory at the path pathname. FIXME: For now, mode is ignored. */
|
||||
/* Creates a new directory at the path pathname with the specified mode. */
|
||||
int mkdir(const char* pathname, mode_t mode);
|
||||
|
||||
/* Returns information about the file pointed to by fd in buf. */
|
||||
|
@ -26,5 +26,7 @@
|
||||
#define SYS_getdents 22
|
||||
#define SYS_stat 23
|
||||
#define SYS_dup2 24
|
||||
#define SYS_setuid 25
|
||||
#define SYS_setgid 26
|
||||
|
||||
#endif
|
@ -28,4 +28,10 @@ typedef long int time_t;
|
||||
/* Type representing signed time in microseconds. */
|
||||
typedef long int suseconds_t;
|
||||
|
||||
/* Type representing a user ID. */
|
||||
typedef int uid_t;
|
||||
|
||||
/* Type representing a group ID. */
|
||||
typedef int gid_t;
|
||||
|
||||
#endif
|
@ -3,13 +3,14 @@
|
||||
#include <luna.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
FILE* stderr;
|
||||
FILE* stdout;
|
||||
FILE* stdin;
|
||||
|
||||
void file_read_buf(FILE* stream)
|
||||
static void file_read_buf(FILE* stream)
|
||||
{
|
||||
if (!stream->f_buf)
|
||||
{
|
||||
@ -31,6 +32,29 @@ void file_read_buf(FILE* stream)
|
||||
stream->f_bufsize = nread;
|
||||
}
|
||||
|
||||
static int file_parse_mode(const char* mode)
|
||||
{
|
||||
int flags = 0;
|
||||
switch (mode[0])
|
||||
{
|
||||
case 'r': flags |= O_RDONLY; break;
|
||||
case 'w':
|
||||
flags |= O_WRONLY;
|
||||
flags |= O_CREAT;
|
||||
flags |= O_TRUNC;
|
||||
break;
|
||||
case 'a':
|
||||
flags |= O_WRONLY;
|
||||
flags |= O_CREAT;
|
||||
flags |= O_APPEND;
|
||||
break;
|
||||
default: errno = EINVAL; return -1;
|
||||
}
|
||||
|
||||
if (strchr(mode, '+')) flags |= O_RDWR;
|
||||
return flags;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int fclose(FILE* stream)
|
||||
@ -56,7 +80,9 @@ extern "C"
|
||||
|
||||
FILE* fopen(const char* pathname, const char* mode)
|
||||
{
|
||||
int fd = open(pathname, O_RDWR); // FIXME: Use the mode string.
|
||||
int flags = file_parse_mode(mode);
|
||||
if (flags < 0) return NULL;
|
||||
int fd = open(pathname, flags, 0666); // If we create the file, create it as rw-rw-rw-.
|
||||
if (fd < 0) { return 0; }
|
||||
return fdopen(fd, mode);
|
||||
}
|
||||
@ -78,10 +104,12 @@ extern "C"
|
||||
return stream;
|
||||
}
|
||||
|
||||
FILE* freopen(const char* pathname, const char*,
|
||||
FILE* freopen(const char* pathname, const char* mode,
|
||||
FILE* stream) // FIXME: If pathname is NULL, open the original file with the new mode.
|
||||
{
|
||||
int fd = open(pathname, O_RDWR); // FIXME: Use the mode string.
|
||||
int flags = file_parse_mode(mode);
|
||||
if (flags < 0) return NULL;
|
||||
int fd = open(pathname, flags, 0666); // If we create the file, create it as rw-rw-rw-.
|
||||
if (fd < 0) { return 0; }
|
||||
|
||||
fflush(stream); // To make it future-proof.
|
||||
|
@ -57,8 +57,8 @@ extern char* program_invocation_name;
|
||||
extern "C" void initialize_libc(int, char** argv)
|
||||
{
|
||||
check_for_file(STDIN_FILENO, &stdin, "/dev/kbd", "r");
|
||||
check_for_file(STDOUT_FILENO, &stdout, "/dev/console", "rw");
|
||||
check_for_file(STDERR_FILENO, &stderr, "/dev/console", "rw");
|
||||
check_for_file(STDOUT_FILENO, &stdout, "/dev/console", "w");
|
||||
check_for_file(STDERR_FILENO, &stderr, "/dev/console", "w");
|
||||
|
||||
if (argv) program_invocation_name = argv[0];
|
||||
|
||||
|
@ -4,9 +4,9 @@
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int mkdir(const char* pathname, mode_t)
|
||||
int mkdir(const char* pathname, mode_t mode)
|
||||
{
|
||||
return (int)__lc_fast_syscall1(SYS_mkdir, pathname);
|
||||
return (int)__lc_fast_syscall2(SYS_mkdir, pathname, mode);
|
||||
}
|
||||
|
||||
int fstat(int fd, struct stat* buf)
|
||||
|
@ -26,6 +26,8 @@ extern "C" long syscall(long number, ...)
|
||||
case SYS_fstat:
|
||||
case SYS_stat:
|
||||
case SYS_dup2:
|
||||
case SYS_setuid:
|
||||
case SYS_setgid:
|
||||
case SYS_pstat: {
|
||||
arg arg0 = va_arg(ap, arg);
|
||||
arg arg1 = va_arg(ap, arg);
|
||||
|
Loading…
Reference in New Issue
Block a user