From 883748c54d4227a03564a639d1b62c2b1076a802 Mon Sep 17 00:00:00 2001 From: Arthur Ishiguro Date: Wed, 28 Oct 2020 13:18:02 -0700 Subject: Use updated AppOps API in SensorService - Pass the "message" field to the noteOp call. - Removes the noteOp call except when delivering sensor samples. - Restructures code to only call checkOp when necessary. Bug: 160153221 Test: Modify SensorLogger to log on onAsyncNoted() and verify message gets displayed Change-Id: I042afb5304150cf5067fdea4f8926b60e64a46e0 --- services/sensorservice/SensorEventConnection.cpp | 8 ++++++-- services/sensorservice/SensorList.cpp | 6 ++++++ services/sensorservice/SensorList.h | 2 ++ services/sensorservice/SensorService.cpp | 17 ++++++++++------- services/sensorservice/SensorService.h | 1 + 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/services/sensorservice/SensorEventConnection.cpp b/services/sensorservice/SensorEventConnection.cpp index 3cccaf9329..6810c1b781 100644 --- a/services/sensorservice/SensorEventConnection.cpp +++ b/services/sensorservice/SensorEventConnection.cpp @@ -161,7 +161,7 @@ bool SensorService::SensorEventConnection::addSensor(int32_t handle) { Mutex::Autolock _l(mConnectionLock); sp si = mService->getSensorInterfaceFromHandle(handle); if (si == nullptr || - !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) || + !canAccessSensor(si->getSensor(), "Add to SensorEventConnection: ", mOpPackageName) || mSensorInfo.count(handle) > 0) { return false; } @@ -460,8 +460,12 @@ bool SensorService::SensorEventConnection::noteOpIfRequired(const sensors_event_ mTargetSdk > 0 && mTargetSdk <= __ANDROID_API_P__) { success = true; } else { + int32_t sensorHandle = event.sensor; + String16 noteMsg("Sensor event ("); + noteMsg.append(String16(mService->getSensorStringType(sensorHandle))); + noteMsg.append(String16(")")); int32_t appOpMode = mService->sAppOpsManager.noteOp(iter->second, mUid, - mOpPackageName); + mOpPackageName, {}, noteMsg); success = (appOpMode == AppOpsManager::MODE_ALLOWED); } } diff --git a/services/sensorservice/SensorList.cpp b/services/sensorservice/SensorList.cpp index 0ce32cc06f..85ce0f0018 100644 --- a/services/sensorservice/SensorList.cpp +++ b/services/sensorservice/SensorList.cpp @@ -57,6 +57,12 @@ String8 SensorList::getName(int handle) const { mNonSensor.getName()); } +String8 SensorList::getStringType(int handle) const { + return getOne( + handle, [] (const Entry& e) -> String8 {return e.si->getSensor().getStringType();}, + mNonSensor.getStringType()); +} + sp SensorList::getInterface(int handle) const { return getOne>( handle, [] (const Entry& e) -> sp {return e.si;}, nullptr); diff --git a/services/sensorservice/SensorList.h b/services/sensorservice/SensorList.h index 8424b22ed2..617ceefd0e 100644 --- a/services/sensorservice/SensorList.h +++ b/services/sensorservice/SensorList.h @@ -53,6 +53,8 @@ public: const Vector getVirtualSensors() const; String8 getName(int handle) const; + String8 getStringType(int handle) const; + sp getInterface(int handle) const; bool isNewHandle(int handle) const; diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp index 29698390fb..8f25bdba4f 100644 --- a/services/sensorservice/SensorService.cpp +++ b/services/sensorservice/SensorService.cpp @@ -1112,6 +1112,10 @@ String8 SensorService::getSensorName(int handle) const { return mSensors.getName(handle); } +String8 SensorService::getSensorStringType(int handle) const { + return mSensors.getStringType(handle); +} + bool SensorService::isVirtualSensor(int handle) const { sp sensor = getSensorInterfaceFromHandle(handle); return sensor != nullptr && sensor->isVirtual(); @@ -1807,9 +1811,6 @@ bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation, } const int32_t opCode = sensor.getRequiredAppOp(); - const int32_t appOpMode = sAppOpsManager.checkOp(opCode, - IPCThreadState::self()->getCallingUid(), opPackageName); - bool appOpAllowed = appOpMode == AppOpsManager::MODE_ALLOWED; int targetSdkVersion = getTargetSdkVersion(opPackageName); bool canAccess = false; @@ -1822,14 +1823,16 @@ bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation, canAccess = true; } else if (hasPermissionForSensor(sensor)) { // Ensure that the AppOp is allowed, or that there is no necessary app op for the sensor - if (opCode < 0 || appOpAllowed) { + if (opCode >= 0) { + const int32_t appOpMode = sAppOpsManager.checkOp(opCode, + IPCThreadState::self()->getCallingUid(), opPackageName); + canAccess = (appOpMode == AppOpsManager::MODE_ALLOWED); + } else { canAccess = true; } } - if (canAccess) { - sAppOpsManager.noteOp(opCode, IPCThreadState::self()->getCallingUid(), opPackageName); - } else { + if (!canAccess) { ALOGE("%s %s a sensor (%s) without holding %s", String8(opPackageName).string(), operation, sensor.getName().string(), sensor.getRequiredPermission().string()); } diff --git a/services/sensorservice/SensorService.h b/services/sensorservice/SensorService.h index 052cbfe290..50c7c2f383 100644 --- a/services/sensorservice/SensorService.h +++ b/services/sensorservice/SensorService.h @@ -295,6 +295,7 @@ private: virtual status_t dump(int fd, const Vector& args); status_t dumpProtoLocked(int fd, ConnectionSafeAutolock* connLock) const; String8 getSensorName(int handle) const; + String8 getSensorStringType(int handle) const; bool isVirtualSensor(int handle) const; sp getSensorInterfaceFromHandle(int handle) const; bool isWakeUpSensor(int type) const; -- cgit v1.2.3-59-g8ed1b