Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Buffer Security Check for function (#pragma strict_gs_check)

I need to suppress Buffer Security Check (/GS) feature (MSVC) for some functions but not for entire Project as /GS- doing. MSVSC documentation https://learn.microsoft.com/en-us/cpp/preprocessor/strict-gs-check?view=vs-2017 prompts to use #pragma strict_gs_check(off). Unfortunately, it doesn't work for me - I still see the "cookies" in the assembly. Any help, please.

This is simplest code to reproduce, godbold link here: https://godbolt.org/z/gYiGam

#include <memory>

struct Tmp {
    char v[8];
};

//#pragma check_stack(off)
#pragma strict_gs_check(off)
int make1(Tmp& a)
{
    Tmp r;
    return memcmp(&r, &a, sizeof(r));
}

//result
//-------------------------
  pop esi
  mov ecx, DWORD PTR __$ArrayPad$[esp+12]
  xor ecx, esp
  call @__security_check_cookie@4
  add esp, 12 ; 0000000cH
  ret 0
$LN6@make1:
  mov ecx, DWORD PTR __$ArrayPad$[esp+16]
  sbb eax, eax
  pop esi
  xor ecx, esp
  or eax, 1
  call @__security_check_cookie@4
  add esp, 12 ; 0000000cH
  ret 0
int make1(Tmp &) ENDP ; make1
like image 261
Iurii Gordiienko Avatar asked Sep 16 '25 04:09

Iurii Gordiienko


1 Answers

I don't think you can disable it that way. The strict GS checking is a request to the compiler to add stricter checks, to functions that would otherwise not have it.

In other words, /GS controls whether GS buffer checking is done and the pragma simply controls how aggressive it is.

If you want to disable it entirely for a specific function, you should use __declspec(safebuffers) on said function (see https://learn.microsoft.com/en-us/cpp/cpp/safebuffers?view=vs-2017). This is an indication that you don't want checking at all.

like image 174
paxdiablo Avatar answered Sep 19 '25 14:09

paxdiablo