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> |
| 19 | |
| 20 | SEC("cgroupskb/ingress/stats") |
| 21 | int bpf_cgroup_ingress(struct __sk_buff* skb) { |
| 22 | return bpf_traffic_account(skb, BPF_INGRESS); |
| 23 | } |
| 24 | |
| 25 | SEC("cgroupskb/egress/stats") |
| 26 | int bpf_cgroup_egress(struct __sk_buff* skb) { |
| 27 | return bpf_traffic_account(skb, BPF_EGRESS); |
| 28 | } |
| 29 | |
| 30 | SEC("skfilter/egress/xtbpf") |
| 31 | int xt_bpf_egress_prog(struct __sk_buff* skb) { |
| 32 | uint32_t key = skb->ifindex; |
| 33 | bpf_update_stats(skb, &iface_stats_map, BPF_EGRESS, &key); |
| 34 | return BPF_MATCH; |
| 35 | } |
| 36 | |
| 37 | SEC("skfilter/ingress/xtbpf") |
| 38 | int xt_bpf_ingress_prog(struct __sk_buff* skb) { |
| 39 | uint32_t key = skb->ifindex; |
| 40 | bpf_update_stats(skb, &iface_stats_map, BPF_INGRESS, &key); |
| 41 | return BPF_MATCH; |
| 42 | } |
| 43 | |
| 44 | SEC("skfilter/whitelist/xtbpf") |
| 45 | int xt_bpf_whitelist_prog(struct __sk_buff* skb) { |
| 46 | uint32_t sock_uid = bpf_get_socket_uid(skb); |
| 47 | if (is_system_uid(sock_uid)) return BPF_MATCH; |
Rubin Xu | af22a43 | 2019-04-16 20:09:48 +0100 | [diff] [blame^] | 48 | struct UidOwnerValue* whitelistMatch = bpf_map_lookup_elem(&uid_owner_map, &sock_uid); |
| 49 | if (whitelistMatch) return whitelistMatch->rule & HAPPY_BOX_MATCH; |
Chenbo Feng | 2236e1b | 2019-02-26 14:30:19 -0800 | [diff] [blame] | 50 | return BPF_NOMATCH; |
| 51 | } |
| 52 | |
| 53 | SEC("skfilter/blacklist/xtbpf") |
| 54 | int xt_bpf_blacklist_prog(struct __sk_buff* skb) { |
| 55 | uint32_t sock_uid = bpf_get_socket_uid(skb); |
Rubin Xu | af22a43 | 2019-04-16 20:09:48 +0100 | [diff] [blame^] | 56 | struct UidOwnerValue* blacklistMatch = bpf_map_lookup_elem(&uid_owner_map, &sock_uid); |
| 57 | if (blacklistMatch) return blacklistMatch->rule & PENALTY_BOX_MATCH; |
Chenbo Feng | 2236e1b | 2019-02-26 14:30:19 -0800 | [diff] [blame] | 58 | return BPF_NOMATCH; |
| 59 | } |
| 60 | |
| 61 | struct bpf_map_def SEC("maps") uid_permission_map = { |
Chenbo Feng | 58bd3d4 | 2019-02-26 15:43:36 -0800 | [diff] [blame] | 62 | .type = BPF_MAP_TYPE_HASH, |
| 63 | .key_size = sizeof(uint32_t), |
| 64 | .value_size = sizeof(uint8_t), |
| 65 | .max_entries = UID_OWNER_MAP_SIZE, |
Chenbo Feng | 2236e1b | 2019-02-26 14:30:19 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 68 | SEC("cgroupsock/inet/create") |
Chenbo Feng | 2236e1b | 2019-02-26 14:30:19 -0800 | [diff] [blame] | 69 | int inet_socket_create(struct bpf_sock* sk) { |
| 70 | uint64_t gid_uid = bpf_get_current_uid_gid(); |
| 71 | /* |
| 72 | * A given app is guaranteed to have the same app ID in all the profiles in |
| 73 | * which it is installed, and install permission is granted to app for all |
| 74 | * user at install time so we only check the appId part of a request uid at |
| 75 | * run time. See UserHandle#isSameApp for detail. |
| 76 | */ |
| 77 | uint32_t appId = (gid_uid & 0xffffffff) % PER_USER_RANGE; |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 78 | uint8_t* permissions = bpf_map_lookup_elem(&uid_permission_map, &appId); |
| 79 | if (!permissions) { |
| 80 | // UID not in map. Default to just INTERNET permission. |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | // A return value of 1 means allow, everything else means deny. |
| 85 | return (*permissions & BPF_PERMISSION_INTERNET) == BPF_PERMISSION_INTERNET; |
Chenbo Feng | 2236e1b | 2019-02-26 14:30:19 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | char _license[] SEC("license") = "Apache 2.0"; |