On Sunday 13 July 2025 21:19:08 LIU Hao wrote:
> 在 2025-7-13 21:06, Pali Rohár 写道:
> > Ok. Thanks for info. And what in the case if mingw-w64 is compiled with
> > gcc-8 together with -fstack-protector in CFLAGS?
> 
> It may be a default option of an environment.
> 
> For example in MSYS2 when building a native package with `makepkg-mingw` the
> default flags are defined in
> 'C:\MSYS64\etc\makepkg_mingw.d\${MSYSTEM}.conf', and in CFLAGS there's
> `-fstack-protector-strong`. Those flags are disabled with `!buildflags` in
> PKGBUILD when building the CRT.

That gcc 8.3 supports -fstack-protector-all flag.

When the file stack_chk_guard.c is compiled without that flag it emits
following code:

00000000 <_init>:
   0:   83 3d 00 00 00 00 00    cmpl   $0x0,0x0
                        2: dir32        ___stack_chk_guard-0x4
   7:   75 2c                   jne    35 <_init+0x35>
   9:   55                      push   %ebp
   a:   89 e5                   mov    %esp,%ebp
   c:   83 ec 28                sub    $0x28,%esp
   f:   8d 45 f4                lea    -0xc(%ebp),%eax
  12:   89 04 24                mov    %eax,(%esp)
  15:   ff 15 00 00 00 00       call   *0x0
                        17: dir32       __imp__rand_s
  1b:   85 c0                   test   %eax,%eax
  1d:   75 0a                   jne    29 <_init+0x29>
  1f:   8b 45 f4                mov    -0xc(%ebp),%eax
  22:   a3 00 00 00 00          mov    %eax,0x0
                        23: dir32       ___stack_chk_guard-0x4
  27:   eb 0a                   jmp    33 <_init+0x33>
  29:   c7 05 00 00 00 00 ef    movl   $0xdeadbeef,0x0
  30:   be ad de
                        2b: dir32       ___stack_chk_guard-0x4
  33:   c9                      leave
  34:   c3                      ret
  35:   c3                      ret
  36:   90                      nop
  37:   90                      nop


And when it is compiled with the -fstack-protector-all flag it emits:

00000000 <_init>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 28                sub    $0x28,%esp
   6:   a1 00 00 00 00          mov    0x0,%eax
                        7: dir32        ___stack_chk_guard-0x4
   b:   89 45 f4                mov    %eax,-0xc(%ebp)
   e:   31 c0                   xor    %eax,%eax
  10:   83 3d 00 00 00 00 00    cmpl   $0x0,0x0
                        12: dir32       ___stack_chk_guard-0x4
  17:   75 24                   jne    3d <_init+0x3d>
  19:   8d 45 f0                lea    -0x10(%ebp),%eax
  1c:   89 04 24                mov    %eax,(%esp)
  1f:   ff 15 00 00 00 00       call   *0x0
                        21: dir32       __imp__rand_s
  25:   85 c0                   test   %eax,%eax
  27:   75 0a                   jne    33 <_init+0x33>
  29:   8b 45 f0                mov    -0x10(%ebp),%eax
  2c:   a3 00 00 00 00          mov    %eax,0x0
                        2d: dir32       ___stack_chk_guard-0x4
  31:   eb 0a                   jmp    3d <_init+0x3d>
  33:   c7 05 00 00 00 00 ef    movl   $0xdeadbeef,0x0
  3a:   be ad de
                        35: dir32       ___stack_chk_guard-0x4
  3d:   8b 45 f4                mov    -0xc(%ebp),%eax
  40:   33 05 00 00 00 00       xor    0x0,%eax
                        42: dir32       ___stack_chk_guard-0x4
  46:   74 05                   je     4d <_init+0x4d>
  48:   e8 00 00 00 00          call   4d <_init+0x4d>
                        49: DISP32      ___stack_chk_fail
  4d:   c9                      leave
  4e:   c3                      ret
  4f:   90                      nop

And throws warning: ‘no_stack_protector’ attribute directive ignored 
[-Wattributes]


If I understand correctly the code at 3d-4c is the stack protection
check, which should have been disabled for that function. But because
the ___stack_chk_guard is being changed in the function, it cause
calling the ___stack_chk_fail.

So the warning is NOT harmless and cannot be ignored. It is a real issue.


_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to