|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ |
| 3 | + |
| 4 | +#include <vmlinux.h> |
| 5 | +#include <string.h> |
| 6 | +#include <stdbool.h> |
| 7 | +#include <bpf/bpf_tracing.h> |
| 8 | +#include "bpf_misc.h" |
| 9 | + |
| 10 | +#define ELFMAG "\177ELF" |
| 11 | +#define SELFMAG 4 |
| 12 | +#define ET_NONE 0 |
| 13 | +#define ET_REL 1 |
| 14 | +#define ET_EXEC 2 |
| 15 | +#define ET_DYN 3 |
| 16 | +#define ET_CORE 4 |
| 17 | +#define ET_LOPROC 0xff00 |
| 18 | +#define ET_HIPROC 0xffff |
| 19 | +#define EI_CLASS 4 |
| 20 | +#define ELFCLASS32 1 |
| 21 | +#define ELFCLASS64 2 |
| 22 | +#define STT_TLS 6 |
| 23 | + |
| 24 | +#define ELF_ST_BIND(x) ((x) >> 4) |
| 25 | +#define ELF_ST_TYPE(x) ((x) & 0xf) |
| 26 | +#define ELF32_ST_BIND(x) ELF_ST_BIND(x) |
| 27 | +#define ELF32_ST_TYPE(x) ELF_ST_TYPE(x) |
| 28 | +#define ELF64_ST_BIND(x) ELF_ST_BIND(x) |
| 29 | +#define ELF64_ST_TYPE(x) ELF_ST_TYPE(x) |
| 30 | + |
| 31 | +char _license[] SEC("license") = "GPL"; |
| 32 | + |
| 33 | +struct { |
| 34 | + __uint(type, BPF_MAP_TYPE_ARRAY); |
| 35 | + __uint(max_entries, 1); |
| 36 | + __type(key, int); |
| 37 | + __type(value, struct elem); |
| 38 | +} arrmap SEC(".maps"); |
| 39 | + |
| 40 | +struct { |
| 41 | + __uint(type, BPF_MAP_TYPE_RINGBUF); |
| 42 | + __uint(max_entries, 10000000); |
| 43 | +} ringbuf SEC(".maps"); |
| 44 | + |
| 45 | +struct elem { |
| 46 | + struct file *file; |
| 47 | + struct bpf_task_work tw; |
| 48 | +}; |
| 49 | + |
| 50 | +enum file_reader_test { |
| 51 | + VALIDATE_LARGE_FILE = 1, |
| 52 | + SEARCH_ELF = 2, |
| 53 | +}; |
| 54 | + |
| 55 | +int err; |
| 56 | +void *user_ptr; |
| 57 | +char buf[1024]; |
| 58 | +char *user_buf; |
| 59 | +volatile const __u32 user_buf_sz; |
| 60 | +volatile const __s32 test_type = -1; |
| 61 | + |
| 62 | +static int process_vma(struct task_struct *task, struct vm_area_struct *vma, void *data); |
| 63 | +static int search_elf(struct file *file); |
| 64 | +static int validate_large_file_read(struct file *file); |
| 65 | +static int task_work_callback(struct bpf_map *map, void *key, void *value); |
| 66 | + |
| 67 | +SEC("raw_tp/sys_enter") |
| 68 | +int on_getpid(void *ctx) |
| 69 | +{ |
| 70 | + struct task_struct *task = bpf_get_current_task_btf(); |
| 71 | + struct elem *work; |
| 72 | + int key = 0; |
| 73 | + |
| 74 | + work = bpf_map_lookup_elem(&arrmap, &key); |
| 75 | + if (!work) { |
| 76 | + err = 1; |
| 77 | + return 0; |
| 78 | + } |
| 79 | + bpf_task_work_schedule_signal(task, &work->tw, &arrmap, task_work_callback, NULL); |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +static int task_work_callback(struct bpf_map *map, void *key, void *value) |
| 84 | +{ |
| 85 | + struct task_struct *task = bpf_get_current_task_btf(); |
| 86 | + |
| 87 | + bpf_find_vma(task, (unsigned long)user_ptr, process_vma, NULL, 0); |
| 88 | + return 0; |
| 89 | +} |
| 90 | + |
| 91 | +static int process_vma(struct task_struct *task, struct vm_area_struct *vma, void *data) |
| 92 | +{ |
| 93 | + switch (test_type) { |
| 94 | + case VALIDATE_LARGE_FILE: |
| 95 | + err = validate_large_file_read(vma->vm_file); |
| 96 | + break; |
| 97 | + case SEARCH_ELF: |
| 98 | + err = search_elf(vma->vm_file); |
| 99 | + break; |
| 100 | + default: |
| 101 | + err = 1; |
| 102 | + } |
| 103 | + return err; |
| 104 | +} |
| 105 | + |
| 106 | +static int validate_large_file_read(struct file *file) |
| 107 | +{ |
| 108 | + struct bpf_dynptr dynptr; |
| 109 | + int err, i; |
| 110 | + char *rbuf1 = NULL, *rbuf2 = NULL; |
| 111 | + |
| 112 | + if (!file) { |
| 113 | + err = 1; |
| 114 | + return 1; |
| 115 | + } |
| 116 | + |
| 117 | + err = bpf_dynptr_from_file(file, 0, &dynptr); |
| 118 | + if (err) |
| 119 | + goto cleanup_file; |
| 120 | + |
| 121 | + rbuf1 = bpf_ringbuf_reserve(&ringbuf, user_buf_sz, 0); |
| 122 | + if (!rbuf1) |
| 123 | + goto cleanup_file; |
| 124 | + |
| 125 | + rbuf2 = bpf_ringbuf_reserve(&ringbuf, user_buf_sz, 0); |
| 126 | + if (!rbuf2) |
| 127 | + goto cleanup_all; |
| 128 | + |
| 129 | + bpf_dynptr_read(rbuf1, user_buf_sz, &dynptr, 0, 0); |
| 130 | + bpf_copy_from_user(rbuf2, user_buf_sz, user_buf); |
| 131 | + /* Verify file contents read from BPF is the same as the one read from userspace */ |
| 132 | + bpf_for(i, 0, user_buf_sz) { |
| 133 | + if (i >= 256000 || rbuf1[i] != rbuf2[i]) { |
| 134 | + err = 1; |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | +cleanup_all: |
| 140 | + if (rbuf1) |
| 141 | + bpf_ringbuf_discard(rbuf1, 0); |
| 142 | + if (rbuf2) |
| 143 | + bpf_ringbuf_discard(rbuf2, 0); |
| 144 | +cleanup_file: |
| 145 | + bpf_dynptr_file_discard(&dynptr); |
| 146 | + return err ? 1 : 0; |
| 147 | +} |
| 148 | + |
| 149 | +/* Finds thread local variable `tls_counter` in this executable's ELF */ |
| 150 | +static int search_elf(struct file *file) |
| 151 | +{ |
| 152 | + Elf64_Ehdr ehdr; |
| 153 | + Elf64_Shdr shdrs; |
| 154 | + Elf64_Shdr symtab, strtab, tmp; |
| 155 | + const Elf64_Sym *symbol; |
| 156 | + int count, off, i, e_shnum, e_shoff, e_shentsize, sections = 0; |
| 157 | + const char *string; |
| 158 | + struct bpf_dynptr dynptr; |
| 159 | + const __u32 slen = 11; |
| 160 | + static const char *needle = "tls_counter"; |
| 161 | + |
| 162 | + if (!file) { |
| 163 | + err = 1; |
| 164 | + return 1; |
| 165 | + } |
| 166 | + |
| 167 | + err = bpf_dynptr_from_file(file, 0, &dynptr); |
| 168 | + if (err) |
| 169 | + goto fail; |
| 170 | + |
| 171 | + err = bpf_dynptr_read(&ehdr, sizeof(ehdr), &dynptr, 0, 0); |
| 172 | + if (err) |
| 173 | + goto fail; |
| 174 | + |
| 175 | + if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) |
| 176 | + goto fail; |
| 177 | + |
| 178 | + if (ehdr.e_type != ET_EXEC && ehdr.e_type != ET_DYN) |
| 179 | + goto fail; |
| 180 | + |
| 181 | + if (ehdr.e_ident[EI_CLASS] != ELFCLASS64) |
| 182 | + goto fail; |
| 183 | + |
| 184 | + e_shnum = ehdr.e_shnum; |
| 185 | + e_shoff = ehdr.e_shoff; |
| 186 | + e_shentsize = ehdr.e_shentsize; |
| 187 | + |
| 188 | + err = bpf_dynptr_read(&shdrs, sizeof(shdrs), &dynptr, |
| 189 | + e_shoff + e_shentsize * ehdr.e_shstrndx, 0); |
| 190 | + if (err) |
| 191 | + goto fail; |
| 192 | + |
| 193 | + off = shdrs.sh_offset; |
| 194 | + |
| 195 | + __builtin_memset(&symtab, 0, sizeof(symtab)); |
| 196 | + __builtin_memset(&strtab, 0, sizeof(strtab)); |
| 197 | + bpf_for(i, 0, e_shnum) |
| 198 | + { |
| 199 | + err = bpf_dynptr_read(&tmp, sizeof(Elf64_Shdr), &dynptr, e_shoff + e_shentsize * i, |
| 200 | + 0); |
| 201 | + if (err) |
| 202 | + goto fail; |
| 203 | + |
| 204 | + string = bpf_dynptr_slice(&dynptr, off + tmp.sh_name, buf, slen); |
| 205 | + if (!string) |
| 206 | + goto fail; |
| 207 | + |
| 208 | + if (bpf_strncmp(string, slen, ".symtab") == 0) { |
| 209 | + symtab = tmp; |
| 210 | + ++sections; |
| 211 | + } else if (bpf_strncmp(string, slen, ".strtab") == 0) { |
| 212 | + strtab = tmp; |
| 213 | + ++sections; |
| 214 | + } |
| 215 | + if (sections == 2) |
| 216 | + break; |
| 217 | + } |
| 218 | + if (sections != 2) |
| 219 | + goto fail; |
| 220 | + |
| 221 | + count = symtab.sh_size / sizeof(Elf64_Sym); |
| 222 | + bpf_for(i, 0, count) |
| 223 | + { |
| 224 | + symbol = bpf_dynptr_slice(&dynptr, symtab.sh_offset + sizeof(Elf64_Sym) * i, buf, |
| 225 | + sizeof(Elf64_Sym)); |
| 226 | + if (!symbol) |
| 227 | + goto fail; |
| 228 | + if (symbol->st_name == 0 || ELF64_ST_TYPE(symbol->st_info) != STT_TLS) |
| 229 | + continue; |
| 230 | + string = bpf_dynptr_slice(&dynptr, strtab.sh_offset + symbol->st_name, buf, slen); |
| 231 | + if (!string) |
| 232 | + goto fail; |
| 233 | + if (bpf_strncmp(string, slen, needle) == 0) |
| 234 | + goto success; |
| 235 | + } |
| 236 | +fail: |
| 237 | + err = 1; |
| 238 | +success: |
| 239 | + bpf_dynptr_file_discard(&dynptr); |
| 240 | + return err ? 1 : 0; |
| 241 | +} |
0 commit comments