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 
45 struct AddrinfoDeleter {
operator ()__anon9701bce90111::AddrinfoDeleter46   void operator()(addrinfo *a) { if (a) freeaddrinfo(a); }
47 };
48 
49 typedef std::unique_ptr<addrinfo, AddrinfoDeleter> ScopedAddrinfo;
50 
checkError(int fd)51 int checkError(int fd) {
52     struct {
53         nlmsghdr h;
54         nlmsgerr err;
55     } __attribute__((__packed__)) ack;
56     ssize_t bytesread = recv(fd, &ack, sizeof(ack), MSG_DONTWAIT | MSG_PEEK);
57     if (bytesread == -1) {
58        // Read failed (error), or nothing to read (good).
59        return (errno == EAGAIN) ? 0 : -errno;
60     } else if (bytesread == (ssize_t) sizeof(ack) && ack.h.nlmsg_type == NLMSG_ERROR) {
61         // We got an error. Consume it.
62         recv(fd, &ack, sizeof(ack), 0);
63         return ack.err.error;
64     } else {
65         // The kernel replied with something. Leave it to the caller.
66         return 0;
67     }
68 }
69 
70 }  // namespace
71 
open()72 bool SockDiag::open() {
73     if (hasSocks()) {
74         return false;
75     }
76 
77     mSock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_INET_DIAG);
78     mWriteSock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_INET_DIAG);
79     if (!hasSocks()) {
80         closeSocks();
81         return false;
82     }
83 
84     sockaddr_nl nl = { .nl_family = AF_NETLINK };
85     if ((connect(mSock, reinterpret_cast<sockaddr *>(&nl), sizeof(nl)) == -1) ||
86         (connect(mWriteSock, reinterpret_cast<sockaddr *>(&nl), sizeof(nl)) == -1)) {
87         closeSocks();
88         return false;
89     }
90 
91     return true;
92 }
93 
sendDumpRequest(uint8_t proto,uint8_t family,uint32_t states,iovec * iov,int iovcnt)94 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states,
95                               iovec *iov, int iovcnt) {
96     struct {
97         nlmsghdr nlh;
98         inet_diag_req_v2 req;
99     } __attribute__((__packed__)) request = {
100         .nlh = {
101             .nlmsg_type = SOCK_DIAG_BY_FAMILY,
102             .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
103         },
104         .req = {
105             .sdiag_family = family,
106             .sdiag_protocol = proto,
107             .idiag_states = states,
108         },
109     };
110 
111     size_t len = 0;
112     iov[0].iov_base = &request;
113     iov[0].iov_len = sizeof(request);
114     for (int i = 0; i < iovcnt; i++) {
115         len += iov[i].iov_len;
116     }
117     request.nlh.nlmsg_len = len;
118 
119     if (writev(mSock, iov, iovcnt) != (ssize_t) len) {
120         return -errno;
121     }
122 
123     return checkError(mSock);
124 }
125 
sendDumpRequest(uint8_t proto,uint8_t family,uint32_t states)126 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states) {
127     iovec iov[] = {
128         { nullptr, 0 },
129     };
130     return sendDumpRequest(proto, family, states, iov, ARRAY_SIZE(iov));
131 }
132 
sendDumpRequest(uint8_t proto,uint8_t family,const char * addrstr)133 int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr) {
134     addrinfo hints = { .ai_flags = AI_NUMERICHOST };
135     addrinfo *res;
136     in6_addr mapped = { .s6_addr32 = { 0, 0, htonl(0xffff), 0 } };
137     int ret;
138 
139     // TODO: refactor the netlink parsing code out of system/core, bring it into netd, and stop
140     // doing string conversions when they're not necessary.
141     if ((ret = getaddrinfo(addrstr, nullptr, &hints, &res)) != 0) {
142         return -EINVAL;
143     }
144 
145     // So we don't have to call freeaddrinfo on every failure path.
146     ScopedAddrinfo resP(res);
147 
148     void *addr;
149     uint8_t addrlen;
150     if (res->ai_family == AF_INET && family == AF_INET) {
151         in_addr& ina = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr;
152         addr = &ina;
153         addrlen = sizeof(ina);
154     } else if (res->ai_family == AF_INET && family == AF_INET6) {
155         in_addr& ina = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr;
156         mapped.s6_addr32[3] = ina.s_addr;
157         addr = &mapped;
158         addrlen = sizeof(mapped);
159     } else if (res->ai_family == AF_INET6 && family == AF_INET6) {
160         in6_addr& in6a = reinterpret_cast<sockaddr_in6*>(res->ai_addr)->sin6_addr;
161         addr = &in6a;
162         addrlen = sizeof(in6a);
163     } else {
164         return -EAFNOSUPPORT;
165     }
166 
167     uint8_t prefixlen = addrlen * 8;
168     uint8_t yesjump = sizeof(inet_diag_bc_op) + sizeof(inet_diag_hostcond) + addrlen;
169     uint8_t nojump = yesjump + 4;
170 
171     struct {
172         nlattr nla;
173         inet_diag_bc_op op;
174         inet_diag_hostcond cond;
175     } __attribute__((__packed__)) attrs = {
176         .nla = {
177             .nla_type = INET_DIAG_REQ_BYTECODE,
178         },
179         .op = {
180             INET_DIAG_BC_S_COND,
181             yesjump,
182             nojump,
183         },
184         .cond = {
185             family,
186             prefixlen,
187             -1,
188             {}
189         },
190     };
191 
192     attrs.nla.nla_len = sizeof(attrs) + addrlen;
193 
194     iovec iov[] = {
195         { nullptr, 0 },
196         { &attrs, sizeof(attrs) },
197         { addr, addrlen },
198     };
199 
200     uint32_t states = ~(1 << TCP_TIME_WAIT);
201     return sendDumpRequest(proto, family, states, iov, ARRAY_SIZE(iov));
202 }
203 
readDiagMsg(uint8_t proto,SockDiag::DumpCallback callback)204 int SockDiag::readDiagMsg(uint8_t proto, SockDiag::DumpCallback callback) {
205     char buf[kBufferSize];
206 
207     ssize_t bytesread;
208     do {
209         bytesread = read(mSock, buf, sizeof(buf));
210 
211         if (bytesread < 0) {
212             return -errno;
213         }
214 
215         uint32_t len = bytesread;
216         for (nlmsghdr *nlh = reinterpret_cast<nlmsghdr *>(buf);
217              NLMSG_OK(nlh, len);
218              nlh = NLMSG_NEXT(nlh, len)) {
219             switch (nlh->nlmsg_type) {
220               case NLMSG_DONE:
221                 callback(proto, NULL);
222                 return 0;
223               case NLMSG_ERROR: {
224                 nlmsgerr *err = reinterpret_cast<nlmsgerr *>(NLMSG_DATA(nlh));
225                 return err->error;
226               }
227               default:
228                 inet_diag_msg *msg = reinterpret_cast<inet_diag_msg *>(NLMSG_DATA(nlh));
229                 if (callback(proto, msg)) {
230                     sockDestroy(proto, msg);
231                 }
232             }
233         }
234     } while (bytesread > 0);
235 
236     return 0;
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(DumpCallback destroyFilter)303 int SockDiag::destroyLiveSockets(DumpCallback destroyFilter) {
304     int proto = IPPROTO_TCP;
305 
306     for (const int family : {AF_INET, AF_INET6}) {
307         const char *familyName = (family == AF_INET) ? "IPv4" : "IPv6";
308         uint32_t states = (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV);
309         if (int ret = sendDumpRequest(proto, family, states)) {
310             ALOGE("Failed to dump %s sockets for UID: %s", familyName, strerror(-ret));
311             return ret;
312         }
313         if (int ret = readDiagMsg(proto, destroyFilter)) {
314             ALOGE("Failed to destroy %s sockets for UID: %s", familyName, strerror(-ret));
315             return ret;
316         }
317     }
318 
319     return 0;
320 }
321 
destroySockets(uint8_t proto,const uid_t uid)322 int SockDiag::destroySockets(uint8_t proto, const uid_t uid) {
323     mSocketsDestroyed = 0;
324     Stopwatch s;
325 
326     auto shouldDestroy = [uid] (uint8_t, const inet_diag_msg *msg) {
327         return (msg != nullptr && msg->idiag_uid == uid);
328     };
329 
330     for (const int family : {AF_INET, AF_INET6}) {
331         const char *familyName = family == AF_INET ? "IPv4" : "IPv6";
332         uint32_t states = (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV);
333         if (int ret = sendDumpRequest(proto, family, states)) {
334             ALOGE("Failed to dump %s sockets for UID: %s", familyName, strerror(-ret));
335             return ret;
336         }
337         if (int ret = readDiagMsg(proto, shouldDestroy)) {
338             ALOGE("Failed to destroy %s sockets for UID: %s", familyName, strerror(-ret));
339             return ret;
340         }
341     }
342 
343     if (mSocketsDestroyed > 0) {
344         ALOGI("Destroyed %d sockets for UID in %.1f ms", mSocketsDestroyed, s.timeTaken());
345     }
346 
347     return 0;
348 }
349 
destroySockets(const UidRanges & uidRanges,const std::set<uid_t> & skipUids)350 int SockDiag::destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids) {
351     mSocketsDestroyed = 0;
352     Stopwatch s;
353 
354     auto shouldDestroy = [&] (uint8_t, const inet_diag_msg *msg) {
355         return msg != nullptr &&
356                uidRanges.hasUid(msg->idiag_uid) &&
357                skipUids.find(msg->idiag_uid) == skipUids.end();
358     };
359 
360     if (int ret = destroyLiveSockets(shouldDestroy)) {
361         return ret;
362     }
363 
364     std::vector<uid_t> skipUidStrings;
365     for (uid_t uid : skipUids) {
366         skipUidStrings.push_back(uid);
367     }
368     std::sort(skipUidStrings.begin(), skipUidStrings.end());
369 
370     if (mSocketsDestroyed > 0) {
371         ALOGI("Destroyed %d sockets for %s skip={%s} in %.1f ms",
372               mSocketsDestroyed, uidRanges.toString().c_str(),
373               android::base::Join(skipUidStrings, " ").c_str(), s.timeTaken());
374     }
375 
376     return 0;
377 }
378