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