From 8d387ee1ecbcde6d7435a909476e50afc9bed464 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Tue, 5 Apr 2016 09:24:59 -0700 Subject: [PATCH] fix oem_XXXX ids to incorporate both ranges The XXXX now lines up to the underlying uid and has no offset. Work with AID_OEM_RESERVED uids. Test uses hard coded values to catch changes in the API expectations that may occur in private/android_filesystem_config.h. SideEffects: names change, some product dependencies. Bug: 27999086 Change-Id: Ic2b4c36de74ae009a44e14711c75834293828207 --- libc/bionic/grp_pwd.cpp | 25 ++++++++++++++++--------- tests/grp_pwd_test.cpp | 32 ++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/libc/bionic/grp_pwd.cpp b/libc/bionic/grp_pwd.cpp index 1cabab47f..d75b94dac 100644 --- a/libc/bionic/grp_pwd.cpp +++ b/libc/bionic/grp_pwd.cpp @@ -312,27 +312,34 @@ static void print_app_name_from_gid(const gid_t gid, char* buffer, const int buf } } +// oem_XXXX -> uid +// Supported ranges: +// AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999) +// AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999) +// Check OEM id is within range. +static bool is_oem_id(id_t id) { + return (((id >= AID_OEM_RESERVED_START) && (id <= AID_OEM_RESERVED_END)) || + ((id >= AID_OEM_RESERVED_2_START) && (id <= AID_OEM_RESERVED_2_END))); +} + // Translate an OEM name to the corresponding user/group id. -// oem_XXX -> AID_OEM_RESERVED_2_START + XXX, iff XXX is within range. static id_t oem_id_from_name(const char* name) { unsigned int id; if (sscanf(name, "oem_%u", &id) != 1) { return 0; } - // Check OEM id is within range. - if (id > (AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START)) { + if (!is_oem_id(id)) { return 0; } - return AID_OEM_RESERVED_2_START + static_cast(id); + return static_cast(id); } static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) { - if (uid < AID_OEM_RESERVED_2_START || uid > AID_OEM_RESERVED_2_END) { + if (!is_oem_id(uid)) { return NULL; } - snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", - uid - AID_OEM_RESERVED_2_START); + snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid); snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); @@ -346,12 +353,12 @@ static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) { } static group* oem_id_to_group(gid_t gid, group_state_t* state) { - if (gid < AID_OEM_RESERVED_2_START || gid > AID_OEM_RESERVED_2_END) { + if (!is_oem_id(gid)) { return NULL; } snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), - "oem_%u", gid - AID_OEM_RESERVED_2_START); + "oem_%u", gid); group* gr = &state->group_; gr->gr_name = state->group_name_buffer_; diff --git a/tests/grp_pwd_test.cpp b/tests/grp_pwd_test.cpp index c81ca5803..29cd907d2 100644 --- a/tests/grp_pwd_test.cpp +++ b/tests/grp_pwd_test.cpp @@ -122,12 +122,20 @@ TEST(getpwnam, app_id_radio) { check_get_passwd("radio", 1001, TYPE_SYSTEM); } -TEST(getpwnam, oem_id_0) { - check_get_passwd("oem_0", 5000, TYPE_SYSTEM); +TEST(getpwnam, oem_id_5000) { + check_get_passwd("oem_5000", 5000, TYPE_SYSTEM); } -TEST(getpwnam, oem_id_999) { - check_get_passwd("oem_999", 5999, TYPE_SYSTEM); +TEST(getpwnam, oem_id_5999) { + check_get_passwd("oem_5999", 5999, TYPE_SYSTEM); +} + +TEST(getpwnam, oem_id_2900) { + check_get_passwd("oem_2900", 2900, TYPE_SYSTEM); +} + +TEST(getpwnam, oem_id_2999) { + check_get_passwd("oem_2999", 2999, TYPE_SYSTEM); } TEST(getpwnam, app_id_nobody) { @@ -255,12 +263,20 @@ TEST(getgrnam, app_id_radio) { check_get_group("radio", 1001); } -TEST(getgrnam, oem_id_0) { - check_get_group("oem_0", 5000); +TEST(getgrnam, oem_id_5000) { + check_get_group("oem_5000", 5000); } -TEST(getgrnam, oem_id_999) { - check_get_group("oem_999", 5999); +TEST(getgrnam, oem_id_5999) { + check_get_group("oem_5999", 5999); +} + +TEST(getgrnam, oem_id_2900) { + check_get_group("oem_2900", 2900); +} + +TEST(getgrnam, oem_id_2999) { + check_get_group("oem_2999", 2999); } TEST(getgrnam, app_id_nobody) {