Enable clang-tidy for sensitive domain.
Since installd has broad access to lots of sensitive data, enable
as many security-related tidy checks as possible to help avoid bugs.
This change provides a default implementation of create_cache_path(),
calculate_odex_file_path(), and calculate_oat_file_path(), along
with tests to verify behavior against old code.
Replace "dir_rec_t" with std::string, since that's really what it's
been all along. Increase paranoia of path checking to reject any
paths containing "..", regardless of where it occurs in path string.
Stricter checking of instruction set values.
Remove now-unused char* manipulation utility methods; people should
be using std::string instead.
(cherry picked from commit 1b9d9a6006f4159e2cc2c41330f316b1fdc53fe1)
Test: adb shell /data/nativetest/installd_cache_test/installd_cache_test
Test: adb shell /data/nativetest/installd_service_test/installd_service_test
Test: adb shell /data/nativetest/installd_utils_test/installd_utils_test
Bug: 36655947
Merged-In: Ib706f0b8c1878be64710c00f56dccdfbe215570f
Change-Id: Ib706f0b8c1878be64710c00f56dccdfbe215570f
diff --git a/cmds/installd/globals.cpp b/cmds/installd/globals.cpp
index edcdb6a..b3a6daf 100644
--- a/cmds/installd/globals.cpp
+++ b/cmds/installd/globals.cpp
@@ -16,15 +16,15 @@
#define LOG_TAG "installd"
-#include <stdlib.h>
-#include <string.h>
-
-#include <log/log.h> // TODO: Move everything to base::logging.
-
#include <globals.h>
#include <installd_constants.h>
#include <utils.h>
+#include <android-base/logging.h>
+
+#include <stdlib.h>
+#include <string.h>
+
namespace android {
namespace installd {
@@ -44,106 +44,78 @@
static constexpr const char* PRIVATE_APP_SUBDIR = "app-private/"; // sub-directory under
// ANDROID_DATA
-/* Directory records that are used in execution of commands. */
-dir_rec_t android_app_dir;
-dir_rec_t android_app_ephemeral_dir;
-dir_rec_t android_app_lib_dir;
-dir_rec_t android_app_private_dir;
-dir_rec_t android_asec_dir;
-dir_rec_t android_data_dir;
-dir_rec_t android_media_dir;
-dir_rec_t android_mnt_expand_dir;
-dir_rec_t android_profiles_dir;
+std::string android_app_dir;
+std::string android_app_ephemeral_dir;
+std::string android_app_lib_dir;
+std::string android_app_private_dir;
+std::string android_asec_dir;
+std::string android_data_dir;
+std::string android_media_dir;
+std::string android_mnt_expand_dir;
+std::string android_profiles_dir;
+std::string android_root_dir;
-dir_rec_array_t android_system_dirs;
+std::vector<std::string> android_system_dirs;
-/**
- * Initialize all the global variables that are used elsewhere. Returns 0 upon
- * success and -1 on error.
- */
-void free_globals() {
- size_t i;
-
- for (i = 0; i < android_system_dirs.count; i++) {
- if (android_system_dirs.dirs[i].path != NULL) {
- free(android_system_dirs.dirs[i].path);
- }
+bool init_globals_from_data_and_root() {
+ const char* data_path = getenv("ANDROID_DATA");
+ if (data_path == nullptr) {
+ LOG(ERROR) << "Could not find ANDROID_DATA";
+ return false;
}
+ const char* root_path = getenv("ANDROID_ROOT");
+ if (root_path == nullptr) {
+ LOG(ERROR) << "Could not find ANDROID_ROOT";
+ return false;
+ }
+ return init_globals_from_data_and_root(data_path, root_path);
+}
- free(android_system_dirs.dirs);
+static std::string ensure_trailing_slash(const std::string& path) {
+ if (path.rfind('/') != path.size() - 1) {
+ return path + '/';
+ } else {
+ return path;
+ }
}
bool init_globals_from_data_and_root(const char* data, const char* root) {
// Get the android data directory.
- if (get_path_from_string(&android_data_dir, data) < 0) {
- return false;
- }
+ android_data_dir = ensure_trailing_slash(data);
+
+ // Get the android root directory.
+ android_root_dir = ensure_trailing_slash(root);
// Get the android app directory.
- if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
- return false;
- }
+ android_app_dir = android_data_dir + APP_SUBDIR;
// Get the android protected app directory.
- if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
- return false;
- }
+ android_app_private_dir = android_data_dir + PRIVATE_APP_SUBDIR;
// Get the android ephemeral app directory.
- if (copy_and_append(&android_app_ephemeral_dir, &android_data_dir, EPHEMERAL_APP_SUBDIR) < 0) {
- return false;
- }
+ android_app_ephemeral_dir = android_data_dir + EPHEMERAL_APP_SUBDIR;
// Get the android app native library directory.
- if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
- return false;
- }
+ android_app_lib_dir = android_data_dir + APP_LIB_SUBDIR;
// Get the sd-card ASEC mount point.
- if (get_path_from_env(&android_asec_dir, ASEC_MOUNTPOINT_ENV_NAME) < 0) {
- return false;
- }
+ android_asec_dir = ensure_trailing_slash(getenv(ASEC_MOUNTPOINT_ENV_NAME));
// Get the android media directory.
- if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
- return false;
- }
+ android_media_dir = android_data_dir + MEDIA_SUBDIR;
// Get the android external app directory.
- if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
- return false;
- }
+ android_mnt_expand_dir = "/mnt/expand/";
// Get the android profiles directory.
- if (copy_and_append(&android_profiles_dir, &android_data_dir, PROFILES_SUBDIR) < 0) {
- return false;
- }
+ android_profiles_dir = android_data_dir + PROFILES_SUBDIR;
// Take note of the system and vendor directories.
- android_system_dirs.count = 4;
-
- android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
- if (android_system_dirs.dirs == NULL) {
- ALOGE("Couldn't allocate array for dirs; aborting\n");
- return false;
- }
-
- dir_rec_t android_root_dir;
- if (get_path_from_string(&android_root_dir, root) < 0) {
- return false;
- }
-
- android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
- android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
-
- android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
- android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
-
- android_system_dirs.dirs[2].path = strdup("/vendor/app/");
- android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
-
- android_system_dirs.dirs[3].path = strdup("/oem/app/");
- android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
+ android_system_dirs.clear();
+ android_system_dirs.push_back(android_root_dir + APP_SUBDIR);
+ android_system_dirs.push_back(android_root_dir + PRIV_APP_SUBDIR);
+ android_system_dirs.push_back("/vendor/app/");
+ android_system_dirs.push_back("/oem/app/");
return true;
}