Started adding sanity checks to syscalls. Added SYS_CHDIR and helpers. Used SYS_CHDIR...
authorJohn Hodge <[email protected]>
Sat, 26 Sep 2009 12:13:03 +0000 (20:13 +0800)
committerJohn Hodge <[email protected]>
Sat, 26 Sep 2009 12:13:03 +0000 (20:13 +0800)
Kernel/include/syscalls.h
Kernel/include/syscalls.inc.asm
Kernel/syscalls.c
Kernel/syscalls.lst
Kernel/vfs/open.c
Usermode/Applications/CLIShell_src/main.c
Usermode/include/acess/sys.h

index b2c13cf..743a1ca 100644 (file)
@@ -53,6 +53,8 @@ enum eSyscalls {
        SYS_FINFO,      // 75 - Get file information
        SYS_SEEK,       // 76 - Seek to a new position in the file
        SYS_TELL,       // 77 - Return the current file position
+       SYS_CHDIR,      // 78 - Change current directory
+       SYS_GETCWD,     // 79 - Get current directory
        NUM_SYSCALLS,
        SYS_DEBUG = 0x100       // 0x100 - Print a debug string
 };
@@ -71,6 +73,6 @@ static const char *cSYSCALL_NAMES[] = {
        "","","","","SYS_OPEN","SYS_REOPEN",
        "SYS_CLOSE","SYS_READ","SYS_WRITE","SYS_IOCTL","SYS_READDIR","SYS_MKDIR",
        "SYS_SYMLINK","SYS_GETACL","SYS_SETACL","SYS_FINFO","SYS_SEEK","SYS_TELL",
-       ""
+       "SYS_CHDIR","SYS_GETCWD",""
 };
 #endif
index 39b8193..c33cd34 100644 (file)
@@ -49,3 +49,5 @@
 %define SYS_FINFO      75      ; Get file information
 %define SYS_SEEK       76      ; Seek to a new position in the file
 %define SYS_TELL       77      ; Return the current file position
+%define SYS_CHDIR      78      ; Change current directory
+%define SYS_GETCWD     79      ; Get current directory
index 2d52b7e..e6d8ed3 100644 (file)
@@ -18,12 +18,17 @@ 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)
@@ -36,6 +41,7 @@ void SyscallHandler(tSyscallRegs *Regs)
                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
@@ -145,15 +151,37 @@ void SyscallHandler(tSyscallRegs *Regs)
        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,
@@ -182,3 +210,36 @@ void SyscallHandler(tSyscallRegs *Regs)
        #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;
+}
index 280656e..8b38f78 100644 (file)
@@ -54,3 +54,5 @@ SYS_SETACL    Set an ACL Value
 SYS_FINFO      Get file information
 SYS_SEEK       Seek to a new position in the file
 SYS_TELL       Return the current file position
+SYS_CHDIR      Change current directory
+SYS_GETCWD     Get current directory
index 3ac3fa3..54838e9 100644 (file)
@@ -495,6 +495,41 @@ void VFS_Close(int FD)
        h->Node = NULL;
 }
 
+/**
+ * \fn int VFS_ChDir(char *New)
+ * \brief Change current working directory
+ */
+int VFS_ChDir(char *New)
+{
+       char    *buf;
+       tVFS_Node       *node;
+       
+       // Create Absolute
+       buf = VFS_GetAbsPath(New);
+       if(buf == NULL) return -1;
+       
+       // Check if path is valid
+       node = VFS_ParsePath(buf, NULL);
+       if(!node)       return -1;
+       
+       // Check if is a directory
+       if( !(node->Flags & VFS_FFLAG_DIRECTORY) ) {
+               if(node->Close) node->Close(node);
+               return -1;
+       }
+       // Close node
+       if(node->Close) node->Close(node);
+       
+       // Copy over
+       strcpy(buf, New);
+       
+       // Free old and set new
+       free( CFGPTR(CFG_VFS_CWD) );
+       CFGPTR(CFG_VFS_CWD) = buf;
+       
+       return 1;
+}
+
 /**
  * \fn tVFS_Handle *VFS_GetHandle(int FD)
  * \brief Gets a pointer to the handle information structure
index aa1d9ab..5c431bd 100644 (file)
@@ -339,6 +339,9 @@ void Command_Cd(int argc, char **argv)
        free(gsCurrentDirectory);\r
        gsCurrentDirectory = malloc(strlen(tmpPath)+1);\r
        strcpy(gsCurrentDirectory, tmpPath);\r
+       \r
+       // Register change with kernel\r
+       chdir( gsCurrentDirectory );\r
 }\r
 \r
 /**\r
index f933486..7fec475 100644 (file)
@@ -57,6 +57,7 @@ void  wait(int miliseconds);
  int   clone(int flags, void *stack);
  int   execve(char *path, char **argv, char **envp);
 // --- VFS ---
+ int   chdir(char *dir);
  int   open(char *path, int flags);
  int   reopen(int fd, char *path, int flags);
 void   close(int fd);

UCC git Repository :: git.ucc.asn.au