api 23 拒绝本地套接字访问,在 api 21 中工作

local socket access denied in api23, working in api21

我有一个应用程序和一个服务,它们通过本机级别 (C++/JNI) 上的本地套接字连接。两者都是使用 target API 级别 21 构建的,多年来一直运行良好。最近我将它们升级到 API 23 级并且 现在我经常收到这个错误:

10-31 10:07:37.413 5760-5760/com.mycompany.myservice W/pool-151-thread: type=1400 audit(0.0:47942): avc: denied { connectto } for path=00494E554445565F4C4F43414C5F534F434B45545F53455256455200 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:untrusted_app_25:s0:c512,c768 tclass=unix_stream_socket permissive=0
10-31 10:17:44.373 4221-4221/com.mycompany.myapp W/pool-1-thread-1: type=1400 audit(0.0:72703): avc: denied { connectto } for path=00494E554445565F4C4F43414C5F534F434B45545F53455256455200 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:untrusted_app_25:s0:c512,c768 tclass=unix_stream_socket permissive=0

这些行在logcat中不断重复,仅此而已。

我可以从这个输出中看到应用程序和服务都被认为是不受信任的。我知道 API 23 请求 运行 时间权限,服务和应用程序都请求并获得写入、读取和互联网 运行 时间权限。

我的服务清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mycompany.myservice"
      android:versionCode="1"
      android:versionName="2.03.0018.104"
    >

    <uses-feature android:name="android.hardware.usb.host" />
    <uses-sdk android:minSdkVersion="23" />

    <!-- Required for TCP IPC and for local socket --> 
    <uses-permission android:name="android.permission.INTERNET"/>
    <!-- Copy data to SDcard + recording + temporary files -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application android:label="@string/app_name">

        <!-- Attacher hack -->
        <activity
            android:name="mynamespace.myservice$myserviceLauncher"
            android:theme="@android:style/Theme.NoDisplay"
            android:launchMode="singleInstance"
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            </intent-filter>
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" android:resource="@xml/device_filter" />
        </activity>

        <!-- Detacher hack -->
        <receiver android:name="mynamespace.myservice$Detacher">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            </intent-filter>
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" android:resource="@xml/device_filter" />
        </receiver>

        <service android:name="mynamespace.myservice">
        </service>

    </application>
</manifest> 

我的应用清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="1"
    android:versionName="2.01.00.06"
>

<uses-sdk
    android:minSdkVersion="21"
    android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:hardwareAccelerated="true"
    android:allowBackup="true"
    android:largeHeap="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:logo="@drawable/icon"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

有没有办法将我的服务和我的应用程序添加到受信任的应用程序列表

或者我还能如何使本地套接字连接正常工作?

这是由于进一步收紧了平台的 SE 政策,以消除潜在的攻击媒介。来自 Android 6.0 的 AOSP 发布信息:

System Hardening. Hardening of the system via policies enforced by SELinux. This offers better isolation between users, IOCTL filtering, reduce threat of exposed services, further tightening of SELinux domains, and extremely limited /proc access.

不受信任的应用程序的 SE 策略文件(第 3 方应用程序安装的默认设置)明确禁用对 UNIX 域套接字的访问:

https://android.googlesource.com/platform/external/sepolicy/+/android-6.0.1_r81/untrusted_app.te#141

Android的推荐做法是不对IPC使用套接字,对调用等方法使用binder。