Skip to content

Commit 2be68d4

Browse files
maxtacommaxim
authored andcommitted
relax the admin check and simplify the helper
- it's too restrictive to only let admins use the helper because some mac users use one-off admin privileges - also let's simplify the helper and get rid of the "move" RPC which is dangerous; instead, just use user perms to install the app bundle - also, check that only group staff can use the helper, so that low privilege accounts (like nobody) can't
1 parent 4b60866 commit 2be68d4

3 files changed

Lines changed: 74 additions & 40 deletions

File tree

‎osx/Helper/KBHelper.m‎

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import <MPMessagePack/MPXPCProtocol.h>
1414
#include <pwd.h>
1515
#include <grp.h>
16+
#include <unistd.h>
1617

1718
@interface KBHelper ()
1819
@property NSTask *redirector;
@@ -73,9 +74,11 @@ - (void)_handleRequestWithMethod:(NSString *)method params:(NSArray *)params mes
7374
return;
7475
}
7576

76-
if (![self _checkRemoteIsAdmin:remote]) {
77-
KBLog(@"Remote caller into Helper process didn't have admin permissions");
78-
completion(KBMakeError(MPXPCErrorCodeInvalidRequest, @"Invalid admin permissions"), nil);
77+
uid_t uid = xpc_connection_get_euid(remote);
78+
79+
if (![self _checkUID:uid inGroup:"staff"]) {
80+
KBLog(@"Remote caller into Helper process didn't have staff permissions");
81+
completion(KBMakeError(MPXPCErrorCodeInvalidRequest, @"Invalid staff permissions"), nil);
7982
return;
8083
}
8184

@@ -100,8 +103,8 @@ - (void)_handleRequestWithMethod:(NSString *)method params:(NSArray *)params mes
100103
[KBKext copyWithSource:args[@"source"] destination:args[@"destination"] removeExisting:YES completion:completion];
101104
} else if ([method isEqualToString:@"remove"]) {
102105
[self remove:args[@"path"] completion:completion];
103-
} else if ([method isEqualToString:@"move"]) {
104-
[self moveFromSource:args[@"source"] destination:args[@"destination"] overwriteDestination:YES completion:completion];
106+
} else if ([method isEqualToString:@"uninstallAppBundle"]) {
107+
[self uninstallAppBundle:completion];
105108
} else if ([method isEqualToString:@"createDirectory"]) {
106109
[self createDirectory:args[@"directory"] uid:args[@"uid"] gid:args[@"gid"] permissions:args[@"permissions"] excludeFromBackup:[args[@"excludeFromBackup"] boolValue] completion:completion];
107110
} else if ([method isEqualToString:@"addToPath"]) {
@@ -117,33 +120,45 @@ - (void)_handleRequestWithMethod:(NSString *)method params:(NSArray *)params mes
117120
}
118121
}
119122

120-
- (BOOL)_checkRemoteIsAdmin:(xpc_connection_t)remote {
123+
- (BOOL)_checkRemote:(xpc_connection_t)remote inGroup:(char *)group {
124+
uid_t uid = xpc_connection_get_euid(remote);
125+
return [self _checkUID:uid inGroup:group];
126+
}
127+
128+
- (BOOL)_checkUID:(uid_t )uid inGroup:(char *)group {
121129
char getpwuid_buf[1024];
122130
char getgrnam_buf[8192];
131+
int gids[1024];
132+
int n_gids = 1024;
123133

124-
uid_t uid = xpc_connection_get_euid(remote);
125134
struct passwd pwentry;
126135
struct passwd *pwentryp;
127136
int tmp = getpwuid_r(uid, &pwentry, getpwuid_buf, sizeof(getpwuid_buf), &pwentryp);
128137
if (tmp != 0 || pwentryp == NULL) {
129138
KBLog(@"Failed to get PW entry for uid %@", uid);
130139
return NO;
131140
}
132-
struct group admin_group;
133-
struct group *admin_group_p;
134-
tmp = getgrnam_r("admin", &admin_group, getgrnam_buf, sizeof(getgrnam_buf), &admin_group_p);
135-
if (tmp != 0 || admin_group_p == NULL) {
136-
KBLog(@"Could not call getgrnam on the 'admin' group, so can't tell if remote is an admin");
141+
struct group wanted_group;
142+
struct group *wanted_group_p;
143+
tmp = getgrnam_r(group, &wanted_group, getgrnam_buf, sizeof(getgrnam_buf), &wanted_group_p);
144+
if (tmp != 0 || wanted_group_p == NULL) {
145+
KBLog(@"Could not call getgrnam on the '%s' group, so can't tell if uid is in it", group);
137146
return NO;
138147
}
139-
int n = 0;
140-
for (char **p = admin_group_p->gr_mem; *p; p++, n++) {
141-
if (strcmp(pwentryp->pw_name, *p) == 0) {
142-
KBLog(@"Yes, the calling user '%s' is an 'admin'", pwentryp->pw_name);
148+
149+
tmp = getgrouplist(pwentryp->pw_name, pwentryp->pw_gid, gids, &n_gids);
150+
if (tmp != 0) {
151+
KBLog(@"Could not call getgrouplist for '%s'", pwentryp->pw_name);
152+
return NO;
153+
}
154+
155+
for (int i = 0; i < n_gids; i++) {
156+
if (gids[i] == wanted_group_p->gr_gid) {
157+
KBLog(@"Checked that the calling user '%s' (%d) is in '%s' (%d)", pwentryp->pw_name, uid, group, wanted_group_p);
143158
return YES;
144159
}
145160
}
146-
KBLog(@"The 'admin' group had %d members but '%s' wasn't one", n, pwentryp->pw_name);
161+
KBLog(@"The user '%s' was in %d groups but not in '%s'", pwentryp->pw_name, n_gids, group);
147162
return NO;
148163
}
149164

@@ -423,19 +438,27 @@ - (void)removeFromPath:(NSString *)directory name:(NSString *)name appName:(NSSt
423438
}
424439
}
425440

426-
- (void)moveFromSource:(NSString *)source destination:(NSString *)destination overwriteDestination:(BOOL)overwriteDestination completion:(void (^)(NSError *error, id value))completion {
441+
- (NSError *)_moveFromSource:(NSString *)source destination:(NSString *)destination {
427442
NSError *error = nil;
428443
if ([NSFileManager.defaultManager fileExistsAtPath:destination isDirectory:NULL] && ![NSFileManager.defaultManager removeItemAtPath:destination error:&error]) {
429-
completion(error, nil);
430-
return;
444+
return error;
431445
}
432-
433446
if (![NSFileManager.defaultManager moveItemAtPath:source toPath:destination error:&error]) {
434-
completion(error, nil);
435-
return;
447+
return error;
436448
}
449+
return nil;
450+
}
437451

438-
completion(nil, @{});
452+
- (void)uninstallAppBundle:(void (^)(NSError *error, id value))completion {
453+
NSError *error = nil;
454+
NSString *source = @"/Applications/Keybase.app";
455+
NSString *destination = @"/tmp/Keybase.app";
456+
error = [self _moveFromSource:source destination:destination];
457+
if (error == nil) {
458+
completion(nil, @{});
459+
} else {
460+
completion(error, nil);
461+
}
439462
}
440463

441464
- (void)remove:(NSString *)path completion:(void (^)(NSError *error, id value))completion {

‎osx/KBKit/KBKit/Component/KBAppBundle.m‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ - (instancetype)initWithConfig:(KBEnvConfig *)config helperTool:(KBHelperTool *)
2121
return self;
2222
}
2323

24+
- (NSError *)_moveFromSource:(NSString *)source destination:(NSString *)destination {
25+
NSError *error = nil;
26+
if ([NSFileManager.defaultManager fileExistsAtPath:destination isDirectory:NULL] && ![NSFileManager.defaultManager removeItemAtPath:destination error:&error]) {
27+
return error;
28+
}
29+
if (![NSFileManager.defaultManager moveItemAtPath:source toPath:destination error:&error]) {
30+
return error;
31+
}
32+
return nil;
33+
}
34+
2435
- (void)validate:(NSString *)sourcePath completion:(KBCompletion)completion {
2536
// Check bundle security/requirement (for source path)
2637
CFURLRef fileRef = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)[sourcePath UTF8String], [sourcePath length], YES);
@@ -33,7 +44,7 @@ - (void)validate:(NSString *)sourcePath completion:(KBCompletion)completion {
3344

3445
/**
3546
The app must satisfy the following codesigning requirements:
36-
47+
3748
1. The root certificate must be an "apple generic" certificate
3849
2. The leaf of the Mac App Store certificate must have the field "field.1.2.840.113635.100.6.1.9", or
3950
3. Certificate 1 corresponds to the "Developer ID Certification Authority" certificate and must have the field "1.2.840.113635.100.6.2.6"
@@ -42,7 +53,7 @@ - (void)validate:(NSString *)sourcePath completion:(KBCompletion)completion {
4253
6. The identifier be "keybase.Keybase" or "keybase.Electron"
4354
4455
This requirement is the standard one issued by Xcode when signing with developer ID certificate.
45-
56+
4657
You can view designated requirements for an app by running: codesign -d -r- /Applications/Keybase.app
4758
4859
References:
@@ -69,7 +80,7 @@ - (void)validate:(NSString *)sourcePath completion:(KBCompletion)completion {
6980
- (void)install:(KBCompletion)completion {
7081
NSString *sourcePath = self.config.sourcePath;
7182
if (!sourcePath || ![NSFileManager.defaultManager fileExistsAtPath:sourcePath]) {
72-
completion(KBMakeError(-1, @"Invalid source path"));
83+
completion(KBMakeError(-1, [NSString stringWithFormat:@"Invalid source path: %@", sourcePath]));
7384
return;
7485
}
7586

@@ -79,25 +90,25 @@ - (void)install:(KBCompletion)completion {
7990
return;
8091
}
8192

93+
DDLogInfo(@"Installing %@ -> %@", sourcePath, destinationPath);
8294
DDLogInfo(@"Checking security requirements");
8395
[self validate:sourcePath completion:^(NSError *error) {
8496
if (error) {
8597
completion(error);
8698
return;
8799
}
100+
NSString *sourceContents = [NSString stringWithFormat:@"%@/%@", sourcePath, @"Contents"];
101+
NSString *destContents = [NSString stringWithFormat:@"%@/%@", destinationPath, @"Contents"];
102+
DDLogInfo(@"Copying app Contents bundle %@ to %@", sourceContents, destContents);
103+
error = [self _moveFromSource:sourceContents destination:destContents];
104+
if (error) {
105+
completion(error);
106+
return;
107+
}
88108

89-
DDLogInfo(@"Copying app bundle %@ to %@", sourcePath, destinationPath);
90-
NSDictionary *params = @{@"source": sourcePath, @"destination": destinationPath};
91-
[self.helperTool.helper sendRequest:@"move" params:@[params] completion:^(NSError *error, id value) {
92-
if (error) {
93-
completion(error);
94-
return;
95-
}
96-
97-
// Re-verify destination
98-
DDLogInfo(@"Checking destination");
99-
[self validate:destinationPath completion:completion];
100-
}];
109+
// Re-verify destination
110+
DDLogInfo(@"Checking destination");
111+
[self validate:destinationPath completion:completion];
101112
}];
102113
}
103114

‎osx/KBKit/KBKit/Component/KBHelperTool.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ @implementation KBHelperTool
2828

2929
- (instancetype)initWithConfig:(KBEnvConfig *)config {
3030
if ((self = [self initWithConfig:config name:@"Privileged Helper" info:@"Runs privileged tasks" image:[KBIcons imageForIcon:KBIconExtension]])) {
31-
31+
3232
}
3333
return self;
3434
}

0 commit comments

Comments
 (0)