Fix: restore NSPersistentStoreFileProtectionKey and iCloud backup exclusion for health data (CWE-311, CWE-312) - #750
Open
iaohkut-from-NightWolf-Team wants to merge 1 commit into
Conversation
…for health data store (CWE-311, CWE-312) Commit 659fbb2 (v3.1.7) removed two security controls from OCKStore's loadStore function: 1. `NSPersistentStoreFileProtectionKey` was removed from NSPersistentStoreDescription. On iOS, CoreData's SQLite files do not inherit the parent directory's file protection class — it must be set explicitly via this key. Without it, the .sqlite/.sqlite-wal/.sqlite-shm files use the default `completeUntilFirstUserAuthentication` instead of the intended `complete`, making health data accessible when the device is locked (after first unlock since boot). 2. The correct `URLResourceValues.isExcludedFromBackup = true` (set via URL.setResourceValues after loadPersistentStores) was replaced with `FileAttributeKey(kCFURLIsExcludedFromBackupKey as String)` passed to createDirectory. This is the wrong API — kCFURLIsExcludedFromBackupKey is a URL resource key and is silently ignored by FileManager. As a result, the SQLite health data files are included in iCloud/iTunes backups. This commit restores both controls to their pre-3.1.7 state and removes the ineffective FileAttributeKey backup exclusion attempt. Co-Authored-By: iaohkut <thb2601@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Security Fix — Missing File Protection and Backup Exclusion (CWE-311, CWE-312)
Summary
Commit 659fbb2 (v3.1.7, 2025-05-13) introduced a regression that removed two security controls protecting the CareKit health data SQLite store. This PR restores both controls.
Issue 1 — Missing
NSPersistentStoreFileProtectionKey(CWE-311)Root cause:
NSPersistentStoreFileProtectionKeywas removed fromNSPersistentStoreDescriptioninloadStore(into:).On iOS, CoreData's SQLite files (
.sqlite,.sqlite-wal,.sqlite-shm) do not inherit the parent directory's file protection class. WithoutNSPersistentStoreFileProtectionKey, these files use the iOS default (completeUntilFirstUserAuthentication) instead of the developer-specified.completeclass.Impact: Health data (OCKPatient, OCKTask, OCKOutcome, OCKCarePlan, OCKContact) is accessible using forensic tools when the device is locked — as long as it was unlocked at least once since the last reboot. The intended
completeprotection requires the device to be actively unlocked.Vulnerable code (current):
Fix (this PR):
Issue 2 — Incorrect iCloud Backup Exclusion API (CWE-312)
Root cause: The correct
URLResourceValues.isExcludedFromBackup = true(set viaURL.setResourceValuesafterloadPersistentStores) was replaced withFileAttributeKey(kCFURLIsExcludedFromBackupKey as String): trueincreateDirectoryattributes.kCFURLIsExcludedFromBackupKeyis a URL resource key (NSURLIsExcludedFromBackupKey), not a POSIXFileAttributeKey. It is silently ignored byFileManager.createDirectory. Additionally, even if the directory exclusion were set correctly, it does not propagate to files CoreData creates inside it.Impact: SQLite health data files are included in iCloud and iTunes backups. If a user's Apple ID is compromised, an attacker can download the backup and read all health records.
Vulnerable code (current):
Fix (this PR): Removes the ineffective
FileAttributeKeyentry and restores the correctURL.setResourceValuesapproach called in theloadPersistentStorescompletion handler — after the store file is created.Affected Versions
Pre-regression Versions (not affected)
Note for Apple Product Security
This regression was identified via static analysis and git history comparison. If preferred, security details can also be reported to product-security@apple.com.
References