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