extern int Proc_Execve(char *File, char **ArgV, char **EnvP);
extern Uint Binary_Load(char *file, Uint *entryPoint);
extern int VFS_FInfo(int FD, void *Dest, int MaxACLs);
+extern int VFS_GetACL(int FD, void *Dest);
+extern int VFS_ChDir(char *Dest);
extern int Threads_SetName(char *NewName);
extern int Threads_GetPID();
extern int Threads_GetTID();
extern int Threads_GetUID();
extern int Threads_GetGID();
+// === PROTOTYPES ===
+ int Syscall_ValidString(Uint Addr);
+ int Syscall_Valid(int Size, Uint Addr);
+
// === CODE ===
+// TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
void SyscallHandler(tSyscallRegs *Regs)
{
Uint64 ret = 0;
Uint err = 0;
#if DEBUG
- ENTER("iThread iNum", gCurrentThread->TID, Regs->Num);
+ ENTER("iThread iNum", Threads_GetTID(), Regs->Num);
if(Regs->Num < NUM_SYSCALLS)
LOG("Syscall %s", cSYSCALL_NAMES[Regs->Num]);
LOG("Arg1: 0x%x, Arg2: 0x%x, Arg3: 0x%x", Regs->Arg1, Regs->Arg2, Regs->Arg3);
#endif
+
switch(Regs->Num)
{
// -- Exit the current thread
- case SYS_EXIT: Threads_Exit(); break;
+ case SYS_EXIT: Threads_Exit(0, Regs->Arg1); break;
// -- Put the current thread to sleep
case SYS_SLEEP: Threads_Sleep(); break;
case SYS_CLONE:
// Call clone system call
ret = Proc_Clone(&err, Regs->Arg1);
- // Change user stack if requested
+ // Change user stack if a new stack address is passed
if(ret == 0 && Regs->Arg2)
Regs->StackPointer = Regs->Arg2;
break;
case SYS_GETUID: ret = Threads_GetUID(); break;
case SYS_GETGID: ret = Threads_GetGID(); break;
+ // -- Set User/Group IDs
+ case SYS_SETUID: Log("Setting User ID to %i", Regs->Arg1); break;
+ case SYS_SETGID: Log("Setting Group ID to %i", Regs->Arg1); break;
+
// -- Send Message
case SYS_SENDMSG:
ret = Proc_SendMessage(&err, Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
ret = Proc_Execve((char*)Regs->Arg1, (char**)Regs->Arg2, (char**)Regs->Arg3);
break;
case SYS_LOADBIN:
+ if( !Syscall_ValidString(Regs->Arg1)
+ || !Syscall_Valid(sizeof(Uint), Regs->Arg2) ) {
+ err = -EINVAL;
+ ret = -1;
+ break;
+ }
ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
break;
// Virtual Filesystem
// ---
case SYS_OPEN:
+ if( !Syscall_ValidString(Regs->Arg1) ) {
+ err = -EINVAL;
+ ret = -1;
+ break;
+ }
ret = VFS_Open((char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
break;
case SYS_WRITE:
#if BITS < 64
- VFS_Write( Regs->Arg1, Regs->Arg2|((Uint64)Regs->Arg3<<32), (void*)Regs->Arg4 );
+ ret = VFS_Write( Regs->Arg1, Regs->Arg2|((Uint64)Regs->Arg3<<32), (void*)Regs->Arg4 );
#else
- VFS_Write( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
+ ret = VFS_Write( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
#endif
break;
case SYS_READ:
#if BITS < 64
- VFS_Read( Regs->Arg1, Regs->Arg2|((Uint64)Regs->Arg3<<32), (void*)Regs->Arg4 );
+ ret = VFS_Read( Regs->Arg1, Regs->Arg2|((Uint64)Regs->Arg3<<32), (void*)Regs->Arg4 );
#else
- VFS_Read( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
+ ret = VFS_Read( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
#endif
break;
case SYS_FINFO:
ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
break;
-
+
+ // Get ACL Value
+ case SYS_GETACL:
+ if( !Syscall_Valid(8, Regs->Arg1) ) {
+ err = -EINVAL;
+ ret = -1;
+ break;
+ }
+ ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
+ break;
+
+ // Read Directory
case SYS_READDIR:
+ if( !Syscall_ValidString(Regs->Arg2) ) {
+ err = -EINVAL;
+ ret = -1;
+ break;
+ }
ret = VFS_ReadDir( Regs->Arg1, (void*)Regs->Arg2 );
break;
+ // Change Directory
+ case SYS_CHDIR:
+ if( !Syscall_ValidString(Regs->Arg1) ) {
+ err = -EINVAL;
+ ret = -1;
+ break;
+ }
+ ret = VFS_ChDir( (void*)Regs->Arg1 );
+ break;
+
// -- Debug
case SYS_DEBUG:
Log((char*)Regs->Arg1,
#endif
}
+/**
+ * \fn int Syscall_ValidString(Uint Addr)
+ * \brief Checks if a memory address contains a valid string
+ */
+int Syscall_ValidString(Uint Addr)
+{
+ // Check 1st page
+ if(!MM_GetPhysAddr(Addr)) return 0;
+
+ // Traverse String
+ while(*(char*)Addr)
+ {
+ if(!MM_GetPhysAddr(Addr)) return 0;
+ // Increment string pointer
+ Addr ++;
+ }
+
+ return 1;
+}
+
+/**
+ * \fn int Syscall_Valid(int Size, Uint Addr)
+ * \brief Checks if a memory address is valid
+ */
+int Syscall_Valid(int Size, Uint Addr)
+{
+ while(Size--)
+ {
+ if(!MM_GetPhysAddr(Addr)) return 0;
+ Addr ++;
+ }
+ return 1;
+}