• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Fwmark.h"
35 #include "NetdConstants.h"
36 #include "Permission.h"
37 #include "SockDiag.h"
38 #include "Stopwatch.h"
39 
40 #include <chrono>
41 
42 #ifndef SOCK_DESTROY
43 #define SOCK_DESTROY 21
44 #endif
45 
46 #define INET_DIAG_BC_MARK_COND 10
47 
48 namespace android {
49 namespace net {
50 
51 namespace {
52 
checkError(int fd)53 int checkError(int fd) {
54     struct {
55         nlmsghdr h;
56         nlmsgerr err;
57     } __attribute__((__packed__)) ack;
58     ssize_t bytesread = recv(fd, &ack, sizeof(ack), MSG_DONTWAIT | MSG_PEEK);
59     if (bytesread == -1) {
60        // Read failed (error), or nothing to read (good).
61        return (errno == EAGAIN) ? 0 : -errno;
62     } else if (bytesread == (ssize_t) sizeof(ack) && ack.h.nlmsg_type == NLMSG_ERROR) {
63         // We got an error. Consume it.
64         recv(fd, &ack, sizeof(ack), 0);
65         return ack.err.error;
66     } else {
67         // The kernel replied with something. Leave it to the caller.
68         return 0;
69     }
70 }
71 
72 }  // namespace
73 
open()74 bool SockDiag::open() {
75     if (hasSocks()) {
76         return false;
77     }
78 
79     mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_INET_DIAG);
80     mWriteSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_INET_DIAG);
81     if (!hasSocks()) {
82         closeSocks();
83         return false;
84     }
85 
86     sockaddr_nl nl = { .nl_family = AF_NETLINK };
87     if ((connect(mSock, reinterpret_cast<sockaddr *>(&nl), sizeof(nl)) == -1) ||
88         (connect(mWriteSock, reinterpret_cast<sockaddr *>(&nl), sizeof(nl)) == -1)) {
89         closeSocks();
90         return false;
91     }
92 
93     return true;
94 }
95 
sendDumpRequest(uint8_t proto,uint8_t family,uint32_t states,iovec * iov,int iovcnt)96 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states,
97                               iovec *iov, int iovcnt) {
98     struct {
99         nlmsghdr nlh;
100         inet_diag_req_v2 req;
101     } __attribute__((__packed__)) request = {
102         .nlh = {
103             .nlmsg_type = SOCK_DIAG_BY_FAMILY,
104             .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
105         },
106         .req = {
107             .sdiag_family = family,
108             .sdiag_protocol = proto,
109             .idiag_states = states,
110         },
111     };
112 
113     size_t len = 0;
114     iov[0].iov_base = &request;
115     iov[0].iov_len = sizeof(request);
116     for (int i = 0; i < iovcnt; i++) {
117         len += iov[i].iov_len;
118     }
119     request.nlh.nlmsg_len = len;
120 
121     if (writev(mSock, iov, iovcnt) != (ssize_t) len) {
122         return -errno;
123     }
124 
125     return checkError(mSock);
126 }
127 
sendDumpRequest(uint8_t proto,uint8_t family,uint32_t states)128 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states) {
129     iovec iov[] = {
130         { nullptr, 0 },
131     };
132     return sendDumpRequest(proto, family, states, iov, ARRAY_SIZE(iov));
133 }
134 
sendDumpRequest(uint8_t proto,uint8_t family,const char * addrstr)135 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr) {
136     addrinfo hints = { .ai_flags = AI_NUMERICHOST };
137     addrinfo *res;
138     in6_addr mapped = { .s6_addr32 = { 0, 0, htonl(0xffff), 0 } };
139     int ret;
140 
141     // TODO: refactor the netlink parsing code out of system/core, bring it into netd, and stop
142     // doing string conversions when they're not necessary.
143     if ((ret = getaddrinfo(addrstr, nullptr, &hints, &res)) != 0) {
144         return -EINVAL;
145     }
146 
147     // So we don't have to call freeaddrinfo on every failure path.
148     ScopedAddrinfo resP(res);
149 
150     void *addr;
151     uint8_t addrlen;
152     if (res->ai_family == AF_INET && family == AF_INET) {
153         in_addr& ina = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr;
154         addr = &ina;
155         addrlen = sizeof(ina);
156     } else if (res->ai_family == AF_INET && family == AF_INET6) {
157         in_addr& ina = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr;
158         mapped.s6_addr32[3] = ina.s_addr;
159         addr = &mapped;
160         addrlen = sizeof(mapped);
161     } else if (res->ai_family == AF_INET6 && family == AF_INET6) {
162         in6_addr& in6a = reinterpret_cast<sockaddr_in6*>(res->ai_addr)->sin6_addr;
163         addr = &in6a;
164         addrlen = sizeof(in6a);
165     } else {
166         return -EAFNOSUPPORT;
167     }
168 
169     uint8_t prefixlen = addrlen * 8;
170     uint8_t yesjump = sizeof(inet_diag_bc_op) + sizeof(inet_diag_hostcond) + addrlen;
171     uint8_t nojump = yesjump + 4;
172 
173     struct {
174         nlattr nla;
175         inet_diag_bc_op op;
176         inet_diag_hostcond cond;
177     } __attribute__((__packed__)) attrs = {
178         .nla = {
179             .nla_type = INET_DIAG_REQ_BYTECODE,
180         },
181         .op = {
182             INET_DIAG_BC_S_COND,
183             yesjump,
184             nojump,
185         },
186         .cond = {
187             family,
188             prefixlen,
189             -1,
190             {}
191         },
192     };
193 
194     attrs.nla.nla_len = sizeof(attrs) + addrlen;
195 
196     iovec iov[] = {
197         { nullptr,           0 },
198         { &attrs,            sizeof(attrs) },
199         { addr,              addrlen },
200     };
201 
202     uint32_t states = ~(1 << TCP_TIME_WAIT);
203     return sendDumpRequest(proto, family, states, iov, ARRAY_SIZE(iov));
204 }
205 
readDiagMsg(uint8_t proto,const SockDiag::DestroyFilter & shouldDestroy)206 int SockDiag::readDiagMsg(uint8_t proto, const SockDiag::DestroyFilter& shouldDestroy) {
207     NetlinkDumpCallback callback = [this, proto, shouldDestroy] (nlmsghdr *nlh) {
208         const inet_diag_msg *msg = reinterpret_cast<inet_diag_msg *>(NLMSG_DATA(nlh));
209         if (shouldDestroy(proto, msg)) {
210             sockDestroy(proto, msg);
211         }
212     };
213 
214     return processNetlinkDump(mSock, callback);
215 }
216 
217 // Determines whether a socket is a loopback socket. Does not check socket state.
isLoopbackSocket(const inet_diag_msg * msg)218 bool SockDiag::isLoopbackSocket(const inet_diag_msg *msg) {
219     switch (msg->idiag_family) {
220         case AF_INET:
221             // Old kernels only copy the IPv4 address and leave the other 12 bytes uninitialized.
222             return IN_LOOPBACK(htonl(msg->id.idiag_src[0])) ||
223                    IN_LOOPBACK(htonl(msg->id.idiag_dst[0])) ||
224                    msg->id.idiag_src[0] == msg->id.idiag_dst[0];
225 
226         case AF_INET6: {
227             const struct in6_addr *src = (const struct in6_addr *) &msg->id.idiag_src;
228             const struct in6_addr *dst = (const struct in6_addr *) &msg->id.idiag_dst;
229             return (IN6_IS_ADDR_V4MAPPED(src) && IN_LOOPBACK(src->s6_addr32[3])) ||
230                    (IN6_IS_ADDR_V4MAPPED(dst) && IN_LOOPBACK(dst->s6_addr32[3])) ||
231                    IN6_IS_ADDR_LOOPBACK(src) || IN6_IS_ADDR_LOOPBACK(dst) ||
232                    !memcmp(src, dst, sizeof(*src));
233         }
234         default:
235             return false;
236     }
237 }
238 
sockDestroy(uint8_t proto,const inet_diag_msg * msg)239 int SockDiag::sockDestroy(uint8_t proto, const inet_diag_msg *msg) {
240     if (msg == nullptr) {
241        return 0;
242     }
243 
244     DestroyRequest request = {
245         .nlh = {
246             .nlmsg_type = SOCK_DESTROY,
247             .nlmsg_flags = NLM_F_REQUEST,
248         },
249         .req = {
250             .sdiag_family = msg->idiag_family,
251             .sdiag_protocol = proto,
252             .idiag_states = (uint32_t) (1 << msg->idiag_state),
253             .id = msg->id,
254         },
255     };
256     request.nlh.nlmsg_len = sizeof(request);
257 
258     if (write(mWriteSock, &request, sizeof(request)) < (ssize_t) sizeof(request)) {
259         return -errno;
260     }
261 
262     int ret = checkError(mWriteSock);
263     if (!ret) mSocketsDestroyed++;
264     return ret;
265 }
266 
destroySockets(uint8_t proto,int family,const char * addrstr)267 int SockDiag::destroySockets(uint8_t proto, int family, const char *addrstr) {
268     if (!hasSocks()) {
269         return -EBADFD;
270     }
271 
272     if (int ret = sendDumpRequest(proto, family, addrstr)) {
273         return ret;
274     }
275 
276     auto destroyAll = [] (uint8_t, const inet_diag_msg*) { return true; };
277 
278     return readDiagMsg(proto, destroyAll);
279 }
280 
destroySockets(const char * addrstr)281 int SockDiag::destroySockets(const char *addrstr) {
282     Stopwatch s;
283     mSocketsDestroyed = 0;
284 
285     if (!strchr(addrstr, ':')) {
286         if (int ret = destroySockets(IPPROTO_TCP, AF_INET, addrstr)) {
287             ALOGE("Failed to destroy IPv4 sockets on %s: %s", addrstr, strerror(-ret));
288             return ret;
289         }
290     }
291     if (int ret = destroySockets(IPPROTO_TCP, AF_INET6, addrstr)) {
292         ALOGE("Failed to destroy IPv6 sockets on %s: %s", addrstr, strerror(-ret));
293         return ret;
294     }
295 
296     if (mSocketsDestroyed > 0) {
297         ALOGI("Destroyed %d sockets on %s in %.1f ms", mSocketsDestroyed, addrstr, s.timeTaken());
298     }
299 
300     return mSocketsDestroyed;
301 }
302 
destroyLiveSockets(DestroyFilter destroyFilter,const char * what,iovec * iov,int iovcnt)303 int SockDiag::destroyLiveSockets(DestroyFilter destroyFilter, const char *what,
304                                  iovec *iov, int iovcnt) {
305     int proto = IPPROTO_TCP;
306 
307     for (const int family : {AF_INET, AF_INET6}) {
308         const char *familyName = (family == AF_INET) ? "IPv4" : "IPv6";
309         uint32_t states = (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV);
310         if (int ret = sendDumpRequest(proto, family, states, iov, iovcnt)) {
311             ALOGE("Failed to dump %s sockets for %s: %s", familyName, what, strerror(-ret));
312             return ret;
313         }
314         if (int ret = readDiagMsg(proto, destroyFilter)) {
315             ALOGE("Failed to destroy %s sockets for %s: %s", familyName, what, strerror(-ret));
316             return ret;
317         }
318     }
319 
320     return 0;
321 }
322 
destroySockets(uint8_t proto,const uid_t uid,bool excludeLoopback)323 int SockDiag::destroySockets(uint8_t proto, const uid_t uid, bool excludeLoopback) {
324     mSocketsDestroyed = 0;
325     Stopwatch s;
326 
327     auto shouldDestroy = [uid, excludeLoopback] (uint8_t, const inet_diag_msg *msg) {
328         return msg != nullptr &&
329                msg->idiag_uid == uid &&
330                !(excludeLoopback && isLoopbackSocket(msg));
331     };
332 
333     for (const int family : {AF_INET, AF_INET6}) {
334         const char *familyName = family == AF_INET ? "IPv4" : "IPv6";
335         uint32_t states = (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV);
336         if (int ret = sendDumpRequest(proto, family, states)) {
337             ALOGE("Failed to dump %s sockets for UID: %s", familyName, strerror(-ret));
338             return ret;
339         }
340         if (int ret = readDiagMsg(proto, shouldDestroy)) {
341             ALOGE("Failed to destroy %s sockets for UID: %s", familyName, strerror(-ret));
342             return ret;
343         }
344     }
345 
346     if (mSocketsDestroyed > 0) {
347         ALOGI("Destroyed %d sockets for UID in %.1f ms", mSocketsDestroyed, s.timeTaken());
348     }
349 
350     return 0;
351 }
352 
destroySockets(const UidRanges & uidRanges,const std::set<uid_t> & skipUids,bool excludeLoopback)353 int SockDiag::destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids,
354                              bool excludeLoopback) {
355     mSocketsDestroyed = 0;
356     Stopwatch s;
357 
358     auto shouldDestroy = [&] (uint8_t, const inet_diag_msg *msg) {
359         return msg != nullptr &&
360                uidRanges.hasUid(msg->idiag_uid) &&
361                skipUids.find(msg->idiag_uid) == skipUids.end() &&
362                !(excludeLoopback && isLoopbackSocket(msg));
363     };
364 
365     iovec iov[] = {
366         { nullptr, 0 },
367     };
368 
369     if (int ret = destroyLiveSockets(shouldDestroy, "UID", iov, ARRAY_SIZE(iov))) {
370         return ret;
371     }
372 
373     std::vector<uid_t> skipUidStrings;
374     for (uid_t uid : skipUids) {
375         skipUidStrings.push_back(uid);
376     }
377     std::sort(skipUidStrings.begin(), skipUidStrings.end());
378 
379     if (mSocketsDestroyed > 0) {
380         ALOGI("Destroyed %d sockets for %s skip={%s} in %.1f ms",
381               mSocketsDestroyed, uidRanges.toString().c_str(),
382               android::base::Join(skipUidStrings, " ").c_str(), s.timeTaken());
383     }
384 
385     return 0;
386 }
387 
388 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets on the specified netId where:
389 // 1. The opening app no longer has permission to use this network, or:
390 // 2. The opening app does have permission, but did not explicitly select this network.
391 //
392 // We destroy sockets without the explicit bit because we want to avoid the situation where a
393 // privileged app uses its privileges without knowing it is doing so. For example, a privileged app
394 // might have opened a socket on this network just because it was the default network at the
395 // time. If we don't kill these sockets, those apps could continue to use them without realizing
396 // that they are now sending and receiving traffic on a network that is now restricted.
destroySocketsLackingPermission(unsigned netId,Permission permission,bool excludeLoopback)397 int SockDiag::destroySocketsLackingPermission(unsigned netId, Permission permission,
398                                               bool excludeLoopback) {
399     struct markmatch {
400         inet_diag_bc_op op;
401         // TODO: switch to inet_diag_markcond
402         __u32 mark;
403         __u32 mask;
404     } __attribute__((packed));
405     constexpr uint8_t matchlen = sizeof(markmatch);
406 
407     Fwmark netIdMark, netIdMask;
408     netIdMark.netId = netId;
409     netIdMask.netId = 0xffff;
410 
411     Fwmark controlMark;
412     controlMark.explicitlySelected = true;
413     controlMark.permission = permission;
414 
415     // A SOCK_DIAG bytecode program that accepts the sockets we intend to destroy.
416     struct bytecode {
417         markmatch netIdMatch;
418         markmatch controlMatch;
419         inet_diag_bc_op controlJump;
420     } __attribute__((packed)) bytecode;
421 
422     // The length of the INET_DIAG_BC_JMP instruction.
423     constexpr uint8_t jmplen = sizeof(inet_diag_bc_op);
424     // Jump exactly this far past the end of the program to reject.
425     constexpr uint8_t rejectoffset = sizeof(inet_diag_bc_op);
426     // Total length of the program.
427     constexpr uint8_t bytecodelen = sizeof(bytecode);
428 
429     bytecode = (struct bytecode) {
430         // If netId matches, continue, otherwise, reject (i.e., leave socket alone).
431         { { INET_DIAG_BC_MARK_COND, matchlen, bytecodelen + rejectoffset },
432           netIdMark.intValue, netIdMask.intValue },
433 
434         // If explicit and permission bits match, go to the JMP below which rejects the socket
435         // (i.e., we leave it alone). Otherwise, jump to the end of the program, which accepts the
436         // socket (so we destroy it).
437         { { INET_DIAG_BC_MARK_COND, matchlen, matchlen + jmplen },
438           controlMark.intValue, controlMark.intValue },
439 
440         // This JMP unconditionally rejects the packet by jumping to the reject target. It is
441         // necessary to keep the kernel bytecode verifier happy. If we don't have a JMP the bytecode
442         // is invalid because the target of every no jump must always be reachable by yes jumps.
443         // Without this JMP, the accept target is not reachable by yes jumps and the program will
444         // be rejected by the validator.
445         { INET_DIAG_BC_JMP, jmplen, jmplen + rejectoffset },
446 
447         // We have reached the end of the program. Accept the socket, and destroy it below.
448     };
449 
450     struct nlattr nla = {
451         .nla_type = INET_DIAG_REQ_BYTECODE,
452         .nla_len = sizeof(struct nlattr) + bytecodelen,
453     };
454 
455     iovec iov[] = {
456         { nullptr,   0 },
457         { &nla,      sizeof(nla) },
458         { &bytecode, bytecodelen },
459     };
460 
461     mSocketsDestroyed = 0;
462     Stopwatch s;
463 
464     auto shouldDestroy = [&] (uint8_t, const inet_diag_msg *msg) {
465         return msg != nullptr && !(excludeLoopback && isLoopbackSocket(msg));
466     };
467 
468     if (int ret = destroyLiveSockets(shouldDestroy, "permission change", iov, ARRAY_SIZE(iov))) {
469         return ret;
470     }
471 
472     if (mSocketsDestroyed > 0) {
473         ALOGI("Destroyed %d sockets for netId %d permission=%d in %.1f ms",
474               mSocketsDestroyed, netId, permission, s.timeTaken());
475     }
476 
477     return 0;
478 }
479 
480 }  // namespace net
481 }  // namespace android
482