Skip to content

Commit 8ba00aa

Browse files
committed
Update
1 parent d93d28b commit 8ba00aa

File tree

8 files changed

+283
-21
lines changed

8 files changed

+283
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Callback.aidl
2+
package me.yifeiyuan.aidl.server;
3+
4+
// Declare any non-default types here with import statements
5+
6+
interface Callback {
7+
8+
void onCallback(String data);
9+
10+
}

aidlmodule/src/main/aidl/me/yifeiyuan/aidl/server/IServer.aidl

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package me.yifeiyuan.aidl.server;
22

33
import me.yifeiyuan.aidl.server.Account;
44
import me.yifeiyuan.aidl.server.ParcelableTest;
5-
5+
import me.yifeiyuan.aidl.server.Callback;
66

77
//对应的实现是 Server
88
interface IServer {
99

10-
boolean connectServer(String token);
10+
boolean connectServer(String token,Callback cb);
1111

1212
Account getAccountByName(String name);
1313

app/src/main/AndroidManifest.xml

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="me.yifeiyuan.hf.aidl">
44

5+
<permission
6+
android:name="com.taobao.taobao.storage.WRITE"
7+
android:protectionLevel="normal"
8+
/>
9+
<uses-permission android:name="com.taobao.taobao.storage.WRITE" />
10+
11+
<!-- <permission-->
12+
<!-- android:name="com.taobao.taobao.ACCESS_STORAGE"-->
13+
<!-- android:protectionLevel="normal" />-->
14+
15+
<!-- <uses-permission android:name="com.taobao.taobao.ACCESS_STORAGE" />-->
16+
517
<application
618
android:icon="@mipmap/ic_launcher"
719
android:label="@string/app_name"
@@ -14,15 +26,19 @@
1426
<service
1527
android:name=".MyService"
1628
android:enabled="true"
17-
android:exported="true">
29+
android:exported="true"
30+
>
1831
<intent-filter>
1932
<action android:name="me.yifeiyuan.hf.aidl.MyService" />
2033
</intent-filter>
2134
</service>
2235
<service
2336
android:name=".Server"
2437
android:enabled="true"
25-
android:exported="true">
38+
android:exported="true"
39+
android:process=":server"
40+
>
41+
<!-- android:permission="com.taobao.taobao.storage.WRITE"-->
2642
<intent-filter>
2743
<action android:name="me.yifeiyuan.hf.aidl.Server.Action" />
2844
</intent-filter>

app/src/main/java/me/yifeiyuan/hf/aidl/MainActivity.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ class MainActivity : AppCompatActivity() {
4949
}
5050

5151
fun bindService(view: View) {
52-
bindService(Intent(this, RemoteService::class.java), connection, BIND_AUTO_CREATE)
52+
val result =
53+
bindService(Intent(this, RemoteService::class.java), connection, BIND_AUTO_CREATE)
54+
55+
Log.d(TAG, "bindService: $result")
5356
}
5457

5558
fun testMessenger(view: View) {

app/src/main/java/me/yifeiyuan/hf/aidl/Server.kt

+91-3
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,62 @@ package me.yifeiyuan.hf.aidl
22

33
import android.app.Service
44
import android.content.Intent
5+
import android.content.pm.PackageManager
6+
import android.os.Binder
57
import android.os.IBinder
8+
import android.os.Process
9+
import android.text.TextUtils
610
import android.util.Log
711
import me.yifeiyuan.aidl.server.Account
12+
import me.yifeiyuan.aidl.server.Callback
813
import me.yifeiyuan.aidl.server.IServer
914
import me.yifeiyuan.aidl.server.ParcelableTest
15+
import java.security.Permission
1016

1117
//IServer 的服务端实现
1218
class Server : Service() {
1319

1420
private val TAG = "Server"
1521

22+
var callback: Callback? = null
23+
1624
private val server: IServer = object : IServer.Stub() {
1725

18-
override fun connectServer(token: String?): Boolean {
26+
override fun connectServer(token: String?, cb: Callback?): Boolean {
1927
Log.d(TAG, "connectServer() called with: token = $token")
2028

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")
2158
if (token.equals("client")) {
2259
return true
2360
}
24-
2561
return false
2662
}
2763

@@ -69,8 +105,60 @@ class Server : Service() {
69105
Log.d(TAG, "testThread() called with thread : ${Thread.currentThread().name}")
70106
}
71107
}
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+
}
72161

73-
override fun onBind(intent: Intent): IBinder {
74162
Log.d(TAG, "onBind() called with: intent = $intent")
75163
return server.asBinder()
76164
}

clientapp/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
<!-- <uses-permission android:name="me.yifeiyuan.hf.aidl.Server" />-->
66

7+
<uses-permission android:name="com.taobao.taobao.storage.WRITE" />
8+
9+
<!-- <uses-permission android:name="com.taobao.taobao.ACCESS_STORAGE" />-->
10+
711
<application
812
android:allowBackup="true"
913
android:icon="@mipmap/ic_launcher"

0 commit comments

Comments
 (0)