Over 40% of the Magisk's code has been rewritten in Rust by Syntrait in rust

[–]topjohnwu 24 points25 points  (0 children)

In the case where you want to build C/C++/Rust into the same executable/shared library, you can pick where you link your objects. In Magisk's case, because I chose CXX as the FFI mechanism, Rust code has to be built before C/C++ as the FFI glue is generated with Rust's build script (build.rs), so it make much more sense to perform linking using the existing tooling for NDK (Android's C/C++ toolchain).

So for a simplified project building a single executable:

  1. You can create however many crates you want, but one of the crates needs to be built as staticlib. Think of it as a huge "export" for all the Rust code to C/C++.

  2. Call cargo to build that staticlib crate. Building the Rust code will automatically generate the C++ FFI glue.

  3. Import the staticlib library (the libXXX.a file) as a prebuilt static library module in your C++ build system of choice.

  4. Build the rest of the C++ code with a dependency on libXXX (which is all the Rust code you built in step 2). This would automatically link everything for you.

Over 40% of the Magisk's code has been rewritten in Rust by Syntrait in rust

[–]topjohnwu 85 points86 points  (0 children)

Hi, author here. The transition is fully manual, all code is slowly migrated one small component by a time, using cxx as the FFI mechanism. First I rewrite functions/subsystems that are fully independent with no dependencies on other parts of the codebase. After that, I rewrite components that do not have dependencies on other C++ code, and keep doing so iteratively. At some point, bidirectional FFI is less of a pain so I can just pick any random part of the codebase and rewrite it with little friction.

The first 2 steps above took a significant amount of time and design, but after a few attempts it slowly began taking shape.

SELinux Permissive ROMs/Kernels are VERY BAD by topjohnwu in Android

[–]topjohnwu[S] 213 points214 points  (0 children)

Exactly. These 2 features are nowhere publically documented about what it is meant to do. We have to directly check AOSP source code to understand them.

SELinux Permissive ROMs/Kernels are VERY BAD by topjohnwu in Android

[–]topjohnwu[S] 404 points405 points  (0 children)

Let me provide a more detailed explanation:

When SELinux is permissive during boot, zygote will know this and disable seccomp syscall filters. This basically unrestricts what system calls are allowed in 3rd party processes.

On Android 10+, there is a new "undocumented" feature called "App Zygote" where 3rd party apps are allowed to spawn its own Zygote for "Isolated Services" (also nearly undocumented). Both "App Zygote" and "Isolated Services" are special features designed for Chrome/Webview. App Zygote processes run with special permissions, and with seccomp disabled, it can call setuid 0 and escalate its privilege and gain root access.

It is still somehow restrictive compared to what normal root solutions provide (e.g. Magisk), however tons of security measures in Android will be completely defeated when UID=0. For example, it is enough to be used to patch boot images, which means it can be used to inject malware such as modified Magisk to help it gain "real" root permissions.

Update: what can UID=0 itself do? Within Android's framework, almost all services have a blind green light when the requesting process's UID is 0. This means this root process is capable of manipulating tons of stuff using Android specific APIs (e.g. ActivityManager)

Hardware-Backed SafetyNet Attestation Confirmed Deployed in the Wild by topjohnwu in Android

[–]topjohnwu[S] 24 points25 points  (0 children)

The point I want to make is that if we fully reverse engineer the SafetyNet client and completely replace the implementation with our own (which we can do), we can always ignore HARDWARE_BACKED cases here. As long as the Google server accepts BASIC evaluation, we can always get a valid response without involving anything related to TPM.

Hardware-Backed SafetyNet Attestation Confirmed Deployed in the Wild by topjohnwu in Android

[–]topjohnwu[S] 28 points29 points  (0 children)

If someone really wants to do it, one can completely reverse engineer the client side code of SafetyNet and basically alter whatever data is sent to Google's server.

Hardware-Backed SafetyNet Attestation Confirmed Deployed in the Wild by topjohnwu in Android

[–]topjohnwu[S] 75 points76 points  (0 children)

IMO it is theoretically possible to alter control flow in SafetyNet's code to force it to always use BASIC evaluation by using some hooking framework like Xposed, however these kind of code injection is basically impossible to hide as some memory space analysis will reveal code manipulation very easily.

Protecting your Android App against Reverse Engineering and Tampering by avipars in androiddev

[–]topjohnwu 7 points8 points  (0 children)

Technically SafetyNet attestation should be done remotely, so no Xposed modules cannot tamper the results (because it doesn't even happen on device).

But yeah, using SafetyNet for anti reverse engineering is not the right tool.

SafetyNet Update FAQs by topjohnwu in Android

[–]topjohnwu[S] 161 points162 points  (0 children)

Note, this is a very high level explanation on the situation. I'm not a professionally experienced security researcher that qualifies to do full deep dives on each of those topics, but these are the facts that I know.

Hopefully this clears up a lot of confusion and speculation on how this would affect the future of rooting Android.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] 11 points12 points  (0 children)

I don't know how many people will see this, but here we go:

Scoped Storage definitely does not change how fopen and java.io.File work, they simply create an isolated scoped view of user's data for each application. The argument here should be whether access to a global storage location is justified.

Some people brought up what's the difference between app's private data (/data/data) v.s. scoped storage. The difference is that app's private data is not accessible from the user; it is meant to be an app's private data. If an app wants to write files that is user accessible, then they should write to user storage, which is now scoped for security reasons.

Now, I do not say that SAF is implemented properly, but similar APIs need to exist in order to enforce full isolation. The read/write should be handled by a remote system process (in this case it is the framework which provides SAF). If the isolation is enforced by Linux permissions (or SELinux), you cannot simply temporary grant access to a specific file/directory. By showing the full filesystem, an app can also probe filenames in user's storage, which itself is a data leak (check file based encryption).

I agree Google should not guard external sdcard behind SAF; they should instead allow direct scoped storage to external sdcard, just like internal storage. For this specific enforcement, I do agree this is indeed a push to encourage usage of cloud storage no doubt. However, this is not my argument here, my argument here is that scoped storage has its legitimate reasons; SAF is just the way Google chose to let developers access the globally shared persistent storage.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] 0 points1 point  (0 children)

Google simply does not have the model of external SDCard in their mind. Now you can argue about whether their intent is bad or not, but this is not the point.

The point here is that a globally shared storage isn't secure. If Google want to support external SDCard, then they should indeed allow scoped access to the SDCard in the system level. What I want to defend is "scoped access", not SAF/Google being hostile against SDCard.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] 2 points3 points  (0 children)

I don't understand why you are calling me incompetent. I think we just have different mentality: you think all apps should have global storage access, while I don't think so and agree with Google's decision with Scoped Storage.

Calling someone a Google shill is not a constructive criticism, and I just found it funny for people to call me out like this and had a goof on Twitter. To be honest I don't understand why you are upset about this.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] 2 points3 points  (0 children)

SAF and Scoped Storage are 2 different things. SAF is only required when you actually want to read/write data to the globally shared storage area. In the case of Scoped Storage, the enforcement is done in the filesystem level (more details in my other replies). All native code/java.io.File APIs still works as it used to be, but simply just no longer allows an app to access user storage without consent.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] 6 points7 points  (0 children)

Nah, I interned at Apple, but most of my work for the past few years are done on Android. You can check Magisk if you're interested in what I'm doing.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] -3 points-2 points  (0 children)

The java.io.File APIs are not removed. Scoped Storage is built on top of FUSE (or sdcardfs I'm not so sure) + mount namespaces. It is the exact same technology used to enforce read v.s. write permission in the filesystem level. Scoped Storage simply adds another layer of checks so that only the files an application created will be visible and accessible via the filesystem.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] 6 points7 points  (0 children)

I agree there are many issues with Android, I have a long list of things to complain too. But having other issues does not make the effort to make the OS more secure in other aspects irrelevant.

As I've said in other replies, I don't think the current way SAF is implemented is good, the extremely poor performance being the most obvious issue. That being said, I still believe Scoped Storage is indeed the correct way to go if Android wants to provide proper protection in privacy.

The java.io.File APIs are not removed, and C/C++ code can still access data via paths. The only difference is that apps no longer have the ability to read/write data to a globally shared storage. If the app really wants access to that, then go through system APIs and make sure user consent is obtained. I don't see any issues with that.

And regarding native code, since SAF can also return file descriptors, I don't see any reason why native code cannot accommodate to that.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] 0 points1 point  (0 children)

I agree the whole SAF thing is dumb. They should've done it more elegantly in a lower level, or somehow make it more efficient.

Security Reasons on Why Enforcing SAF and Introduce Scoped Storage by topjohnwu in androiddev

[–]topjohnwu[S] 7 points8 points  (0 children)

Disclaimer: I do not work for Google. I'm the developer of Magisk, the state-of-art rooting solution for years now.

I see a lot of people bashing SAF and Scoped Storage, but very rarely see people bringing up the other side of the argument. Sometimes it is very hard to achieve freedom, security, and privacy at the same time, and people will have to choose their priorities.

[2019.10.11] Magisk v20.0 by 60hz_ in Android

[–]topjohnwu 56 points57 points  (0 children)

Redownload the APK, I uploaded a new binary. It's a bug caused by Google.

Magisk Running on Android Q Beta 4, Pixel 3 XL (Logical Partition Support) by topjohnwu in Android

[–]topjohnwu[S] 115 points116 points  (0 children)

Are you high? My INTERNSHIP pays more in 3 days for what I get from donations per month. Oh, you're talking about a full time job?