blob: bc5654aad7b8ac5bd0563ad76bf3da6a46ba8696 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/FileUtils.h"
18
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070019#include <random>
Mårten Kongstad02751232018-04-27 13:16:32 +020020#include <string>
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070021#include <string_view>
Mårten Kongstad02751232018-04-27 13:16:32 +020022
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010023#include "android-base/file.h"
24#include "android-base/macros.h"
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070025#include "android-base/strings.h"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010026#include "private/android_filesystem_config.h"
27
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010028namespace android::idmap2::utils {
Mårten Kongstad02751232018-04-27 13:16:32 +020029
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010030#ifdef __ANDROID__
31bool UidHasWriteAccessToPath(uid_t uid, const std::string& path) {
32 // resolve symlinks and relative paths; the directories must exist
33 std::string canonical_path;
34 if (!base::Realpath(base::Dirname(path), &canonical_path)) {
35 return false;
36 }
37
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070038 if (base::StartsWith(canonical_path, kIdmapCacheDir) &&
39 (canonical_path.size() == kIdmapCacheDir.size() ||
40 canonical_path[kIdmapCacheDir.size()] == '/')) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010041 // limit access to /data/resource-cache to root and system
42 return uid == AID_ROOT || uid == AID_SYSTEM;
43 }
44 return true;
45}
46#else
47bool UidHasWriteAccessToPath(uid_t uid ATTRIBUTE_UNUSED, const std::string& path ATTRIBUTE_UNUSED) {
48 return true;
49}
50#endif
51
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070052std::string RandomStringForPath(size_t length) {
53 constexpr std::string_view kChars =
54 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080055
56 std::string out_rand;
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070057 out_rand.resize(length);
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080058
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070059 static thread_local std::random_device rd;
60 std::uniform_int_distribution<int> dist(0, kChars.size() - 1);
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080061 for (size_t i = 0; i < length; i++) {
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070062 out_rand[i] = kChars[dist(rd)];
Ryan Mitchell6a2ca782021-01-19 13:51:15 -080063 }
64 return out_rand;
65}
66
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010067} // namespace android::idmap2::utils