From 5e4aabfcaae66b6a169dfe4fb623c2f4d9da5b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Mon, 27 Jun 2022 01:15:53 -0700 Subject: [PATCH] Perform map chmod *before* chown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After we chown, we potentially are no longer the owner (if we chown'ed to a different uid then AID_ROOT) and thus can no longer chmod. If we chmod before we chown, this problem is eliminated. This fixes uid != AID_ROOT for maps. Bug: 218408035 Bug: 237716689 Test: TreeHugger Signed-off-by: Maciej Żenczykowski Change-Id: Ifbbe37d8dceff245d4def950712f1847615d5856 --- libbpf_android/Loader.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp index 2983c76..2c0db08 100644 --- a/libbpf_android/Loader.cpp +++ b/libbpf_android/Loader.cpp @@ -30,9 +30,9 @@ #include #include -// This is BpfLoader v0.20 +// This is BpfLoader v0.21 #define BPFLOADER_VERSION_MAJOR 0u -#define BPFLOADER_VERSION_MINOR 20u +#define BPFLOADER_VERSION_MINOR 21u #define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR) #include "bpf/BpfUtils.h" @@ -873,13 +873,6 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector& ret = bpf_obj_pin(fd, mapPinLoc.c_str()); if (ret) return -errno; } - ret = chown(mapPinLoc.c_str(), (uid_t)md[i].uid, (gid_t)md[i].gid); - if (ret) { - int err = errno; - ALOGE("chown(%s, %u, %u) = %d [%d:%s]", mapPinLoc.c_str(), md[i].uid, md[i].gid, - ret, err, strerror(err)); - return -err; - } ret = chmod(mapPinLoc.c_str(), md[i].mode); if (ret) { int err = errno; @@ -887,6 +880,13 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector& strerror(err)); return -err; } + ret = chown(mapPinLoc.c_str(), (uid_t)md[i].uid, (gid_t)md[i].gid); + if (ret) { + int err = errno; + ALOGE("chown(%s, %u, %u) = %d [%d:%s]", mapPinLoc.c_str(), md[i].uid, md[i].gid, + ret, err, strerror(err)); + return -err; + } } struct bpf_map_info map_info = {};