Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-[SNTFileInfo initWithEndpointSecurityFile:error:] stat after open #1421

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Source/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ objc_library(

santa_unit_test(
name = "SNTFileInfoTest",
srcs = ["SNTFileInfoTest.m"],
srcs = ["SNTFileInfoTest.mm"],
resources = [
"testdata/32bitplist",
"testdata/bad_pagezero",
Expand All @@ -459,7 +459,10 @@ santa_unit_test(
"testdata/BundleExample.app/**",
"testdata/DirectoryBundle/**",
]),
deps = [":SNTFileInfo"],
deps = [
":SNTFileInfo",
":TestUtils",
],
)

santa_unit_test(
Expand Down
80 changes: 43 additions & 37 deletions Source/common/SNTFileInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,6 @@ @implementation SNTFileInfo
extern NSString *const NSURLQuarantinePropertiesKey WEAK_IMPORT_ATTRIBUTE;

- (instancetype)initWithResolvedPath:(NSString *)path error:(NSError **)error {
struct stat fileStat;
if (path.length) {
lstat(path.UTF8String, &fileStat);
}
return [self initWithResolvedPath:path stat:&fileStat error:error];
}

- (instancetype)initWithEndpointSecurityFile:(const es_file_t *)esFile error:(NSError **)error {
return [self initWithResolvedPath:@(esFile->path.data) stat:&esFile->stat error:error];
}

- (instancetype)initWithResolvedPath:(NSString *)path
stat:(const struct stat *)fileStat
error:(NSError **)error {
if (!fileStat) {
// This is a programming error. Bail.
LOGE(@"NULL stat buffer unsupported");
exit(EXIT_FAILURE);
}

self = [super init];
if (self) {
_path = path;
Expand All @@ -100,39 +80,50 @@ - (instancetype)initWithResolvedPath:(NSString *)path
return nil;
}

if (!((S_IFMT & fileStat->st_mode) == S_IFREG)) {
int fd = open([_path UTF8String], O_RDONLY | O_CLOEXEC);
if (fd < 0) {
if (error) {
NSString *errStr = [NSString stringWithFormat:@"Non regular file: %s", strerror(errno)];
NSString *errStr = [NSString stringWithFormat:@"Unable to open file: %s", strerror(errno)];
*error = [NSError errorWithDomain:@"com.google.santa.fileinfo"
code:290
code:280
userInfo:@{NSLocalizedDescriptionKey : errStr}];
}
return nil;
}
_fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES];

_fileSize = fileStat->st_size;
_vnode = (SantaVnode){.fsid = fileStat->st_dev, .fileid = fileStat->st_ino};

if (_fileSize == 0) return nil;

if (fileStat->st_uid != 0) {
struct passwd *pwd = getpwuid(fileStat->st_uid);
if (pwd) {
_fileOwnerHomeDir = @(pwd->pw_dir);
struct stat fileStat;
if (fstat(fd, &fileStat) == -1) {
if (error) {
NSString *errStr = [NSString stringWithFormat:@"Unable to stat file: %s", strerror(errno)];
*error = [NSError errorWithDomain:@"com.google.santa.fileinfo"
code:281
userInfo:@{NSLocalizedDescriptionKey : errStr}];
}
return nil;
}

int fd = open([_path UTF8String], O_RDONLY | O_CLOEXEC);
if (fd < 0) {
if (!((S_IFMT & fileStat.st_mode) == S_IFREG)) {
if (error) {
NSString *errStr = [NSString stringWithFormat:@"Unable to open file: %s", strerror(errno)];
NSString *errStr = [NSString stringWithFormat:@"Non regular file: %s", strerror(errno)];
*error = [NSError errorWithDomain:@"com.google.santa.fileinfo"
code:280
code:290
userInfo:@{NSLocalizedDescriptionKey : errStr}];
}
return nil;
}
_fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES];

_fileSize = fileStat.st_size;
_vnode = (SantaVnode){.fsid = fileStat.st_dev, .fileid = fileStat.st_ino};

if (_fileSize == 0) return nil;

if (fileStat.st_uid != 0) {
struct passwd *pwd = getpwuid(fileStat.st_uid);
if (pwd) {
_fileOwnerHomeDir = @(pwd->pw_dir);
}
}
}

return self;
Expand All @@ -156,6 +147,21 @@ - (instancetype)initWithPath:(NSString *)path error:(NSError **)error {
return self;
}

- (instancetype)initWithEndpointSecurityFile:(const es_file_t *)esFile error:(NSError **)error {
SNTFileInfo *fi = [self initWithResolvedPath:@(esFile->path.data) error:error];
if (fi.vnode.fsid != esFile->stat.st_dev || fi.vnode.fileid != esFile->stat.st_ino ||
fi.fileSize != esFile->stat.st_size) {
if (error) {
NSString *errStr = @"ES stat mismatch";
*error = [NSError errorWithDomain:@"com.google.santa.fileinfo"
code:261
userInfo:@{NSLocalizedDescriptionKey : errStr}];
}
return nil;
}
return fi;
}

- (instancetype)initWithPath:(NSString *)path {
return [self initWithPath:path error:NULL];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/// limitations under the License.

#import <XCTest/XCTest.h>
#include <sys/stat.h>
#include "Source/common/TestUtils.h"

#import "Source/common/SNTFileInfo.h"

Expand Down Expand Up @@ -250,4 +252,21 @@ - (void)testEmbeddedInfoPlist {
XCTAssertNotNil([sut infoPlist]);
}

- (void)testWithEndpointSecurityFile {
struct stat sb;
XCTAssertEqual(stat("/usr/bin/yes", &sb), 0);
es_file_t file = MakeESFile("/usr/bin/yes", sb);
NSError *error;
SNTFileInfo *sut = [[SNTFileInfo alloc] initWithEndpointSecurityFile:&file error:&error];
XCTAssertNotNil(sut);
}

- (void)testWithEndpointSecurityFileError {
// The constructed ES stat will not match `/usr/bin/yes` on disk stat values.
es_file_t file = MakeESFile("/usr/bin/yes");
NSError *error;
SNTFileInfo *sut = [[SNTFileInfo alloc] initWithEndpointSecurityFile:&file error:&error];
XCTAssertNil(sut);
}

@end
Loading