Swift and Stack smashing protection

2020-03-11 23:01发布

问题:

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 tab in xCode, but is seems that, this flag is unused or have no impact on builded application.

To verifie build I'm using otool -Iv AppBinary | grep stack.

回答1:

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


回答2:

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.