Commit 054a95a8 authored by Paul Gofman's avatar Paul Gofman Committed by Alexandre Julliard

win32u: Avoid writing past allocated memory in peek_message().

parent 8b7ac247
...@@ -469,9 +469,13 @@ static inline void push_string( struct packed_message *data, LPCWSTR str ) ...@@ -469,9 +469,13 @@ static inline void push_string( struct packed_message *data, LPCWSTR str )
} }
/* make sure that there is space for 'size' bytes in buffer, growing it if needed */ /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
static inline void *get_buffer_space( void **buffer, size_t size, size_t prev_size ) static inline void *get_buffer_space( void **buffer, size_t size, size_t *buffer_size )
{ {
if (prev_size < size) *buffer = malloc( size ); if (*buffer_size < size)
{
*buffer = malloc( size );
*buffer_size = size;
}
return *buffer; return *buffer;
} }
...@@ -522,7 +526,7 @@ BOOL set_keyboard_auto_repeat( BOOL enable ) ...@@ -522,7 +526,7 @@ BOOL set_keyboard_auto_repeat( BOOL enable )
* Unpack a message received from another process. * Unpack a message received from another process.
*/ */
static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam, static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
void **buffer, size_t size ) void **buffer, size_t size, size_t *buffer_size )
{ {
size_t minsize = 0; size_t minsize = 0;
union packed_structs *ps = *buffer; union packed_structs *ps = *buffer;
...@@ -585,7 +589,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa ...@@ -585,7 +589,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
break; break;
case WM_GETTEXT: case WM_GETTEXT:
case WM_ASKCBFORMATNAME: case WM_ASKCBFORMATNAME:
if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)), size )) return FALSE; if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)), buffer_size )) return FALSE;
break; break;
case WM_WININICHANGE: case WM_WININICHANGE:
if (!*lparam) return TRUE; if (!*lparam) return TRUE;
...@@ -726,17 +730,17 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa ...@@ -726,17 +730,17 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
minsize = sizeof(SCROLLINFO); minsize = sizeof(SCROLLINFO);
break; break;
case SBM_GETSCROLLINFO: case SBM_GETSCROLLINFO:
if (!get_buffer_space( buffer, sizeof(SCROLLINFO), size )) return FALSE; if (!get_buffer_space( buffer, sizeof(SCROLLINFO), buffer_size )) return FALSE;
break; break;
case SBM_GETSCROLLBARINFO: case SBM_GETSCROLLBARINFO:
if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO), size )) return FALSE; if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO), buffer_size )) return FALSE;
break; break;
case EM_GETSEL: case EM_GETSEL:
case SBM_GETRANGE: case SBM_GETRANGE:
case CB_GETEDITSEL: case CB_GETEDITSEL:
if (*wparam || *lparam) if (*wparam || *lparam)
{ {
if (!get_buffer_space( buffer, 2 * sizeof(DWORD), size )) return FALSE; if (!get_buffer_space( buffer, 2 * sizeof(DWORD), buffer_size )) return FALSE;
if (*wparam) *wparam = (WPARAM)*buffer; if (*wparam) *wparam = (WPARAM)*buffer;
if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1); if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
} }
...@@ -744,7 +748,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa ...@@ -744,7 +748,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
case EM_GETRECT: case EM_GETRECT:
case LB_GETITEMRECT: case LB_GETITEMRECT:
case CB_GETDROPPEDCONTROLRECT: case CB_GETDROPPEDCONTROLRECT:
if (!get_buffer_space( buffer, sizeof(RECT), size )) return FALSE; if (!get_buffer_space( buffer, sizeof(RECT), buffer_size )) return FALSE;
break; break;
case EM_SETRECT: case EM_SETRECT:
case EM_SETRECTNP: case EM_SETRECTNP:
...@@ -755,7 +759,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa ...@@ -755,7 +759,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
WORD *len_ptr, len; WORD *len_ptr, len;
if (size < sizeof(WORD)) return FALSE; if (size < sizeof(WORD)) return FALSE;
len = *(WORD *)*buffer; len = *(WORD *)*buffer;
if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR), size )) return FALSE; if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR), buffer_size )) return FALSE;
len_ptr = *buffer; len_ptr = *buffer;
len_ptr[0] = len_ptr[1] = len; len_ptr[0] = len_ptr[1] = len;
*lparam = (LPARAM)(len_ptr + 1); *lparam = (LPARAM)(len_ptr + 1);
...@@ -780,26 +784,24 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa ...@@ -780,26 +784,24 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
break; break;
case CB_GETLBTEXT: case CB_GETLBTEXT:
{ {
size_t prev_size = size;
if (combobox_has_strings( hwnd )) if (combobox_has_strings( hwnd ))
size = (send_message( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR); size = (send_message( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
else else
size = sizeof(ULONG_PTR); size = sizeof(ULONG_PTR);
if (!get_buffer_space( buffer, size, prev_size )) return FALSE; if (!get_buffer_space( buffer, size, buffer_size )) return FALSE;
break; break;
} }
case LB_GETTEXT: case LB_GETTEXT:
{ {
size_t prev_size = size;
if (listbox_has_strings( hwnd )) if (listbox_has_strings( hwnd ))
size = (send_message( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR); size = (send_message( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
else else
size = sizeof(ULONG_PTR); size = sizeof(ULONG_PTR);
if (!get_buffer_space( buffer, size, prev_size )) return FALSE; if (!get_buffer_space( buffer, size, buffer_size )) return FALSE;
break; break;
} }
case LB_GETSELITEMS: case LB_GETSELITEMS:
if (!get_buffer_space( buffer, *wparam * sizeof(UINT), size )) return FALSE; if (!get_buffer_space( buffer, *wparam * sizeof(UINT), buffer_size )) return FALSE;
break; break;
case WM_NEXTMENU: case WM_NEXTMENU:
{ {
...@@ -814,7 +816,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa ...@@ -814,7 +816,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
case WM_SIZING: case WM_SIZING:
case WM_MOVING: case WM_MOVING:
minsize = sizeof(RECT); minsize = sizeof(RECT);
if (!get_buffer_space( buffer, sizeof(RECT), size )) return FALSE; if (!get_buffer_space( buffer, sizeof(RECT), buffer_size )) return FALSE;
break; break;
case WM_MDICREATE: case WM_MDICREATE:
{ {
...@@ -880,7 +882,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa ...@@ -880,7 +882,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
} }
case WM_MDIGETACTIVE: case WM_MDIGETACTIVE:
if (!*lparam) return TRUE; if (!*lparam) return TRUE;
if (!get_buffer_space( buffer, sizeof(BOOL), size )) return FALSE; if (!get_buffer_space( buffer, sizeof(BOOL), buffer_size )) return FALSE;
break; break;
case WM_DEVICECHANGE: case WM_DEVICECHANGE:
if (!(*wparam & 0x8000)) return TRUE; if (!(*wparam & 0x8000)) return TRUE;
...@@ -2788,7 +2790,7 @@ int peek_message( MSG *msg, const struct peek_message_filter *filter ) ...@@ -2788,7 +2790,7 @@ int peek_message( MSG *msg, const struct peek_message_filter *filter )
case MSG_NOTIFY: case MSG_NOTIFY:
info.flags = ISMEX_NOTIFY; info.flags = ISMEX_NOTIFY;
if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam, if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
&info.msg.lParam, &buffer, size )) &info.msg.lParam, &buffer, size, &buffer_size ))
continue; continue;
break; break;
case MSG_CALLBACK: case MSG_CALLBACK:
...@@ -2866,7 +2868,7 @@ int peek_message( MSG *msg, const struct peek_message_filter *filter ) ...@@ -2866,7 +2868,7 @@ int peek_message( MSG *msg, const struct peek_message_filter *filter )
case MSG_OTHER_PROCESS: case MSG_OTHER_PROCESS:
info.flags = ISMEX_SEND; info.flags = ISMEX_SEND;
if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam, if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
&info.msg.lParam, &buffer, size )) &info.msg.lParam, &buffer, size, &buffer_size ))
{ {
/* ignore it */ /* ignore it */
reply_message( &info, 0, &info.msg ); reply_message( &info, 0, &info.msg );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment