blob: 74585b294f85363cb7a255d06e5f50c685305a82 [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
2 * Copyright (C) 2017 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#define LOG_TAG "BpfUtils"
18
Bernie Innocenti26ffded2018-10-19 15:41:53 +090019#include "bpf/BpfUtils.h"
20
Chenbo Feng75b410b2018-10-10 15:01:19 -070021#include <elf.h>
22#include <inttypes.h>
23#include <linux/bpf.h>
24#include <linux/if_ether.h>
25#include <linux/in.h>
Chenbo Feng9cd8f142018-12-04 16:54:56 -080026#include <linux/pfkeyv2.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070027#include <stdlib.h>
28#include <string.h>
29#include <sys/mman.h>
30#include <sys/socket.h>
31#include <sys/stat.h>
32#include <sys/utsname.h>
33#include <sstream>
34#include <string>
35
36#include <android-base/properties.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070037#include <android-base/unique_fd.h>
Bernie Innocenti26ffded2018-10-19 15:41:53 +090038#include <log/log.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070039#include <netdutils/MemBlock.h>
40#include <netdutils/Slice.h>
Suren Baghdasaryan9217ccb2018-12-19 17:29:13 -080041#include <processgroup/processgroup.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070042
43using android::base::GetUintProperty;
Chenbo Feng75b410b2018-10-10 15:01:19 -070044using android::base::unique_fd;
45using android::netdutils::MemBlock;
46using android::netdutils::Slice;
Chenbo Feng75b410b2018-10-10 15:01:19 -070047
Chenbo Feng9cd8f142018-12-04 16:54:56 -080048// The buffer size for the buffer that records program loading logs, needs to be large enough for
49// the largest kernel program.
50constexpr size_t LOG_BUF_SIZE = 0x20000;
Chenbo Feng75b410b2018-10-10 15:01:19 -070051
52namespace android {
53namespace bpf {
54
55/* The bpf_attr is a union which might have a much larger size then the struct we are using, while
56 * The inline initializer only reset the field we are using and leave the reset of the memory as
57 * is. The bpf kernel code will performs a much stricter check to ensure all unused field is 0. So
58 * this syscall will normally fail with E2BIG if we don't do a memset to bpf_attr.
59 */
60bool operator==(const StatsKey& lhs, const StatsKey& rhs) {
61 return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag) && (lhs.counterSet == rhs.counterSet) &&
62 (lhs.ifaceIndex == rhs.ifaceIndex));
63}
64
65bool operator==(const UidTag& lhs, const UidTag& rhs) {
66 return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag));
67}
68
69bool operator==(const StatsValue& lhs, const StatsValue& rhs) {
70 return ((lhs.rxBytes == rhs.rxBytes) && (lhs.txBytes == rhs.txBytes) &&
71 (lhs.rxPackets == rhs.rxPackets) && (lhs.txPackets == rhs.txPackets));
72}
73
74int bpf(int cmd, Slice bpfAttr) {
75 return syscall(__NR_bpf, cmd, bpfAttr.base(), bpfAttr.size());
76}
77
78int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
79 uint32_t map_flags) {
80 bpf_attr attr;
81 memset(&attr, 0, sizeof(attr));
82 attr.map_type = map_type;
83 attr.key_size = key_size;
84 attr.value_size = value_size;
85 attr.max_entries = max_entries;
86 attr.map_flags = map_flags;
87
88 return bpf(BPF_MAP_CREATE, Slice(&attr, sizeof(attr)));
89}
90
91int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags) {
92 bpf_attr attr;
93 memset(&attr, 0, sizeof(attr));
94 attr.map_fd = map_fd.get();
95 attr.key = ptr_to_u64(key);
96 attr.value = ptr_to_u64(value);
97 attr.flags = flags;
98
99 return bpf(BPF_MAP_UPDATE_ELEM, Slice(&attr, sizeof(attr)));
100}
101
102int findMapEntry(const base::unique_fd& map_fd, void* key, void* value) {
103 bpf_attr attr;
104 memset(&attr, 0, sizeof(attr));
105 attr.map_fd = map_fd.get();
106 attr.key = ptr_to_u64(key);
107 attr.value = ptr_to_u64(value);
108
109 return bpf(BPF_MAP_LOOKUP_ELEM, Slice(&attr, sizeof(attr)));
110}
111
112int deleteMapEntry(const base::unique_fd& map_fd, void* key) {
113 bpf_attr attr;
114 memset(&attr, 0, sizeof(attr));
115 attr.map_fd = map_fd.get();
116 attr.key = ptr_to_u64(key);
117
118 return bpf(BPF_MAP_DELETE_ELEM, Slice(&attr, sizeof(attr)));
119}
120
121int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key) {
122 bpf_attr attr;
123 memset(&attr, 0, sizeof(attr));
124 attr.map_fd = map_fd.get();
125 attr.key = ptr_to_u64(key);
126 attr.next_key = ptr_to_u64(next_key);
127
128 return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
129}
130
131int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey) {
132 bpf_attr attr;
133 memset(&attr, 0, sizeof(attr));
134 attr.map_fd = map_fd.get();
135 attr.key = 0;
136 attr.next_key = ptr_to_u64(firstKey);
137
138 return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
139}
140
141int bpfProgLoad(bpf_prog_type prog_type, Slice bpf_insns, const char* license,
142 uint32_t kern_version, Slice bpf_log) {
143 bpf_attr attr;
144 memset(&attr, 0, sizeof(attr));
145 attr.prog_type = prog_type;
146 attr.insns = ptr_to_u64(bpf_insns.base());
147 attr.insn_cnt = bpf_insns.size() / sizeof(struct bpf_insn);
148 attr.license = ptr_to_u64((void*)license);
149 attr.log_buf = ptr_to_u64(bpf_log.base());
150 attr.log_size = bpf_log.size();
151 attr.log_level = DEFAULT_LOG_LEVEL;
152 attr.kern_version = kern_version;
153 int ret = bpf(BPF_PROG_LOAD, Slice(&attr, sizeof(attr)));
154
155 if (ret < 0) {
156 std::string prog_log = netdutils::toString(bpf_log);
157 std::istringstream iss(prog_log);
158 for (std::string line; std::getline(iss, line);) {
159 ALOGE("%s", line.c_str());
160 }
161 }
162 return ret;
163}
164
165int bpfFdPin(const base::unique_fd& map_fd, const char* pathname) {
166 bpf_attr attr;
167 memset(&attr, 0, sizeof(attr));
168 attr.pathname = ptr_to_u64((void*)pathname);
169 attr.bpf_fd = map_fd.get();
170
171 return bpf(BPF_OBJ_PIN, Slice(&attr, sizeof(attr)));
172}
173
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800174int bpfFdGet(const char* pathname, uint32_t flag) {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700175 bpf_attr attr;
176 memset(&attr, 0, sizeof(attr));
177 attr.pathname = ptr_to_u64((void*)pathname);
178 attr.file_flags = flag;
179 return bpf(BPF_OBJ_GET, Slice(&attr, sizeof(attr)));
180}
181
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800182int mapRetrieve(const char* pathname, uint32_t flag) {
183 return bpfFdGet(pathname, flag);
184}
185
Chenbo Feng75b410b2018-10-10 15:01:19 -0700186int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd) {
187 bpf_attr attr;
188 memset(&attr, 0, sizeof(attr));
189 attr.target_fd = cg_fd;
190 attr.attach_bpf_fd = prog_fd;
191 attr.attach_type = type;
192
193 return bpf(BPF_PROG_ATTACH, Slice(&attr, sizeof(attr)));
194}
195
196int detachProgram(bpf_attach_type type, uint32_t cg_fd) {
197 bpf_attr attr;
198 memset(&attr, 0, sizeof(attr));
199 attr.target_fd = cg_fd;
200 attr.attach_type = type;
201
202 return bpf(BPF_PROG_DETACH, Slice(&attr, sizeof(attr)));
203}
204
205uint64_t getSocketCookie(int sockFd) {
206 uint64_t sock_cookie;
207 socklen_t cookie_len = sizeof(sock_cookie);
208 int res = getsockopt(sockFd, SOL_SOCKET, SO_COOKIE, &sock_cookie, &cookie_len);
209 if (res < 0) {
210 res = -errno;
211 ALOGE("Failed to get socket cookie: %s\n", strerror(errno));
212 errno = -res;
213 // 0 is an invalid cookie. See sock_gen_cookie.
214 return NONEXISTENT_COOKIE;
215 }
216 return sock_cookie;
217}
218
Chenbo Feng9cd8f142018-12-04 16:54:56 -0800219int synchronizeKernelRCU() {
220 // This is a temporary hack for network stats map swap on devices running
221 // 4.9 kernels. The kernel code of socket release on pf_key socket will
222 // explicitly call synchronize_rcu() which is exactly what we need.
223 int pfSocket = socket(AF_KEY, SOCK_RAW | SOCK_CLOEXEC, PF_KEY_V2);
224
225 if (pfSocket < 0) {
226 int ret = -errno;
227 ALOGE("create PF_KEY socket failed: %s", strerror(errno));
228 return ret;
229 }
230
231 // When closing socket, synchronize_rcu() gets called in sock_release().
232 if (close(pfSocket)) {
233 int ret = -errno;
234 ALOGE("failed to close the PF_KEY socket: %s", strerror(errno));
235 return ret;
236 }
237 return 0;
238}
239
Chenbo Feng75b410b2018-10-10 15:01:19 -0700240bool hasBpfSupport() {
241 struct utsname buf;
242 int kernel_version_major;
243 int kernel_version_minor;
244
245 uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
246 if (api_level == 0) {
247 ALOGE("Cannot determine initial API level of the device");
248 api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
249 }
250
251 int ret = uname(&buf);
252 if (ret) {
253 return false;
254 }
255 char dummy;
256 ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800257 if (ret >= 2 &&
258 ((kernel_version_major > 4) || (kernel_version_major == 4 && kernel_version_minor >= 9))) {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700259 // Check if the device is shipped originally with android P.
260 return api_level >= MINIMUM_API_REQUIRED;
261 }
262 return false;
263}
264
265int loadAndPinProgram(BpfProgInfo* prog, Slice progBlock) {
266 // Program doesn't exist. Try to load it.
267 char bpf_log_buf[LOG_BUF_SIZE];
268 Slice bpfLog = Slice(bpf_log_buf, sizeof(bpf_log_buf));
269 prog->fd.reset(bpfProgLoad(prog->loadType, progBlock, "Apache 2.0", 0, bpfLog));
270 if (prog->fd < 0) {
271 int ret = -errno;
272 ALOGE("load %s failed: %s", prog->name, strerror(errno));
273 return ret;
274 }
275 if (prog->attachType == BPF_CGROUP_INET_EGRESS || prog->attachType == BPF_CGROUP_INET_INGRESS) {
Suren Baghdasaryan9217ccb2018-12-19 17:29:13 -0800276 std::string cg2_path;
277 if (!CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path)) {
278 int ret = -errno;
279 ALOGE("Failed to find cgroup v2 root");
280 return ret;
281 }
282 unique_fd cg_fd(open(cg2_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700283 if (cg_fd < 0) {
284 int ret = -errno;
285 ALOGE("Failed to open the cgroup directory");
286 return ret;
287 }
288 int ret = android::bpf::attachProgram(prog->attachType, prog->fd, cg_fd);
289 if (ret) {
290 ret = -errno;
291 ALOGE("%s attach failed: %s", prog->name, strerror(errno));
292 return ret;
293 }
294 }
295 if (prog->path) {
296 int ret = android::bpf::bpfFdPin(prog->fd, prog->path);
297 if (ret) {
298 ret = -errno;
299 ALOGE("Pin %s as file %s failed: %s", prog->name, prog->path, strerror(errno));
300 return ret;
301 }
302 }
303 return 0;
304}
305
306int extractAndLoadProg(BpfProgInfo* prog, Elf64_Shdr* sectionPtr, Slice fileContents,
307 const std::vector<BpfMapInfo>& mapPatterns) {
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800308 uint64_t progSize = (uint64_t)sectionPtr->sh_size;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700309 Slice progSection = take(drop(fileContents, sectionPtr->sh_offset), progSize);
310 if (progSection.size() < progSize) {
311 ALOGE("programSection out of bound");
312 return -EINVAL;
313 }
314 MemBlock progCopy(progSection);
315 if (progCopy.get().size() != progSize) {
316 ALOGE("program cannot be extracted");
317 return -EINVAL;
318 }
319 Slice remaining = progCopy.get();
320 while (remaining.size() >= MAP_CMD_SIZE) {
321 // Scan the program, examining all possible places that might be the start of a
322 // map load operation (i.e., all bytes of value MAP_LD_CMD_HEAD).
323 // In each of these places, check whether it is the start of one of the patterns
324 // we want to replace, and if so, replace it.
325 Slice mapHead = findFirstMatching(remaining, MAP_LD_CMD_HEAD);
326 if (mapHead.size() < MAP_CMD_SIZE) break;
327 bool replaced = false;
328 for (const auto& pattern : mapPatterns) {
329 if (!memcmp(mapHead.base(), pattern.search.data(), MAP_CMD_SIZE)) {
330 memcpy(mapHead.base(), pattern.replace.data(), MAP_CMD_SIZE);
331 replaced = true;
332 break;
333 }
334 }
335 remaining = drop(mapHead, replaced ? MAP_CMD_SIZE : sizeof(uint8_t));
336 }
337 if (!(prog->path) || access(prog->path, R_OK) == -1) {
338 return loadAndPinProgram(prog, progCopy.get());
339 }
340 return 0;
341}
342
343int parsePrograms(Slice fileContents, BpfProgInfo* programs, size_t size,
344 const std::vector<BpfMapInfo>& mapPatterns) {
345 Slice elfHeader = take(fileContents, sizeof(Elf64_Ehdr));
346 if (elfHeader.size() < sizeof(Elf64_Ehdr)) {
347 ALOGE("bpf fileContents does not have complete elf header");
348 return -EINVAL;
349 }
350
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800351 Elf64_Ehdr* elf = (Elf64_Ehdr*)elfHeader.base();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700352 // Find section names string table. This is the section whose index is e_shstrndx.
353 if (elf->e_shstrndx == SHN_UNDEF) {
354 ALOGE("cannot locate namesSection\n");
355 return -EINVAL;
356 }
357 size_t totalSectionSize = (elf->e_shnum) * sizeof(Elf64_Shdr);
358 Slice sections = take(drop(fileContents, elf->e_shoff), totalSectionSize);
359 if (sections.size() < totalSectionSize) {
360 ALOGE("sections corrupted");
361 return -EMSGSIZE;
362 }
363
364 Slice namesSection =
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800365 take(drop(sections, elf->e_shstrndx * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700366 if (namesSection.size() != sizeof(Elf64_Shdr)) {
367 ALOGE("namesSection corrupted");
368 return -EMSGSIZE;
369 }
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800370 size_t strTabOffset = ((Elf64_Shdr*)namesSection.base())->sh_offset;
371 size_t strTabSize = ((Elf64_Shdr*)namesSection.base())->sh_size;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700372
373 Slice strTab = take(drop(fileContents, strTabOffset), strTabSize);
374 if (strTab.size() < strTabSize) {
375 ALOGE("string table out of bound\n");
376 return -EMSGSIZE;
377 }
378
379 for (int i = 0; i < elf->e_shnum; i++) {
380 Slice section = take(drop(sections, i * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr));
381 if (section.size() < sizeof(Elf64_Shdr)) {
382 ALOGE("section %d is out of bound, section size: %zu, header size: %zu, total size: "
383 "%zu",
384 i, section.size(), sizeof(Elf64_Shdr), sections.size());
385 return -EBADF;
386 }
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800387 Elf64_Shdr* sectionPtr = (Elf64_Shdr*)section.base();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700388 Slice nameSlice = drop(strTab, sectionPtr->sh_name);
389 if (nameSlice.size() == 0) {
390 ALOGE("nameSlice out of bound, i: %d, strTabSize: %zu, sh_name: %u", i, strTabSize,
391 sectionPtr->sh_name);
392 return -EBADF;
393 }
394 for (size_t i = 0; i < size; i++) {
395 BpfProgInfo* prog = programs + i;
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800396 if (!strcmp((char*)nameSlice.base(), prog->name)) {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700397 int ret = extractAndLoadProg(prog, sectionPtr, fileContents, mapPatterns);
398 if (ret) return ret;
399 }
400 }
401 }
402
403 // Check all the program struct passed in to make sure they all have a valid fd.
404 for (size_t i = 0; i < size; i++) {
405 BpfProgInfo* prog = programs + i;
406 if (access(prog->path, R_OK) == -1) {
407 ALOGE("Load program %s failed", prog->name);
408 return -EINVAL;
409 }
410 }
411 return 0;
412}
413
414int parseProgramsFromFile(const char* path, BpfProgInfo* programs, size_t size,
415 const std::vector<BpfMapInfo>& mapPatterns) {
Chenbo Feng4c9e9ec2018-10-16 20:31:52 -0700416 unique_fd fd(open(path, O_RDONLY | O_CLOEXEC));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700417 int ret;
418 if (fd < 0) {
419 ret = -errno;
420 ALOGE("Failed to open %s program: %s", path, strerror(errno));
421 return ret;
422 }
423
424 struct stat stat;
425 if (fstat(fd.get(), &stat)) {
426 ret = -errno;
427 ALOGE("Failed to get file (%s) size: %s", path, strerror(errno));
428 return ret;
429 }
430
431 off_t fileLen = stat.st_size;
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800432 char* baseAddr = (char*)mmap(NULL, fileLen, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd.get(), 0);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700433 if (baseAddr == MAP_FAILED) {
434 ALOGE("Failed to map the program (%s) into memory: %s", path, strerror(errno));
435 ret = -errno;
436 return ret;
437 }
438
439 ret = parsePrograms(Slice(baseAddr, fileLen), programs, size, mapPatterns);
440
441 munmap(baseAddr, fileLen);
442 return ret;
443}
444
445} // namespace bpf
446} // namespace android