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