Skip to content

Commit

Permalink
android: Set DexFile to read-only in Java.registerClass() (#318)
Browse files Browse the repository at this point in the history
As of Android 14, apps with targetSdk >= 34 are not allowed to have
writable permissions on dynamically loaded Dex files.

https://developer.android.com/about/versions/14/behavior-changes-14#safer-dynamic-code-loading

This removes writable permissions from the temporary DexFile created by
Java.registerClass() after writing to it and before it gets loaded.

Co-authored-by: Georgi Boiko <[email protected]>
  • Loading branch information
pandasauce and Georgi Boiko authored May 21, 2024
1 parent 8d7b714 commit 37c1ec5
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/class-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,7 @@ class DexFile {
const file = new File(filePath, 'w');
file.write(buffer.buffer);
file.close();
setReadOnlyDex(filePath, factory);

return new DexFile(filePath, fileValue, factory);
}
Expand Down Expand Up @@ -2251,6 +2252,12 @@ function createTemporaryDex (factory) {
return JFile.createTempFile(tempFileNaming.prefix, tempFileNaming.suffix + '.dex', cacheDirValue);
}

function setReadOnlyDex (filePath, factory) {
const JFile = factory.use('java.io.File');
const file = JFile.$new(filePath);
file.setWritable(false, false);
}

function getFactoryCache () {
switch (factoryCache.state) {
case 'empty': {
Expand Down

0 comments on commit 37c1ec5

Please sign in to comment.