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 | |
| 39 | UidRangeParcel makeUidRangeParcel(int start, int stop) { |
| 40 | UidRangeParcel res; |
| 41 | res.start = start; |
| 42 | res.stop = stop; |
| 43 | |
| 44 | return res; |
| 45 | } |
| 46 | |
| 47 | uint32_t length(const UidRangeParcel& t) { |
| 48 | if (t.start == -1 || t.stop == -1) { |
| 49 | return 0; |
| 50 | } |
| 51 | return static_cast<uint32_t>(t.stop - t.start + 1); |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 56 | bool UidRanges::hasUid(uid_t uid) const { |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame] | 57 | if (uid > (unsigned) INT32_MAX) { |
| 58 | ALOGW("UID larger than 32 bits: %" PRIu64, static_cast<uint64_t>(uid)); |
| 59 | return false; |
| 60 | } |
| 61 | const int32_t intUid = static_cast<int32_t>(uid); |
| 62 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 63 | auto iter = std::lower_bound(mRanges.begin(), mRanges.end(), makeUidRangeParcel(intUid, intUid), |
| 64 | compUidRangeParcel); |
| 65 | return (iter != mRanges.end() && iter->start == intUid) || |
| 66 | (iter != mRanges.begin() && (--iter)->stop >= intUid); |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 69 | const std::vector<UidRangeParcel>& UidRanges::getRanges() const { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 70 | return mRanges; |
| 71 | } |
| 72 | |
| 73 | bool UidRanges::parseFrom(int argc, char* argv[]) { |
| 74 | mRanges.clear(); |
| 75 | for (int i = 0; i < argc; ++i) { |
| 76 | if (!*argv[i]) { |
| 77 | // The UID string is empty. |
| 78 | return false; |
| 79 | } |
| 80 | char* endPtr; |
| 81 | uid_t uidStart = strtoul(argv[i], &endPtr, 0); |
| 82 | uid_t uidEnd; |
| 83 | if (!*endPtr) { |
| 84 | // Found a single UID. The range contains just the one UID. |
| 85 | uidEnd = uidStart; |
| 86 | } else if (*endPtr == '-') { |
| 87 | if (!*++endPtr) { |
| 88 | // Unexpected end of string. |
| 89 | return false; |
| 90 | } |
| 91 | uidEnd = strtoul(endPtr, &endPtr, 0); |
| 92 | if (*endPtr) { |
| 93 | // Illegal trailing chars. |
| 94 | return false; |
| 95 | } |
| 96 | if (uidEnd < uidStart) { |
| 97 | // Invalid order. |
| 98 | return false; |
| 99 | } |
| 100 | } else { |
| 101 | // Not a single uid, not a range. Found some other illegal char. |
| 102 | return false; |
| 103 | } |
| 104 | if (uidStart == INVALID_UID || uidEnd == INVALID_UID) { |
| 105 | // Invalid UIDs. |
| 106 | return false; |
| 107 | } |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 108 | mRanges.push_back(makeUidRangeParcel(uidStart, uidEnd)); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 109 | } |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 110 | std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 114 | UidRanges::UidRanges(const std::vector<UidRangeParcel>& ranges) { |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame] | 115 | mRanges = ranges; |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 116 | std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 119 | void UidRanges::add(const UidRanges& other) { |
| 120 | auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end()); |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 121 | std::inplace_merge(mRanges.begin(), middle, mRanges.end(), compUidRangeParcel); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void UidRanges::remove(const UidRanges& other) { |
| 125 | auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(), |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 126 | other.mRanges.end(), mRanges.begin(), compUidRangeParcel); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 127 | mRanges.erase(end, mRanges.end()); |
| 128 | } |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 129 | |
| 130 | std::string UidRanges::toString() const { |
| 131 | std::string s("UidRanges{ "); |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame] | 132 | for (const auto &range : mRanges) { |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 133 | if (length(range) == 0) { |
| 134 | StringAppendF(&s, "<BAD: %u-%u> ", range.start, range.stop); |
| 135 | } else if (length(range) == 1) { |
| 136 | StringAppendF(&s, "%u ", range.start); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 137 | } else { |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 138 | StringAppendF(&s, "%u-%u ", range.start, range.stop); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | StringAppendF(&s, "}"); |
| 142 | return s; |
| 143 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 144 | |
| 145 | } // namespace net |
| 146 | } // namespace android |