Repository navigation
feat: validate integrity of ASAR Integrity dictionary on macOS - #48587
Conversation
|
💖 Thanks for opening this pull request! 💖 Semantic PR titlesWe use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix. Examples of commit messages with semantic prefixes:
Commit signingThis repo enforces commit signatures for all incoming PRs. PR tipsThings that will help get your PR across the finish line:
We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can. |
MarshallOfSound
left a comment
There was a problem hiding this comment.
What happened to the idea of embedded the integrity dict in the data section of the binary?
To restate what I wrote in my original post:
Since end app developers aren't building Electron from source each time, I would have to write code to parse through the |
We already kinda do this with Electron Fuses. The 20ms hit isn't awful but I'd love to avoid it if possible. Is that 20ms on every launch (even with theoretically cached gatekeeper results) and on what hardware? |
Can you point me where, in the source code, that is done? It might help me craft something akin to the original idea embedded hash idea.
That's fair. And, for clarification, the cached Gatekeeper results are the source of this issue (and I don't think they're involved in this performance of this check). As for my hardware, I am running a MacBook Pro with an Apple M3 Max chip while plugged into AC power. |
|
Fuses reference: And the fuse module that modifies the binary: TLDR is there is a magic byte sequence that is compiled as We could do a similar thing here with for instance a SHA256 of the integrity dictionary. That would allow us to read the dictionary from Info.plist, validate it with a SHA256 from the binary (which will get auto validated by the kernel as the code page is loaded) and we can avoid calling Sec apis ourselves. This can all be transparent to developers because we can just do it magically during packaging 🤷 |
Is this byte-sequence fixed-size, though? Some Electron apps may have multiple ASAR archives they want to protect. Even just a single archive can have a different-length name, depending on configuration. I'm not sure if we can use the same trick for variable-length data. |
Yeah that's why I suggested |
Ok, so instead of storing another copy of the integrity dictionary, we hash the integrity dictionary just store that hash? Sounds like it could work! Although now, it makes me wonder, will that take more than 20 ms? |
That's what I'm thinking 🤔
I believe not, modern CPUs can run a single SHA256 operation substantially faster than 20ms on that size of payload |
I'll look into it! Honestly, now that I think about it, I think a lot of the 20ms I'm seeing for the current implementation may be due to IPC overhead (the Security framework does a lot of its operations in daemons). Doing everything in a single process sounds like it would definitely help. |
|
@MarshallOfSound If we're going to be hashing the integrity dictionary, both the checking end and the generating end will need to use the same serialization format. I think JSON is probably the best. |
This reverts commit 836bc8d.
|
Given we need to serialize this across platforms (as packaging doesn't always happen on macOS and we don't have easy NS API access in the packaging tooling) we should probably serialize this ourselves using a non-json format. E.g. "for each asar integrity entry sorted alphabetically by key, add the key to the hash, then the integrity algo, then the integrity hash" And treat that as the serialization format. That's easily implementable in any language and isn't dependent on JSON semantics |
Oh, interesting! I was thinking this implementation would be macOS-specific, since we are serializing the integrity dictionary from |
|
This is macOS specific, but packaging a macOS electron app does not have to occur on a macOS host. Therefore we might not have NS apis even if we wanted them when we get to generating this hash at packaging time |
Oh yeah, sorry. For clarification, I was talking about using the NS API's within the app itself (checking time), not at package time. Packagers like electron-packager are already written in JavaScript, so they can use the built-in |
|
Ah sorry I thought you'd pointed this out, but different JSON serialization implementations aren't by spec guaranteed to output the same result. In particular key ordering of objects is not strictly specified and so using two JSON impls and comparing them is not guaranteed to be safe (even if it appears to be in a few tests) |
Yeah, this was something at the back of my mind. I'm just wondering about the complication (however minor) of creating our own custom serialization format for this. Are there any other existing ones out there that have better guarantees for their byte/char-string serializations? EDIT: Re-reading your suggestion, I think I'm parsing out what you're saying (adding stuff to the hash one piece at a time compared to doing any sort of serialization). I'll look into that as well. |
Yeah this is what I was angling for |
|
@MarshallOfSound After wrestling with GN, macOS codesigning, and the like, I think I finally have something in place. Unfortunately, I had to commit with |
|
An additional note: I was incorrect about the IPC overhead of my original approach that used the Security framework (or at least my reasoning for it, I thought a daemon did the validation). It turns out there's an unskippable ticket registering IPC call that happens on the first validation. The actual validation check is trivially fast. Too bad we can't really skip that IPC call, though. |
|
@nmggithub CI builds testing builds not release builds, but you could be running into errors on GN check that only show up on release builds. You can verify locally if GN check is good by running |
|
@MarshallOfSound is there anything more you need from me? I'd like to see this get merged soon. |
|
Congrats on merging your first pull request! 🎉🎉🎉 |
|
Release Notes Persisted
|
|
@MarshallOfSound thank you very much for your help in this! Really happy to see this merged. |
|
Honestly, i dont see any sense in this change.
An attacker with access to the App Management or Full Disk Access (or if user disabled SIP. When no additional permissions needed) can just resign app locally. So Gatekeeper will not prevent app launch after reboot. Also it will allow modify hash of Asar Integrity dictionary in the executable. Asar Integrity can be bypassed like this: import crypto from "crypto";
import * as fs from 'fs';
import { execSync } from "child_process";
import { getRawHeader } from '@electron/asar';
import plist from 'plist';
function calcASARHeaderHash(archivePath) {
const headerString = getRawHeader(archivePath).headerString;
const hash = crypto.createHash('sha256').update(headerString).digest('hex');
return { algorithm: 'SHA256', hash };
}
function replaceSignDarwin(appPath) {
execSync(`codesign -d --entitlements :- '${appPath}' > '${EXTRACTED_ENTITLEMENTS_PATH}'`);
execSync(`codesign --force --entitlements '${EXTRACTED_ENTITLEMENTS_PATH}' --sign - '${appPath}'`);
}
function bypassAsarIntegrityDarwin(appPath, oldHash) {
const newHash = calcASARHeaderHash(ASAR_PATH).hash;
const plistContent = fs.readFileSync(INFO_PLIST_PATH, 'utf8');
const plistData = plist.parse(plistContent);
plistData.ElectronAsarIntegrity["Resources/app.asar"].hash = newHash;
fs.writeFileSync(INFO_PLIST_PATH, plist.build(plistData));
findAndReplaceHashInExecutable(appPath, oldHash, newHash); // Step required only after Electron 41.0.0
replaceSignDarwin(appPath);
}
// Based on https://github.com/TheKing-OfTime/YandexMusicModPatcher/blob/master/src/main/modules/patcher.js |
While you are correct here that it is possible to bypass this change with additional effort, that does not mean this security layer lacks any value. It's meant to be an additional deterrent for attackers. Having to re-sign an app is a meaningfully higher bar to clear (requiring additional steps and tooling as opposed to simply editing files), and can result in the app losing things like TCC privileges, access to Safe Storage, and can potentially break other features of the app. Finally, this change has been discussed thoroughly with the Electron team and has been accepted and merged. If you believe there is more that can be done or changed, I'd encourage you to file an issue or PR with your suggestions. |
|
npx asar integrity-digest on "${appBundlePath}" , when this will be ready to replace the asar command? and how is it related to electron/packager#1890? |
… again Since Electron 41, Electron Framework holds a SHA-256 of Info.plist's ElectronAsarIntegrity dictionary, and Electron aborts on launch when the dictionary doesn't match it (electron/electron#48587). Claude 4.4212.0 ships Electron 44 with the digest set for both architectures, so after we update the app.asar hash in Info.plist, Claude dies on every launch with "Failed to get integrity for validatable asar archive" (SIGTRAP). After updating the hash, recompute the digest and write it into each architecture's slot in Electron Framework, before re-signing. Bump PatchVersion so existing installs get re-patched. Fixes lugia19#68.
… again (#73) Since Electron 41, Electron Framework holds a SHA-256 of Info.plist's ElectronAsarIntegrity dictionary, and Electron aborts on launch when the dictionary doesn't match it (electron/electron#48587). Claude 4.4212.0 ships Electron 44 with the digest set for both architectures, so after we update the app.asar hash in Info.plist, Claude dies on every launch with "Failed to get integrity for validatable asar archive" (SIGTRAP). After updating the hash, recompute the digest and write it into each architecture's slot in Electron Framework, before re-signing. Bump PatchVersion so existing installs get re-patched. Fixes #68.
Description of Change
cc @MarshallOfSound
This closes up a potential risk where an attacker with access to the App Management or Full Disk Access TCC services tampers with both the
app.asararchive and theInfo.plistfor an app. I have found, through practice, that doing so does not always render the app un-launchable and can essentially bypass ASAR Integrity (however upon reboot, the app will fail to launch). All signs point to Gatekeeper (or some related functionality) caching validation results in a cache that is invalidated upon reboot.Due to this risk, I believe it is important to do our own validation on launch right before we check the hashes.
An alternative mitigation, storing the hashes inside the main executable so they inherit the protections of the executable's code signature, was deemed infeasible due to the flimsiness of injecting new content into an already-built Mach-O binary (the state in which Electron is delivered to end developers in most cases).UPDATE: The executable is now used as part of this additional layer of security. The above and below stricken text is no longer relevant.
The validation is performed with the flags:kSecCSBasicValidateOnly | kSecCSNoNetworkAccess. These were chosen in order to minimize the validation overhead. Even under a basic validation, any tampering ofInfo.plistwill be caught and anerrSecCSInfoPlistFailederror code will be returned. As the hypothetical compromise above requires the tampering ofInfo.plist, a basic validation is a sufficient mitigation. ThekSecCSNoNetworkAccessflag is used to ensure the validation does not perform any network I/O (unneeded for the validation we're performing).Overall, when testing this validation code in isolation on my machine, I find it takes around 20 milliseconds. I personally believe this will have a negligible-to-nonexistent impact on user experience.Checklist
Release Notes
Notes: Added additional defence against privileged user modifications to ASAR Integrity protected applications on macOS