Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #include "UidRanges.h" |
| 18 | |
| 19 | #include "NetdConstants.h" |
| 20 | |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame] | 21 | #include <inttypes.h> |
| 22 | #include <limits.h> |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame] | 26 | #include <log/log.h> |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 27 | |
Tomasz Wasilczyk | d4c1d7d | 2024-01-09 11:42:58 -0800 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 30 | using android::base::StringAppendF; |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace net { |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 34 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 35 | namespace { |
| 36 | |
Bernie Innocenti | b9c5e85 | 2018-11-21 18:17:22 +0900 | [diff] [blame] | 37 | bool compUidRangeParcel(const UidRangeParcel& lhs, const UidRangeParcel& rhs) { |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 38 | return lhs.start != rhs.start ? (lhs.start < rhs.start) : (lhs.stop < rhs.stop); |
| 39 | }; |
| 40 | |
Ken Chen | 7006bec | 2021-07-09 23:50:37 +0800 | [diff] [blame] | 41 | // Check whether two uid ranges have overlapped uid. For any uid included in both ranges, it is |
| 42 | // considered as overlap. |
| 43 | bool isOverlapped(const UidRangeParcel& r1, const UidRangeParcel& r2) { |
| 44 | return (r1.stop >= r2.start) && (r1.start <= r2.stop); |
| 45 | } |
| 46 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 47 | UidRangeParcel makeUidRangeParcel(int start, int stop) { |
| 48 | UidRangeParcel res; |
| 49 | res.start = start; |
| 50 | res.stop = stop; |
| 51 | |
| 52 | return res; |
| 53 | } |
| 54 | |
| 55 | uint32_t length(const UidRangeParcel& t) { |
| 56 | if (t.start == -1 || t.stop == -1) { |
| 57 | return 0; |
| 58 | } |
| 59 | return static_cast<uint32_t>(t.stop - t.start + 1); |
| 60 | } |
| 61 | |
| 62 | } // namespace |
| 63 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 64 | bool UidRanges::hasUid(uid_t uid) const { |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame] | 65 | if (uid > (unsigned) INT32_MAX) { |
| 66 | ALOGW("UID larger than 32 bits: %" PRIu64, static_cast<uint64_t>(uid)); |
| 67 | return false; |
| 68 | } |
| 69 | const int32_t intUid = static_cast<int32_t>(uid); |
| 70 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 71 | auto iter = std::lower_bound(mRanges.begin(), mRanges.end(), makeUidRangeParcel(intUid, intUid), |
| 72 | compUidRangeParcel); |
| 73 | return (iter != mRanges.end() && iter->start == intUid) || |
| 74 | (iter != mRanges.begin() && (--iter)->stop >= intUid); |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 77 | const std::vector<UidRangeParcel>& UidRanges::getRanges() const { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 78 | return mRanges; |
| 79 | } |
| 80 | |
| 81 | bool UidRanges::parseFrom(int argc, char* argv[]) { |
| 82 | mRanges.clear(); |
| 83 | for (int i = 0; i < argc; ++i) { |
| 84 | if (!*argv[i]) { |
| 85 | // The UID string is empty. |
| 86 | return false; |
| 87 | } |
| 88 | char* endPtr; |
| 89 | uid_t uidStart = strtoul(argv[i], &endPtr, 0); |
| 90 | uid_t uidEnd; |
| 91 | if (!*endPtr) { |
| 92 | // Found a single UID. The range contains just the one UID. |
| 93 | uidEnd = uidStart; |
| 94 | } else if (*endPtr == '-') { |
| 95 | if (!*++endPtr) { |
| 96 | // Unexpected end of string. |
| 97 | return false; |
| 98 | } |
| 99 | uidEnd = strtoul(endPtr, &endPtr, 0); |
| 100 | if (*endPtr) { |
| 101 | // Illegal trailing chars. |
| 102 | return false; |
| 103 | } |
| 104 | if (uidEnd < uidStart) { |
| 105 | // Invalid order. |
| 106 | return false; |
| 107 | } |
| 108 | } else { |
| 109 | // Not a single uid, not a range. Found some other illegal char. |
| 110 | return false; |
| 111 | } |
| 112 | if (uidStart == INVALID_UID || uidEnd == INVALID_UID) { |
| 113 | // Invalid UIDs. |
| 114 | return false; |
| 115 | } |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 116 | mRanges.push_back(makeUidRangeParcel(uidStart, uidEnd)); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 117 | } |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 118 | std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 122 | UidRanges::UidRanges(const std::vector<UidRangeParcel>& ranges) { |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame] | 123 | mRanges = ranges; |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 124 | std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 127 | void UidRanges::add(const UidRanges& other) { |
| 128 | auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end()); |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 129 | std::inplace_merge(mRanges.begin(), middle, mRanges.end(), compUidRangeParcel); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void UidRanges::remove(const UidRanges& other) { |
| 133 | auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(), |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 134 | other.mRanges.end(), mRanges.begin(), compUidRangeParcel); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 135 | mRanges.erase(end, mRanges.end()); |
| 136 | } |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 137 | |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 138 | bool UidRanges::overlapsSelf() const { |
| 139 | // Compare each element one by one |
| 140 | for (size_t i = 0; i < mRanges.size(); i++) { |
| 141 | for (size_t j = i + 1; j < mRanges.size(); j++) { |
| 142 | if (isOverlapped(mRanges[i], mRanges[j])) { |
| 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 150 | std::string UidRanges::toString() const { |
Ken Chen | 8e0ba5a | 2021-06-11 03:29:45 +0800 | [diff] [blame] | 151 | std::string s("uids{ "); |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame] | 152 | for (const auto &range : mRanges) { |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 153 | if (length(range) == 0) { |
| 154 | StringAppendF(&s, "<BAD: %u-%u> ", range.start, range.stop); |
| 155 | } else if (length(range) == 1) { |
| 156 | StringAppendF(&s, "%u ", range.start); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 157 | } else { |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 158 | StringAppendF(&s, "%u-%u ", range.start, range.stop); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | StringAppendF(&s, "}"); |
| 162 | return s; |
| 163 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 164 | |
| 165 | } // namespace net |
| 166 | } // namespace android |