@@ -2,26 +2,62 @@ package me.yifeiyuan.hf.aidl
2
2
3
3
import android.app.Service
4
4
import android.content.Intent
5
+ import android.content.pm.PackageManager
6
+ import android.os.Binder
5
7
import android.os.IBinder
8
+ import android.os.Process
9
+ import android.text.TextUtils
6
10
import android.util.Log
7
11
import me.yifeiyuan.aidl.server.Account
12
+ import me.yifeiyuan.aidl.server.Callback
8
13
import me.yifeiyuan.aidl.server.IServer
9
14
import me.yifeiyuan.aidl.server.ParcelableTest
15
+ import java.security.Permission
10
16
11
17
// IServer 的服务端实现
12
18
class Server : Service () {
13
19
14
20
private val TAG = " Server"
15
21
22
+ var callback: Callback ? = null
23
+
16
24
private val server: IServer = object : IServer .Stub () {
17
25
18
- override fun connectServer (token : String? ): Boolean {
26
+ override fun connectServer (token : String? , cb : Callback ? ): Boolean {
19
27
Log .d(TAG , " connectServer() called with: token = $token " )
20
28
29
+ val permission = checkCallingPermission(" com.taobao.taobao.storage.WRITE" )
30
+ Log .d(TAG , " connectServer: permission = $permission " )
31
+
32
+ Log .d(
33
+ TAG ,
34
+ " connectServer() called with calling info: ${Binder .getCallingPid()} ,${Binder .getCallingUid()} ,${Binder .getCallingUserHandle()} ,"
35
+ )
36
+
37
+ Log .d(
38
+ TAG ,
39
+ " connectServer() called with my info: ${Process .myPid()} ,${Process .myUid()} ,${Process .myTid()} ,"
40
+ )
41
+
42
+ var callerPackageName: String? = null
43
+ packageManager.getPackagesForUid(Binder .getCallingUid())?.forEach {
44
+ Log .d(TAG , " connectServer() called with pkgs: ${it} ," )
45
+ callerPackageName = it
46
+ }
47
+
48
+ Log .d(TAG , " connectServer() called with getNameForUid: ${packageManager.getNameForUid(Binder .getCallingUid())} ," )
49
+
50
+ if (TextUtils .isEmpty(callerPackageName) || whitePackageNameList.indexOf(callerPackageName) < 0 ) {
51
+ // 非法访问
52
+ Log .d(TAG , " connectServer() called callerPackageName 非法" )
53
+ // return null
54
+ }
55
+
56
+ callback = cb
57
+ callback?.onCallback(" callback data" )
21
58
if (token.equals(" client" )) {
22
59
return true
23
60
}
24
-
25
61
return false
26
62
}
27
63
@@ -69,8 +105,60 @@ class Server : Service() {
69
105
Log .d(TAG , " testThread() called with thread : ${Thread .currentThread().name} " )
70
106
}
71
107
}
108
+ val whitePackageNameList = mutableListOf<String >(" me.yifeiyuan.hf.aidl" ," me.yifeiyuan.hf.clientapp" )
109
+
110
+ override fun onBind (intent : Intent ): IBinder ? {
111
+
112
+ Log .d(
113
+ TAG ,
114
+ " onBind() called with calling info: ${Binder .getCallingPid()} ,${Binder .getCallingUid()} ,${Binder .getCallingUserHandle()} ,"
115
+ )
116
+
117
+ Log .d(
118
+ TAG ,
119
+ " onBind() called with my info: ${Process .myPid()} ,${Process .myUid()} ,${Process .myTid()} ,"
120
+ )
121
+
122
+ if (Binder .getCallingUid() == Process .myUid()) {
123
+ Log .d(TAG , " onBind() called uid 合法" )
124
+ }
125
+
126
+ if (Binder .getCallingPid() == Process .myPid()) {
127
+ Log .d(TAG , " onBind() called pid 合法" )
128
+ }
129
+
130
+ if (packageManager.checkSignatures(Binder .getCallingUid(), Process .myUid()) == PackageManager .SIGNATURE_MATCH ) {
131
+ Log .d(TAG , " onBind() called 签名信息 合法" )
132
+ }
133
+
134
+ var callerPackageName: String? = null
135
+ packageManager.getPackagesForUid(Binder .getCallingUid())?.forEach {
136
+ Log .d(TAG , " onBind() called with pkgs: ${it} ," )
137
+ callerPackageName = it
138
+ }
139
+
140
+ Log .d(TAG , " onBind() called with getNameForUid: ${packageManager.getNameForUid(Binder .getCallingUid())} ," )
141
+
142
+ if (TextUtils .isEmpty(callerPackageName) || whitePackageNameList.indexOf(callerPackageName) < 0 ) {
143
+ // 非法访问
144
+ Log .d(TAG , " onBind() called callerPackageName 非法" )
145
+ // return null
146
+ }
147
+
148
+ if (Binder .getCallingPid() != Process .myPid()) {
149
+ Log .d(TAG , " onBind() called pid 非法" )
150
+ // return null
151
+ }
152
+
153
+ // onbind 的时候不是
154
+ // 权限校验
155
+ val permission = checkCallingOrSelfPermission(" com.taobao.taobao.storage.WRITE" )
156
+ Log .d(TAG , " onBind: permission = $permission " )
157
+ if (permission != PackageManager .PERMISSION_GRANTED ) {
158
+ Log .d(TAG , " onBind() called 权限非法" )
159
+ // return null
160
+ }
72
161
73
- override fun onBind (intent : Intent ): IBinder {
74
162
Log .d(TAG , " onBind() called with: intent = $intent " )
75
163
return server.asBinder()
76
164
}
0 commit comments