Swift and Stack smashing protection

前端 未结 3 1178
灰色年华
灰色年华 2021-01-04 07:20

How to enable Stack Smashing Protection in pure swift application?

I did try to put \"-fstack-protector-all\" flag to Other C++ Flags under project build settings t

相关标签:
3条回答
  • 2021-01-04 07:46

    I was also facing this in my 100% Swift project.

    Whenever I added -fstack-protector-all to the "Other C-Flags" Build Settings the flags did not show up in the binary as described in the other comments.

    What I did was to create an Objective-C Class…

    // DummyClassForSSP.h
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface DummyClassForSSP : NSObject
    + (void)dummyCallSoFlagsShowUpInIPA;
    @end
    
    NS_ASSUME_NONNULL_END
    

    … added a dummy implementation …

    // DummyClassForSSP.m
    #import "DummyClassForSSP.h"
    
    @implementation DummyClassForSSP
    
    + (void)dummyCallSoFlagsShowUpInIPA {}
    
    @end
    

    … and called it from my AppDelegate.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    ....
    DummyClassForSSP.dummyCallSoFlagsShowUpInIPA()
    ...
    }
    

    After that the flags showed up as described.

    0 讨论(0)
  • 2021-01-04 07:52

    A bit late to the party but as it might be helpful for other's seeking advice: Joe's answer is not correct. -fstack-protector-all is a flag on the GCC compiler. (Documentation here: https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)

    For swift applications, the GCC compiler is not involved here. Instead it's the swiftc compiler with different options and settings. Alas, I don't know of comparable options for the latter one.

    Adding an objective-c class to the project as @btype suggests, is not helping your swift sourcecode, as it only affects the Obj-C and C code. But if you use 3rd party libraries written in Obj-C it's still a valuable setting. But make sure, you set this flag on the right target, if 3rd party libraries are embedded as separate targets.

    Cheers, Chris

    0 讨论(0)
  • 2021-01-04 07:59

    In Swift, Stack smashing is enabled by default one only need to add the "-fstack-protector-all" flag under build settings in objective-c applications.

    How to check if stack smashing is enabled. Run the otool command and presence of stack_chk_guard and stack_chk_fail means the code is stack smashing protected.

    $ otool -Iv <appname>|grep stack
    0x0013dfg   520 ___stack_chk_fail
    0x001d009   521 ___stack_chk_guard
    0x001fd345   520 ___stack_chk_fail
    0x000000010087efd   513 ___stack_chk_fail
    0x0000000100098hf3 514 ___stack_chk_guard
    0x00000001000897gfr   513 ___stack_chk_fail
    
    0 讨论(0)
提交回复
热议问题