syswm: prevent buffer overflow if SDL and app have different config headers. This only affects Wayland and DirectFB, as a Unix system generally has X11 support. Other platforms also have different sizes for the C union in question, but are likely the only target for that platform, etc. Apps that might run on Wayland or DirectFB will need to be compiled against new headers from an official 2.0.6 release, or be prepared to force the x11 target, or not use SDL_GetWindowWMInfo(). Fixes Bugzilla #3428.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
diff --git a/include/SDL_syswm.h b/include/SDL_syswm.h
index fcb3a35..762bf45 100644
--- a/include/SDL_syswm.h
+++ b/include/SDL_syswm.h
@@ -280,8 +280,9 @@ struct SDL_SysWMinfo
} vivante;
#endif
- /* Can't have an empty union */
- int dummy;
+ /* Make sure this union is always 64 bytes (8 64-bit pointers). */
+ /* Be careful not to overflow this if you add a new target! */
+ Uint8 dummy[64];
} info;
};
diff --git a/src/video/directfb/SDL_DirectFB_window.c b/src/video/directfb/SDL_DirectFB_window.c
index a66aa59..936e0a0 100644
--- a/src/video/directfb/SDL_DirectFB_window.c
+++ b/src/video/directfb/SDL_DirectFB_window.c
@@ -458,9 +458,28 @@ SDL_bool
DirectFB_GetWindowWMInfo(_THIS, SDL_Window * window,
struct SDL_SysWMinfo * info)
{
+ const Uint32 version = ((((Uint32) info->version.major) * 1000000) +
+ (((Uint32) info->version.minor) * 10000) +
+ (((Uint32) info->version.patch)));
+
SDL_DFB_DEVICEDATA(_this);
SDL_DFB_WINDOWDATA(window);
+ /* Before 2.0.6, it was possible to build an SDL with DirectFB support
+ (SDL_SysWMinfo will be large enough to hold DirectFB info), but build
+ your app against SDL headers that didn't have DirectFB support
+ (SDL_SysWMinfo could be smaller than Wayland needs. This would lead
+ to an app properly using SDL_GetWindowWMInfo() but we'd accidentally
+ overflow memory on the stack or heap. To protect against this, we've
+ padded out the struct unconditionally in the headers and DirectFB will
+ just return an error for older apps using this function. Those apps
+ will need to be recompiled against newer headers or not use DirectFB,
+ maybe by forcing SDL_VIDEODRIVER=x11. */
+ if (version < 2000006) {
+ info->subsystem = SDL_SYSWM_UNKNOWN;
+ return SDL_FALSE;
+ }
+
if (info->version.major == SDL_MAJOR_VERSION &&
info->version.minor == SDL_MINOR_VERSION) {
info->subsystem = SDL_SYSWM_DIRECTFB;
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index db0fbe5..7113485 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -129,6 +129,24 @@ SDL_bool
Wayland_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+ const Uint32 version = ((((Uint32) info->version.major) * 1000000) +
+ (((Uint32) info->version.minor) * 10000) +
+ (((Uint32) info->version.patch)));
+
+ /* Before 2.0.6, it was possible to build an SDL with Wayland support
+ (SDL_SysWMinfo will be large enough to hold Wayland info), but build
+ your app against SDL headers that didn't have Wayland support
+ (SDL_SysWMinfo could be smaller than Wayland needs. This would lead
+ to an app properly using SDL_GetWindowWMInfo() but we'd accidentally
+ overflow memory on the stack or heap. To protect against this, we've
+ padded out the struct unconditionally in the headers and Wayland will
+ just return an error for older apps using this function. Those apps
+ will need to be recompiled against newer headers or not use Wayland,
+ maybe by forcing SDL_VIDEODRIVER=x11. */
+ if (version < 2000006) {
+ info->subsystem = SDL_SYSWM_UNKNOWN;
+ return SDL_FALSE;
+ }
info->info.wl.display = data->waylandData->display;
info->info.wl.surface = data->surface;