2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
30 #include <sys/types.h>
32 #include <android-base/logging.h>
33 #include <android-base/scopeguard.h>
35 #include <cutils/fs.h>
36 #include <selinux/android.h>
39 #include "android/os/IVold.h"
41 #include <private/android_filesystem_config.h>
43 static void usage(const char* progname) {
44 std::cerr << "Usage: " << progname << " [ prepare | destroy ] <volume_uuid> <user_id> <flags>"
49 static bool small_int(const std::string& s) {
50 return !s.empty() && s.size() < 7 && s.find_first_not_of("0123456789") == std::string::npos;
53 static bool valid_uuid(const std::string& s) {
54 return s.size() < 40 && s.find_first_not_of("0123456789abcdefABCDEF-_") == std::string::npos;
57 static bool prepare_dir_for_user(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid,
58 const std::string& path, uid_t user_id) {
59 auto clearfscreatecon = android::base::make_scope_guard([] { setfscreatecon(nullptr); });
60 auto secontext = std::unique_ptr<char, void (*)(char*)>(nullptr, freecon);
64 if (selabel_lookup(sehandle, &tmp_secontext, path.c_str(), S_IFDIR) == 0) {
65 secontext.reset(tmp_secontext);
67 if (user_id != (uid_t)-1) {
68 if (selinux_android_context_with_level(secontext.get(), &tmp_secontext, user_id,
70 PLOG(ERROR) << "Unable to create context with level for: " << path;
73 secontext.reset(tmp_secontext); // Free the context
78 LOG(DEBUG) << "Setting up mode " << std::oct << mode << std::dec << " uid " << uid << " gid "
79 << gid << " context " << (secontext ? secontext.get() : "null")
80 << " on path: " << path;
82 if (setfscreatecon(secontext.get()) != 0) {
83 PLOG(ERROR) << "Unable to setfscreatecon for: " << path;
87 if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) {
91 char* tmp_oldsecontext = nullptr;
92 if (lgetfilecon(path.c_str(), &tmp_oldsecontext) < 0) {
93 PLOG(ERROR) << "Unable to read secontext for: " << path;
96 auto oldsecontext = std::unique_ptr<char, void (*)(char*)>(tmp_oldsecontext, freecon);
97 if (strcmp(secontext.get(), oldsecontext.get()) != 0) {
98 LOG(INFO) << "Relabelling from " << ((char*)oldsecontext.get()) << " to "
99 << ((char*)secontext.get()) << ": " << path;
100 if (lsetfilecon(path.c_str(), secontext.get()) != 0) {
101 PLOG(ERROR) << "Relabelling failed for: " << path;
109 static bool prepare_dir(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid,
110 const std::string& path) {
111 return prepare_dir_for_user(sehandle, mode, uid, gid, path, (uid_t)-1);
114 static bool rmrf_contents(const std::string& path) {
115 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir);
117 PLOG(ERROR) << "Unable to open directory: " << path;
123 auto const entry = readdir(dirp.get());
126 PLOG(ERROR) << "readdir failed on: " << path;
131 if (entry->d_name[0] == '.') continue;
132 auto subdir = path + "/" + entry->d_name;
134 android::vold::ForkExecvp(std::vector<std::string>{"/system/bin/rm", "-rf", subdir})) {
135 LOG(ERROR) << "rm -rf failed on " << subdir;
141 static bool prepare_apex_subdirs(struct selabel_handle* sehandle, const std::string& path) {
142 if (!prepare_dir(sehandle, 0711, 0, 0, path + "/apexdata")) return false;
144 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/apex"), closedir);
146 PLOG(ERROR) << "Unable to open apex directory";
149 struct dirent* entry;
150 while ((entry = readdir(dirp.get())) != nullptr) {
151 if (entry->d_type != DT_DIR) continue;
153 const char* name = entry->d_name;
154 // skip any starting with "."
155 if (name[0] == '.') continue;
157 if (strchr(name, '@') != NULL) continue;
159 if (!prepare_dir(sehandle, 0771, AID_ROOT, AID_SYSTEM, path + "/apexdata/" + name)) {
166 static bool prepare_subdirs(const std::string& volume_uuid, int user_id, int flags) {
167 struct selabel_handle* sehandle = selinux_android_file_context_handle();
169 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
170 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
171 if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, user_de_path, user_id)) {
175 if (volume_uuid.empty()) {
176 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
177 if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/vold")) return false;
178 if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/storaged")) return false;
179 if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/rollback")) return false;
180 // TODO: Return false if this returns false once sure this should succeed.
181 prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/apexrollback");
182 prepare_apex_subdirs(sehandle, misc_de_path);
184 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
185 if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, profiles_de_path,
190 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
191 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, vendor_de_path + "/fpdata")) {
194 auto facedata_path = vendor_de_path + "/facedata";
195 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) {
200 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
201 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
202 if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, user_ce_path, user_id)) {
206 if (volume_uuid.empty()) {
207 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
208 if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/vold")) return false;
209 if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/storaged")) return false;
210 if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/rollback")) return false;
212 // TODO: Return false if this returns false once sure this should succeed.
213 prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/apexrollback");
214 prepare_apex_subdirs(sehandle, misc_ce_path);
216 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
217 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, system_ce_path + "/backup")) {
220 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM,
221 system_ce_path + "/backup_stage")) {
224 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
225 auto facedata_path = vendor_ce_path + "/facedata";
226 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) {
234 static bool destroy_subdirs(const std::string& volume_uuid, int user_id, int flags) {
236 if (volume_uuid.empty()) {
237 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
238 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
239 res &= rmrf_contents(misc_ce_path);
241 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
242 res &= rmrf_contents(vendor_ce_path);
244 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
245 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
246 res &= rmrf_contents(misc_de_path);
248 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
249 res &= rmrf_contents(vendor_de_path);
255 int main(int argc, const char* const argv[]) {
256 android::base::InitLogging(const_cast<char**>(argv));
257 std::vector<std::string> args(argv + 1, argv + argc);
259 if (args.size() != 4 || !valid_uuid(args[1]) || !small_int(args[2]) || !small_int(args[3])) {
264 auto volume_uuid = args[1];
265 int user_id = stoi(args[2]);
266 int flags = stoi(args[3]);
267 if (args[0] == "prepare") {
268 if (!prepare_subdirs(volume_uuid, user_id, flags)) return -1;
269 } else if (args[0] == "destroy") {
270 if (!destroy_subdirs(volume_uuid, user_id, flags)) return -1;