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