Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
7cdf2d24
Commit
7cdf2d24
authored
Jan 20, 2022
by
vvb2060
Committed by
John Wu
Jan 23, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup su handler
parent
c59a41a6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
38 additions
and
59 deletions
+38
-59
PolicyDao.kt
...main/java/com/topjohnwu/magisk/core/magiskdb/PolicyDao.kt
+3
-8
SuLog.kt
...src/main/java/com/topjohnwu/magisk/core/model/su/SuLog.kt
+2
-5
SuPolicy.kt
.../main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt
+2
-3
SuCallbackHandler.kt
...in/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt
+24
-23
SuRequestHandler.kt
...ain/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt
+4
-3
XTime.kt
app/src/main/java/com/topjohnwu/magisk/ktx/XTime.kt
+3
-17
No files found.
app/src/main/java/com/topjohnwu/magisk/core/magiskdb/PolicyDao.kt
View file @
7cdf2d24
package
com.topjohnwu.magisk.core.magiskdb
import
android.content.pm.PackageManager
import
com.topjohnwu.magisk.core.Const
import
com.topjohnwu.magisk.core.model.su.SuPolicy
import
com.topjohnwu.magisk.core.model.su.toPolicy
...
...
@@ -54,13 +53,9 @@ class PolicyDao : BaseDao() {
private
fun
Map
<
String
,
String
>.
toPolicyOrNull
():
SuPolicy
?
{
return
runCatching
{
toPolicy
(
AppContext
.
packageManager
)
}.
getOrElse
{
Timber
.
e
(
it
)
if
(
it
is
PackageManager
.
NameNotFoundException
)
{
val
uid
=
getOrElse
(
"uid"
)
{
null
}
?:
return
null
GlobalScope
.
launch
{
delete
(
uid
.
toInt
())
}
}
Timber
.
w
(
it
)
val
uid
=
getOrElse
(
"uid"
)
{
return
null
}
GlobalScope
.
launch
{
delete
(
uid
.
toInt
())
}
null
}
}
...
...
app/src/main/java/com/topjohnwu/magisk/core/model/su/SuLog.kt
View file @
7cdf2d24
package
com.topjohnwu.magisk.core.model.su
import
androidx.room.Entity
import
androidx.room.Ignore
import
androidx.room.PrimaryKey
import
com.topjohnwu.magisk.ktx.timeFormatTime
import
com.topjohnwu.magisk.ktx.toTime
import
com.topjohnwu.magisk.ktx.now
@Entity
(
tableName
=
"logs"
)
data class
SuLog
(
...
...
@@ -15,8 +13,7 @@ data class SuLog(
val
appName
:
String
,
val
command
:
String
,
val
action
:
Boolean
,
val
time
:
Long
=
-
1
val
time
:
Long
=
now
)
{
@PrimaryKey
(
autoGenerate
=
true
)
var
id
:
Int
=
0
@Ignore
val
timeString
=
time
.
toTime
(
timeFormatTime
)
}
app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt
View file @
7cdf2d24
...
...
@@ -9,7 +9,7 @@ import com.topjohnwu.magisk.core.model.su.SuPolicy.Companion.INTERACTIVE
import
com.topjohnwu.magisk.ktx.getLabel
data class
SuPolicy
(
va
r
uid
:
Int
,
va
l
uid
:
Int
,
val
packageName
:
String
,
val
appName
:
String
,
val
icon
:
Drawable
,
...
...
@@ -27,8 +27,7 @@ data class SuPolicy(
fun
toLog
(
toUid
:
Int
,
fromPid
:
Int
,
command
:
String
)
=
SuLog
(
uid
,
toUid
,
fromPid
,
packageName
,
appName
,
command
,
policy
==
ALLOW
,
System
.
currentTimeMillis
()
)
command
,
policy
==
ALLOW
)
fun
toMap
()
=
mapOf
(
"uid"
to
uid
,
...
...
app/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt
View file @
7cdf2d24
...
...
@@ -2,7 +2,6 @@ package com.topjohnwu.magisk.core.su
import
android.content.Context
import
android.os.Bundle
import
android.os.Process
import
android.widget.Toast
import
com.topjohnwu.magisk.BuildConfig
import
com.topjohnwu.magisk.R
...
...
@@ -41,36 +40,37 @@ object SuCallbackHandler {
}
}
private
fun
Any
?.
toInt
():
Int
?
{
return
when
(
this
)
{
is
Number
->
this
.
toInt
()
else
->
null
// https://android.googlesource.com/platform/frameworks/base/+/547bf5487d52b93c9fe183aa6d56459c170b17a4
private
fun
Bundle
.
getIntComp
(
key
:
String
,
defaultValue
:
Int
):
Int
{
val
value
=
get
(
key
)
?:
return
defaultValue
return
when
(
value
)
{
is
Int
->
value
is
Long
->
value
.
toInt
()
else
->
defaultValue
}
}
private
fun
handleLogging
(
context
:
Context
,
data
:
Bundle
)
{
val
fromUid
=
data
[
"from.uid"
].
toInt
()
?:
return
if
(
fromUid
==
Process
.
myUid
()
)
return
val
fromUid
=
data
.
getIntComp
(
"from.uid"
,
-
1
)
val
notify
=
data
.
getBoolean
(
"notify"
,
true
)
val
allow
=
data
.
getIntComp
(
"policy"
,
SuPolicy
.
ALLOW
)
val
pm
=
context
.
packageManager
val
notify
=
data
.
getBoolean
(
"notify"
,
true
)
val
allow
=
data
[
"policy"
].
toInt
()
?:
return
val
policy
=
runCatching
{
fromUid
.
toPolicy
(
pm
,
allow
)
}.
getOrElse
{
GlobalScope
.
launch
{
ServiceLocator
.
policyDB
.
delete
(
fromUid
)
}
fromUid
.
toUidPolicy
(
pm
,
allow
)
}
if
(
notify
)
notify
(
context
,
policy
)
val
toUid
=
data
[
"to.uid"
].
toInt
()
?:
return
val
pid
=
data
[
"pid"
].
toInt
()
?:
return
val
toUid
=
data
.
getIntComp
(
"to.uid"
,
-
1
)
val
pid
=
data
.
getIntComp
(
"pid"
,
-
1
)
val
command
=
data
.
getString
(
"command"
)
?:
return
val
command
=
data
.
getString
(
"command"
,
""
)
val
log
=
policy
.
toLog
(
toUid
=
toUid
,
fromPid
=
pid
,
...
...
@@ -83,22 +83,23 @@ object SuCallbackHandler {
}
private
fun
handleNotify
(
context
:
Context
,
data
:
Bundle
)
{
val
fromUid
=
data
[
"from.uid"
].
toInt
()
?:
return
if
(
fromUid
==
Process
.
myUid
())
return
val
fromUid
=
data
.
getIntComp
(
"from.uid"
,
-
1
)
val
allow
=
data
.
getIntComp
(
"policy"
,
SuPolicy
.
ALLOW
)
val
pm
=
context
.
packageManager
val
allow
=
data
[
"policy"
].
toInt
()
?:
return
runCatching
{
val
policy
=
fromUid
.
toPolicy
(
pm
,
allow
)
if
(
policy
.
policy
>=
0
)
notify
(
context
,
policy
)
val
policy
=
runCatching
{
fromUid
.
toPolicy
(
pm
,
allow
)
}.
getOrElse
{
GlobalScope
.
launch
{
ServiceLocator
.
policyDB
.
delete
(
fromUid
)
}
fromUid
.
toUidPolicy
(
pm
,
allow
)
}
notify
(
context
,
policy
)
}
private
fun
notify
(
context
:
Context
,
policy
:
SuPolicy
)
{
if
(
policy
.
notification
&&
Config
.
suNotification
==
Config
.
Value
.
NOTIFICATION_TOAST
)
{
if
(
Config
.
suNotification
==
Config
.
Value
.
NOTIFICATION_TOAST
)
{
val
resId
=
if
(
policy
.
policy
==
SuPolicy
.
ALLOW
)
R
.
string
.
su_allow_toast
else
...
...
app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt
View file @
7cdf2d24
...
...
@@ -4,11 +4,11 @@ import android.content.Intent
import
android.content.pm.PackageManager
import
com.topjohnwu.magisk.BuildConfig
import
com.topjohnwu.magisk.core.Config
import
com.topjohnwu.magisk.core.Const
import
com.topjohnwu.magisk.core.magiskdb.PolicyDao
import
com.topjohnwu.magisk.core.model.su.SuPolicy
import
com.topjohnwu.magisk.core.model.su.toPolicy
import
com.topjohnwu.magisk.ktx.now
import
com.topjohnwu.superuser.Shell
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.GlobalScope
import
kotlinx.coroutines.launch
...
...
@@ -35,8 +35,10 @@ class SuRequestHandler(
return
false
// Never allow com.topjohnwu.magisk (could be malware)
if
(
policy
.
packageName
==
BuildConfig
.
APPLICATION_ID
)
if
(
policy
.
packageName
==
BuildConfig
.
APPLICATION_ID
)
{
Shell
.
su
(
"(pm uninstall ${BuildConfig.APPLICATION_ID})& >/dev/null 2>&1"
).
exec
()
return
false
}
when
(
Config
.
suAutoResponse
)
{
Config
.
Value
.
SU_AUTO_DENY
->
{
...
...
@@ -87,7 +89,6 @@ class SuRequestHandler(
policy
.
policy
=
action
policy
.
until
=
until
policy
.
uid
=
policy
.
uid
%
100000
+
Const
.
USER_ID
*
100000
GlobalScope
.
launch
(
Dispatchers
.
IO
)
{
try
{
...
...
app/src/main/java/com/topjohnwu/magisk/ktx/XTime.kt
View file @
7cdf2d24
...
...
@@ -2,31 +2,17 @@ package com.topjohnwu.magisk.ktx
import
com.topjohnwu.magisk.core.utils.currentLocale
import
java.text.DateFormat
import
java.text.ParseException
import
java.text.SimpleDateFormat
val
now
get
()
=
System
.
currentTimeMillis
()
fun
Long
.
toTime
(
format
:
DateFormat
)
=
format
.
format
(
this
).
orEmpty
()
fun
String
.
toTime
(
format
:
DateFormat
)
=
try
{
format
.
parse
(
this
)
?.
time
?:
-
1
}
catch
(
e
:
ParseException
)
{
-
1L
}
val
timeFormatFull
by
lazy
{
SimpleDateFormat
(
"yyyy/MM/dd_HH:mm:ss"
,
currentLocale
)
}
val
timeFormatStandard
by
lazy
{
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
,
currentLocale
)
}
val
timeFormatMedium
by
lazy
{
DateFormat
.
getDateInstance
(
DateFormat
.
MEDIUM
,
currentLocale
)
}
val
timeFormatTime
by
lazy
{
SimpleDateFormat
(
"h:mm a"
,
val
timeFormatStandard
by
lazy
{
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss"
,
currentLocale
)
}
val
timeDateFormat
by
lazy
{
val
timeDateFormat
:
DateFormat
by
lazy
{
DateFormat
.
getDateTimeInstance
(
DateFormat
.
DEFAULT
,
DateFormat
.
DEFAULT
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment