From 0fff839ae65147f1a5bfd282ceca15073df43366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Sat, 15 Jun 2024 02:43:12 -0700 Subject: [PATCH] introduce mapRetrieveLocklessRW() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the clat verifyPerms() checker only wants to check the existence and accessibility of the maps. Test: TreeHugger Signed-off-by: Maciej Żenczykowski Change-Id: I3079ab316bcd88370d774fec5e88c1b5ff438266 --- ...om_android_server_connectivity_ClatCoordinator.cpp | 3 ++- .../bpf_syscall_wrappers/include/BpfSyscallWrappers.h | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp index 18a274a12d..c0082bbe3a 100644 --- a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp +++ b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp @@ -80,7 +80,8 @@ static void verifyPerms(const char * const path, case VERIFY_BIN: return; case VERIFY_PROG: fd = bpf::retrieveProgram(path); break; case VERIFY_MAP_RO: fd = bpf::mapRetrieveRO(path); break; - case VERIFY_MAP_RW: fd = bpf::mapRetrieveRW(path); break; + // lockless: we're just checking access rights and will immediately close the fd + case VERIFY_MAP_RW: fd = bpf::mapRetrieveLocklessRW(path); break; } if (fd < 0) ALOGF("bpf_obj_get '%s' failed, errno=%d", path, errno); diff --git a/staticlibs/native/bpf_syscall_wrappers/include/BpfSyscallWrappers.h b/staticlibs/native/bpf_syscall_wrappers/include/BpfSyscallWrappers.h index 5bb3100a78..73cef8913d 100644 --- a/staticlibs/native/bpf_syscall_wrappers/include/BpfSyscallWrappers.h +++ b/staticlibs/native/bpf_syscall_wrappers/include/BpfSyscallWrappers.h @@ -166,11 +166,16 @@ inline int bpfLock(int fd, short type) { return ret; // most likely -1 with errno == EAGAIN, due to already held lock } -inline int mapRetrieveExclusiveRW(const char* pathname) { - return bpfLock(bpfFdGet(pathname, 0), F_WRLCK); +inline int mapRetrieveLocklessRW(const char* pathname) { + return bpfFdGet(pathname, 0); } + +inline int mapRetrieveExclusiveRW(const char* pathname) { + return bpfLock(mapRetrieveLocklessRW(pathname), F_WRLCK); +} + inline int mapRetrieveRW(const char* pathname) { - return bpfLock(bpfFdGet(pathname, 0), F_RDLCK); + return bpfLock(mapRetrieveLocklessRW(pathname), F_RDLCK); } inline int mapRetrieveRO(const char* pathname) {