Skip to content

Commit

Permalink
[gRPC ObjC] Fix setenv crash for invalid c string access (grpc#27197)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennycd authored Sep 5, 2021
1 parent 2349aad commit 2d85886
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@

#import "GRPCSecureChannelFactory.h"

#import <GRPCClient/GRPCTypes.h>
#include <grpc/grpc_security.h>

#import "ChannelArgsUtil.h"
#import "GRPCChannel.h"

@implementation GRPCSecureChannelFactory {
grpc_channel_credentials *_channelCreds;
NSString *_sslRootPathStr;
}

+ (instancetype)factoryWithPEMRootCertificates:(NSString *)rootCerts
privateKey:(NSString *)privateKey
certChain:(NSString *)certChain
error:(NSError **)errorPtr {
error:(NSError *__autoreleasing *)errorPtr {
return [[self alloc] initWithPEMRootCerts:rootCerts
privateKey:privateKey
certChain:certChain
Expand All @@ -51,7 +53,7 @@ - (NSData *)nullTerminatedDataWithString:(NSString *)string {
- (instancetype)initWithPEMRootCerts:(NSString *)rootCerts
privateKey:(NSString *)privateKey
certChain:(NSString *)certChain
error:(NSError **)errorPtr {
error:(NSError *__autoreleasing *)errorPtr {
static dispatch_once_t loading;
dispatch_once(&loading, ^{
NSString *rootsPEM = @"roots";
Expand All @@ -60,8 +62,20 @@ - (instancetype)initWithPEMRootCerts:(NSString *)rootCerts
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSBundle *resourceBundle = [NSBundle
bundleWithURL:[[bundle resourceURL] URLByAppendingPathComponent:resourceBundlePath]];
NSString *path = [resourceBundle pathForResource:rootsPEM ofType:@"pem"];
setenv("GRPC_DEFAULT_SSL_ROOTS_FILE_PATH", [path cStringUsingEncoding:NSUTF8StringEncoding], 1);
_sslRootPathStr = [resourceBundle pathForResource:rootsPEM ofType:@"pem"];
const char *utf8Str = [_sslRootPathStr cStringUsingEncoding:NSUTF8StringEncoding];
if (utf8Str != NULL) {
setenv("GRPC_DEFAULT_SSL_ROOTS_FILE_PATH", utf8Str, 1);
} else {
if (errorPtr) {
*errorPtr = [NSError
errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeInternal
userInfo:@{
NSLocalizedDescriptionKey : @"Failed to set default ssl root file path."
}];
}
}
});

NSData *rootsASCII = nil;
Expand Down

0 comments on commit 2d85886

Please sign in to comment.