• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace {
34 
compUidRangeParcel(const UidRangeParcel & lhs,const UidRangeParcel & rhs)35 bool compUidRangeParcel(const UidRangeParcel& lhs, const UidRangeParcel& rhs) {
36     return lhs.start != rhs.start ? (lhs.start < rhs.start) : (lhs.stop < rhs.stop);
37 };
38 
makeUidRangeParcel(int start,int stop)39 UidRangeParcel makeUidRangeParcel(int start, int stop) {
40     UidRangeParcel res;
41     res.start = start;
42     res.stop = stop;
43 
44     return res;
45 }
46 
length(const UidRangeParcel & t)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 
hasUid(uid_t uid) const56 bool UidRanges::hasUid(uid_t uid) const {
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 
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);
67 }
68 
getRanges() const69 const std::vector<UidRangeParcel>& UidRanges::getRanges() const {
70     return mRanges;
71 }
72 
parseFrom(int argc,char * argv[])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         }
108         mRanges.push_back(makeUidRangeParcel(uidStart, uidEnd));
109     }
110     std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel);
111     return true;
112 }
113 
UidRanges(const std::vector<UidRangeParcel> & ranges)114 UidRanges::UidRanges(const std::vector<UidRangeParcel>& ranges) {
115     mRanges = ranges;
116     std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel);
117 }
118 
add(const UidRanges & other)119 void UidRanges::add(const UidRanges& other) {
120     auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end());
121     std::inplace_merge(mRanges.begin(), middle, mRanges.end(), compUidRangeParcel);
122 }
123 
remove(const UidRanges & other)124 void UidRanges::remove(const UidRanges& other) {
125     auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(),
126                                    other.mRanges.end(), mRanges.begin(), compUidRangeParcel);
127     mRanges.erase(end, mRanges.end());
128 }
129 
isOverlapped(const UidRangeParcel & r1,const UidRangeParcel & r2) const130 bool UidRanges::isOverlapped(const UidRangeParcel& r1, const UidRangeParcel& r2) const {
131     return (r1.stop >= r2.start) && (r1.start <= r2.stop);
132 }
133 
overlapsSelf() const134 bool UidRanges::overlapsSelf() const {
135     // Compare each element one by one
136     for (size_t i = 0; i < mRanges.size(); i++) {
137         for (size_t j = i + 1; j < mRanges.size(); j++) {
138             if (isOverlapped(mRanges[i], mRanges[j])) {
139                 return true;
140             }
141         }
142     }
143     return false;
144 }
145 
overlaps(const UidRanges & other) const146 bool UidRanges::overlaps(const UidRanges& other) const {
147     for (const auto& thisRange : mRanges) {
148         for (const auto& inputRange : other.getRanges()) {
149             if (isOverlapped(thisRange, inputRange)) {
150                 return true;
151             }
152         }
153     }
154     return false;
155 }
156 
toString() const157 std::string UidRanges::toString() const {
158     std::string s("uids{ ");
159     for (const auto &range : mRanges) {
160         if (length(range) == 0) {
161             StringAppendF(&s, "<BAD: %u-%u> ", range.start, range.stop);
162         } else if (length(range) == 1) {
163             StringAppendF(&s, "%u ", range.start);
164         } else {
165             StringAppendF(&s, "%u-%u ", range.start, range.stop);
166         }
167     }
168     StringAppendF(&s, "}");
169     return s;
170 }
171 
172 }  // namespace net
173 }  // namespace android
174