SockDiag.cpp 17 KB

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