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