Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 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 Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2/FileUtils.h" |
| 18 | |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 19 | #include <random> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 20 | #include <string> |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 21 | #include <string_view> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 22 | |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 23 | #include "android-base/file.h" |
| 24 | #include "android-base/macros.h" |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 25 | #include "android-base/strings.h" |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 26 | #include "private/android_filesystem_config.h" |
| 27 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 28 | namespace android::idmap2::utils { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 29 | |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 30 | #ifdef __ANDROID__ |
| 31 | bool 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 Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 38 | if (base::StartsWith(canonical_path, kIdmapCacheDir) && |
| 39 | (canonical_path.size() == kIdmapCacheDir.size() || |
| 40 | canonical_path[kIdmapCacheDir.size()] == '/')) { |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 41 | // 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 |
| 47 | bool UidHasWriteAccessToPath(uid_t uid ATTRIBUTE_UNUSED, const std::string& path ATTRIBUTE_UNUSED) { |
| 48 | return true; |
| 49 | } |
| 50 | #endif |
| 51 | |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 52 | std::string RandomStringForPath(size_t length) { |
| 53 | constexpr std::string_view kChars = |
| 54 | "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 55 | |
| 56 | std::string out_rand; |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 57 | out_rand.resize(length); |
Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 58 | |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 59 | static thread_local std::random_device rd; |
| 60 | std::uniform_int_distribution<int> dist(0, kChars.size() - 1); |
Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 61 | for (size_t i = 0; i < length; i++) { |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 62 | out_rand[i] = kChars[dist(rd)]; |
Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 63 | } |
| 64 | return out_rand; |
| 65 | } |
| 66 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 67 | } // namespace android::idmap2::utils |