• 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 
39 // Check whether two uid ranges have overlapped uid. For any uid included in both ranges, it is
40 // considered as overlap.
isOverlapped(const UidRangeParcel & r1,const UidRangeParcel & r2)41 bool isOverlapped(const UidRangeParcel& r1, const UidRangeParcel& r2) {
42     return (r1.stop >= r2.start) && (r1.start <= r2.stop);
43 }
44 
makeUidRangeParcel(int start,int stop)45 UidRangeParcel makeUidRangeParcel(int start, int stop) {
46     UidRangeParcel res;
47     res.start = start;
48     res.stop = stop;
49 
50     return res;
51 }
52 
length(const UidRangeParcel & t)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 
hasUid(uid_t uid) const62 bool UidRanges::hasUid(uid_t uid) const {
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 
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);
73 }
74 
getRanges() const75 const std::vector<UidRangeParcel>& UidRanges::getRanges() const {
76     return mRanges;
77 }
78 
parseFrom(int argc,char * argv[])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         }
114         mRanges.push_back(makeUidRangeParcel(uidStart, uidEnd));
115     }
116     std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel);
117     return true;
118 }
119 
UidRanges(const std::vector<UidRangeParcel> & ranges)120 UidRanges::UidRanges(const std::vector<UidRangeParcel>& ranges) {
121     mRanges = ranges;
122     std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel);
123 }
124 
add(const UidRanges & other)125 void UidRanges::add(const UidRanges& other) {
126     auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end());
127     std::inplace_merge(mRanges.begin(), middle, mRanges.end(), compUidRangeParcel);
128 }
129 
remove(const UidRanges & other)130 void UidRanges::remove(const UidRanges& other) {
131     auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(),
132                                    other.mRanges.end(), mRanges.begin(), compUidRangeParcel);
133     mRanges.erase(end, mRanges.end());
134 }
135 
overlapsSelf() const136 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 
toString() const148 std::string UidRanges::toString() const {
149     std::string s("uids{ ");
150     for (const auto &range : mRanges) {
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);
155         } else {
156             StringAppendF(&s, "%u-%u ", range.start, range.stop);
157         }
158     }
159     StringAppendF(&s, "}");
160     return s;
161 }
162 
163 }  // namespace net
164 }  // namespace android
165