From 38be1b2300f8647e8db6e9268a643ed5dbf02b77 Mon Sep 17 00:00:00 2001
From: Timo Rothenpieler <[email protected]>
Date: Wed, 9 Sep 2026 14:16:34 +0200
Subject: [PATCH] ssp: prioritize stack guard init constructor

Other libraries might also be running constructors, which themselves are built 
with SSP enabled,
or are calling functions that are.
If those then get ordered befor the ssp constructor, they might crash due to 
the yet to be initialized
__stack_chk_guard.

This at least affects a statically linked glib, which launches a background 
thread in the
libgio constructor.
If the ssp constructor then runs at a random point after, it initializes the 
stack guard, and once
the thread returns from whatever function it's in, it crashes because the stack 
guard changed.

Signed-off-by: Timo Rothenpieler <[email protected]>
---
 mingw-w64-crt/ssp/stack_chk_guard.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-crt/ssp/stack_chk_guard.c 
b/mingw-w64-crt/ssp/stack_chk_guard.c
index 499f0033b..adffb537b 100644
--- a/mingw-w64-crt/ssp/stack_chk_guard.c
+++ b/mingw-w64-crt/ssp/stack_chk_guard.c
@@ -10,6 +10,11 @@

 uintptr_t __stack_chk_guard = 0;

+#if defined(__GNUC__) && __GNUC__ >= 9 && !defined(__clang__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wprio-ctor-dtor"
+#endif
+
 #if defined __SSP__ || defined __SSP_STRONG__ || defined __SSP_ALL__
 // This function requires `no_stack_protector` because it changes the
 // value of `__stack_chk_guard`, causing stack checks to fail before
@@ -21,7 +26,7 @@ __attribute__((__no_stack_protector__))
          -fstack-protector* options from CFLAGS.
 # endif
 #endif
-__attribute__((__constructor__))
+__attribute__((constructor(0)))
 static void __cdecl init(void)
 {
   unsigned int ui;
@@ -50,3 +55,7 @@ static void __cdecl init(void)
   __stack_chk_guard = 0xdeadbeef;
 #endif
 }
+
+#if defined(__GNUC__) && __GNUC__ >= 9 && !defined(__clang__)
+#pragma GCC diagnostic pop
+#endif
From 38be1b2300f8647e8db6e9268a643ed5dbf02b77 Mon Sep 17 00:00:00 2001
From: Timo Rothenpieler <[email protected]>
Date: Wed, 9 Sep 2026 14:16:34 +0200
Subject: [PATCH] ssp: prioritize stack guard init constructor

Other libraries might also be running constructors, which themselves are built 
with SSP enabled,
or are calling functions that are.
If those then get ordered befor the ssp constructor, they might crash due to 
the yet to be initialized
__stack_chk_guard.

This at least affects a statically linked glib, which launches a background 
thread in the
libgio constructor.
If the ssp constructor then runs at a random point after, it initializes the 
stack guard, and once
the thread returns from whatever function it's in, it crashes because the stack 
guard changed.

Signed-off-by: Timo Rothenpieler <[email protected]>
---
 mingw-w64-crt/ssp/stack_chk_guard.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-crt/ssp/stack_chk_guard.c 
b/mingw-w64-crt/ssp/stack_chk_guard.c
index 499f0033b..adffb537b 100644
--- a/mingw-w64-crt/ssp/stack_chk_guard.c
+++ b/mingw-w64-crt/ssp/stack_chk_guard.c
@@ -10,6 +10,11 @@
 
 uintptr_t __stack_chk_guard = 0;
 
+#if defined(__GNUC__) && __GNUC__ >= 9 && !defined(__clang__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wprio-ctor-dtor"
+#endif
+
 #if defined __SSP__ || defined __SSP_STRONG__ || defined __SSP_ALL__
 // This function requires `no_stack_protector` because it changes the
 // value of `__stack_chk_guard`, causing stack checks to fail before
@@ -21,7 +26,7 @@ __attribute__((__no_stack_protector__))
          -fstack-protector* options from CFLAGS.
 # endif
 #endif
-__attribute__((__constructor__))
+__attribute__((constructor(0)))
 static void __cdecl init(void)
 {
   unsigned int ui;
@@ -50,3 +55,7 @@ static void __cdecl init(void)
   __stack_chk_guard = 0xdeadbeef;
 #endif
 }
+
+#if defined(__GNUC__) && __GNUC__ >= 9 && !defined(__clang__)
+#pragma GCC diagnostic pop
+#endif

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to