@@ -59,33 +59,20 @@ def test_ELF(self):
5959 arch = get_arch (cxx , source , executable )
6060
6161 if arch == lief .ARCHITECTURES .X86 :
62- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-zexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
63- (1 , executable + ': failed PIE NX RELRO CONTROL_FLOW' ))
64- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
65- (1 , executable + ': failed PIE RELRO CONTROL_FLOW' ))
66- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
67- (1 , executable + ': failed PIE RELRO CONTROL_FLOW' ))
68- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
69- (1 , executable + ': failed RELRO CONTROL_FLOW' ))
70- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,noseparate-code' ]),
71- (1 , executable + ': failed separate_code CONTROL_FLOW' ))
72- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
73- (1 , executable + ': failed CONTROL_FLOW' ))
74- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' , '-fcf-protection=full' ]),
75- (0 , '' ))
62+ pass_flags = ['-Wl,-znoexecstack' , '-Wl,-zrelro' , '-Wl,-z,now' , '-pie' , '-fPIE' , '-Wl,-z,separate-code' , '-fcf-protection=full' ]
63+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-zexecstack' ]), (1 , executable + ': failed NX' ))
64+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-no-pie' ,'-fno-PIE' ]), (1 , executable + ': failed PIE' ))
65+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-znorelro' ]), (1 , executable + ': failed RELRO' ))
66+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-z,noseparate-code' ]), (1 , executable + ': failed SEPARATE_CODE' ))
67+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-fcf-protection=none' ]), (1 , executable + ': failed CONTROL_FLOW' ))
68+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags ), (0 , '' ))
7669 else :
77- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-zexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
78- (1 , executable + ': failed PIE NX RELRO' ))
79- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
80- (1 , executable + ': failed PIE RELRO' ))
81- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-no-pie' ,'-fno-PIE' , '-Wl,-z,separate-code' ]),
82- (1 , executable + ': failed PIE RELRO' ))
83- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-znorelro' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
84- (1 , executable + ': failed RELRO' ))
85- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,noseparate-code' ]),
86- (1 , executable + ': failed separate_code' ))
87- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-znoexecstack' ,'-Wl,-zrelro' ,'-Wl,-z,now' ,'-pie' ,'-fPIE' , '-Wl,-z,separate-code' ]),
88- (0 , '' ))
70+ pass_flags = ['-Wl,-znoexecstack' , '-Wl,-zrelro' , '-Wl,-z,now' , '-pie' , '-fPIE' , '-Wl,-z,separate-code' ]
71+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-zexecstack' ]), (1 , executable + ': failed NX' ))
72+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-no-pie' ,'-fno-PIE' ]), (1 , executable + ': failed PIE' ))
73+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-znorelro' ]), (1 , executable + ': failed RELRO' ))
74+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-z,noseparate-code' ]), (1 , executable + ': failed SEPARATE_CODE' ))
75+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags ), (0 , '' ))
8976
9077 clean_files (source , executable )
9178
@@ -95,20 +82,16 @@ def test_PE(self):
9582 cxx = determine_wellknown_cmd ('CXX' , 'x86_64-w64-mingw32-g++' )
9683 write_testcode (source )
9784
98- 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' ]),
99- (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA NX RELOC_SECTION CONTROL_FLOW Canary' ))
100- 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' ]),
101- (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA RELOC_SECTION CONTROL_FLOW' ))
102- 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' ]),
103- (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW' ))
104- 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' ]),
105- (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW' )) # -pie -fPIE does nothing unless --dynamicbase is also supplied
106- 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' ]),
107- (1 , executable + ': failed HIGH_ENTROPY_VA CONTROL_FLOW' ))
108- 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' ]),
109- (1 , executable + ': failed CONTROL_FLOW' ))
110- 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' ]),
111- (0 , '' ))
85+ pass_flags = ['-Wl,--nxcompat' , '-Wl,--enable-reloc-section' , '-Wl,--dynamicbase' , '-Wl,--high-entropy-va' , '-pie' , '-fPIE' , '-fcf-protection=full' , '-fstack-protector-all' , '-lssp' ]
86+
87+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-fno-stack-protector' ]), (1 , executable + ': failed CANARY' ))
88+ # https://github.com/lief-project/LIEF/issues/1076 - in future, we could test this individually.
89+ # self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,--disable-reloc-section']), (1, executable + ': failed RELOC_SECTION'))
90+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,--disable-nxcompat' ]), (1 , executable + ': failed NX' ))
91+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,--disable-dynamicbase' ]), (1 , executable + ': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA' )) # -pie -fPIE does nothing without --dynamicbase
92+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,--disable-high-entropy-va' ]), (1 , executable + ': failed HIGH_ENTROPY_VA' ))
93+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-fcf-protection=none' ]), (1 , executable + ': failed CONTROL_FLOW' ))
94+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags ), (0 , '' ))
11295
11396 clean_files (source , executable )
11497
@@ -120,27 +103,21 @@ def test_MACHO(self):
120103 arch = get_arch (cxx , source , executable )
121104
122105 if arch == lief .ARCHITECTURES .X86 :
123- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-no_pie' ,'-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-no_fixup_chains' ]),
124- (1 , executable + ': failed NOUNDEFS Canary FIXUP_CHAINS PIE CONTROL_FLOW' ))
125- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-fixup_chains' ]),
126- (1 , executable + ': failed NOUNDEFS Canary CONTROL_FLOW' ))
127- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fstack-protector-all' , '-Wl,-fixup_chains' ]),
128- (1 , executable + ': failed NOUNDEFS CONTROL_FLOW' ))
129- self .assertEqual (call_security_check (cxx , source , executable , ['-fstack-protector-all' , '-Wl,-fixup_chains' ]),
130- (1 , executable + ': failed CONTROL_FLOW' ))
131- self .assertEqual (call_security_check (cxx , source , executable , ['-fstack-protector-all' , '-fcf-protection=full' , '-Wl,-fixup_chains' ]),
132- (0 , '' ))
106+ pass_flags = ['-Wl,-pie' , '-fstack-protector-all' , '-fcf-protection=full' , '-Wl,-fixup_chains' ]
107+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-no_pie' , '-Wl,-no_fixup_chains' ]), (1 , executable + ': failed FIXUP_CHAINS PIE' )) # -fixup_chains is incompatible with -no_pie
108+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-no_fixup_chains' ]), (1 , executable + ': failed FIXUP_CHAINS' ))
109+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-fno-stack-protector' ]), (1 , executable + ': failed CANARY' ))
110+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-flat_namespace' ]), (1 , executable + ': failed NOUNDEFS' ))
111+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-fcf-protection=none' ]), (1 , executable + ': failed CONTROL_FLOW' ))
112+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags ), (0 , '' ))
133113 else :
134- # arm64 darwin doesn't support non-PIE binaries, control flow or executable stacks
135- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-no_fixup_chains' ]),
136- (1 , executable + ': failed NOUNDEFS Canary FIXUP_CHAINS BRANCH_PROTECTION' ))
137- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fno-stack-protector' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
138- (1 , executable + ': failed NOUNDEFS Canary' ))
139- self .assertEqual (call_security_check (cxx , source , executable , ['-Wl,-flat_namespace' ,'-fstack-protector-all' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
140- (1 , executable + ': failed NOUNDEFS' ))
141- self .assertEqual (call_security_check (cxx , source , executable , ['-fstack-protector-all' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]),
142- (0 , '' ))
143-
114+ # arm64 darwin doesn't support non-PIE binaries or executable stacks
115+ pass_flags = ['-fstack-protector-all' , '-Wl,-fixup_chains' , '-mbranch-protection=bti' ]
116+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-mbranch-protection=none' ]), (1 , executable + ': failed BRANCH_PROTECTION' ))
117+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-no_fixup_chains' ]), (1 , executable + ': failed FIXUP_CHAINS' ))
118+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-fno-stack-protector' ]), (1 , executable + ': failed CANARY' ))
119+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags + ['-Wl,-flat_namespace' ]), (1 , executable + ': failed NOUNDEFS' ))
120+ self .assertEqual (call_security_check (cxx , source , executable , pass_flags ), (0 , '' ))
144121
145122 clean_files (source , executable )
146123
0 commit comments