From: John Hodge Date: Thu, 4 Oct 2012 13:41:13 +0000 (+0800) Subject: Syscalls - Changed SysGetMessage to provide a buffer size X-Git-Tag: rel0.15~700 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=3b864de01887f22d74ec1d564bc99913e5d82be4;p=tpg%2Facess2.git Syscalls - Changed SysGetMessage to provide a buffer size --- diff --git a/KernelLand/Kernel/include/threads.h b/KernelLand/Kernel/include/threads.h index 0362cdb4..6382cf00 100644 --- a/KernelLand/Kernel/include/threads.h +++ b/KernelLand/Kernel/include/threads.h @@ -37,6 +37,6 @@ extern char **Threads_GetCWD(void); extern char **Threads_GetChroot(void); extern int Proc_SendMessage(Uint Dest, int Length, void *Data); -extern int Proc_GetMessage(Uint *Source, void *Buffer); +extern int Proc_GetMessage(Uint *Source, Uint BufSize, void *Buffer); #endif diff --git a/KernelLand/Kernel/messages.c b/KernelLand/Kernel/messages.c index 09c8c14e..c9919780 100644 --- a/KernelLand/Kernel/messages.c +++ b/KernelLand/Kernel/messages.c @@ -78,10 +78,11 @@ int Proc_SendMessage(Uint Dest, int Length, void *Data) * \fn int Proc_GetMessage(Uint *Source, void *Buffer) * \brief Gets a message * \param Source Where to put the source TID + * \param BufSize Size of \a Buffer, only this many bytes will be copied * \param Buffer Buffer to place the message data (set to NULL to just get message length) * \return Message length */ -int Proc_GetMessage(Uint *Source, void *Buffer) +int Proc_GetMessage(Uint *Source, Uint BufSize, void *Buffer) { int ret; void *tmp; @@ -113,7 +114,7 @@ int Proc_GetMessage(Uint *Source, void *Buffer) // Get message if(Buffer != GETMSG_IGNORE) { - if( !CheckMem( Buffer, cur->Messages->Length ) ) + if( !CheckMem( Buffer, BufSize ) ) { LOG("Invalid buffer"); errno = -EINVAL; @@ -121,8 +122,15 @@ int Proc_GetMessage(Uint *Source, void *Buffer) LEAVE('i', -1); return -1; } + if( BufSize < cur->Messages->Length ) + Log_Notice("Threads", "Buffer of 0x%x passed, but 0x%x long message, truncated", + BufSize, cur->Messages->Length); + else if( BufSize < cur->Messages->Length ) + BufSize = cur->Messages->Length; + else + ; // equal LOG("Copied to buffer"); - memcpy(Buffer, cur->Messages->Data, cur->Messages->Length); + memcpy(Buffer, cur->Messages->Data, BufSize); } ret = cur->Messages->Length; diff --git a/KernelLand/Kernel/syscalls.c b/KernelLand/Kernel/syscalls.c index 0348d57a..bfd68bf1 100644 --- a/KernelLand/Kernel/syscalls.c +++ b/KernelLand/Kernel/syscalls.c @@ -135,13 +135,10 @@ void SyscallHandler(tSyscallRegs *Regs) // -- Check for messages case SYS_GETMSG: CHECK_NUM_NULLOK( (Uint*)Regs->Arg1, sizeof(Uint) ); - // NOTE: Can't do range checking as we don't know the size - // - Should be done by Proc_GetMessage - if( Regs->Arg2 && Regs->Arg2 != -1 && !MM_IsUser(Regs->Arg2) ) { - err = -EINVAL; ret = -1; break; - } + if( Regs->Arg3 != -1 ) + CHECK_NUM_NULLOK((void*)Regs->Arg3, Regs->Arg2); // *Source, *Data - ret = Proc_GetMessage((Uint*)Regs->Arg1, (void*)Regs->Arg2); + ret = Proc_GetMessage((Uint*)Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3); break; // -- Get the current timestamp diff --git a/Usermode/Applications/axwin3_src/WM/ipc.c b/Usermode/Applications/axwin3_src/WM/ipc.c index cbed7fd1..1d4ac33c 100644 --- a/Usermode/Applications/axwin3_src/WM/ipc.c +++ b/Usermode/Applications/axwin3_src/WM/ipc.c @@ -112,12 +112,12 @@ void IPC_HandleSelect(fd_set *set) } } - while(SysGetMessage(NULL, NULL)) + size_t len; + pid_t tid; + while( (len = SysGetMessage(&tid, 0, NULL)) ) { - pid_t tid; - int len = SysGetMessage(&tid, NULL); char data[len]; - SysGetMessage(NULL, data); + SysGetMessage(NULL, len, data); IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data); // _SysDebug("IPC_HandleSelect: Message handled"); diff --git a/Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c b/Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c index 2b501672..ac51e277 100644 --- a/Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c +++ b/Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c @@ -127,7 +127,7 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void) pid_t tid; // Wait for a message to arrive - while( !(len = SysGetMessage(&tid, NULL)) ) + while( !(len = SysGetMessage(&tid, 0, NULL)) ) { _SysWaitEvent(THREAD_EVENT_IPCMSG); } @@ -140,7 +140,7 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void) if( gAxWin3_MessageCallback ) gAxWin3_MessageCallback(tid, len); else - SysGetMessage(NULL, GETMSG_IGNORE); + SysGetMessage(NULL, 0, GETMSG_IGNORE); continue ; } @@ -148,10 +148,10 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void) ret = malloc(len); if(ret == NULL) { _SysDebug("malloc() failed, ignoring message"); - SysGetMessage(NULL, GETMSG_IGNORE); + SysGetMessage(NULL, 0, GETMSG_IGNORE); return NULL; } - SysGetMessage(NULL, ret); + SysGetMessage(NULL, len, ret); break; } break; diff --git a/Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h b/Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h index 1a954d33..2452972d 100644 --- a/Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h +++ b/Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h @@ -85,7 +85,7 @@ extern int unlink(const char *pathname); // --- IPC --- extern int SysSendMessage(pid_t dest, uint length, const void *Data); -extern int SysGetMessage(pid_t *src, void *Data); +extern int SysGetMessage(pid_t *src, uint buflen, void *Data); // --- MEMORY --- uint64_t _SysGetPhys(uint vaddr);