|
| 1 | +/* |
| 2 | + Copyright (c) 2013-2026 AM Crypto. All rights reserved. |
| 3 | +
|
| 4 | + Governed by the Apache License 2.0 the full text of which is |
| 5 | + contained in the file License.txt included in VeraCrypt binary and source |
| 6 | + code distribution packages. |
| 7 | +*/ |
| 8 | + |
| 9 | +#include "System.h" |
| 10 | +#include "MacOSXSleepLock.h" |
| 11 | + |
| 12 | +#ifdef TC_MACOSX |
| 13 | +#import <Cocoa/Cocoa.h> |
| 14 | +#import <ApplicationServices/ApplicationServices.h> |
| 15 | + |
| 16 | +// Queries the window server for the actual lock state of the current session. |
| 17 | +// The com.apple.screenIsLocked distributed notification can be posted by any |
| 18 | +// local process, so it is treated only as a hint and the dismount is performed |
| 19 | +// only if the session really is locked. |
| 20 | +static bool SessionIsLocked () |
| 21 | +{ |
| 22 | + bool locked = false; |
| 23 | + CFDictionaryRef session = CGSessionCopyCurrentDictionary (); |
| 24 | + if (session) |
| 25 | + { |
| 26 | + CFTypeRef value = CFDictionaryGetValue (session, CFSTR ("CGSSessionScreenIsLocked")); |
| 27 | + locked = value && CFGetTypeID (value) == CFBooleanGetTypeID () && CFBooleanGetValue ((CFBooleanRef) value); |
| 28 | + CFRelease (session); |
| 29 | + } |
| 30 | + return locked; |
| 31 | +} |
| 32 | + |
| 33 | +@interface VCSleepLockObserver : NSObject |
| 34 | +- (void) systemWillSleep: (NSNotification *) notification; |
| 35 | +- (void) screenLocked: (NSNotification *) notification; |
| 36 | +- (void) dismountIfSessionLocked; |
| 37 | +@end |
| 38 | + |
| 39 | +@implementation VCSleepLockObserver |
| 40 | + |
| 41 | +// The callbacks below are entered from AppKit notification dispatch; a C++ |
| 42 | +// exception must not unwind through those Objective-C frames, so every call |
| 43 | +// into VeraCrypt code is guarded here. The dismount is best-effort: showing |
| 44 | +// UI is not reliable while the session is locking or the system is going to |
| 45 | +// sleep, so failures are ignored. |
| 46 | + |
| 47 | +- (void) systemWillSleep: (NSNotification *) notification |
| 48 | +{ |
| 49 | + (void) notification; |
| 50 | + try |
| 51 | + { |
| 52 | + VeraCrypt::OnMacOSXSystemWillSleep (); |
| 53 | + } |
| 54 | + catch (...) { } |
| 55 | +} |
| 56 | + |
| 57 | +- (void) screenLocked: (NSNotification *) notification |
| 58 | +{ |
| 59 | + (void) notification; |
| 60 | + // The window server can update the session dictionary slightly after the |
| 61 | + // notification is delivered, so if the session does not report itself as |
| 62 | + // locked yet, re-check once after a short delay instead of giving up. |
| 63 | + if (SessionIsLocked ()) |
| 64 | + { |
| 65 | + [self dismountIfSessionLocked]; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector (dismountIfSessionLocked) object: nil]; |
| 70 | + [self performSelector: @selector (dismountIfSessionLocked) withObject: nil afterDelay: 1.0]; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +- (void) dismountIfSessionLocked |
| 75 | +{ |
| 76 | + if (!SessionIsLocked ()) |
| 77 | + return; |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + VeraCrypt::OnMacOSXScreenLocked (); |
| 82 | + } |
| 83 | + catch (...) { } |
| 84 | +} |
| 85 | + |
| 86 | +@end |
| 87 | + |
| 88 | +namespace VeraCrypt |
| 89 | +{ |
| 90 | + // Non-ARC build (see the .mm compile rule in Build/Include/Makefile.inc), |
| 91 | + // so the observer is retained by alloc/init and released in Uninstall. |
| 92 | + static VCSleepLockObserver *SleepLockObserver = nil; |
| 93 | + |
| 94 | + void InstallMacOSXSleepLockHandler () |
| 95 | + { |
| 96 | + if (SleepLockObserver) |
| 97 | + return; |
| 98 | + |
| 99 | + SleepLockObserver = [[VCSleepLockObserver alloc] init]; |
| 100 | + |
| 101 | + // System sleep (lid close, idle sleep, Apple menu > Sleep). Delivered on |
| 102 | + // the main thread by NSWorkspace's own notification center, which only |
| 103 | + // the system feeds (it is per-process, unlike the distributed center). |
| 104 | + [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 105 | + addObserver: SleepLockObserver |
| 106 | + selector: @selector (systemWillSleep:) |
| 107 | + name: NSWorkspaceWillSleepNotification |
| 108 | + object: nil]; |
| 109 | + |
| 110 | + // Screen lock. macOS exposes no public notification for this; the |
| 111 | + // com.apple.screenIsLocked distributed notification is the long-standing |
| 112 | + // de-facto mechanism used to detect it. It is untrusted input: the |
| 113 | + // handler verifies the actual session lock state before dismounting. |
| 114 | + [[NSDistributedNotificationCenter defaultCenter] |
| 115 | + addObserver: SleepLockObserver |
| 116 | + selector: @selector (screenLocked:) |
| 117 | + name: @"com.apple.screenIsLocked" |
| 118 | + object: nil]; |
| 119 | + } |
| 120 | + |
| 121 | + void UninstallMacOSXSleepLockHandler () |
| 122 | + { |
| 123 | + if (!SleepLockObserver) |
| 124 | + return; |
| 125 | + |
| 126 | + [NSObject cancelPreviousPerformRequestsWithTarget: SleepLockObserver]; |
| 127 | + [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver: SleepLockObserver]; |
| 128 | + [[NSDistributedNotificationCenter defaultCenter] removeObserver: SleepLockObserver]; |
| 129 | + [SleepLockObserver release]; |
| 130 | + SleepLockObserver = nil; |
| 131 | + } |
| 132 | +} |
| 133 | +#endif |
0 commit comments