Commit graph

301 commits

Message SHA1
add DEFINE_BPF_MAP_GWO macro for group write-only maps
Test: build, atest
Bug: 138317993
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0b2bed9eb94e0aeaca79b5476c6e91484c7e1847

Maciej Żenczykowski   commited 2020-02-18 14:30:39 -08:00
62a4dddbba
add more precise bpf levels
This is to allow fixing a broken OffloadUtils netd test, but this will
also be useful for future support to make programs per kernel version.

We make the string versions of the enum more readable, because there
is exactly one user:

adb shell dumpsys netd | egrep -i bpf
  BPF module status: enabled
  BPF support level: Extended [4.14]
  ...

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I843bd7355703a78acc8343ca7f647ce9366927eb

Maciej Żenczykowski   commited 2020-02-18 04:46:58 +00:00
8a09a5ad1c
bpfloader: add option to set owner & group for pinned programs
Unlike maps, BPF programs currently have no natural place to declare
metadata like their desired owner & group. Add a bpf_prog_def struct
to allow setting these, located in a new "progs" section, and update
bpfloader to chown pinned programs appropriately based on this
information.
Add a #DEFINE_BPF_PROG macro to simplify adding this data for
programs. The struct name is the name of the corresponding function
with "_def" appended, which bpfloader uses to correlate a bpf_map_def
with the correct program.
Also have bpfloader set mode to 0440 for all programs, since only read
access should ever be needed

Bug: 149434314
Test: load a program that uses DEFINE_BPF_PROG and check that owner &
group are set as expected
Change-Id: I914c355f114368fe53de2c7f272d877463cba461
Signed-off-by: Connor O'Brien <connoro@google.com>

Connor O'Brien   authored and   Maciej Żenczykowski   commited 2020-02-14 23:28:44 +00:00
3278a1634b
add bpf_map_def support for setting uid/gid/mode
Test: build, atest, adb shell ls -lZ /sys/fs/bpf
Bug: 149434314
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie4001fbe16b4bc84fc8ec7138ae4928cd86f5ce7

Maciej Żenczykowski   commited 2020-02-14 23:28:26 +00:00
83f2977da8
bpfloader - add AID_* constants to bpf_map_def.h
Test: build, atest
Bug: 149434314
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I607f304a7e7bd76b8ce33489f9e239596f4d7398

Maciej Żenczykowski   commited 2020-02-14 23:27:45 +00:00
6f87896bdf
grant bpfloader CAP_CHOWN
so that it can change the uid/gid of pinned bpf progs and maps

Test: build, atest
Bug: 149434314
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id23f9caaddf620d4e99d970523f0a9768826e0d7

Maciej Żenczykowski   commited 2020-02-13 13:08:40 -08:00
669f7388df
BpfUtils.h - add SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED
(being moved from //system/netd/tests/bpf_base_test.cpp)

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7a38eff6d86a1bcd6dde193fd360d198bd57a900

Maciej Żenczykowski   commited 2020-02-12 04:15:29 -08:00
672b0e7aae
improve logging in case of bpf load failure
(we don't want to truncate this output, since it can be very useful
for debugging failures)

Test: builds, more useful ebpf load failure logs
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I817853f33b7f59296a041fb0d26ca3ae502b29da

Maciej Żenczykowski   commited 2020-02-11 19:12:34 -08:00
524deefc88
'static inline' -> 'inline'
Per Bernie: "in C++, inline implies internal linkage, you can drop static"

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I139af1d826d4982a1480fbee433fd9a743509786

Maciej Żenczykowski   commited 2020-02-11 16:43:15 -08:00
06caf87004
introduce isBpfSupported() helper
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9e2a7e049746ad193eea01e7c9d6786d27728c72

Maciej Żenczykowski   commited 2020-02-11 15:05:27 -08:00
c3a640db94
fix getBpfSupportLevel() cache wrt. multi-threading
In case it happens to get called twice in parallel on
startup.  It's never a mistake to do extra calls to the
uncached version.

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2605ed2ebc4ee331b2ca6c9fa915203d70415d7f

Maciej Żenczykowski   commited 2020-02-11 11:20:35 -08:00
8bd5f39348
Merge "make getBpfSupportLevel() cached"
Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2020-02-11 19:08:03 +00:00
61588e0e64
make getBpfSupportLevel() cached
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6948934c88ce8268ad38e8c0061807a81f5ea4bb

Maciej Żenczykowski   commited 2020-02-10 19:30:06 -08:00
fd90bc8ba1
More missing calls to Result::ok() in libbpf_android tests
Test: m checkbuild
Change-Id: Idb70b975e3c3bcbaa78cb493ec8f66b9b625ef74

Bernie Innocenti   commited 2020-02-10 07:28:13 +09:00
43d2538871
Merge "Drop base:: in front of ErrnoErrorf"
Tom Cherry   authored and   Gerrit Code Review   commited 2020-02-06 19:14:15 +00:00
2cc5aeb816
Merge "Convert system/bpf to Result::ok()"
Treehugger Robot   authored and   Gerrit Code Review   commited 2020-02-06 13:33:12 +00:00
4a480c057e
Convert system/bpf to Result::ok()
No functionality changes, this is a mechanical cleanup.

Test: m
Change-Id: Ia37f81c9861be0da83a91663a9d32b6b753a527c

Bernie Innocenti   commited 2020-02-06 04:26:17 +09:00
5953dee87d
bpfloader - add dependency on offload.o
Test: builds, atest, /system/etc/bpf/offload.o exists
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I043369f5e0ae4315dc8db0cf70dc8806e4997fe4

Maciej Żenczykowski   commited 2020-02-05 03:57:57 -08:00
51b71237cc
Drop base:: in front of ErrnoErrorf
ErrnoErrorf() is now a #define, so it cannot be qualified.

Test: build
Change-Id: I4c70a234ae6459ca071975b912353ee2501988c2

Tom Cherry   commited 2020-02-04 15:27:30 -08:00
a7146db6fb
Merge "Set /proc/sys/net/core/bpf_jit_{enable,kallsyms} to 1"
Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2020-02-01 01:26:01 +00:00
089fb876e4
Merge changes from topic "b129773125"
* changes:
  make BpfMap.reset() harder to use
  BpfLoadTest.cpp - construct BpfMap from path not fd
  BpfMap - add pinned path based constructors

Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2020-01-30 15:24:02 +00:00
ef2dc8e0a9
Merge "BpfMapTest.cpp - construct BpfMap from type/size/flags and not from fd"
Treehugger Robot   authored and   Gerrit Code Review   commited 2020-01-30 09:46:32 +00:00
7ddcfb5d21
make BpfMap.reset() harder to use
Long term we don't even want reset() to exist.

It's simply not useful since only the bpfloader can create maps,
and thus all we can do is construct from pre-existing pinned bpf
path - and we should never destroy/unpin/delete these.

Similarly the move constructor is probably spurious.

Basically the only real use for reset() is in tests,
and even there the overall utility is not clear.

Hence, in the mean time, make it harder to use incorrectly,
and harder to use in general.

To do that we get rid of reset() -> you must call reset(-1)
and we eliminate the ability to call reset(unique_fd)
which has unclear fd-leaking semantics.

Also remove a spot where it is called spuriously.

Test: build, atest
Bug: 129773125
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia018224aca0005ab68809b350595485ccd3f1bd3

Maciej Żenczykowski   commited 2020-01-30 08:59:53 +00:00
4f6573959b
BpfLoadTest.cpp - construct BpfMap from path not fd
switch over to the newer pinned path using constructor

Test: build, atest
  mMapFd no longer shows up in this file
Bug: 129773125
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie70149ffd5a541c80b35804ffd38df07f28b509b

Maciej Żenczykowski   commited 2020-01-30 08:59:44 +00:00
cd5c3023ce
BpfMap - add pinned path based constructors
(and remove the old straight from fd constructor)

Note that BpfMapRO is not yet truly compile time read-only,
since it still has methods that can modify things (although those
modifications will of course fail due to the map fd being r/o).

Test: build, atest with followup fixes
Bug: 129773125
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I70b511e3f0a62cd4d9d5a923e658b1086337ec4c

Maciej Żenczykowski   commited 2020-01-30 08:50:58 +00:00
dfd941f0fd
BpfMapTest.cpp - construct BpfMap from type/size/flags and not from fd
Test: build, atest
  mMapFd no longer shows up in this file
Bug: 129773125
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie49f0027d0f04565426ed800b079800b364dd049

Maciej Żenczykowski   commited 2020-01-29 22:43:10 -08:00
7c9a984f1d
Set /proc/sys/net/core/bpf_jit_{enable,kallsyms} to 1
bpf_jit_enable = 1 is mostly a no-op since on most future
devices it will be force enabled by BPF_JIT_ALWAYS_ON

It is required for Pixel 3 & co Linux 4.9 based devices, which
can only do JIT for some ebpf programs (and thus can't enable
BPF_JIT_ALWAYS_ON without some netd programs refusing to load)

We also set bpf_jit_kallsyms = 1 because it makes debugging
failures easier, but it is incompatible with bpf_jit_harden != 0.

We don't bother setting bpf_jit_harden because we both want
bpf_jit_kallsyms to work, and because the only entity allowed
to load ebpf programs is the bpfloader and it only loads trusted
(verified file system signed) prebuilt bpf programs.

Test: built and booted, verified settings
Bug: 140377409
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9b401ee7b01a2042da87ff48d548b11e0cf78efa

Maciej Żenczykowski   commited 2020-01-30 06:23:08 +00:00
a391148649
Merge "bpfloader - bump memlock rlimit to 1GiB"
Treehugger Robot   authored and   Gerrit Code Review   commited 2020-01-30 03:48:29 +00:00
083c0776d9
refactor bpf_map_def into a single header file
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iafd6016f4f24cc8fa263939c67e1a096aa91b125

Maciej Żenczykowski   commited 2020-01-29 08:41:26 +00:00
730a3860c2
bpfloader - bump memlock rlimit to 1GiB
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5a1728fe29e510c06978a0c7c5cc90af3e9ea46d

Maciej Żenczykowski   commited 2020-01-27 22:28:40 -08:00
e1deaecda4
Revert "Grant IPC_LOCK capability to bpfloader"
This reverts commit 77494d2b70.

This doesn't apparently work...

Kernel doesn't actually test capabilities, just blindly charges
against user's memlock limit:

//kernel/bpf/syscall.c:

static int bpf_charge_memlock(struct user_struct *user, u32 pages) {
  unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;

  if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
    atomic_long_sub(pages, &user->locked_vm);
    return -EPERM;
  }
  return 0;
}

Test: N/A, revert
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Icf6d6e8a36e4b9f3771a5ce80e25ef3644ff4e83

Maciej Żenczykowski   commited 2020-01-27 15:33:15 +00:00
3c057e6b7b
Grant IPC_LOCK capability to bpfloader
(instead of explicit memlock limit)

This reduces chance for memory allocation failures.

Any bpfloader failure is a critical boot time failure
(since without eBPF initializing properly netd will crash and
device will enter crash loop).

Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5ed0118d63c53100e7431324914bf22e9870abfe

Maciej Żenczykowski   commited 2020-01-27 01:08:03 -08:00
77494d2b70
bpf_helpers - add more const annotations
Keys and values passed in to lookup/update/delete are not modified.

Test: builds
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I222615af7695f41bfb59c872c7368829631c08a9

Maciej Żenczykowski   commited 2020-01-24 18:53:10 -08:00
0bfbf665b9
BpfUtils - remove some spurious things
Test: builds
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4ffa11220794d64c324d9de8890ba27cac986710

Maciej Żenczykowski   commited 2020-01-24 22:00:10 +00:00
974e0ed197
libbpf_android: remove libnetdutils dep
Remove libnetdutils dependency because bpf is being used for things other
than networking these days, and we don't want to make libnetdutils
vendor-available in the future. libbase provides an alternative type now.

Bug: 140330870
Test: atest libbpf_android_test netd_integration_test netd_unit_test
    libnetdbpf_test bpf_module_test
Change-Id: I72ae8cd7f58a49bfc7dcb914a332a4c4bad5dea5

Steven Moreland   commited 2020-01-21 10:47:33 -08:00
e7cd2a72d0
BpfUtils - pass file descriptors as 'const unique_fd&'s not raw ints.
Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Idde6d9c81ac9ba02aa5a42f9ca9ec72ef7cd5cad

Maciej Żenczykowski   commited 2020-01-18 19:39:39 -08:00
289742f537
BpfMap/Utils - fix const-ness of key & value
When accessing maps keys are always const/read-only.
Similarly for the value when it is being stored in the map.

This eliminates four const_casts.

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Icc57b46a8e9e81b5b06264260bfbf34a4ba9105d

Maciej Żenczykowski   commited 2020-01-18 19:32:09 -08:00
79365da151
BpfUtils - move trivial functions from .cpp to static inline's in .h
Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I49d1ac6e196c5b1afbe460a7247d2ad38f571958

Maciej Żenczykowski   commited 2020-01-17 09:07:15 +00:00
b479fd6a9f
BpfUtils - use aggregate initializers for bpf()
Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic8383e92973ff68a1b54299949121a31fb5c66d1

Maciej Żenczykowski   commited 2020-01-16 02:19:22 -08:00
2a1be9c22e
TEST_MAPPING += libbpf_android_test
Note, this does not add libbpf_load_test as well because this test
requires adding a bpf program to the system image.

Bug: N/A
Test: `atest --test-mapping .`
Change-Id: I770ec4876e1cb93a526e99bcb5a234f964f46971

Steven Moreland   authored and   Maciej Żenczykowski   commited 2020-01-15 08:28:44 +00:00
b572378120
BpfUtils - replace memset() with = {}
We can't specify which union member we want to initialize
or the compiler will be too smart and do a partial init,
but a = {} will still zero the entire struct.

Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0f9208be1be65cec35d848d37a0142d03e0919bf

Maciej Żenczykowski   commited 2020-01-15 07:08:21 +00:00
bc32ac641d
get rid of netdutils::Slice in BpfUtils.cpp
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic17181be20480c6db0806cc8dafef97d61736ec3

Maciej Żenczykowski   commited 2020-01-15 04:17:45 +00:00
c827aaa235
Merge "libbpf_load_test: fix map location"
Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2020-01-15 02:29:18 +00:00
0cb8b5ca7a
Merge "libbpf_android/BpfUtils - remove unused bpfProgLoad"
Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2020-01-14 23:44:11 +00:00
cd6f89bbb6
Merge "libbpf_android: allow bpf w/o maps section"
Treehugger Robot   authored and   Gerrit Code Review   commited 2020-01-14 22:03:43 +00:00
22b82c47d6
libbpf_load_test: fix map location
This test had bitrot.

Bug: N/A
Test: `atest libbpf_load_test`
Change-Id: I3480286e225d8dfbd0badd7bbb7cb77ff29ab0ea

Steven Moreland   commited 2020-01-14 11:37:49 -08:00
cdeb9203af
libbpf_android/BpfUtils - remove unused bpfProgLoad
(due to switch to bpfloader from loading via netd)

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic95cdfde26b8e7b905551a7fd2b465e0cf99bbce

Maciej Żenczykowski   authored and   Steven Moreland   commited 2020-01-14 09:10:32 -08:00
c66f5c6bc1
Merge "remove network specific struct definitions from BpfUtils.h"
Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2020-01-14 09:03:11 +00:00
d5f6cf505b
Merge "libbpf_android: waitForProgsLoaded"
Steven Moreland   authored and   Gerrit Code Review   commited 2020-01-13 22:12:06 +00:00
1362229a2e
OWNERS: +smoreland +connoro -fengc -joelaf
Bug: N/A
Test: N/A
Change-Id: I33e5cb5a5e2b0bce588cb90c4d0b74da7cc8dace

Steven Moreland   commited 2020-01-10 15:42:06 -08:00
87a1b19052