1616def write_testcode (filename ):
1717 with open (filename , 'w' , encoding = "utf8" ) as f :
1818 f .write ('''
19- #include <stdio.h >
19+ #include <cstdio >
2020 int main()
2121 {
22- printf("the quick brown fox jumps over the lazy god\\ n");
22+ std:: printf("the quick brown fox jumps over the lazy god\\ n");
2323 return 0;
2424 }
2525 ''' )
@@ -35,111 +35,111 @@ def env_flags() -> List[str]:
3535 # See the definitions for ac_link in autoconf's lib/autoconf/c.m4 file for
3636 # reference.
3737 flags : List [str ] = []
38- for var in ['CFLAGS ' , 'CPPFLAGS' , 'LDFLAGS' ]:
38+ for var in ['CXXFLAGS ' , 'CPPFLAGS' , 'LDFLAGS' ]:
3939 flags += filter (None , os .environ .get (var , '' ).split (' ' ))
4040 return flags
4141
42- def call_security_check (cc , source , executable , options ):
43- subprocess .run ([* cc ,source ,'-o' ,executable ] + env_flags () + options , check = True )
42+ def call_security_check (cxx , source , executable , options ):
43+ subprocess .run ([* cxx ,source ,'-o' ,executable ] + env_flags () + options , check = True )
4444 p = subprocess .run ([os .path .join (os .path .dirname (__file__ ), 'security-check.py' ), executable ], stdout = subprocess .PIPE , universal_newlines = True )
4545 return (p .returncode , p .stdout .rstrip ())
4646
47- def get_arch (cc , source , executable ):
48- subprocess .run ([* cc , source , '-o' , executable ] + env_flags (), check = True )
47+ def get_arch (cxx , source , executable ):
48+ subprocess .run ([* cxx , source , '-o' , executable ] + env_flags (), check = True )
4949 binary = lief .parse (executable )
5050 arch = binary .abstract .header .architecture
5151 os .remove (executable )
5252 return arch
5353
5454class TestSecurityChecks (unittest .TestCase ):
5555 def test_ELF (self ):
56- source = 'test1.c '
56+ source = 'test1.cpp '
5757 executable = 'test1'
58- cc = determine_wellknown_cmd ('CC ' , 'gcc ' )
58+ cxx = determine_wellknown_cmd ('CXX ' , 'g++ ' )
5959 write_testcode (source )
60- arch = get_arch (cc , source , executable )
60+ arch = get_arch (cxx , source , executable )
6161
6262 if arch == lief .ARCHITECTURES .X86 :
63- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-zexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
63+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-zexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
6464 (1 , executable + ': failed PIE NX RELRO CONTROL_FLOW' ))
65- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
65+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
6666 (1 , executable + ': failed PIE RELRO CONTROL_FLOW' ))
67- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
67+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
6868 (1 , executable + ': failed PIE RELRO CONTROL_FLOW' ))
69- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
69+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
7070 (1 , executable + ': failed RELRO CONTROL_FLOW' ))
71- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,noseparate-code' ]),
71+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,noseparate-code' ]),
7272 (1 , executable + ': failed separate_code CONTROL_FLOW' ))
73- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
73+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
7474 (1 , executable + ': failed CONTROL_FLOW' ))
75- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' , '-fcf-protection=full' ]),
75+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' , '-fcf-protection=full' ]),
7676 (0 , '' ))
7777 else :
78- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-zexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
78+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-zexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
7979 (1 , executable + ': failed PIE NX RELRO' ))
80- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
80+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
8181 (1 , executable + ': failed PIE RELRO' ))
82- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
82+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
8383 (1 , executable + ': failed PIE RELRO' ))
84- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
84+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
8585 (1 , executable + ': failed RELRO' ))
86- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,noseparate-code' ]),
86+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,noseparate-code' ]),
8787 (1 , executable + ': failed separate_code' ))
88- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
88+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
8989 (0 , '' ))
9090
9191 clean_files (source , executable )
9292
9393 def test_PE (self ):
94- source = 'test1.c '
94+ source = 'test1.cpp '
9595 executable = 'test1.exe'
96- cc = determine_wellknown_cmd ('CC ' , 'x86_64-w64-mingw32-gcc ' )
96+ cxx = determine_wellknown_cmd ('CXX ' , 'x86_64-w64-mingw32-g++ ' )
9797 write_testcode (source )
9898
99- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,--disable-nxcompat' ,'-Wl,--disable-reloc-section' ,'-Wl,--disable-dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-no-pie' ,'-fno-PIE' ,'-fno-stack-protector' ]),
99+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,--disable-nxcompat' ,'-Wl,--disable-reloc-section' ,'-Wl,--disable-dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-no-pie' ,'-fno-PIE' ,'-fno-stack-protector' ]),
100100 (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA NX RELOC_SECTION CONTROL_FLOW Canary' ))
101- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,--nxcompat' ,'-Wl,--disable-reloc-section' ,'-Wl,--disable-dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-no-pie' ,'-fno-PIE' ,'-fstack-protector-all' , '-lssp' ]),
101+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,--nxcompat' ,'-Wl,--disable-reloc-section' ,'-Wl,--disable-dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-no-pie' ,'-fno-PIE' ,'-fstack-protector-all' , '-lssp' ]),
102102 (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA RELOC_SECTION CONTROL_FLOW' ))
103- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--disable-dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-no-pie' ,'-fno-PIE' ,'-fstack-protector-all' , '-lssp' ]),
103+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--disable-dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-no-pie' ,'-fno-PIE' ,'-fstack-protector-all' , '-lssp' ]),
104104 (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW' ))
105- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--disable-dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-pie' ,'-fPIE' ,'-fstack-protector-all' , '-lssp' ]),
105+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--disable-dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-pie' ,'-fPIE' ,'-fstack-protector-all' , '-lssp' ]),
106106 (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW' )) # -pie -fPIE does nothing unless --dynamicbase is also supplied
107- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-pie' ,'-fPIE' ,'-fstack-protector-all' , '-lssp' ]),
107+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--dynamicbase' ,'-Wl,--disable-high-entropy-va' ,'-pie' ,'-fPIE' ,'-fstack-protector-all' , '-lssp' ]),
108108 (1 , executable + ': failed HIGH_ENTROPY_VA CONTROL_FLOW' ))
109- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--dynamicbase' ,'-Wl,--high-entropy-va' ,'-pie' ,'-fPIE' ,'-fstack-protector-all' , '-lssp' ]),
109+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--dynamicbase' ,'-Wl,--high-entropy-va' ,'-pie' ,'-fPIE' ,'-fstack-protector-all' , '-lssp' ]),
110110 (1 , executable + ': failed CONTROL_FLOW' ))
111- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--dynamicbase' ,'-Wl,--high-entropy-va' ,'-pie' ,'-fPIE' , '-fcf-protection=full' ,'-fstack-protector-all' , '-lssp' ]),
111+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,--nxcompat' ,'-Wl,--enable-reloc-section' ,'-Wl,--dynamicbase' ,'-Wl,--high-entropy-va' ,'-pie' ,'-fPIE' , '-fcf-protection=full' ,'-fstack-protector-all' , '-lssp' ]),
112112 (0 , '' ))
113113
114114 clean_files (source , executable )
115115
116116 def test_MACHO (self ):
117- source = 'test1.c '
117+ source = 'test1.cpp '
118118 executable = 'test1'
119- cc = determine_wellknown_cmd ('CC ' , 'clang' )
119+ cxx = determine_wellknown_cmd ('CXX ' , 'clang++ ' )
120120 write_testcode (source )
121- arch = get_arch (cc , source , executable )
121+ arch = get_arch (cxx , source , executable )
122122
123123 if arch == lief .ARCHITECTURES .X86 :
124- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-no_pie' ,'-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-no_fixup_chains' ]),
124+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-no_pie' ,'-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-no_fixup_chains' ]),
125125 (1 , executable + ': failed NOUNDEFS Canary FIXUP_CHAINS PIE CONTROL_FLOW' ))
126- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-fixup_chains' ]),
126+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-fixup_chains' ]),
127127 (1 , executable + ': failed NOUNDEFS Canary CONTROL_FLOW' ))
128- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-flat_namespace' ,'-fstack-protector-all' , '-Wl,-fixup_chains' ]),
128+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fstack-protector-all' , '-Wl,-fixup_chains' ]),
129129 (1 , executable + ': failed NOUNDEFS CONTROL_FLOW' ))
130- self .assertEqual (call_security_check (cc , source , executable , ['-fstack-protector-all' , '-Wl,-fixup_chains' ]),
130+ self .assertEqual (call_security_check (cxx , source , executable , ['-fstack-protector-all' , '-Wl,-fixup_chains' ]),
131131 (1 , executable + ': failed CONTROL_FLOW' ))
132- self .assertEqual (call_security_check (cc , source , executable , ['-fstack-protector-all' , '-fcf-protection=full' , '-Wl,-fixup_chains' ]),
132+ self .assertEqual (call_security_check (cxx , source , executable , ['-fstack-protector-all' , '-fcf-protection=full' , '-Wl,-fixup_chains' ]),
133133 (0 , '' ))
134134 else :
135135 # arm64 darwin doesn't support non-PIE binaries, control flow or executable stacks
136- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-no_fixup_chains' ]),
136+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-no_fixup_chains' ]),
137137 (1 , executable + ': failed NOUNDEFS Canary FIXUP_CHAINS BRANCH_PROTECTION' ))
138- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
138+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
139139 (1 , executable + ': failed NOUNDEFS Canary' ))
140- self .assertEqual (call_security_check (cc , source , executable , ['-Wl,-flat_namespace' ,'-fstack-protector-all' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
140+ self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fstack-protector-all' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
141141 (1 , executable + ': failed NOUNDEFS' ))
142- self .assertEqual (call_security_check (cc , source , executable , ['-fstack-protector-all' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
142+ self .assertEqual (call_security_check (cxx , source , executable , ['-fstack-protector-all' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
143143 (0 , '' ))
144144
145145
0 commit comments