netd.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2018 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 "netd.h"
  17. #include <linux/bpf.h>
  18. #include <linux/if_packet.h>
  19. SEC("cgroupskb/ingress/stats")
  20. int bpf_cgroup_ingress(struct __sk_buff* skb) {
  21. return bpf_traffic_account(skb, BPF_INGRESS);
  22. }
  23. SEC("cgroupskb/egress/stats")
  24. int bpf_cgroup_egress(struct __sk_buff* skb) {
  25. return bpf_traffic_account(skb, BPF_EGRESS);
  26. }
  27. SEC("skfilter/egress/xtbpf")
  28. int xt_bpf_egress_prog(struct __sk_buff* skb) {
  29. uint32_t key = skb->ifindex;
  30. update_iface_stats_map(skb, BPF_EGRESS, &key);
  31. return BPF_MATCH;
  32. }
  33. SEC("skfilter/ingress/xtbpf")
  34. int xt_bpf_ingress_prog(struct __sk_buff* skb) {
  35. uint32_t key = skb->ifindex;
  36. update_iface_stats_map(skb, BPF_INGRESS, &key);
  37. return BPF_MATCH;
  38. }
  39. SEC("skfilter/whitelist/xtbpf")
  40. int xt_bpf_whitelist_prog(struct __sk_buff* skb) {
  41. uint32_t sock_uid = bpf_get_socket_uid(skb);
  42. if (is_system_uid(sock_uid)) return BPF_MATCH;
  43. // 65534 is the overflow 'nobody' uid, usually this being returned means
  44. // that skb->sk is NULL during RX (early decap socket lookup failure),
  45. // which commonly happens for incoming packets to an unconnected udp socket.
  46. // Additionally bpf_get_socket_cookie() returns 0 if skb->sk is NULL
  47. if ((sock_uid == 65534) && !bpf_get_socket_cookie(skb) &&
  48. (skb->pkt_type == PACKET_HOST || skb->pkt_type == PACKET_BROADCAST ||
  49. skb->pkt_type == PACKET_MULTICAST))
  50. return BPF_MATCH;
  51. UidOwnerValue* whitelistMatch = bpf_uid_owner_map_lookup_elem(&sock_uid);
  52. if (whitelistMatch) return whitelistMatch->rule & HAPPY_BOX_MATCH ? BPF_MATCH : BPF_NOMATCH;
  53. return BPF_NOMATCH;
  54. }
  55. SEC("skfilter/blacklist/xtbpf")
  56. int xt_bpf_blacklist_prog(struct __sk_buff* skb) {
  57. uint32_t sock_uid = bpf_get_socket_uid(skb);
  58. UidOwnerValue* blacklistMatch = bpf_uid_owner_map_lookup_elem(&sock_uid);
  59. if (blacklistMatch) return blacklistMatch->rule & PENALTY_BOX_MATCH ? BPF_MATCH : BPF_NOMATCH;
  60. return BPF_NOMATCH;
  61. }
  62. DEFINE_BPF_MAP(uid_permission_map, HASH, uint32_t, uint8_t, UID_OWNER_MAP_SIZE)
  63. SEC("cgroupsock/inet/create")
  64. int inet_socket_create(struct bpf_sock* sk) {
  65. uint64_t gid_uid = bpf_get_current_uid_gid();
  66. /*
  67. * A given app is guaranteed to have the same app ID in all the profiles in
  68. * which it is installed, and install permission is granted to app for all
  69. * user at install time so we only check the appId part of a request uid at
  70. * run time. See UserHandle#isSameApp for detail.
  71. */
  72. uint32_t appId = (gid_uid & 0xffffffff) % PER_USER_RANGE;
  73. uint8_t* permissions = bpf_uid_permission_map_lookup_elem(&appId);
  74. if (!permissions) {
  75. // UID not in map. Default to just INTERNET permission.
  76. return 1;
  77. }
  78. // A return value of 1 means allow, everything else means deny.
  79. return (*permissions & BPF_PERMISSION_INTERNET) == BPF_PERMISSION_INTERNET;
  80. }
  81. char _license[] SEC("license") = "Apache 2.0";