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 21 #include <inttypes.h> 22 #include <limits.h> 23 #include <stdlib.h> 24 25 #include <android-base/stringprintf.h> 26 #include <log/log.h> 27 28 using android::base::StringAppendF; 29 30 namespace android { 31 namespace net { 32 33 bool UidRanges::hasUid(uid_t uid) const { 34 if (uid > (unsigned) INT32_MAX) { 35 ALOGW("UID larger than 32 bits: %" PRIu64, static_cast<uint64_t>(uid)); 36 return false; 37 } 38 const int32_t intUid = static_cast<int32_t>(uid); 39 40 auto iter = std::lower_bound(mRanges.begin(), mRanges.end(), UidRange(intUid, intUid)); 41 return (iter != mRanges.end() && iter->getStart() == intUid) || 42 (iter != mRanges.begin() && (--iter)->getStop() >= intUid); 43 } 44 45 const std::vector<UidRange>& UidRanges::getRanges() const { 46 return mRanges; 47 } 48 49 bool UidRanges::parseFrom(int argc, char* argv[]) { 50 mRanges.clear(); 51 for (int i = 0; i < argc; ++i) { 52 if (!*argv[i]) { 53 // The UID string is empty. 54 return false; 55 } 56 char* endPtr; 57 uid_t uidStart = strtoul(argv[i], &endPtr, 0); 58 uid_t uidEnd; 59 if (!*endPtr) { 60 // Found a single UID. The range contains just the one UID. 61 uidEnd = uidStart; 62 } else if (*endPtr == '-') { 63 if (!*++endPtr) { 64 // Unexpected end of string. 65 return false; 66 } 67 uidEnd = strtoul(endPtr, &endPtr, 0); 68 if (*endPtr) { 69 // Illegal trailing chars. 70 return false; 71 } 72 if (uidEnd < uidStart) { 73 // Invalid order. 74 return false; 75 } 76 } else { 77 // Not a single uid, not a range. Found some other illegal char. 78 return false; 79 } 80 if (uidStart == INVALID_UID || uidEnd == INVALID_UID) { 81 // Invalid UIDs. 82 return false; 83 } 84 mRanges.push_back(UidRange(uidStart, uidEnd)); 85 } 86 std::sort(mRanges.begin(), mRanges.end()); 87 return true; 88 } 89 90 UidRanges::UidRanges(const std::vector<UidRange>& ranges) { 91 mRanges = ranges; 92 std::sort(mRanges.begin(), mRanges.end()); 93 } 94 95 void UidRanges::add(const UidRanges& other) { 96 auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end()); 97 std::inplace_merge(mRanges.begin(), middle, mRanges.end()); 98 } 99 100 void UidRanges::remove(const UidRanges& other) { 101 auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(), 102 other.mRanges.end(), mRanges.begin()); 103 mRanges.erase(end, mRanges.end()); 104 } 105 106 std::string UidRanges::toString() const { 107 std::string s("UidRanges{ "); 108 for (const auto &range : mRanges) { 109 if (range.length() == 0) { 110 StringAppendF(&s, "<BAD: %u-%u> ", range.getStart(), range.getStop()); 111 } else if (range.length() == 1) { 112 StringAppendF(&s, "%u ", range.getStart()); 113 } else { 114 StringAppendF(&s, "%u-%u ", range.getStart(), range.getStop()); 115 } 116 } 117 StringAppendF(&s, "}"); 118 return s; 119 } 120 121 } // namespace net 122 } // namespace android 123