StatsService allows uids to impersonate themselves
Previously, most StatsService calls only allow the caller to specify the
uid if it is Userdebug/EngBuild. This applied even if the caller was
just specifying its own UID; this cl allows such a case. It also allows
ROOT to impersonate SHELL.
Test: Manual testing on userdebug and userbuild devices.
Test: make statsd_test && adb sync data && adb shell data/nativetest64/statsd_test/statsd_test
Change-Id: I2685d13a8dc24d40b5dab8be9842f53be6888ba3
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index 04173b2..51a7316 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -466,23 +466,12 @@
name.assign(args[1].c_str(), args[1].size());
good = true;
} else if (argCount == 3) {
- // If it's a userdebug or eng build, then the shell user can
- // impersonate other uids.
- if (mEngBuild) {
- const char* s = args[1].c_str();
- if (*s != '\0') {
- char* end = NULL;
- uid = strtol(s, &end, 0);
- if (*end == '\0') {
- name.assign(args[2].c_str(), args[2].size());
- good = true;
- }
- }
- } else {
- dprintf(out,
- "The metrics can only be dumped for other UIDs on eng or userdebug "
- "builds.\n");
+ good = getUidFromArgs(args, 1, uid);
+ if (!good) {
+ dprintf(out, "Invalid UID. Note that the metrics can only be dumped for "
+ "other UIDs on eng or userdebug builds.\n");
}
+ name.assign(args[2].c_str(), args[2].size());
}
if (!good) {
print_cmd_help(out);
@@ -518,23 +507,12 @@
name.assign(args[2].c_str(), args[2].size());
good = true;
} else if (argCount == 4) {
- // If it's a userdebug or eng build, then the shell user can
- // impersonate other uids.
- if (mEngBuild) {
- const char* s = args[2].c_str();
- if (*s != '\0') {
- char* end = NULL;
- uid = strtol(s, &end, 0);
- if (*end == '\0') {
- name.assign(args[3].c_str(), args[3].size());
- good = true;
- }
- }
- } else {
- dprintf(err,
- "The config can only be set for other UIDs on eng or userdebug "
- "builds.\n");
+ good = getUidFromArgs(args, 2, uid);
+ if (!good) {
+ dprintf(err, "Invalid UID. Note that the config can only be set for "
+ "other UIDs on eng or userdebug builds.\n");
}
+ name.assign(args[3].c_str(), args[3].size());
} else if (argCount == 2 && args[1] == "remove") {
good = true;
}
@@ -612,23 +590,12 @@
name.assign(args[1].c_str(), args[1].size());
good = true;
} else if (argCount == 3) {
- // If it's a userdebug or eng build, then the shell user can
- // impersonate other uids.
- if (mEngBuild) {
- const char* s = args[1].c_str();
- if (*s != '\0') {
- char* end = NULL;
- uid = strtol(s, &end, 0);
- if (*end == '\0') {
- name.assign(args[2].c_str(), args[2].size());
- good = true;
- }
- }
- } else {
- dprintf(out,
- "The metrics can only be dumped for other UIDs on eng or userdebug "
- "builds.\n");
+ good = getUidFromArgs(args, 1, uid);
+ if (!good) {
+ dprintf(out, "Invalid UID. Note that the metrics can only be dumped for "
+ "other UIDs on eng or userdebug builds.\n");
}
+ name.assign(args[2].c_str(), args[2].size());
}
if (good) {
vector<uint8_t> data;
@@ -714,18 +681,14 @@
state = atoi(args[2].c_str());
good = true;
} else if (argCount == 4) {
- uid = atoi(args[1].c_str());
- // If it's a userdebug or eng build, then the shell user can impersonate other uids.
- // Otherwise, the uid must match the actual caller's uid.
- if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) {
- label = atoi(args[2].c_str());
- state = atoi(args[3].c_str());
- good = true;
- } else {
+ good = getUidFromArgs(args, 1, uid);
+ if (!good) {
dprintf(out,
- "Selecting a UID for writing AppBreadcrumb can only be done for other UIDs "
- "on eng or userdebug builds.\n");
+ "Invalid UID. Note that selecting a UID for writing AppBreadcrumb can only be "
+ "done for other UIDs on eng or userdebug builds.\n");
}
+ label = atoi(args[2].c_str());
+ state = atoi(args[3].c_str());
}
if (good) {
dprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state);
@@ -792,6 +755,28 @@
}
}
+bool StatsService::getUidFromArgs(const Vector<String8>& args, size_t uidArgIndex, int32_t& uid) {
+ const char* s = args[uidArgIndex].c_str();
+ if (*s == '\0') {
+ return false;
+ }
+ char* endc = NULL;
+ int64_t longUid = strtol(s, &endc, 0);
+ if (*endc != '\0') {
+ return false;
+ }
+ int32_t goodUid = static_cast<int32_t>(longUid);
+ if (longUid < 0 || static_cast<uint64_t>(longUid) != static_cast<uid_t>(goodUid)) {
+ return false; // It was not of uid_t type.
+ }
+ uid = goodUid;
+
+ int32_t callingUid = IPCThreadState::self()->getCallingUid();
+ return mEngBuild // UserDebug/EngBuild are allowed to impersonate uids.
+ || (callingUid == goodUid) // Anyone can 'impersonate' themselves.
+ || (callingUid == AID_ROOT && goodUid == AID_SHELL); // ROOT can impersonate SHELL.
+}
+
Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
const vector<String16>& version_string,
const vector<String16>& app,