1 /*
2 * Copyright (C) 2016 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 <errno.h>
18 #include <netdb.h>
19 #include <string.h>
20 #include <netinet/in.h>
21 #include <netinet/tcp.h>
22 #include <sys/socket.h>
23 #include <sys/uio.h>
24
25 #include <linux/netlink.h>
26 #include <linux/sock_diag.h>
27 #include <linux/inet_diag.h>
28
29 #define LOG_TAG "Netd"
30
31 #include <android-base/strings.h>
32 #include <cutils/log.h>
33
34 #include "NetdConstants.h"
35 #include "SockDiag.h"
36
37 #include <chrono>
38
39 #ifndef SOCK_DESTROY
40 #define SOCK_DESTROY 21
41 #endif
42
43 namespace {
44
checkError(int fd)45 int checkError(int fd) {
46 struct {
47 nlmsghdr h;
48 nlmsgerr err;
49 } __attribute__((__packed__)) ack;
50 ssize_t bytesread = recv(fd, &ack, sizeof(ack), MSG_DONTWAIT | MSG_PEEK);
51 if (bytesread == -1) {
52 // Read failed (error), or nothing to read (good).
53 return (errno == EAGAIN) ? 0 : -errno;
54 } else if (bytesread == (ssize_t) sizeof(ack) && ack.h.nlmsg_type == NLMSG_ERROR) {
55 // We got an error. Consume it.
56 recv(fd, &ack, sizeof(ack), 0);
57 return ack.err.error;
58 } else {
59 // The kernel replied with something. Leave it to the caller.
60 return 0;
61 }
62 }
63
64 } // namespace
65
open()66 bool SockDiag::open() {
67 if (hasSocks()) {
68 return false;
69 }
70
71 mSock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_INET_DIAG);
72 mWriteSock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_INET_DIAG);
73 if (!hasSocks()) {
74 closeSocks();
75 return false;
76 }
77
78 sockaddr_nl nl = { .nl_family = AF_NETLINK };
79 if ((connect(mSock, reinterpret_cast<sockaddr *>(&nl), sizeof(nl)) == -1) ||
80 (connect(mWriteSock, reinterpret_cast<sockaddr *>(&nl), sizeof(nl)) == -1)) {
81 closeSocks();
82 return false;
83 }
84
85 return true;
86 }
87
sendDumpRequest(uint8_t proto,uint8_t family,uint32_t states,iovec * iov,int iovcnt)88 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states,
89 iovec *iov, int iovcnt) {
90 struct {
91 nlmsghdr nlh;
92 inet_diag_req_v2 req;
93 } __attribute__((__packed__)) request = {
94 .nlh = {
95 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
96 .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
97 },
98 .req = {
99 .sdiag_family = family,
100 .sdiag_protocol = proto,
101 .idiag_states = states,
102 },
103 };
104
105 size_t len = 0;
106 iov[0].iov_base = &request;
107 iov[0].iov_len = sizeof(request);
108 for (int i = 0; i < iovcnt; i++) {
109 len += iov[i].iov_len;
110 }
111 request.nlh.nlmsg_len = len;
112
113 if (writev(mSock, iov, iovcnt) != (ssize_t) len) {
114 return -errno;
115 }
116
117 return checkError(mSock);
118 }
119
sendDumpRequest(uint8_t proto,uint8_t family,uint32_t states)120 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states) {
121 iovec iov[] = {
122 { nullptr, 0 },
123 };
124 return sendDumpRequest(proto, family, states, iov, ARRAY_SIZE(iov));
125 }
126
sendDumpRequest(uint8_t proto,uint8_t family,const char * addrstr)127 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr) {
128 addrinfo hints = { .ai_flags = AI_NUMERICHOST };
129 addrinfo *res;
130 in6_addr mapped = { .s6_addr32 = { 0, 0, htonl(0xffff), 0 } };
131 int ret;
132
133 // TODO: refactor the netlink parsing code out of system/core, bring it into netd, and stop
134 // doing string conversions when they're not necessary.
135 if ((ret = getaddrinfo(addrstr, nullptr, &hints, &res)) != 0) {
136 return -EINVAL;
137 }
138
139 // So we don't have to call freeaddrinfo on every failure path.
140 ScopedAddrinfo resP(res);
141
142 void *addr;
143 uint8_t addrlen;
144 if (res->ai_family == AF_INET && family == AF_INET) {
145 in_addr& ina = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr;
146 addr = &ina;
147 addrlen = sizeof(ina);
148 } else if (res->ai_family == AF_INET && family == AF_INET6) {
149 in_addr& ina = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr;
150 mapped.s6_addr32[3] = ina.s_addr;
151 addr = &mapped;
152 addrlen = sizeof(mapped);
153 } else if (res->ai_family == AF_INET6 && family == AF_INET6) {
154 in6_addr& in6a = reinterpret_cast<sockaddr_in6*>(res->ai_addr)->sin6_addr;
155 addr = &in6a;
156 addrlen = sizeof(in6a);
157 } else {
158 return -EAFNOSUPPORT;
159 }
160
161 uint8_t prefixlen = addrlen * 8;
162 uint8_t yesjump = sizeof(inet_diag_bc_op) + sizeof(inet_diag_hostcond) + addrlen;
163 uint8_t nojump = yesjump + 4;
164
165 struct {
166 nlattr nla;
167 inet_diag_bc_op op;
168 inet_diag_hostcond cond;
169 } __attribute__((__packed__)) attrs = {
170 .nla = {
171 .nla_type = INET_DIAG_REQ_BYTECODE,
172 },
173 .op = {
174 INET_DIAG_BC_S_COND,
175 yesjump,
176 nojump,
177 },
178 .cond = {
179 family,
180 prefixlen,
181 -1,
182 {}
183 },
184 };
185
186 attrs.nla.nla_len = sizeof(attrs) + addrlen;
187
188 iovec iov[] = {
189 { nullptr, 0 },
190 { &attrs, sizeof(attrs) },
191 { addr, addrlen },
192 };
193
194 uint32_t states = ~(1 << TCP_TIME_WAIT);
195 return sendDumpRequest(proto, family, states, iov, ARRAY_SIZE(iov));
196 }
197
readDiagMsg(uint8_t proto,SockDiag::DumpCallback callback)198 int SockDiag::readDiagMsg(uint8_t proto, SockDiag::DumpCallback callback) {
199 char buf[kBufferSize];
200
201 ssize_t bytesread;
202 do {
203 bytesread = read(mSock, buf, sizeof(buf));
204
205 if (bytesread < 0) {
206 return -errno;
207 }
208
209 uint32_t len = bytesread;
210 for (nlmsghdr *nlh = reinterpret_cast<nlmsghdr *>(buf);
211 NLMSG_OK(nlh, len);
212 nlh = NLMSG_NEXT(nlh, len)) {
213 switch (nlh->nlmsg_type) {
214 case NLMSG_DONE:
215 callback(proto, NULL);
216 return 0;
217 case NLMSG_ERROR: {
218 nlmsgerr *err = reinterpret_cast<nlmsgerr *>(NLMSG_DATA(nlh));
219 return err->error;
220 }
221 default:
222 inet_diag_msg *msg = reinterpret_cast<inet_diag_msg *>(NLMSG_DATA(nlh));
223 if (callback(proto, msg)) {
224 sockDestroy(proto, msg);
225 }
226 }
227 }
228 } while (bytesread > 0);
229
230 return 0;
231 }
232
233 // Determines whether a socket is a loopback socket. Does not check socket state.
isLoopbackSocket(const inet_diag_msg * msg)234 bool SockDiag::isLoopbackSocket(const inet_diag_msg *msg) {
235 switch (msg->idiag_family) {
236 case AF_INET:
237 // Old kernels only copy the IPv4 address and leave the other 12 bytes uninitialized.
238 return IN_LOOPBACK(htonl(msg->id.idiag_src[0])) ||
239 IN_LOOPBACK(htonl(msg->id.idiag_dst[0])) ||
240 msg->id.idiag_src[0] == msg->id.idiag_dst[0];
241
242 case AF_INET6: {
243 const struct in6_addr *src = (const struct in6_addr *) &msg->id.idiag_src;
244 const struct in6_addr *dst = (const struct in6_addr *) &msg->id.idiag_dst;
245 return (IN6_IS_ADDR_V4MAPPED(src) && IN_LOOPBACK(src->s6_addr32[3])) ||
246 (IN6_IS_ADDR_V4MAPPED(dst) && IN_LOOPBACK(dst->s6_addr32[3])) ||
247 IN6_IS_ADDR_LOOPBACK(src) || IN6_IS_ADDR_LOOPBACK(dst) ||
248 !memcmp(src, dst, sizeof(*src));
249 }
250 default:
251 return false;
252 }
253 }
254
sockDestroy(uint8_t proto,const inet_diag_msg * msg)255 int SockDiag::sockDestroy(uint8_t proto, const inet_diag_msg *msg) {
256 if (msg == nullptr) {
257 return 0;
258 }
259
260 DestroyRequest request = {
261 .nlh = {
262 .nlmsg_type = SOCK_DESTROY,
263 .nlmsg_flags = NLM_F_REQUEST,
264 },
265 .req = {
266 .sdiag_family = msg->idiag_family,
267 .sdiag_protocol = proto,
268 .idiag_states = (uint32_t) (1 << msg->idiag_state),
269 .id = msg->id,
270 },
271 };
272 request.nlh.nlmsg_len = sizeof(request);
273
274 if (write(mWriteSock, &request, sizeof(request)) < (ssize_t) sizeof(request)) {
275 return -errno;
276 }
277
278 int ret = checkError(mWriteSock);
279 if (!ret) mSocketsDestroyed++;
280 return ret;
281 }
282
destroySockets(uint8_t proto,int family,const char * addrstr)283 int SockDiag::destroySockets(uint8_t proto, int family, const char *addrstr) {
284 if (!hasSocks()) {
285 return -EBADFD;
286 }
287
288 if (int ret = sendDumpRequest(proto, family, addrstr)) {
289 return ret;
290 }
291
292 auto destroyAll = [] (uint8_t, const inet_diag_msg*) { return true; };
293
294 return readDiagMsg(proto, destroyAll);
295 }
296
destroySockets(const char * addrstr)297 int SockDiag::destroySockets(const char *addrstr) {
298 Stopwatch s;
299 mSocketsDestroyed = 0;
300
301 if (!strchr(addrstr, ':')) {
302 if (int ret = destroySockets(IPPROTO_TCP, AF_INET, addrstr)) {
303 ALOGE("Failed to destroy IPv4 sockets on %s: %s", addrstr, strerror(-ret));
304 return ret;
305 }
306 }
307 if (int ret = destroySockets(IPPROTO_TCP, AF_INET6, addrstr)) {
308 ALOGE("Failed to destroy IPv6 sockets on %s: %s", addrstr, strerror(-ret));
309 return ret;
310 }
311
312 if (mSocketsDestroyed > 0) {
313 ALOGI("Destroyed %d sockets on %s in %.1f ms", mSocketsDestroyed, addrstr, s.timeTaken());
314 }
315
316 return mSocketsDestroyed;
317 }
318
destroyLiveSockets(DumpCallback destroyFilter)319 int SockDiag::destroyLiveSockets(DumpCallback destroyFilter) {
320 int proto = IPPROTO_TCP;
321
322 for (const int family : {AF_INET, AF_INET6}) {
323 const char *familyName = (family == AF_INET) ? "IPv4" : "IPv6";
324 uint32_t states = (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV);
325 if (int ret = sendDumpRequest(proto, family, states)) {
326 ALOGE("Failed to dump %s sockets for UID: %s", familyName, strerror(-ret));
327 return ret;
328 }
329 if (int ret = readDiagMsg(proto, destroyFilter)) {
330 ALOGE("Failed to destroy %s sockets for UID: %s", familyName, strerror(-ret));
331 return ret;
332 }
333 }
334
335 return 0;
336 }
337
destroySockets(uint8_t proto,const uid_t uid,bool excludeLoopback)338 int SockDiag::destroySockets(uint8_t proto, const uid_t uid, bool excludeLoopback) {
339 mSocketsDestroyed = 0;
340 Stopwatch s;
341
342 auto shouldDestroy = [uid, excludeLoopback] (uint8_t, const inet_diag_msg *msg) {
343 return msg != nullptr &&
344 msg->idiag_uid == uid &&
345 !(excludeLoopback && isLoopbackSocket(msg));
346 };
347
348 for (const int family : {AF_INET, AF_INET6}) {
349 const char *familyName = family == AF_INET ? "IPv4" : "IPv6";
350 uint32_t states = (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV);
351 if (int ret = sendDumpRequest(proto, family, states)) {
352 ALOGE("Failed to dump %s sockets for UID: %s", familyName, strerror(-ret));
353 return ret;
354 }
355 if (int ret = readDiagMsg(proto, shouldDestroy)) {
356 ALOGE("Failed to destroy %s sockets for UID: %s", familyName, strerror(-ret));
357 return ret;
358 }
359 }
360
361 if (mSocketsDestroyed > 0) {
362 ALOGI("Destroyed %d sockets for UID in %.1f ms", mSocketsDestroyed, s.timeTaken());
363 }
364
365 return 0;
366 }
367
destroySockets(const UidRanges & uidRanges,const std::set<uid_t> & skipUids,bool excludeLoopback)368 int SockDiag::destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids,
369 bool excludeLoopback) {
370 mSocketsDestroyed = 0;
371 Stopwatch s;
372
373 auto shouldDestroy = [&] (uint8_t, const inet_diag_msg *msg) {
374 return msg != nullptr &&
375 uidRanges.hasUid(msg->idiag_uid) &&
376 skipUids.find(msg->idiag_uid) == skipUids.end() &&
377 !(excludeLoopback && isLoopbackSocket(msg));
378 };
379
380 if (int ret = destroyLiveSockets(shouldDestroy)) {
381 return ret;
382 }
383
384 std::vector<uid_t> skipUidStrings;
385 for (uid_t uid : skipUids) {
386 skipUidStrings.push_back(uid);
387 }
388 std::sort(skipUidStrings.begin(), skipUidStrings.end());
389
390 if (mSocketsDestroyed > 0) {
391 ALOGI("Destroyed %d sockets for %s skip={%s} in %.1f ms",
392 mSocketsDestroyed, uidRanges.toString().c_str(),
393 android::base::Join(skipUidStrings, " ").c_str(), s.timeTaken());
394 }
395
396 return 0;
397 }
398