add bpf_map_def support for setting uid/gid/mode

Test: build, atest, adb shell ls -lZ /sys/fs/bpf
Bug: 149434314
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie4001fbe16b4bc84fc8ec7138ae4928cd86f5ce7
This commit is contained in:
Maciej Żenczykowski 2020-01-27 03:11:51 -08:00
parent 6f87896bdf
commit 83f2977da8
3 changed files with 29 additions and 11 deletions

View file

@ -24,6 +24,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <unistd.h> #include <unistd.h>
@ -411,7 +412,11 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>&
if (!reuse) { if (!reuse) {
ret = bpf_obj_pin(fd, mapPinLoc.c_str()); ret = bpf_obj_pin(fd, mapPinLoc.c_str());
if (ret < 0) return ret; if (ret) return -errno;
ret = chown(mapPinLoc.c_str(), (uid_t)md[i].uid, (gid_t)md[i].gid);
if (ret) return -errno;
ret = chmod(mapPinLoc.c_str(), md[i].mode);
if (ret) return -errno;
} }
mapFds.push_back(std::move(fd)); mapFds.push_back(std::move(fd));

View file

@ -53,16 +53,16 @@ static int (*bpf_map_delete_elem_unsafe)(const void* map,
const void* key) = (void*)BPF_FUNC_map_delete_elem; const void* key) = (void*)BPF_FUNC_map_delete_elem;
/* type safe macro to declare a map and related accessor functions */ /* type safe macro to declare a map and related accessor functions */
#define DEFINE_BPF_MAP_NO_ACCESSORS(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \ #define DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, usr, grp, md) \
const struct bpf_map_def SEC("maps") the_map = { \ const struct bpf_map_def SEC("maps") the_map = { \
.type = BPF_MAP_TYPE_##TYPE, \ .type = BPF_MAP_TYPE_##TYPE, \
.key_size = sizeof(TypeOfKey), \ .key_size = sizeof(TypeOfKey), \
.value_size = sizeof(TypeOfValue), \ .value_size = sizeof(TypeOfValue), \
.max_entries = (num_entries), \ .max_entries = (num_entries), \
}; .uid = (usr), \
.gid = (grp), \
#define DEFINE_BPF_MAP(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \ .mode = (md), \
DEFINE_BPF_MAP_NO_ACCESSORS(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \ }; \
\ \
static inline __always_inline __unused TypeOfValue* bpf_##the_map##_lookup_elem( \ static inline __always_inline __unused TypeOfValue* bpf_##the_map##_lookup_elem( \
const TypeOfKey* k) { \ const TypeOfKey* k) { \
@ -78,6 +78,15 @@ static int (*bpf_map_delete_elem_unsafe)(const void* map,
return bpf_map_delete_elem_unsafe(&the_map, k); \ return bpf_map_delete_elem_unsafe(&the_map, k); \
}; };
#define DEFINE_BPF_MAP(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \
DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, AID_ROOT, 0600)
#define DEFINE_BPF_MAP_GRO(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \
DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0640)
#define DEFINE_BPF_MAP_GRW(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \
DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0660)
static int (*bpf_probe_read)(void* dst, int size, void* unsafe_ptr) = (void*) BPF_FUNC_probe_read; static int (*bpf_probe_read)(void* dst, int size, void* unsafe_ptr) = (void*) BPF_FUNC_probe_read;
static int (*bpf_probe_read_str)(void* dst, int size, void* unsafe_ptr) = (void*) BPF_FUNC_probe_read_str; static int (*bpf_probe_read_str)(void* dst, int size, void* unsafe_ptr) = (void*) BPF_FUNC_probe_read_str;
static unsigned long long (*bpf_ktime_get_ns)(void) = (void*) BPF_FUNC_ktime_get_ns; static unsigned long long (*bpf_ktime_get_ns)(void) = (void*) BPF_FUNC_ktime_get_ns;

View file

@ -62,4 +62,8 @@ struct bpf_map_def {
// The following are not supported by the Android bpfloader: // The following are not supported by the Android bpfloader:
// unsigned int inner_map_idx; // unsigned int inner_map_idx;
// unsigned int numa_node; // unsigned int numa_node;
unsigned int uid; // uid_t
unsigned int gid; // gid_t
unsigned int mode; // mode_t
}; };