Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Tool to create a directory with the right SELinux context applied, or |
| 19 | * apply the context if it's absent. Also fixes mode, uid, gid. |
| 20 | */ |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <dirent.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/scopeguard.h> |
| 34 | |
| 35 | #include <cutils/fs.h> |
| 36 | #include <selinux/android.h> |
| 37 | |
| 38 | #include "Utils.h" |
| 39 | #include "android/os/IVold.h" |
| 40 | |
| 41 | static void usage(const char* progname) { |
| 42 | std::cerr << "Usage: " << progname << " [ prepare | destroy ] <volume_uuid> <user_id> <flags>" |
| 43 | << std::endl; |
| 44 | exit(-1); |
| 45 | } |
| 46 | |
| 47 | static bool small_int(const std::string& s) { |
| 48 | return !s.empty() && s.size() < 7 && s.find_first_not_of("0123456789") == std::string::npos; |
| 49 | } |
| 50 | |
| 51 | static bool valid_uuid(const std::string& s) { |
| 52 | return s.size() < 40 && s.find_first_not_of("0123456789abcdefABCDEF-_") == std::string::npos; |
| 53 | } |
| 54 | |
| 55 | static bool prepare_dir(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid, |
| 56 | const std::string& path) { |
| 57 | auto clearfscreatecon = android::base::make_scope_guard([] { setfscreatecon(nullptr); }); |
| 58 | auto secontext = std::unique_ptr<char, void (*)(char*)>(nullptr, freecon); |
| 59 | char* tmp_secontext; |
| 60 | if (sehandle && selabel_lookup(sehandle, &tmp_secontext, path.c_str(), S_IFDIR) == 0) { |
| 61 | secontext.reset(tmp_secontext); |
| 62 | } |
| 63 | LOG(DEBUG) << "Setting up mode " << std::oct << mode << std::dec << " uid " << uid << " gid " |
| 64 | << gid << " context " << secontext.get() << " on path: " << path; |
| 65 | if (secontext) { |
| 66 | if (setfscreatecon(secontext.get()) != 0) { |
| 67 | PLOG(ERROR) << "Unable to read setfscreatecon for: " << path; |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) { |
| 72 | return false; |
| 73 | } |
| 74 | if (secontext) { |
| 75 | char* tmp_oldsecontext = nullptr; |
| 76 | if (lgetfilecon(path.c_str(), &tmp_oldsecontext) < 0) { |
| 77 | PLOG(ERROR) << "Unable to read secontext for: " << path; |
| 78 | return false; |
| 79 | } |
| 80 | auto oldsecontext = std::unique_ptr<char, void (*)(char*)>(tmp_oldsecontext, freecon); |
| 81 | if (strcmp(secontext.get(), oldsecontext.get()) != 0) { |
| 82 | LOG(INFO) << "Relabelling from " << ((char*)oldsecontext.get()) << " to " |
| 83 | << ((char*)secontext.get()) << ": " << path; |
| 84 | if (lsetfilecon(path.c_str(), secontext.get()) != 0) { |
| 85 | PLOG(ERROR) << "Relabelling failed for: " << path; |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | static bool rmrf_contents(const std::string& path) { |
| 94 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir); |
| 95 | if (!dirp) { |
| 96 | PLOG(ERROR) << "Unable to open directory: " << path; |
| 97 | return false; |
| 98 | } |
| 99 | bool res = true; |
| 100 | for (;;) { |
| 101 | errno = 0; |
| 102 | auto const entry = readdir(dirp.get()); |
| 103 | if (!entry) { |
| 104 | if (errno) { |
| 105 | PLOG(ERROR) << "readdir failed on: " << path; |
| 106 | return false; |
| 107 | } |
| 108 | return res; |
| 109 | } |
| 110 | if (entry->d_name[0] == '.') continue; |
| 111 | auto subdir = path + "/" + entry->d_name; |
| 112 | if (0 != |
| 113 | android::vold::ForkExecvp(std::vector<std::string>{"/system/bin/rm", "-rf", subdir})) { |
| 114 | LOG(ERROR) << "rm -rf failed on " << subdir; |
| 115 | res = false; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static bool prepare_subdirs(const std::string& volume_uuid, int user_id, int flags) { |
| 121 | struct selabel_handle* sehandle = selinux_android_file_context_handle(); |
| 122 | |
| 123 | if (volume_uuid.empty()) { |
| 124 | if (flags & android::os::IVold::STORAGE_FLAG_DE) { |
| 125 | auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); |
| 126 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/vold")) return false; |
Jin Qian | f396144 | 2017-10-19 14:44:37 -0700 | [diff] [blame] | 127 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/storaged")) return false; |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 128 | } |
| 129 | if (flags & android::os::IVold::STORAGE_FLAG_CE) { |
| 130 | auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); |
| 131 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/vold")) return false; |
Jin Qian | f396144 | 2017-10-19 14:44:37 -0700 | [diff] [blame] | 132 | if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/storaged")) return false; |
Paul Crowley | 82b41ff | 2017-10-20 08:17:54 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | static bool destroy_subdirs(const std::string& volume_uuid, int user_id, int flags) { |
| 139 | bool res = true; |
| 140 | if (volume_uuid.empty()) { |
| 141 | if (flags & android::os::IVold::STORAGE_FLAG_CE) { |
| 142 | auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); |
| 143 | res &= rmrf_contents(misc_ce_path); |
| 144 | } |
| 145 | if (flags & android::os::IVold::STORAGE_FLAG_DE) { |
| 146 | auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); |
| 147 | res &= rmrf_contents(misc_de_path); |
| 148 | } |
| 149 | } |
| 150 | return res; |
| 151 | } |
| 152 | |
| 153 | int main(int argc, const char* const argv[]) { |
| 154 | android::base::InitLogging(const_cast<char**>(argv)); |
| 155 | std::vector<std::string> args(argv + 1, argv + argc); |
| 156 | |
| 157 | if (args.size() != 4 || !valid_uuid(args[1]) || !small_int(args[2]) || !small_int(args[3])) { |
| 158 | usage(argv[0]); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | auto volume_uuid = args[1]; |
| 163 | int user_id = stoi(args[2]); |
| 164 | int flags = stoi(args[3]); |
| 165 | if (args[0] == "prepare") { |
| 166 | if (!prepare_subdirs(volume_uuid, user_id, flags)) return -1; |
| 167 | } else if (args[0] == "destroy") { |
| 168 | if (!destroy_subdirs(volume_uuid, user_id, flags)) return -1; |
| 169 | } else { |
| 170 | usage(argv[0]); |
| 171 | return -1; |
| 172 | } |
| 173 | return 0; |
| 174 | } |