summaryrefslogtreecommitdiff
path: root/server/UidRanges.cpp
blob: bf86356809dec76511fb177a42f67800ab0a56ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "UidRanges.h"

#include "NetdConstants.h"

#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>

#include <android-base/stringprintf.h>
#include <log/log.h>

#include <algorithm>

using android::base::StringAppendF;

namespace android {
namespace net {

namespace {

bool compUidRangeParcel(const UidRangeParcel& lhs, const UidRangeParcel& rhs) {
    return lhs.start != rhs.start ? (lhs.start < rhs.start) : (lhs.stop < rhs.stop);
};

// Check whether two uid ranges have overlapped uid. For any uid included in both ranges, it is
// considered as overlap.
bool isOverlapped(const UidRangeParcel& r1, const UidRangeParcel& r2) {
    return (r1.stop >= r2.start) && (r1.start <= r2.stop);
}

UidRangeParcel makeUidRangeParcel(int start, int stop) {
    UidRangeParcel res;
    res.start = start;
    res.stop = stop;

    return res;
}

uint32_t length(const UidRangeParcel& t) {
    if (t.start == -1 || t.stop == -1) {
        return 0;
    }
    return static_cast<uint32_t>(t.stop - t.start + 1);
}

}  // namespace

bool UidRanges::hasUid(uid_t uid) const {
    if (uid > (unsigned) INT32_MAX) {
        ALOGW("UID larger than 32 bits: %" PRIu64, static_cast<uint64_t>(uid));
        return false;
    }
    const int32_t intUid = static_cast<int32_t>(uid);

    auto iter = std::lower_bound(mRanges.begin(), mRanges.end(), makeUidRangeParcel(intUid, intUid),
                                 compUidRangeParcel);
    return (iter != mRanges.end() && iter->start == intUid) ||
           (iter != mRanges.begin() && (--iter)->stop >= intUid);
}

const std::vector<UidRangeParcel>& UidRanges::getRanges() const {
    return mRanges;
}

bool UidRanges::parseFrom(int argc, char* argv[]) {
    mRanges.clear();
    for (int i = 0; i < argc; ++i) {
        if (!*argv[i]) {
            // The UID string is empty.
            return false;
        }
        char* endPtr;
        uid_t uidStart = strtoul(argv[i], &endPtr, 0);
        uid_t uidEnd;
        if (!*endPtr) {
            // Found a single UID. The range contains just the one UID.
            uidEnd = uidStart;
        } else if (*endPtr == '-') {
            if (!*++endPtr) {
                // Unexpected end of string.
                return false;
            }
            uidEnd = strtoul(endPtr, &endPtr, 0);
            if (*endPtr) {
                // Illegal trailing chars.
                return false;
            }
            if (uidEnd < uidStart) {
                // Invalid order.
                return false;
            }
        } else {
            // Not a single uid, not a range. Found some other illegal char.
            return false;
        }
        if (uidStart == INVALID_UID || uidEnd == INVALID_UID) {
            // Invalid UIDs.
            return false;
        }
        mRanges.push_back(makeUidRangeParcel(uidStart, uidEnd));
    }
    std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel);
    return true;
}

UidRanges::UidRanges(const std::vector<UidRangeParcel>& ranges) {
    mRanges = ranges;
    std::sort(mRanges.begin(), mRanges.end(), compUidRangeParcel);
}

void UidRanges::add(const UidRanges& other) {
    auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end());
    std::inplace_merge(mRanges.begin(), middle, mRanges.end(), compUidRangeParcel);
}

void UidRanges::remove(const UidRanges& other) {
    auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(),
                                   other.mRanges.end(), mRanges.begin(), compUidRangeParcel);
    mRanges.erase(end, mRanges.end());
}

bool UidRanges::overlapsSelf() const {
    // Compare each element one by one
    for (size_t i = 0; i < mRanges.size(); i++) {
        for (size_t j = i + 1; j < mRanges.size(); j++) {
            if (isOverlapped(mRanges[i], mRanges[j])) {
                return true;
            }
        }
    }
    return false;
}

std::string UidRanges::toString() const {
    std::string s("uids{ ");
    for (const auto &range : mRanges) {
        if (length(range) == 0) {
            StringAppendF(&s, "<BAD: %u-%u> ", range.start, range.stop);
        } else if (length(range) == 1) {
            StringAppendF(&s, "%u ", range.start);
        } else {
            StringAppendF(&s, "%u-%u ", range.start, range.stop);
        }
    }
    StringAppendF(&s, "}");
    return s;
}

}  // namespace net
}  // namespace android