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

Commit 27c30987 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Fix permission checking in takeUidSnapshotsAsync

Bug: 394714331
Test: CtsOsTestCases:android.os.health.cts.SystemHealthManagerTest
Flag: com.android.server.power.optimization.oneway_battery_stats_service
Change-Id: I2a99ab261071960b0b9b9bdf60ac02024a93efb9
parent 9be8fec9
Loading
Loading
Loading
Loading
+22 −8
Original line number Diff line number Diff line
@@ -473,6 +473,8 @@ public class SystemHealthManager {
            }
        }

        switch (result.resultCode) {
            case IBatteryStats.RESULT_OK: {
                final HealthStats[] results = new HealthStats[uids.length];
                if (result.bundle != null) {
                    HealthStatsParceler[] parcelers = result.bundle.getParcelableArray(
@@ -485,6 +487,18 @@ public class SystemHealthManager {
                }
                return results;
            }
            case IBatteryStats.RESULT_SECURITY_EXCEPTION: {
                throw new SecurityException(result.bundle != null
                        ? result.bundle.getString(IBatteryStats.KEY_EXCEPTION_MESSAGE) : null);
            }
            case IBatteryStats.RESULT_RUNTIME_EXCEPTION: {
                throw new RuntimeException(result.bundle != null
                        ? result.bundle.getString(IBatteryStats.KEY_EXCEPTION_MESSAGE) : null);
            }
            default:
                throw new RuntimeException("Error code: " + result.resultCode);
        }
    }

    /**
     * Asynchronously retrieves a list of supported  {@link PowerMonitor}'s, which include raw ODPM
+12 −0
Original line number Diff line number Diff line
@@ -34,9 +34,21 @@ import android.telephony.ModemActivityInfo;
import android.telephony.SignalStrength;

interface IBatteryStats {
    /** @hide */
    const int RESULT_OK = 0;

    /** @hide */
    const int RESULT_RUNTIME_EXCEPTION = 1;

    /** @hide */
    const int RESULT_SECURITY_EXCEPTION = 2;

    /** @hide */
    const String KEY_UID_SNAPSHOTS = "uid_snapshots";

    /** @hide */
    const String KEY_EXCEPTION_MESSAGE = "exception";

    // These first methods are also called by native code, so must
    // be kept in sync with frameworks/native/libs/binder/include_batterystats/batterystats/IBatteryStats.h
    @EnforcePermission("UPDATE_DEVICE_STATS")
+11 −4
Original line number Diff line number Diff line
@@ -3705,8 +3705,14 @@ public final class BatteryStatsService extends IBatteryStats.Stub
    @Override
    public void takeUidSnapshotsAsync(int[] requestUids, ResultReceiver resultReceiver) {
        if (!onlyCaller(requestUids)) {
            try {
                mContext.enforceCallingOrSelfPermission(
                        android.Manifest.permission.BATTERY_STATS, null);
            } catch (SecurityException ex) {
                resultReceiver.send(IBatteryStats.RESULT_SECURITY_EXCEPTION,
                        Bundle.forPair(IBatteryStats.KEY_EXCEPTION_MESSAGE, ex.getMessage()));
                return;
            }
        }

        if (shouldCollectExternalStats()) {
@@ -3727,13 +3733,14 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                }
                Bundle resultData = new Bundle(1);
                resultData.putParcelableArray(IBatteryStats.KEY_UID_SNAPSHOTS, results);
                resultReceiver.send(0, resultData);
                resultReceiver.send(IBatteryStats.RESULT_OK, resultData);
            } catch (Exception ex) {
                if (DBG) {
                    Slog.d(TAG, "Crashed while returning results for takeUidSnapshots("
                            + Arrays.toString(requestUids) + ") i=" + i, ex);
                }
                throw ex;
                resultReceiver.send(IBatteryStats.RESULT_RUNTIME_EXCEPTION,
                        Bundle.forPair(IBatteryStats.KEY_EXCEPTION_MESSAGE, ex.getMessage()));
            } finally {
                Binder.restoreCallingIdentity(ident);
            }