xref: /aosp_15_r20/system/netd/server/UidRanges.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
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 #include <algorithm>
29 
30 using android::base::StringAppendF;
31 
32 namespace android {
33 namespace net {
34 
35 namespace {
36 
compUidRangeParcel(const UidRangeParcel & lhs,const UidRangeParcel & rhs)37 bool compUidRangeParcel(const UidRangeParcel& lhs, const UidRangeParcel& rhs) {
38     return lhs.start != rhs.start ? (lhs.start < rhs.start) : (lhs.stop < rhs.stop);
39 };
40 
41 // Check whether two uid ranges have overlapped uid. For any uid included in both ranges, it is
42 // considered as overlap.
isOverlapped(const UidRangeParcel & r1,const UidRangeParcel & r2)43 bool isOverlapped(const UidRangeParcel& r1, const UidRangeParcel& r2) {
44     return (r1.stop >= r2.start) && (r1.start <= r2.stop);
45 }
46 
makeUidRangeParcel(int start,int stop)47 UidRangeParcel makeUidRangeParcel(int start, int stop) {
48     UidRangeParcel res;
49     res.start = start;
50     res.stop = stop;
51 
52     return res;
53 }
54 
length(const UidRangeParcel & t)55 uint32_t length(const UidRangeParcel& t) {
56     if (t.start == -1 || t.stop == -1) {
57         return 0;
58     }
59     return static_cast<uint32_t>(t.stop - t.start + 1);
60 }
61 
62 }  // namespace
63 
hasUid(uid_t uid) const64 bool UidRanges::hasUid(uid_t uid) const {
65     if (uid > (unsigned) INT32_MAX) {
66         ALOGW("UID larger than 32 bits: %" PRIu64, static_cast<uint64_t>(uid));
67         return false;
68     }
69     const int32_t intUid = static_cast<int32_t>(uid);
70 
71     auto iter = std::lower_bound(mRanges.begin(), mRanges.end(), makeUidRangeParcel(intUid, intUid),
72                                  compUidRangeParcel);
73     return (iter != mRanges.end() && iter->start == intUid) ||
74            (iter != mRanges.begin() && (--iter)->stop >= intUid);
75 }
76 
getRanges() const77 const std::vector<UidRangeParcel>& UidRanges::getRanges() const {
78     return mRanges;
79 }
80 
parseFrom(int argc,char * argv[])81 bool UidRanges::parseFrom(int argc, char* argv[]) {
82     mRanges.clear();
83     for (int i = 0; i < argc; ++i) {
84         if (!*argv[i]) {
85             // The UID string is empty.
86             return false;
87         }
88         char* endPtr;
89         uid_t uidStart = strtoul(argv[i], &endPtr, 0);
90         uid_t uidEnd;
91         if (!*endPtr) {
92             // Found a single UID. The range contains just the one UID.
93             uidEnd = uidStart;
94         } else if (*endPtr == '-') {
95             if (!*++endPtr) {
96                 // Unexpected end of string.
97                 return false;
98             }
99             uidEnd = strtoul(endPtr, &endPtr, 0);
100             if (*endPtr) {
101                 // Illegal trailing chars.
102                 return false;
103             }
104             if (uidEnd < uidStart) {
105                 // Invalid order.
106                 return false;
107             }
108         } else {
109             // Not a single uid, not a range. Found some other illegal char.
110             return false;
111         }
112         if (uidStart == INVALID_UID || uidEnd == INVALID_UID) {
113             // Invalid UIDs.
114             return false;
115         }
116         mRanges.push_back(makeUidRangeParcel(uidStart, uidEnd));
117     }
118     std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel);
119     return true;
120 }
121 
UidRanges(const std::vector<UidRangeParcel> & ranges)122 UidRanges::UidRanges(const std::vector<UidRangeParcel>& ranges) {
123     mRanges = ranges;
124     std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel);
125 }
126 
add(const UidRanges & other)127 void UidRanges::add(const UidRanges& other) {
128     auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end());
129     std::inplace_merge(mRanges.begin(), middle, mRanges.end(), compUidRangeParcel);
130 }
131 
remove(const UidRanges & other)132 void UidRanges::remove(const UidRanges& other) {
133     auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(),
134                                    other.mRanges.end(), mRanges.begin(), compUidRangeParcel);
135     mRanges.erase(end, mRanges.end());
136 }
137 
overlapsSelf() const138 bool UidRanges::overlapsSelf() const {
139     // Compare each element one by one
140     for (size_t i = 0; i < mRanges.size(); i++) {
141         for (size_t j = i + 1; j < mRanges.size(); j++) {
142             if (isOverlapped(mRanges[i], mRanges[j])) {
143                 return true;
144             }
145         }
146     }
147     return false;
148 }
149 
toString() const150 std::string UidRanges::toString() const {
151     std::string s("uids{ ");
152     for (const auto &range : mRanges) {
153         if (length(range) == 0) {
154             StringAppendF(&s, "<BAD: %u-%u> ", range.start, range.stop);
155         } else if (length(range) == 1) {
156             StringAppendF(&s, "%u ", range.start);
157         } else {
158             StringAppendF(&s, "%u-%u ", range.start, range.stop);
159         }
160     }
161     StringAppendF(&s, "}");
162     return s;
163 }
164 
165 }  // namespace net
166 }  // namespace android
167