Skip to content

Commit

Permalink
Updated SELinux support
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Ferretti committed Dec 22, 2020
1 parent 0e44afe commit 93cb67a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
7 changes: 2 additions & 5 deletions app/src/main/java/me/hexile/odexpatcher/core/SELinux.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ package me.hexile.odexpatcher.core
import android.annotation.SuppressLint

object SELinux {
val isEnabled = isSELinuxEnabled()
val isEnforced = isSELinuxEnforced()

@SuppressLint("PrivateApi")
private fun invoke(methodName: String): Any? {
val c = Class.forName("android.os.SELinux")
val method = c.getMethod(methodName)
return method.invoke(c)
}

fun isSELinuxEnabled(): Boolean {
fun isEnabled(): Boolean {
return invoke("isSELinuxEnabled") as Boolean
}

fun isSELinuxEnforced(): Boolean {
fun isEnforced(): Boolean {
return invoke("isSELinuxEnforced") as Boolean
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,11 @@ class HomeFragment : BaseFragment() {
}

// Fix permissions
// This is a temp fix because Shell.sh runs as root and idk what to do instead
// (https://github.com/topjohnwu/libsu/issues/42)
val appUid = (App.context.packageManager.getApplicationInfo(App.context.packageName, 0).uid % 100000)
Shell.su("chown $appUid:$appUid ${App.context.getFileInFilesDir("*")}").exec()
Shell.su("chmod 600 ${App.context.getFileInFilesDir("*")}").exec()

// TODO: Change context instead of setting SELinux to Permissive
// Disable SELinux
if (SELinux.isEnabled && SELinux.isEnforced) {
Shell.su("setenforce 0").exec()
if (SELinux.isEnabled()) {
Shell.su("chcon u:object_r:app_data_file:s0:c512,c768 ${App.context.getFileInFilesDir("*")}").exec()
}

// Patch files
Expand All @@ -352,10 +347,6 @@ class HomeFragment : BaseFragment() {
targetClasses
)
} catch (e: Exception) {
// TODO: Change context instead of setting SELinux to Permissive
if (SELinux.isEnabled && SELinux.isEnforced) {
Shell.su("setenforce 1").exec()
}
e.printStackTrace()
viewModel.addLog("[E] ERROR: ${e.message}")
viewModel.state.postValue(true)
Expand All @@ -377,12 +368,6 @@ class HomeFragment : BaseFragment() {
viewModel.addLog(" Done!", false)
}

// TODO: Change context instead of setting SELinux to Permissive
// Enable SELinux
if (SELinux.isEnabled && SELinux.isEnforced) {
Shell.su("setenforce 1").exec()
}

// Create folder if non-existent
if (!File(Art.getOatFolder(targetApk)).isDirectory && isSdkGreaterThan(Build.VERSION_CODES.M)) {
// https://cs.android.com/android/platform/superproject/+/master:frameworks/native/cmds/installd/InstalldNativeService.cpp;l=2591
Expand Down

0 comments on commit 93cb67a

Please sign in to comment.