Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 07eb4217 authored by Anthony Stange's avatar Anthony Stange
Browse files

Only get target SDK version if not a VNDK client

The target SDK version is used to avoid a permission check for apps
targeting an earlier version of Android. VNDK clients don't provide a
valid op package name which causes the target SDK version check to fail.

This CL removes that target SDK version for those clients (or anyone
that fakes their VNDK op package name) and instead *always* performs the
permission check for them.

Bug: 165055606
Test: Run VNDK client and verify error is no longer printed. Also, try
popular step counter and verify it still can get step data.

Change-Id: I71c0dfa95bd4f753f6819ad46886541605a24213
parent 3f7ecb47
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -28,6 +28,12 @@
#define UNUSED(x) (void)(x)

namespace android {
namespace {

// Used as the default value for the target SDK until it's obtained via getTargetSdkVersion.
constexpr int kTargetSdkUnknown = 0;

}  // namespace

SensorService::SensorEventConnection::SensorEventConnection(
        const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode,
@@ -35,9 +41,9 @@ SensorService::SensorEventConnection::SensorEventConnection(
    : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
      mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(nullptr),
      mCacheSize(0), mMaxCacheSize(0), mTimeOfLastEventDrop(0), mEventsDropped(0),
      mPackageName(packageName), mOpPackageName(opPackageName), mDestroyed(false) {
      mPackageName(packageName), mOpPackageName(opPackageName), mTargetSdk(kTargetSdkUnknown),
      mDestroyed(false) {
    mChannel = new BitTube(mService->mSocketBufferSize);
    mTargetSdk = SensorService::getTargetSdkVersion(opPackageName);
#if DEBUG_CONNECTIONS
    mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
    mTotalAcksNeeded = mTotalAcksReceived = 0;
@@ -445,6 +451,14 @@ bool SensorService::SensorEventConnection::noteOpIfRequired(const sensors_event_
    bool success = true;
    const auto iter = mHandleToAppOp.find(event.sensor);
    if (iter != mHandleToAppOp.end()) {
        if (mTargetSdk == kTargetSdkUnknown) {
            // getTargetSdkVersion returns -1 if it fails so this operation should only be run once
            // per connection and then cached. Perform this here as opposed to in the constructor to
            // avoid log spam for NDK/VNDK clients that don't use sensors guarded with permissions
            // and pass in invalid op package names.
            mTargetSdk = SensorService::getTargetSdkVersion(mOpPackageName);
        }

        // Special handling for step count/detect backwards compatibility: if the app's target SDK
        // is pre-Q, still permit delivering events to the app even if permission isn't granted
        // (since this permission was only introduced in Q)
+9 −0
Original line number Diff line number Diff line
@@ -79,6 +79,8 @@ uint8_t SensorService::sHmacGlobalKey[128] = {};
bool SensorService::sHmacGlobalKeyIsValid = false;
std::map<String16, int> SensorService::sPackageTargetVersion;
Mutex SensorService::sPackageTargetVersionLock;
String16 SensorService::sSensorInterfaceDescriptorPrefix =
        String16("android.frameworks.sensorservice@");
AppOpsManager SensorService::sAppOpsManager;

#define SENSOR_SERVICE_DIR "/data/system/sensor_service"
@@ -1847,6 +1849,13 @@ bool SensorService::hasPermissionForSensor(const Sensor& sensor) {
}

int SensorService::getTargetSdkVersion(const String16& opPackageName) {
    // Don't query the SDK version for the ISensorManager descriptor as it doesn't have one. This
    // descriptor tends to be used for VNDK clients, but can technically be set by anyone so don't
    // give it elevated privileges.
    if (opPackageName.startsWith(sSensorInterfaceDescriptorPrefix)) {
        return -1;
    }

    Mutex::Autolock packageLock(sPackageTargetVersionLock);
    int targetSdkVersion = -1;
    auto entry = sPackageTargetVersion.find(opPackageName);
+1 −0
Original line number Diff line number Diff line
@@ -424,6 +424,7 @@ private:
    static AppOpsManager sAppOpsManager;
    static std::map<String16, int> sPackageTargetVersion;
    static Mutex sPackageTargetVersionLock;
    static String16 sSensorInterfaceDescriptorPrefix;
};

} // namespace android