Merge "NetBpfLoad: workaround for Xiaomi kernel uapi bug" into main

This commit is contained in:
Maciej Żenczykowski 2025-06-16 13:46:34 -07:00 • committed by Android (Google) Code Review
commit 798e2f5f87
2 changed files with 18 additions and 3 deletions

View file

@ -59,6 +59,7 @@
#include <android-base/unique_fd.h>
#include <android/api-level.h>
#define BPF_SUPPORT_CMD_FIXUP
#include "BpfSyscallWrappers.h"
#include "bpf/BpfUtils.h"
#include "bpf_map_def.h"
@ -1871,7 +1872,11 @@ static int doLoad(char** argv, char * const envp[]) {
// which causes bpfGetNextMapId to behave as bpfGetNextProgId,
// and thus it should return 0 with errno == ENOENT.
ALOGE("bpfGetNextMapId(final %d) returned %d errno %d", mapId, next, errno);
return 1;
if (next || errno != ENOENT) return 1;
if (isAtLeastT || isAtLeastKernelVersion(4, 20, 0)) return 1;
// implies Android S with 4.14 or 4.19 kernel
ALOGW("Enabling bpfCmdFixupIsNeeded.");
bpfCmdFixupIsNeeded = true;
}
} else { // implies S/T with 4.9 kernel
// nothing we can do.

View file

@ -34,18 +34,28 @@ inline uint64_t ptr_to_u64(const void * const x) {
return (uint64_t)(uintptr_t)x;
}
#ifdef BPF_SUPPORT_CMD_FIXUP
static bool bpfCmdFixupIsNeeded = false;
#else
static constexpr bool bpfCmdFixupIsNeeded = false;
#endif
inline long bpfCmdFixup(enum bpf_cmd cmd) {
return cmd + (cmd >= BPF_PROG_TEST_RUN && bpfCmdFixupIsNeeded);
}
/* Note: bpf_attr is a union which might have a much larger size then the anonymous struct portion
* of it that we are using. The kernel's bpf() system call will perform a strict check to ensure
* all unused portions are zero. It will fail with E2BIG if we don't fully zero bpf_attr.
*/
inline int bpf(enum bpf_cmd cmd, const bpf_attr& attr) {
return syscall(__NR_bpf, cmd, &attr, sizeof(attr));
return syscall(__NR_bpf, bpfCmdFixup(cmd), &attr, sizeof(attr));
}
// this version is meant for use with cmd's which mutate the argument
inline int bpf(enum bpf_cmd cmd, bpf_attr *attr) {
return syscall(__NR_bpf, cmd, attr, sizeof(*attr));
return syscall(__NR_bpf, bpfCmdFixup(cmd), attr, sizeof(*attr));
}
inline int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size,