Skip to content

Commit

Permalink
kthd: verify permissions for chdir()
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Oct 1, 2024
1 parent 6ad267e commit 400fa58
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
41 changes: 41 additions & 0 deletions kthd/src/chdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* luxOS - a unix-like operating system
* Omar Elghoul, 2024
*
* kthd: Kernel Thread Helper Daemon
*/

#include <liblux/liblux.h>
#include <sys/stat.h>
#include <errno.h>

void kthdChdir(ChdirCommand *cmd) {
cmd->header.header.response = 1;
cmd->header.header.length = sizeof(ChdirCommand);

// simply issue a stat() syscall and ensure the directory is valid
struct stat st;
if(stat(cmd->path, &st)) {
cmd->header.header.status = -1*errno;
luxSendLumen(cmd);
return;
}

if((st.st_mode & S_IFMT) != S_IFDIR) {
cmd->header.header.status = -ENOTDIR;
luxSendLumen(cmd);
return;
}

// ensure the requesting process has execute permissions on the directory
cmd->header.header.status = 0;
if(cmd->uid == st.st_uid) {
if(!(st.st_mode & S_IXUSR)) cmd->header.header.status = -EPERM;
} else if(cmd->gid == st.st_gid) {
if(!(st.st_mode & S_IXGRP)) cmd->header.header.status = -EPERM;
} else {
if(!(st.st_mode & S_IXOTH)) cmd->header.header.status = -EPERM;
}

luxSendLumen(cmd);
}
2 changes: 2 additions & 0 deletions kthd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdlib.h>

void kthdExec(ExecCommand *);
void kthdChdir(ChdirCommand *);

int main() {
luxInit("kthd");
Expand Down Expand Up @@ -41,6 +42,7 @@ int main() {

switch(msg->header.command) {
case COMMAND_EXEC: kthdExec((ExecCommand *) msg); break;
case COMMAND_CHDIR: kthdChdir((ChdirCommand *) msg); break;
default:
luxLogf(KPRINT_LEVEL_WARNING, "unimplemented command 0x%04X, dropping message...\n", msg->header.command);
}
Expand Down

0 comments on commit 400fa58

Please sign in to comment.