blob: 109715b7851e741d4d0665ba363553f57869dbf5 [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 Feng79b7e612018-12-11 12:24:23 -0800240std::string BpfLevelToString(BpfLevel bpfLevel) {
241 switch (bpfLevel) {
242 case BpfLevel::NONE: return "NONE_SUPPORT";
243 case BpfLevel::BASIC: return "BPF_LEVEL_BASIC";
244 case BpfLevel::EXTENDED: return "BPF_LEVEL_EXTENDED";
245 // No default statement. We want to see errors of the form:
246 // "enumeration value 'BPF_LEVEL_xxx' not handled in switch [-Werror,-Wswitch]".
247 }
248}
249
250BpfLevel getBpfSupportLevel() {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700251 struct utsname buf;
252 int kernel_version_major;
253 int kernel_version_minor;
254
255 uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
256 if (api_level == 0) {
257 ALOGE("Cannot determine initial API level of the device");
258 api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
259 }
260
Chenbo Feng79b7e612018-12-11 12:24:23 -0800261 // Check if the device is shipped originally with android P.
262 if (api_level < MINIMUM_API_REQUIRED) return BpfLevel::NONE;
263
Chenbo Feng75b410b2018-10-10 15:01:19 -0700264 int ret = uname(&buf);
265 if (ret) {
Chenbo Feng79b7e612018-12-11 12:24:23 -0800266 return BpfLevel::NONE;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700267 }
268 char dummy;
269 ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
Chenbo Feng79b7e612018-12-11 12:24:23 -0800270 // Check the device kernel version
271 if (ret < 2) return BpfLevel::NONE;
272 if (kernel_version_major > 4 || (kernel_version_major == 4 && kernel_version_minor >= 14))
273 return BpfLevel::EXTENDED;
274 if (kernel_version_major == 4 && kernel_version_minor >= 9) return BpfLevel::BASIC;
275
276 return BpfLevel::NONE;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700277}
278
279int loadAndPinProgram(BpfProgInfo* prog, Slice progBlock) {
280 // Program doesn't exist. Try to load it.
281 char bpf_log_buf[LOG_BUF_SIZE];
282 Slice bpfLog = Slice(bpf_log_buf, sizeof(bpf_log_buf));
283 prog->fd.reset(bpfProgLoad(prog->loadType, progBlock, "Apache 2.0", 0, bpfLog));
284 if (prog->fd < 0) {
285 int ret = -errno;
286 ALOGE("load %s failed: %s", prog->name, strerror(errno));
287 return ret;
288 }
289 if (prog->attachType == BPF_CGROUP_INET_EGRESS || prog->attachType == BPF_CGROUP_INET_INGRESS) {
Suren Baghdasaryan9217ccb2018-12-19 17:29:13 -0800290 std::string cg2_path;
291 if (!CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path)) {
292 int ret = -errno;
293 ALOGE("Failed to find cgroup v2 root");
294 return ret;
295 }
296 unique_fd cg_fd(open(cg2_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700297 if (cg_fd < 0) {
298 int ret = -errno;
299 ALOGE("Failed to open the cgroup directory");
300 return ret;
301 }
302 int ret = android::bpf::attachProgram(prog->attachType, prog->fd, cg_fd);
303 if (ret) {
304 ret = -errno;
305 ALOGE("%s attach failed: %s", prog->name, strerror(errno));
306 return ret;
307 }
308 }
309 if (prog->path) {
310 int ret = android::bpf::bpfFdPin(prog->fd, prog->path);
311 if (ret) {
312 ret = -errno;
313 ALOGE("Pin %s as file %s failed: %s", prog->name, prog->path, strerror(errno));
314 return ret;
315 }
316 }
317 return 0;
318}
319
320int extractAndLoadProg(BpfProgInfo* prog, Elf64_Shdr* sectionPtr, Slice fileContents,
321 const std::vector<BpfMapInfo>& mapPatterns) {
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800322 uint64_t progSize = (uint64_t)sectionPtr->sh_size;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700323 Slice progSection = take(drop(fileContents, sectionPtr->sh_offset), progSize);
324 if (progSection.size() < progSize) {
325 ALOGE("programSection out of bound");
326 return -EINVAL;
327 }
328 MemBlock progCopy(progSection);
329 if (progCopy.get().size() != progSize) {
330 ALOGE("program cannot be extracted");
331 return -EINVAL;
332 }
333 Slice remaining = progCopy.get();
334 while (remaining.size() >= MAP_CMD_SIZE) {
335 // Scan the program, examining all possible places that might be the start of a
336 // map load operation (i.e., all bytes of value MAP_LD_CMD_HEAD).
337 // In each of these places, check whether it is the start of one of the patterns
338 // we want to replace, and if so, replace it.
339 Slice mapHead = findFirstMatching(remaining, MAP_LD_CMD_HEAD);
340 if (mapHead.size() < MAP_CMD_SIZE) break;
341 bool replaced = false;
342 for (const auto& pattern : mapPatterns) {
343 if (!memcmp(mapHead.base(), pattern.search.data(), MAP_CMD_SIZE)) {
344 memcpy(mapHead.base(), pattern.replace.data(), MAP_CMD_SIZE);
345 replaced = true;
346 break;
347 }
348 }
349 remaining = drop(mapHead, replaced ? MAP_CMD_SIZE : sizeof(uint8_t));
350 }
351 if (!(prog->path) || access(prog->path, R_OK) == -1) {
352 return loadAndPinProgram(prog, progCopy.get());
353 }
354 return 0;
355}
356
357int parsePrograms(Slice fileContents, BpfProgInfo* programs, size_t size,
358 const std::vector<BpfMapInfo>& mapPatterns) {
359 Slice elfHeader = take(fileContents, sizeof(Elf64_Ehdr));
360 if (elfHeader.size() < sizeof(Elf64_Ehdr)) {
361 ALOGE("bpf fileContents does not have complete elf header");
362 return -EINVAL;
363 }
364
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800365 Elf64_Ehdr* elf = (Elf64_Ehdr*)elfHeader.base();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700366 // Find section names string table. This is the section whose index is e_shstrndx.
367 if (elf->e_shstrndx == SHN_UNDEF) {
368 ALOGE("cannot locate namesSection\n");
369 return -EINVAL;
370 }
371 size_t totalSectionSize = (elf->e_shnum) * sizeof(Elf64_Shdr);
372 Slice sections = take(drop(fileContents, elf->e_shoff), totalSectionSize);
373 if (sections.size() < totalSectionSize) {
374 ALOGE("sections corrupted");
375 return -EMSGSIZE;
376 }
377
378 Slice namesSection =
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800379 take(drop(sections, elf->e_shstrndx * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700380 if (namesSection.size() != sizeof(Elf64_Shdr)) {
381 ALOGE("namesSection corrupted");
382 return -EMSGSIZE;
383 }
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800384 size_t strTabOffset = ((Elf64_Shdr*)namesSection.base())->sh_offset;
385 size_t strTabSize = ((Elf64_Shdr*)namesSection.base())->sh_size;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700386
387 Slice strTab = take(drop(fileContents, strTabOffset), strTabSize);
388 if (strTab.size() < strTabSize) {
389 ALOGE("string table out of bound\n");
390 return -EMSGSIZE;
391 }
392
393 for (int i = 0; i < elf->e_shnum; i++) {
394 Slice section = take(drop(sections, i * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr));
395 if (section.size() < sizeof(Elf64_Shdr)) {
396 ALOGE("section %d is out of bound, section size: %zu, header size: %zu, total size: "
397 "%zu",
398 i, section.size(), sizeof(Elf64_Shdr), sections.size());
399 return -EBADF;
400 }
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800401 Elf64_Shdr* sectionPtr = (Elf64_Shdr*)section.base();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700402 Slice nameSlice = drop(strTab, sectionPtr->sh_name);
403 if (nameSlice.size() == 0) {
404 ALOGE("nameSlice out of bound, i: %d, strTabSize: %zu, sh_name: %u", i, strTabSize,
405 sectionPtr->sh_name);
406 return -EBADF;
407 }
408 for (size_t i = 0; i < size; i++) {
409 BpfProgInfo* prog = programs + i;
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800410 if (!strcmp((char*)nameSlice.base(), prog->name)) {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700411 int ret = extractAndLoadProg(prog, sectionPtr, fileContents, mapPatterns);
412 if (ret) return ret;
413 }
414 }
415 }
416
417 // Check all the program struct passed in to make sure they all have a valid fd.
418 for (size_t i = 0; i < size; i++) {
419 BpfProgInfo* prog = programs + i;
420 if (access(prog->path, R_OK) == -1) {
421 ALOGE("Load program %s failed", prog->name);
422 return -EINVAL;
423 }
424 }
425 return 0;
426}
427
428int parseProgramsFromFile(const char* path, BpfProgInfo* programs, size_t size,
429 const std::vector<BpfMapInfo>& mapPatterns) {
Chenbo Feng4c9e9ec2018-10-16 20:31:52 -0700430 unique_fd fd(open(path, O_RDONLY | O_CLOEXEC));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700431 int ret;
432 if (fd < 0) {
433 ret = -errno;
434 ALOGE("Failed to open %s program: %s", path, strerror(errno));
435 return ret;
436 }
437
438 struct stat stat;
439 if (fstat(fd.get(), &stat)) {
440 ret = -errno;
441 ALOGE("Failed to get file (%s) size: %s", path, strerror(errno));
442 return ret;
443 }
444
445 off_t fileLen = stat.st_size;
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800446 char* baseAddr = (char*)mmap(NULL, fileLen, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd.get(), 0);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700447 if (baseAddr == MAP_FAILED) {
448 ALOGE("Failed to map the program (%s) into memory: %s", path, strerror(errno));
449 ret = -errno;
450 return ret;
451 }
452
453 ret = parsePrograms(Slice(baseAddr, fileLen), programs, size, mapPatterns);
454
455 munmap(baseAddr, fileLen);
456 return ret;
457}
458
459} // namespace bpf
460} // namespace android