eBPF-based security platform
util: read the identity record relative to a directory
Daniel Borkmanne95b5e012e76
diff --git a/util/identity.c b/util/identity.cindex 36f79c3a..f2615b4b 100644--- a/util/identity.c+++ b/util/identity.c@@ -6,6 +6,7 @@*/#include <errno.h>+#include <fcntl.h>#include <string.h>#include <sys/prctl.h>#include <unistd.h>@@ -31,27 +32,38 @@ int enforcement_active(void)return errno == EINVAL || errno == ENOSYS ? 0 : -errno;}-int identity_read(struct identity *out)+static int identity_map_read(int fd, struct identity *out){struct bpf_map_info info = {};__u32 len = sizeof(info);__u32 zero = 0;++ if (bpf_map_get_info_by_fd(fd, &info, &len))+ return -errno;+ if (info.value_size != sizeof(*out))+ return -EPROTO;+ if (bpf_map_lookup_elem(fd, &zero, out))+ return -errno;+ return 0;+}++int identity_read_at(int dirfd, const char *name, struct identity *out)+{+ LIBBPF_OPTS(bpf_obj_get_opts, opts,+ .file_flags = BPF_F_PATH_FD,+ .path_fd = dirfd);int fd, ret;memset(out, 0, sizeof(*out));- fd = bpf_obj_get(IDENTITY_PIN);+ fd = bpf_obj_get_opts(name, &opts);if (fd < 0)return -errno;- ret = bpf_map_get_info_by_fd(fd, &info, &len);- if (ret) {- ret = -errno;- } else if (info.value_size != sizeof(*out)) {- ret = -EPROTO;- } else {- ret = bpf_map_lookup_elem(fd, &zero, out);- if (ret)- ret = -errno;- }+ ret = identity_map_read(fd, out);close(fd);return ret;}++int identity_read(struct identity *out)+{+ return identity_read_at(AT_FDCWD, IDENTITY_PIN, out);+}diff --git a/util/identity.h b/util/identity.hindex 8d21be66..fc3e712c 100644--- a/util/identity.h+++ b/util/identity.h@@ -81,12 +81,16 @@ struct mount_record {BUILD_BUG_ON(sizeof(struct mount_record) != 8);+/* 1 when enforcement is active, 0 before init, otherwise -errno. */+int enforcement_active(void);+/** The identity map init populated, froze and pinned this boot. -ENOENT* when init has not run, another -errno when the pin cannot be read.+ * identity_read_at() takes the pin's name relative to dirfd, as openat()+ * does.*/-/* 1 when enforcement is active, 0 before init, otherwise -errno. */-int enforcement_active(void);int identity_read(struct identity *out);+int identity_read_at(int dirfd, const char *name, struct identity *out);#endif /* SHIRUDO_UTIL_IDENTITY_H */diff --git a/util/paths.h b/util/paths.hindex dbe015ec..e2968699 100644--- a/util/paths.h+++ b/util/paths.h@@ -105,7 +105,8 @@#define SEAL_REMOVE_ACL_PIN BPFFS_DIR "/seal_inode_remove_acl_link"#define SEAL_FILE_SETATTR_PIN BPFFS_DIR "/seal_inode_file_setattr_link"#define BPFFS_UPATCHES_DIR BPFFS_DIR "/upatches"-#define IDENTITY_PIN BPFFS_DIR "/identity"+#define IDENTITY_PIN_NAME "identity"+#define IDENTITY_PIN BPFFS_DIR "/" IDENTITY_PIN_NAME#define TAMPER_PIN BPFFS_DIR "/tamper"#define INTEGRITY_PIN BPFFS_DIR "/integrity"#define POLICY_PIN BPFFS_DIR "/task_policy"