• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 
18 #pragma once
19 
20 #include <arpa/nameser.h>
21 #include <netdb.h>
22 
23 #include <string>
24 #include <vector>
25 
26 #include <aidl/android/net/INetd.h>
27 #include <gtest/gtest.h>
28 #include <netdutils/InternetAddresses.h>
29 
30 #include "dns_responder/dns_responder.h"
31 
32 class ScopeBlockedUIDRule {
33     using INetd = aidl::android::net::INetd;
34 
35   public:
ScopeBlockedUIDRule(INetd * netSrv,uid_t testUid)36     ScopeBlockedUIDRule(INetd* netSrv, uid_t testUid)
37         : mNetSrv(netSrv), mTestUid(testUid), mSavedUid(getuid()) {
38         // Add drop rule for testUid. Also enable the standby chain because it might not be
39         // enabled. Unfortunately we cannot use FIREWALL_CHAIN_NONE, or custom iptables rules, for
40         // this purpose because netd calls fchown() on the DNS query sockets, and "iptables -m
41         // owner" matches the UID of the socket creator, not the UID set by fchown().
42         // TODO: migrate FIREWALL_CHAIN_NONE to eBPF as well.
43         EXPECT_TRUE(mNetSrv->firewallEnableChildChain(INetd::FIREWALL_CHAIN_STANDBY, true).isOk());
44         EXPECT_TRUE(mNetSrv->firewallSetUidRule(INetd::FIREWALL_CHAIN_STANDBY, mTestUid,
45                                                 INetd::FIREWALL_RULE_DENY)
46                             .isOk());
47         EXPECT_TRUE(seteuid(mTestUid) == 0);
48     };
~ScopeBlockedUIDRule()49     ~ScopeBlockedUIDRule() {
50         // Restore uid
51         EXPECT_TRUE(seteuid(mSavedUid) == 0);
52         // Remove drop rule for testUid, and disable the standby chain.
53         EXPECT_TRUE(mNetSrv->firewallSetUidRule(INetd::FIREWALL_CHAIN_STANDBY, mTestUid,
54                                                 INetd::FIREWALL_RULE_ALLOW)
55                             .isOk());
56         EXPECT_TRUE(mNetSrv->firewallEnableChildChain(INetd::FIREWALL_CHAIN_STANDBY, false).isOk());
57     }
58 
59   private:
60     INetd* mNetSrv;
61     const uid_t mTestUid;
62     const uid_t mSavedUid;
63 };
64 
65 class ScopedChangeUID {
66   public:
ScopedChangeUID(uid_t testUid)67     ScopedChangeUID(uid_t testUid) : mTestUid(testUid), mSavedUid(getuid()) {
68         EXPECT_TRUE(seteuid(mTestUid) == 0);
69     };
~ScopedChangeUID()70     ~ScopedChangeUID() { EXPECT_TRUE(seteuid(mSavedUid) == 0); }
71 
72   private:
73     const uid_t mTestUid;
74     const uid_t mSavedUid;
75 };
76 
77 struct DnsRecord {
78     std::string host_name;  // host name
79     ns_type type;           // record type
80     std::string addr;       // ipv4/v6 address
81 };
82 
83 // TODO: make this dynamic and stop depending on implementation details.
84 constexpr int TEST_NETID = 30;
85 // Use the biggest two reserved appId for applications to avoid conflict with existing uids.
86 constexpr int TEST_UID = 99999;
87 constexpr int TEST_UID2 = 99998;
88 
89 static constexpr char kLocalHost[] = "localhost";
90 static constexpr char kLocalHostAddr[] = "127.0.0.1";
91 static constexpr char kIp6LocalHost[] = "ip6-localhost";
92 static constexpr char kIp6LocalHostAddr[] = "::1";
93 static constexpr char kHelloExampleCom[] = "hello.example.com.";
94 static constexpr char kHelloExampleComAddrV4[] = "1.2.3.4";
95 static constexpr char kHelloExampleComAddrV6[] = "::1.2.3.4";
96 static constexpr char kExampleComDomain[] = ".example.com";
97 
98 constexpr size_t kMaxmiumLabelSize = 63;  // see RFC 1035 section 2.3.4.
99 
100 static const std::vector<uint8_t> kHelloExampleComQueryV4 = {
101         /* Header */
102         0x00, 0x00, /* Transaction ID: 0x0000 */
103         0x01, 0x00, /* Flags: rd */
104         0x00, 0x01, /* Questions: 1 */
105         0x00, 0x00, /* Answer RRs: 0 */
106         0x00, 0x00, /* Authority RRs: 0 */
107         0x00, 0x00, /* Additional RRs: 0 */
108         /* Queries */
109         0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
110         0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
111         0x00, 0x01,             /* Type: A */
112         0x00, 0x01              /* Class: IN */
113 };
114 
115 static const std::vector<uint8_t> kHelloExampleComResponseV4 = {
116         /* Header */
117         0x00, 0x00, /* Transaction ID: 0x0000 */
118         0x81, 0x80, /* Flags: qr rd ra */
119         0x00, 0x01, /* Questions: 1 */
120         0x00, 0x01, /* Answer RRs: 1 */
121         0x00, 0x00, /* Authority RRs: 0 */
122         0x00, 0x00, /* Additional RRs: 0 */
123         /* Queries */
124         0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
125         0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
126         0x00, 0x01,             /* Type: A */
127         0x00, 0x01,             /* Class: IN */
128         /* Answers */
129         0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
130         0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
131         0x00, 0x01,             /* Type: A */
132         0x00, 0x01,             /* Class: IN */
133         0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */
134         0x00, 0x04,             /* Data length: 4 */
135         0x01, 0x02, 0x03, 0x04  /* Address: 1.2.3.4 */
136 };
137 
138 // Illegal hostnames
139 static constexpr char kBadCharAfterPeriodHost[] = "hello.example.^com.";
140 static constexpr char kBadCharBeforePeriodHost[] = "hello.example^.com.";
141 static constexpr char kBadCharAtTheEndHost[] = "hello.example.com^.";
142 static constexpr char kBadCharInTheMiddleOfLabelHost[] = "hello.ex^ample.com.";
143 
144 static const test::DNSHeader kDefaultDnsHeader = {
145         // Don't need to initialize the flag "id" and "rd" because DNS responder assigns them from
146         // query to response. See RFC 1035 section 4.1.1.
147         .id = 0,                // unused. should be assigned from query to response
148         .ra = false,            // recursive query support is not available
149         .rcode = ns_r_noerror,  // no error
150         .qr = true,             // message is a response
151         .opcode = QUERY,        // a standard query
152         .aa = false,            // answer/authority portion was not authenticated by the server
153         .tr = false,            // message is not truncated
154         .rd = false,            // unused. should be assigned from query to response
155         .ad = false,            // non-authenticated data is unacceptable
156 };
157 
158 // The CNAME chain records for building a response message which exceeds 512 bytes.
159 //
160 // Ignoring the other fields of the message, the response message has 8 CNAMEs in 5 answer RRs
161 // and each CNAME has 77 bytes as the follows. The response message at least has 616 bytes in
162 // answer section and has already exceeded 512 bytes totally.
163 //
164 // The CNAME is presented as:
165 //   0   1            64  65                          72  73          76  77
166 //   +---+--........--+---+---+---+---+---+---+---+---+---+---+---+---+---+
167 //   | 63| {x, .., x} | 7 | e | x | a | m | p | l | e | 3 | c | o | m | 0 |
168 //   +---+--........--+---+---+---+---+---+---+---+---+---+---+---+---+---+
169 //          ^-- x = {a, b, c, d}
170 //
171 const std::string kCnameA = std::string(kMaxmiumLabelSize, 'a') + kExampleComDomain + ".";
172 const std::string kCnameB = std::string(kMaxmiumLabelSize, 'b') + kExampleComDomain + ".";
173 const std::string kCnameC = std::string(kMaxmiumLabelSize, 'c') + kExampleComDomain + ".";
174 const std::string kCnameD = std::string(kMaxmiumLabelSize, 'd') + kExampleComDomain + ".";
175 const std::vector<DnsRecord> kLargeCnameChainRecords = {
176         {kHelloExampleCom, ns_type::ns_t_cname, kCnameA},
177         {kCnameA, ns_type::ns_t_cname, kCnameB},
178         {kCnameB, ns_type::ns_t_cname, kCnameC},
179         {kCnameC, ns_type::ns_t_cname, kCnameD},
180         {kCnameD, ns_type::ns_t_a, kHelloExampleComAddrV4},
181 };
182 
183 // TODO: Integrate GetNumQueries relevent functions
184 size_t GetNumQueries(const test::DNSResponder& dns, const char* name);
185 size_t GetNumQueriesForProtocol(const test::DNSResponder& dns, const int protocol,
186                                 const char* name);
187 size_t GetNumQueriesForType(const test::DNSResponder& dns, ns_type type, const char* name);
188 std::string ToString(const hostent* he);
189 std::string ToString(const addrinfo* ai);
190 std::string ToString(const android::netdutils::ScopedAddrinfo& ai);
191 std::string ToString(const sockaddr_storage* addr);
192 std::vector<std::string> ToStrings(const hostent* he);
193 std::vector<std::string> ToStrings(const addrinfo* ai);
194 std::vector<std::string> ToStrings(const android::netdutils::ScopedAddrinfo& ai);
195 
196 // Wait for |condition| to be met until |timeout|.
197 bool PollForCondition(const std::function<bool()>& condition,
198                       std::chrono::milliseconds timeout = std::chrono::milliseconds(1000));
199