blob: f4d4309e2b4db648c868f4a8149bf2164db62195 [file] [log] [blame]
Paul Crowley82b41ff2017-10-20 08:17:54 -07001/*
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
Andreas Huber71cd43f2018-01-22 11:25:29 -080041#include <private/android_filesystem_config.h>
42
Paul Crowley82b41ff2017-10-20 08:17:54 -070043static void usage(const char* progname) {
44 std::cerr << "Usage: " << progname << " [ prepare | destroy ] <volume_uuid> <user_id> <flags>"
45 << std::endl;
46 exit(-1);
47}
48
49static bool small_int(const std::string& s) {
50 return !s.empty() && s.size() < 7 && s.find_first_not_of("0123456789") == std::string::npos;
51}
52
53static bool valid_uuid(const std::string& s) {
54 return s.size() < 40 && s.find_first_not_of("0123456789abcdefABCDEF-_") == std::string::npos;
55}
56
Alan Stokesbe3db7b2020-02-07 09:29:38 +000057static 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) {
Paul Crowley82b41ff2017-10-20 08:17:54 -070059 auto clearfscreatecon = android::base::make_scope_guard([] { setfscreatecon(nullptr); });
60 auto secontext = std::unique_ptr<char, void (*)(char*)>(nullptr, freecon);
Eric Biggers2ef4e852023-02-24 18:09:25 +000061 char* tmp_secontext;
Alan Stokesbe3db7b2020-02-07 09:29:38 +000062
Eric Biggers2ef4e852023-02-24 18:09:25 +000063 if (selabel_lookup(sehandle, &tmp_secontext, path.c_str(), S_IFDIR) != 0) {
64 PLOG(ERROR) << "Failed to look up selabel for directory " << path;
65 return false;
66 }
67 secontext.reset(tmp_secontext);
Alan Stokesbe3db7b2020-02-07 09:29:38 +000068
Eric Biggers2ef4e852023-02-24 18:09:25 +000069 if (user_id != (uid_t)-1) {
70 if (selinux_android_context_with_level(secontext.get(), &tmp_secontext, user_id,
71 (uid_t)-1) != 0) {
72 PLOG(ERROR) << "Unable to create context with level for: " << path;
73 return false;
Alan Stokesbe3db7b2020-02-07 09:29:38 +000074 }
Eric Biggers2ef4e852023-02-24 18:09:25 +000075 secontext.reset(tmp_secontext);
Paul Crowley82b41ff2017-10-20 08:17:54 -070076 }
Alan Stokesbe3db7b2020-02-07 09:29:38 +000077
Paul Crowley82b41ff2017-10-20 08:17:54 -070078 LOG(DEBUG) << "Setting up mode " << std::oct << mode << std::dec << " uid " << uid << " gid "
Eric Biggers2ef4e852023-02-24 18:09:25 +000079 << gid << " context " << secontext.get() << " on path: " << path;
80 if (setfscreatecon(secontext.get()) != 0) {
81 LOG(ERROR) << "Failed to setfscreatecon for directory " << path;
82 return false;
Paul Crowley82b41ff2017-10-20 08:17:54 -070083 }
84 if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) {
85 return false;
86 }
Eric Biggers2ef4e852023-02-24 18:09:25 +000087 char* tmp_oldsecontext = nullptr;
88 if (lgetfilecon(path.c_str(), &tmp_oldsecontext) < 0) {
89 PLOG(ERROR) << "Unable to read secontext for: " << path;
90 return false;
91 }
92 auto oldsecontext = std::unique_ptr<char, void (*)(char*)>(tmp_oldsecontext, freecon);
93 if (strcmp(secontext.get(), oldsecontext.get()) != 0) {
94 LOG(INFO) << "Relabelling from " << ((char*)oldsecontext.get()) << " to "
95 << ((char*)secontext.get()) << ": " << path;
96 if (lsetfilecon(path.c_str(), secontext.get()) != 0) {
97 PLOG(ERROR) << "Relabelling failed for: " << path;
Paul Crowley82b41ff2017-10-20 08:17:54 -070098 return false;
99 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700100 }
101 return true;
102}
103
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000104static bool prepare_dir(struct selabel_handle* sehandle, mode_t mode, uid_t uid, gid_t gid,
105 const std::string& path) {
106 return prepare_dir_for_user(sehandle, mode, uid, gid, path, (uid_t)-1);
107}
108
Paul Crowley82b41ff2017-10-20 08:17:54 -0700109static bool rmrf_contents(const std::string& path) {
110 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir);
111 if (!dirp) {
Eric Biggers629c6342023-02-24 17:26:10 +0000112 if (errno == ENOENT) {
113 return true;
114 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700115 PLOG(ERROR) << "Unable to open directory: " << path;
116 return false;
117 }
118 bool res = true;
119 for (;;) {
120 errno = 0;
121 auto const entry = readdir(dirp.get());
122 if (!entry) {
123 if (errno) {
124 PLOG(ERROR) << "readdir failed on: " << path;
125 return false;
126 }
127 return res;
128 }
129 if (entry->d_name[0] == '.') continue;
130 auto subdir = path + "/" + entry->d_name;
131 if (0 !=
132 android::vold::ForkExecvp(std::vector<std::string>{"/system/bin/rm", "-rf", subdir})) {
133 LOG(ERROR) << "rm -rf failed on " << subdir;
134 res = false;
135 }
136 }
137}
138
Oli Lan94457212019-11-19 18:09:34 +0000139static bool prepare_apex_subdirs(struct selabel_handle* sehandle, const std::string& path) {
Oli Lane1b3f5c2020-01-17 11:01:38 +0000140 if (!prepare_dir(sehandle, 0711, 0, 0, path + "/apexdata")) return false;
Oli Lan94457212019-11-19 18:09:34 +0000141
142 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/apex"), closedir);
143 if (!dirp) {
144 PLOG(ERROR) << "Unable to open apex directory";
145 return false;
146 }
147 struct dirent* entry;
148 while ((entry = readdir(dirp.get())) != nullptr) {
149 if (entry->d_type != DT_DIR) continue;
150
151 const char* name = entry->d_name;
152 // skip any starting with "."
153 if (name[0] == '.') continue;
154
155 if (strchr(name, '@') != NULL) continue;
156
Oli Lane1b3f5c2020-01-17 11:01:38 +0000157 if (!prepare_dir(sehandle, 0771, AID_ROOT, AID_SYSTEM, path + "/apexdata/" + name)) {
Oli Lan94457212019-11-19 18:09:34 +0000158 return false;
159 }
160 }
161 return true;
162}
163
Paul Crowley82b41ff2017-10-20 08:17:54 -0700164static bool prepare_subdirs(const std::string& volume_uuid, int user_id, int flags) {
165 struct selabel_handle* sehandle = selinux_android_file_context_handle();
Eric Biggers2ef4e852023-02-24 18:09:25 +0000166 if (!sehandle) {
167 LOG(ERROR) << "Failed to get SELinux file contexts handle";
168 return false;
169 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700170
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000171 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
172 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
Alan Stokese0b7f302020-12-22 14:49:18 +0000173 if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, user_de_path, user_id)) {
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000174 return false;
175 }
176
Mohammad Samiul Islamb4595912022-03-07 20:27:06 +0000177 auto misc_de_path = android::vold::BuildDataMiscDePath(volume_uuid, user_id);
Samiul Islam0cf90d72022-02-11 16:17:49 +0000178 if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM,
Nikita Ioffebad7cd02022-02-21 19:03:26 +0000179 misc_de_path + "/sdksandbox", user_id)) {
Samiul Islam0cf90d72022-02-11 16:17:49 +0000180 return false;
181 }
182
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000183 if (volume_uuid.empty()) {
Paul Crowley82b41ff2017-10-20 08:17:54 -0700184 if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/vold")) return false;
Jin Qianf3961442017-10-19 14:44:37 -0700185 if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/storaged")) return false;
Narayan Kamatha232fd72019-01-14 10:03:07 +0000186 if (!prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/rollback")) return false;
Oli Lan94457212019-11-19 18:09:34 +0000187 // TODO: Return false if this returns false once sure this should succeed.
Oli Lanac003c42019-12-02 18:27:24 +0000188 prepare_dir(sehandle, 0700, 0, 0, misc_de_path + "/apexrollback");
Oli Lan94457212019-11-19 18:09:34 +0000189 prepare_apex_subdirs(sehandle, misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800190
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000191 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
192 if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, profiles_de_path,
Alan Stokese0b7f302020-12-22 14:49:18 +0000193 user_id)) {
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000194 return false;
195 }
196
Andreas Huber71cd43f2018-01-22 11:25:29 -0800197 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
198 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, vendor_de_path + "/fpdata")) {
199 return false;
200 }
Kevin Chyncdd42282018-11-20 19:09:15 +0000201 auto facedata_path = vendor_de_path + "/facedata";
202 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) {
203 return false;
204 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700205 }
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000206 }
207 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
208 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Alan Stokese0b7f302020-12-22 14:49:18 +0000209 if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM, user_ce_path, user_id)) {
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000210 return false;
211 }
212
Mohammad Samiul Islamb4595912022-03-07 20:27:06 +0000213 auto misc_ce_path = android::vold::BuildDataMiscCePath(volume_uuid, user_id);
Samiul Islam0cf90d72022-02-11 16:17:49 +0000214 if (!prepare_dir_for_user(sehandle, 0771, AID_SYSTEM, AID_SYSTEM,
Nikita Ioffebad7cd02022-02-21 19:03:26 +0000215 misc_ce_path + "/sdksandbox", user_id)) {
Samiul Islam0cf90d72022-02-11 16:17:49 +0000216 return false;
217 }
218
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000219 if (volume_uuid.empty()) {
Paul Crowley82b41ff2017-10-20 08:17:54 -0700220 if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/vold")) return false;
Jin Qianf3961442017-10-19 14:44:37 -0700221 if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/storaged")) return false;
Narayan Kamatha232fd72019-01-14 10:03:07 +0000222 if (!prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/rollback")) return false;
Oli Lan94457212019-11-19 18:09:34 +0000223 // TODO: Return false if this returns false once sure this should succeed.
Oli Lanac003c42019-12-02 18:27:24 +0000224 prepare_dir(sehandle, 0700, 0, 0, misc_ce_path + "/apexrollback");
Oli Lan94457212019-11-19 18:09:34 +0000225 prepare_apex_subdirs(sehandle, misc_ce_path);
Tianjie62487c92021-10-15 20:32:42 -0700226 // Give gmscore (who runs in cache group) access to the checkin directory. Also provide
227 // the user id to set the correct selinux mls_level.
228 if (!prepare_dir_for_user(sehandle, 0770, AID_SYSTEM, AID_CACHE,
229 misc_ce_path + "/checkin", user_id)) {
Tianjieb2ee9e02021-10-21 15:16:49 -0700230 // TODO(b/203742483) the checkin directory was created with the wrong permission &
231 // context. Delete the directory to get these devices out of the bad state. Revert
232 // the change once the droidfood population is on newer build.
233 LOG(INFO) << "Failed to prepare the checkin directory, deleting for recreation";
234 android::vold::DeleteDirContentsAndDir(misc_ce_path + "/checkin");
235 if (!prepare_dir_for_user(sehandle, 0770, AID_SYSTEM, AID_CACHE,
236 misc_ce_path + "/checkin", user_id)) {
237 return false;
238 }
Tianjie62487c92021-10-15 20:32:42 -0700239 }
Annie Meng66176c52019-01-16 21:32:27 +0000240
241 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
242 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, system_ce_path + "/backup")) {
243 return false;
244 }
245 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM,
246 system_ce_path + "/backup_stage")) {
247 return false;
248 }
Paul Crowleyb409ade2019-04-23 17:04:35 -0700249 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
250 auto facedata_path = vendor_ce_path + "/facedata";
251 if (!prepare_dir(sehandle, 0700, AID_SYSTEM, AID_SYSTEM, facedata_path)) {
252 return false;
253 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700254 }
255 }
256 return true;
257}
258
259static bool destroy_subdirs(const std::string& volume_uuid, int user_id, int flags) {
260 bool res = true;
Mohammad Samiul Islamb4595912022-03-07 20:27:06 +0000261 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
262 auto misc_ce_path = android::vold::BuildDataMiscCePath(volume_uuid, user_id);
263 res &= rmrf_contents(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800264
Mohammad Samiul Islamb4595912022-03-07 20:27:06 +0000265 if (volume_uuid.empty()) {
Andreas Huber71cd43f2018-01-22 11:25:29 -0800266 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
267 res &= rmrf_contents(vendor_ce_path);
Paul Crowley82b41ff2017-10-20 08:17:54 -0700268 }
Mohammad Samiul Islamb4595912022-03-07 20:27:06 +0000269 }
270 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
271 auto misc_de_path = android::vold::BuildDataMiscDePath(volume_uuid, user_id);
272 res &= rmrf_contents(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800273
Mohammad Samiul Islamb4595912022-03-07 20:27:06 +0000274 if (volume_uuid.empty()) {
Andreas Huber71cd43f2018-01-22 11:25:29 -0800275 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
276 res &= rmrf_contents(vendor_de_path);
Paul Crowley82b41ff2017-10-20 08:17:54 -0700277 }
278 }
279 return res;
280}
281
282int main(int argc, const char* const argv[]) {
283 android::base::InitLogging(const_cast<char**>(argv));
284 std::vector<std::string> args(argv + 1, argv + argc);
285
286 if (args.size() != 4 || !valid_uuid(args[1]) || !small_int(args[2]) || !small_int(args[3])) {
287 usage(argv[0]);
288 return -1;
289 }
290
291 auto volume_uuid = args[1];
292 int user_id = stoi(args[2]);
293 int flags = stoi(args[3]);
294 if (args[0] == "prepare") {
295 if (!prepare_subdirs(volume_uuid, user_id, flags)) return -1;
296 } else if (args[0] == "destroy") {
297 if (!destroy_subdirs(volume_uuid, user_id, flags)) return -1;
298 } else {
299 usage(argv[0]);
300 return -1;
301 }
302 return 0;
303}