Perform map chmod *before* chown

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 <maze@google.com>
Change-Id: Ifbbe37d8dceff245d4def950712f1847615d5856
This commit is contained in:
Maciej Żenczykowski 2022-06-27 01:15:53 -07:00
commit 5e4aabfcaa

View file

@ -30,9 +30,9 @@
#include <sys/wait.h>
#include <unistd.h>
// 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<unique_fd>&
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<unique_fd>&
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 = {};