2017-10-20 17:17:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tool to create a directory with the right SELinux context applied, or
|
|
|
|
* apply the context if it's absent. Also fixes mode, uid, gid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/scopeguard.h>
|
|
|
|
|
|
|
|
#include <cutils/fs.h>
|
|
|
|
#include <selinux/android.h>
|
|
|
|
|
|
|
|
#include "Utils.h"
|
|
|
|
#include "android/os/IVold.h"
|
|
|
|
|
2018-01-22 20:25:29 +01:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
static void usage(const char* progname) {
|
|
|
|
std::cerr << "Usage: " << progname << " [ prepare | destroy ] <volume_uuid> <user_id> <flags>"
|
|
|
|
<< std::endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool small_int(const std::string& s) {
|
|
|
|
return !s.empty() && s.size() < 7 && s.find_first_not_of("0123456789") == std::string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool valid_uuid(const std::string& s) {
|
|
|
|
return s.size() < 40 && s.find_first_not_of("0123456789abcdefABCDEF-_") == std::string::npos;
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:29:38 +01:00
|
|
|
static bool prepare_dir_for_user(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid,
|
|
|
|
const std::string& path, uid_t user_id) {
|
2017-10-20 17:17:54 +02:00
|
|
|
auto clearfscreatecon = android::base::make_scope_guard([] { setfscreatecon(nullptr); });
|
|
|
|
auto secontext = std::unique_ptr<char, void (*)(char*)>(nullptr, freecon);
|
2020-02-07 10:29:38 +01:00
|
|
|
if (sehandle) {
|
|
|
|
char* tmp_secontext;
|
|
|
|
|
|
|
|
if (selabel_lookup(sehandle, &tmp_secontext, path.c_str(), S_IFDIR) == 0) {
|
|
|
|
secontext.reset(tmp_secontext);
|
|
|
|
|
|
|
|
if (user_id != (uid_t)-1) {
|
|
|
|
if (selinux_android_context_with_level(secontext.get(), &tmp_secontext, user_id,
|
|
|
|
(uid_t)-1) != 0) {
|
|
|
|
PLOG(ERROR) << "Unable to create context with level for: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
secontext.reset(tmp_secontext); // Free the context
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 17:17:54 +02:00
|
|
|
}
|
2020-02-07 10:29:38 +01:00
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
LOG(DEBUG) << "Setting up mode " << std::oct << mode << std::dec << " uid " << uid << " gid "
|
2018-07-26 22:41:14 +02:00
|
|
|
<< gid << " context " << (secontext ? secontext.get() : "null")
|
|
|
|
<< " on path: " << path;
|
2017-10-20 17:17:54 +02:00
|
|
|
if (secontext) {
|
|
|
|
if (setfscreatecon(secontext.get()) != 0) {
|
2020-02-07 10:29:38 +01:00
|
|
|
PLOG(ERROR) << "Unable to setfscreatecon for: " << path;
|
2017-10-20 17:17:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (secontext) {
|
|
|
|
char* tmp_oldsecontext = nullptr;
|
|
|
|
if (lgetfilecon(path.c_str(), &tmp_oldsecontext) < 0) {
|
|
|
|
PLOG(ERROR) << "Unable to read secontext for: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto oldsecontext = std::unique_ptr<char, void (*)(char*)>(tmp_oldsecontext, freecon);
|
|
|
|
if (strcmp(secontext.get(), oldsecontext.get()) != 0) {
|
|
|
|
LOG(INFO) << "Relabelling from " << ((char*)oldsecontext.get()) << " to "
|
|
|
|
<< ((char*)secontext.get()) << ": " << path;
|
|
|
|
if (lsetfilecon(path.c_str(), secontext.get()) != 0) {
|
|
|
|
PLOG(ERROR) << "Relabelling failed for: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:29:38 +01:00
|
|
|
static bool prepare_dir(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid,
|
|
|
|
const std::string& path) {
|
|
|
|
return prepare_dir_for_user(sehandle, mode, uid, gid, path, (uid_t)-1);
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
static bool rmrf_contents(const std::string& path) {
|
|
|
|
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir);
|
|
|
|
if (!dirp) {
|
2023-02-24 18:26:10 +01:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-20 17:17:54 +02:00
|
|
|
PLOG(ERROR) << "Unable to open directory: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool res = true;
|
|
|
|
for (;;) {
|
|
|
|
errno = 0;
|
|
|
|
auto const entry = readdir(dirp.get());
|
|
|
|
if (!entry) {
|
|
|
|
if (errno) {
|
|
|
|
PLOG(ERROR) << "readdir failed on: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (entry->d_name[0] == '.') continue;
|
|
|
|
auto subdir = path + "/" + entry->d_name;
|
|
|
|
if (0 !=
|
|
|
|
android::vold::ForkExecvp(std::vector<std::string>{"/system/bin/rm", "-rf", subdir})) {
|
|
|
|
LOG(ERROR) << "rm -rf failed on " << subdir;
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 19:09:34 +01:00
|
|
|
static bool prepare_apex_subdirs(struct selabel_handle* sehandle, const std::string& path) {
|
2020-01-17 12:01:38 +01:00
|
|
|
if (!prepare_dir(sehandle, 0711, 0, 0, path + "/apexdata")) return false;
|
2019-11-19 19:09:34 +01:00
|
|
|
|
|
|
|
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/apex"), closedir);
|
|
|
|
if (!dirp) {
|
|
|
|
PLOG(ERROR) << "Unable to open apex directory";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct dirent* entry;
|
|
|
|
while ((entry = readdir(dirp.get())) != nullptr) {
|
|
|
|
if (entry->d_type != DT_DIR) continue;
|
|
|
|
|
|
|
|
const char* name = entry->d_name;
|
|
|
|
// skip any starting with "."
|
|
|
|
if (name[0] == '.') continue;
|
|
|
|
|
|
|
|
if (strchr(name, '@') != NULL) continue;
|
|
|
|
|
2020-01-17 12:01:38 +01:00
|
|
|
if (!prepare_dir(sehandle, 0771, AID_ROOT, AID_SYSTEM, path + "/apexdata/" + name)) {
|
2019-11-19 19:09:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
static bool prepare_subdirs(const std::string& volume_uuid, int user_id, int flags) {
|
|
|
|
struct selabel_handle* sehandle = selinux_android_file_context_handle();
|
|
|
|
|
2020-02-07 10:29:38 +01:00
|
|
|
if (flags & android::os::IVold::STORAGE_FLAG_DE) {
|
|
|
|
auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
|
2020-12-22 15:49:18 +01:00
|
|
|
if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, user_de_path, user_id)) {
|
2020-02-07 10:29:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-07 21:27:06 +01:00
|
|
|
auto misc_de_path = android::vold::BuildDataMiscDePath(volume_uuid, user_id);
|
2022-02-11 17:17:49 +01:00
|
|
|
if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM,
|
2022-02-21 20:03:26 +01:00
|
|
|
misc_de_path + "/sdksandbox", user_id)) {
|
2022-02-11 17:17:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:29:38 +01:00
|
|
|
if (volume_uuid.empty()) {
|
2017-10-20 17:17:54 +02:00
|
|
|
if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/vold")) return false;
|
2017-10-19 23:44:37 +02:00
|
|
|
if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/storaged")) return false;
|
2019-01-14 11:03:07 +01:00
|
|
|
if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/rollback")) return false;
|
2019-11-19 19:09:34 +01:00
|
|
|
// TODO: Return false if this returns false once sure this should succeed.
|
2019-12-02 19:27:24 +01:00
|
|
|
prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/apexrollback");
|
2019-11-19 19:09:34 +01:00
|
|
|
prepare_apex_subdirs(sehandle, misc_de_path);
|
2018-01-22 20:25:29 +01:00
|
|
|
|
2020-02-07 10:29:38 +01:00
|
|
|
auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
|
|
|
|
if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, profiles_de_path,
|
2020-12-22 15:49:18 +01:00
|
|
|
user_id)) {
|
2020-02-07 10:29:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:25:29 +01:00
|
|
|
auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
|
|
|
|
if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, vendor_de_path + "/fpdata")) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-20 20:09:15 +01:00
|
|
|
auto facedata_path = vendor_de_path + "/facedata";
|
|
|
|
if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-20 17:17:54 +02:00
|
|
|
}
|
2020-02-07 10:29:38 +01:00
|
|
|
}
|
|
|
|
if (flags & android::os::IVold::STORAGE_FLAG_CE) {
|
|
|
|
auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
|
2020-12-22 15:49:18 +01:00
|
|
|
if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, user_ce_path, user_id)) {
|
2020-02-07 10:29:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-07 21:27:06 +01:00
|
|
|
auto misc_ce_path = android::vold::BuildDataMiscCePath(volume_uuid, user_id);
|
2022-02-11 17:17:49 +01:00
|
|
|
if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM,
|
2022-02-21 20:03:26 +01:00
|
|
|
misc_ce_path + "/sdksandbox", user_id)) {
|
2022-02-11 17:17:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:29:38 +01:00
|
|
|
if (volume_uuid.empty()) {
|
2017-10-20 17:17:54 +02:00
|
|
|
if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/vold")) return false;
|
2017-10-19 23:44:37 +02:00
|
|
|
if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/storaged")) return false;
|
2019-01-14 11:03:07 +01:00
|
|
|
if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/rollback")) return false;
|
2019-11-19 19:09:34 +01:00
|
|
|
// TODO: Return false if this returns false once sure this should succeed.
|
2019-12-02 19:27:24 +01:00
|
|
|
prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/apexrollback");
|
2019-11-19 19:09:34 +01:00
|
|
|
prepare_apex_subdirs(sehandle, misc_ce_path);
|
2021-10-16 05:32:42 +02:00
|
|
|
// Give gmscore (who runs in cache group) access to the checkin directory. Also provide
|
|
|
|
// the user id to set the correct selinux mls_level.
|
|
|
|
if (!prepare_dir_for_user(sehandle, 0770, AID_SYSTEM, AID_CACHE,
|
|
|
|
misc_ce_path + "/checkin", user_id)) {
|
2021-10-22 00:16:49 +02:00
|
|
|
// TODO(b/203742483) the checkin directory was created with the wrong permission &
|
|
|
|
// context. Delete the directory to get these devices out of the bad state. Revert
|
|
|
|
// the change once the droidfood population is on newer build.
|
|
|
|
LOG(INFO) << "Failed to prepare the checkin directory, deleting for recreation";
|
|
|
|
android::vold::DeleteDirContentsAndDir(misc_ce_path + "/checkin");
|
|
|
|
if (!prepare_dir_for_user(sehandle, 0770, AID_SYSTEM, AID_CACHE,
|
|
|
|
misc_ce_path + "/checkin", user_id)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-16 05:32:42 +02:00
|
|
|
}
|
2019-01-16 22:32:27 +01:00
|
|
|
|
|
|
|
auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
|
|
|
|
if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, system_ce_path + "/backup")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM,
|
|
|
|
system_ce_path + "/backup_stage")) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-24 02:04:35 +02:00
|
|
|
auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
|
|
|
|
auto facedata_path = vendor_ce_path + "/facedata";
|
|
|
|
if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-20 17:17:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool destroy_subdirs(const std::string& volume_uuid, int user_id, int flags) {
|
|
|
|
bool res = true;
|
2022-03-07 21:27:06 +01:00
|
|
|
if (flags & android::os::IVold::STORAGE_FLAG_CE) {
|
|
|
|
auto misc_ce_path = android::vold::BuildDataMiscCePath(volume_uuid, user_id);
|
|
|
|
res &= rmrf_contents(misc_ce_path);
|
2018-01-22 20:25:29 +01:00
|
|
|
|
2022-03-07 21:27:06 +01:00
|
|
|
if (volume_uuid.empty()) {
|
2018-01-22 20:25:29 +01:00
|
|
|
auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
|
|
|
|
res &= rmrf_contents(vendor_ce_path);
|
2017-10-20 17:17:54 +02:00
|
|
|
}
|
2022-03-07 21:27:06 +01:00
|
|
|
}
|
|
|
|
if (flags & android::os::IVold::STORAGE_FLAG_DE) {
|
|
|
|
auto misc_de_path = android::vold::BuildDataMiscDePath(volume_uuid, user_id);
|
|
|
|
res &= rmrf_contents(misc_de_path);
|
2018-01-22 20:25:29 +01:00
|
|
|
|
2022-03-07 21:27:06 +01:00
|
|
|
if (volume_uuid.empty()) {
|
2018-01-22 20:25:29 +01:00
|
|
|
auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
|
|
|
|
res &= rmrf_contents(vendor_de_path);
|
2017-10-20 17:17:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* const argv[]) {
|
|
|
|
android::base::InitLogging(const_cast<char**>(argv));
|
|
|
|
std::vector<std::string> args(argv + 1, argv + argc);
|
|
|
|
|
|
|
|
if (args.size() != 4 || !valid_uuid(args[1]) || !small_int(args[2]) || !small_int(args[3])) {
|
|
|
|
usage(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto volume_uuid = args[1];
|
|
|
|
int user_id = stoi(args[2]);
|
|
|
|
int flags = stoi(args[3]);
|
|
|
|
if (args[0] == "prepare") {
|
|
|
|
if (!prepare_subdirs(volume_uuid, user_id, flags)) return -1;
|
|
|
|
} else if (args[0] == "destroy") {
|
|
|
|
if (!destroy_subdirs(volume_uuid, user_id, flags)) return -1;
|
|
|
|
} else {
|
|
|
|
usage(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|