Commit Graph

23 Commits

Author SHA1 Message Date
Maciej Żenczykowski
7e2f53e762 reverse BpfMap & BpfMapRO inheritence to make sense
Bug: 235590615
Bug: 235907076
Bug: 286003437
Test: TreeHugger, m droid gpuservice_unittest libtimeinstate_test bpf_benchmark bpf_module_test libbpf_load_test && mma
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I020a898fe8b257040a100f146654d4a04b19f843
2023-09-29 00:44:33 +00:00
Maciej Żenczykowski
c2dd01c739 BpfHandler: U requires 4.14+, V requires 4.19+
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3f6440dfe3eb71fbe845eb70c0a6c53110f8d46f
2023-09-01 21:07:15 +00:00
Maciej Żenczykowski
85d4a5e198 BpfHandler - remove use of getMap()
The FD number isn't actually useful debugging information,
and I want to get rid of this fd leak out of BpfMap class.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I981832b019b57f534bed1c1fbfedf6457abba6fa
2023-08-20 16:42:09 +00:00
Maciej Żenczykowski
2582445ec7 use unique_fd !ok() instead of == -1
while at it add 'const' to satisfy the linter

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I207be20c75b103bc5332c111291e4c439ba82219
2023-06-14 10:09:08 +00:00
Maciej Żenczykowski
9287d69620 Merge "Added additional logging to bpf handler" 2023-06-05 21:39:41 +00:00
Nick Wille
5076a021c8 Added additional logging to bpf handler
Bug: b/278923463
Test: Treehugger
Change-Id: I5b46b488723ebcde223b758c03c70fa8b1f14c25
2023-06-05 18:45:13 +00:00
Maciej Żenczykowski
65075bb8f8 BpfHandler: abort on U+ if unexpected cgroup mount path
This is defined here:
http://cs/h/android/platform/superproject/+/master:system/core/libprocessgroup/profiles/cgroups.json?l=27
and has been a constant since
  https://android-review.googlesource.com/c/platform/system/core/+/1324649
was merged back on August 21, 2020 (ie. Android S)

But currently there's no easy way to get at this constant
from mainline code... which means it's very difficult
to do any bpf cgroup attach/detach from mainline.

btw. this appears to also be already hardcoded in:
http://cs/h/android/platform/superproject/+/master:packages/modules/Connectivity/service/src/com/android/server/connectivity/ConnectivityNativeService.java?l=48
as well

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I99f4a5a26dd10f6ea70bf73114af0d18098de630
2023-06-02 03:44:11 +00:00
Maciej Żenczykowski
d3fe154610 get rid of needless use of NONEXISTENT_COOKIE which is simply 0
The random grab bag of stuff in BpfUtils.h is weird,
and I'm trying to trim it down.

Since this is coming from the kernel, 0 meaning error / no socket
is actually kernel api and thus guaranteed.

IMHO if (!cookie) is also clearer to read.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I433dfca5695a04a42d632b7e0c2434db26c4ed00
2023-04-19 11:53:38 +00:00
Maciej Żenczykowski
0309362cc9 enforce 4.14+ instead of program being present
These two checks are theoretically equivalent,
since netd.o marks the program as requiring 4.14+.

Thus unless netd.o bpf program is broken, or bpfloader
is misbehaving, this change is a no-op.

Bug: 270276754
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2c6982e7476c2fa56b67532c80d97d7389030d32
2023-02-22 22:10:48 +00:00
Lorenzo Colitti
3505b584d3 Un-drop support for pre-4.14 kernels.
This effectively reverts commit 6ed2ab9b57,
while ensuring that the program has the right permissions as
defined in r.android.com/2130014 :

oriole:/ # ls -lZ /sys/fs/bpf/netd_shared/prog_netd_cgroupsock_inet_create
-r--r----- 1 root root u:object_r:fs_bpf_netd_readonly:s0  0 2022-10-27 20:05 /sys/fs/bpf/netd_shared/prog_netd_cgroupsock_inet_create

Reason for revert: need to support 4.9 devices upgrading to T.

The only thing that cannot currently be supported on those
devices is the inet_create program which implements the
INTERNET permission.

Also, update bpf_existence_test so it does not check for the
existence of the program on pre-4.14 devices.

Bug: 254001921
Test: atest bpf_existence_test
Change-Id: I14f26cee5feeaae93b4d9710a7b9a2f835ff405f
2022-10-27 20:06:16 +09:00
Maciej Żenczykowski
b5868a0a40 BpfHandler - eliminate mutex
since it doesn't actually work (this runs in netd, but is meant
to protect against system server modifications)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I87f268f1a68c559b19caa4d625cb9eb8395c91e2
2022-09-01 12:36:17 +00:00
Maciej Żenczykowski
4938d40501 simplify netd updatable
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8987f33b3f3427683e5ae9b2931528f91d6f0aeb
2022-08-15 17:40:31 +00:00
Maciej Żenczykowski
c576c0d6cf make sure netd can open all bpf programs
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib026f9d7cddcd266a634fbc0d50c5589665f7bb5
2022-08-07 22:18:37 +00:00
Maciej Żenczykowski
b10e055f4b switch netd_configuration_map from hash map to array
This eliminates the need for netd_updatable BpfHandler.cpp
to initialize the hash map with a zero.

On startup the map will be freshly initialized and thus zero.

On restart it might not be empty, but it doesn't matter to netd.
Furthermore the mainline component of the system server will
re-initialize it again anyway:
see service/native/TrafficController.cpp initMaps()

This does remove the ability to call deleteValue on a key,
since that would always return -EINVAL, but since we don't
currently do that, that's really a feature.

(It does suggest though that we should have a BpfMapNonNullable
 class which is writeable, but without a deleteValue() function)

Additionally BpfMap arrays are more efficient for the kernel bpf jit
compiler, as - on newer kernels - it can optimize the read/write
into a simple memory access (as opposed to a bpf helper call).

Before:
  $ adb shell ls -l /sys/fs/bpf/netd_shared/map_netd_configuration_map
  -rw-rw---- 1 root net_bw_acct 0 2022-06-11 08:20 /sys/fs/bpf/netd_shared/ map_netd_configuration_map

After:
  $ adbz shell ls -l /sys/fs/bpf/netd_shared/map_netd_configuration_map
  -r--rw---- 1 root net_bw_acct 0 2022-06-16 15:03 /sys/fs/bpf/netd_shared/map_netd_configuration_map

Bug: 235590615
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I21730e4fa22fbf0c94ab0ca5c5db03aa000b7680
2022-06-16 22:53:42 +00:00
Maciej Żenczykowski
21ce721bc0 BpfHandler.h - switch mStatsMapB from BpfMap to BpfMapRO
We'd like to switch more of these, but this is the only one
where tests don't get in our way.

We'll fix the rest later.  This gives us an *example* for
further testing.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9b280c7ba2d19644fc3cdd0c132de85f45df69b4
2022-06-13 19:39:06 -07:00
Treehugger Robot
7ded34d8fc Merge "remove spurious newlines on ALOG lines" 2022-05-24 00:09:43 +00:00
Maciej Żenczykowski
e0f5846897 remove spurious newlines on ALOG lines
(for consistency with rest of code base)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5660615f24daf4285e2b6cbacecb7cd99061c5f5
2022-05-23 20:53:12 +00:00
Ken Chen
322ffcb724 [NetworkStats] Return error on invalid value
Configuration map index 1(CURRENT_STATS_MAP_CONFIGURATION_KEY) can only
have value 0(SELECT_MAP_A) or 1(SELECT_MAP_B). Return error if it is any
other values. Otherwise, read out of array boundary can cause memory
corruption or security issues.

Bug: 231420457
Test: TH
Change-Id: Ia800ad78781f72b8118469c0230cc550796d334e
2022-05-23 22:27:40 +08:00
Maciej Żenczykowski
6ed2ab9b57 drop support for pre-4.14 kernels
S had a minimum kernel requirement of 4.9,
T bumps this up to 4.14, which adds net cgroup bpf support.

It's important to ship T with this otherwise we'll be forced
into trying to somehow support 4.9 kernels with T mainline
module updates for years and years, when we no longer have
any tests of the old code paths...

Test: TreeHugger
Bug: 232017472
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6e873f0815f176f5618278087132156c9974d6ea
2022-05-16 11:04:22 -07:00
Hungming Chen
bcc0f5b14f BpfHandler: only allow to tag INET/INET6 socket
This is a follow up commit for the family validation {INET, INET6}.
The protocol validation {TCP, UDP} has been added in previous
commit.

The TrafficController socket destroy listener only monitors
on the group {INET_TCP, INET_UDP, INET6_TCP, INET6_UDP}.
Tagging listener unsupported socket causes that the tag can't
be removed from tag map automatically. Eventually, the tag map
run out of space because of dead tag entries.

See TrafficController::makeSkDestroyListener in
packages/modules/Connectivity/service/native/TrafficController.cpp

Also address the comments from previous commit.
- Remove the useless else-statment in tagSocket protocol validation.
- Make the socket cookie query and test into one line in
  BpfHandlerTest#TestTagSocketWithUnsupportedProtocol

Bug: 223094609
Test: atest BpfHandlerTest
Change-Id: I0f571fc00caa01c86399f0dbb593e8a40ad94bbd
2022-03-07 16:04:32 +08:00
Hungming Chen
478c0eb21c BpfHandler: only allow to tag udp/tcp socket
The TrafficController socket destroy listener only monitors
on the group {INET_TCP, INET_UDP, INET6_TCP, INET6_UDP}.
Tagging listener unsupported socket causes that the tag can't
be removed from tag map automatically. Eventually, the tag map
run out of space because of dead tag entries.

See TrafficController::makeSkDestroyListener in
packages/modules/Connectivity/service/native/TrafficController.cpp

Bug: 223094609
Test: atest BpfHandlerTest
Change-Id: Icc19b7c9f37fef498b89f43e44767f6b9e931a5a
2022-03-07 16:03:05 +08:00
Hungming Chen
436547e368 [CLATJ#30] Not allow tagSocket() to tag with uid AID_CLAT
The process is not allowed to tag socket to AID_CLAT via tagSocket()
which would cause process data usage accounting to be bypassed.

Bug: 218407445
Test: atest BpfHandlerTest
Change-Id: Ia7b071e1065733da25e9f02f34ccd17f63653217
2022-03-04 17:21:23 +08:00
Ken Chen
1647f60d66 [NETD-BPF#19] Mainline part of bpf code from netd
1. Add libnetd_updatable.so in com.android.tethering. The library is
loaded by netd. Currently, it mainly targets on a few functions which
access BPF maps. The functionality may extend in the future.

2. Attach gcroup progs from libnetd_updatable.so.

3. Move (privileged)TagSocket and untagSocket implementation to mainline
module. Combine privilegedTagSocket and untagSocket into a single
function.

4. Split related unit tests from netd_unit_test to
libnetd_updatable_unit_test as well.

Bug: 202086915
Test: cd system/netd; atest
Test: atest TrafficStatsTest NetworkUsageStatsTest

Change-Id: Ib556458103a4cbb643c1342d9b689ac692160de0
2022-01-30 09:51:25 +08:00