eBPF-based security platform
treewide: follow the coding conventions in the policy code
Daniel Borkmann962eb0c34683
diff --git a/bpf/seal.c b/bpf/seal.cindex 2f29704b..7dd38e2d 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -139,8 +139,8 @@ void BPF_PROG(shirudo_seal_bprm_committed_creds,const struct linux_binprm *bprm){struct task_struct *task = bpf_get_current_task_btf();- struct task_policy *policy;struct task_authority *authority;+ struct task_policy *policy;policy = bpf_task_storage_get(&policy_map, task, NULL, 0);if (policy) {@@ -161,10 +161,10 @@ int BPF_PROG(shirudo_seal_task_alloc, struct task_struct *child,__u64 clone_flags, int ret){struct task_struct *parent = bpf_get_current_task_btf();- struct task_policy *parent_policy;struct task_authority *parent_authority;- struct task_policy *policy;+ struct task_policy *parent_policy;struct task_authority *authority;+ struct task_policy *policy;if (ret)return ret;@@ -539,13 +539,13 @@ int BPF_PROG(shirudo_seal_bprm_check_security,{struct task_struct *task = bpf_get_current_task_btf();struct task_authority *authority;- struct task_policy *policy;struct seal_scratch *scratch;- struct bpf_dynptr tdp;+ struct task_policy *policy;struct inode *real_inode;+ struct bpf_dynptr tdp;+ bool trusted;__u64 value;int len;- bool trusted;if (ret)return ret;diff --git a/builtin-upatch.c b/builtin-upatch.cindex 91999996..1ec2f9a1 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1589,26 +1589,26 @@ static int open_shared_policy_map(const char *pin, __u32 type,{struct bpf_map_info info = {};__u32 info_len = sizeof(info);- int fd, rc;+ int fd, ret;fd = bpf_obj_get(pin);if (fd < 0)return -errno;- rc = bpf_map_get_info_by_fd(fd, &info, &info_len);- if (rc) {- rc = -errno;+ ret = bpf_map_get_info_by_fd(fd, &info, &info_len);+ if (ret) {+ ret = -errno;goto err;}if (info.type != type || info.key_size != sizeof(__s32) ||info.value_size != value_size || info.map_flags != map_flags) {- rc = -EINVAL;+ ret = -EINVAL;goto err;}if (fd == 0) {int copy_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);if (copy_fd < 0) {- rc = -errno;+ ret = -errno;goto err;}close(fd);@@ -1617,7 +1617,7 @@ static int open_shared_policy_map(const char *pin, __u32 type,return fd;err:close(fd);- return rc;+ return ret;}/*diff --git a/testing/end2end/policy_exec.c b/testing/end2end/policy_exec.cindex a3ca06ac..9261ec38 100644--- a/testing/end2end/policy_exec.c+++ b/testing/end2end/policy_exec.c@@ -25,8 +25,8 @@ static int exec_child(const char *path, const char *name,const char *mode, const char *arg){char *const argv[] = {(char *)name, (char *)mode, (char *)arg, NULL};- pid_t pid;int status;+ pid_t pid;pid = fork();if (pid < 0)@@ -64,7 +64,7 @@ static int listen_loopback(int *port)static int socket_connect(const char *name, const char *port_string){struct sockaddr_in address = {};- int fd, port, rc;+ int fd, port, err;port = atoi(port_string);fd = socket(AF_INET, SOCK_STREAM, 0);@@ -73,14 +73,13 @@ static int socket_connect(const char *name, const char *port_string)address.sin_family = AF_INET;address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);address.sin_port = htons(port);- rc = connect(fd, (struct sockaddr *)&address, sizeof(address));- if (!rc) {+ if (!connect(fd, (struct sockaddr *)&address, sizeof(address))) {close(fd);return !strcmp(name, "socket-a") ? 28 : 29;}- rc = errno;+ err = errno;close(fd);- return !strcmp(name, "socket-b") && rc == EACCES ? 100 : 101;+ return !strcmp(name, "socket-b") && err == EACCES ? 100 : 101;}static void report(int *failed, int number, int passed, const char *title)@@ -95,11 +94,11 @@ static void report(int *failed, int number, int passed, const char *title)int main(int argc, char **argv){+ int listener, accepted, port, port_len, socket_a, socket_b;const char *name = basename(argv[0]);- char port_string[8];struct sockaddr_in peer = {};socklen_t peer_len = sizeof(peer);- int listener, accepted, port, port_len, socket_a, socket_b;+ char port_string[8];int socket_b_probe;int failed = 0;diff --git a/testing/helper/policy-probe.c b/testing/helper/policy-probe.cindex a9ea9c08..252463e9 100644--- a/testing/helper/policy-probe.c+++ b/testing/helper/policy-probe.c@@ -63,7 +63,7 @@ static int read_policy(const char *pin, __u64 *active){struct task_policy policy = {};union bpf_attr attr = {};- int fd, pidfd, rc;+ int fd, pidfd, ret;pidfd = syscall(SYS_pidfd_open, getpid(), 0);if (pidfd < 0)@@ -72,30 +72,30 @@ static int read_policy(const char *pin, __u64 *active)attr.file_flags = BPF_F_RDONLY;fd = syscall(SYS_bpf, BPF_OBJ_GET, &attr, sizeof(attr));if (fd < 0) {- rc = -errno;+ ret = -errno;close(pidfd);- return rc;+ return ret;}memset(&attr, 0, sizeof(attr));attr.map_fd = fd;attr.key = (uintptr_t)&pidfd;attr.value = (uintptr_t)&policy;- rc = syscall(SYS_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)) ?+ ret = syscall(SYS_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)) ?-errno : 0;close(fd);close(pidfd);- if (!rc)+ if (!ret)*active = policy.active;- return rc;+ return ret;}static int read_socket_policy(int socket_fd, __u64 *active){union bpf_attr attr = {};__u64 value = 0;- int fd, rc;+ int fd, ret;- attr.pathname = (uintptr_t)"/run/shirudo/bpffs/socket_policy_map";+ attr.pathname = (uintptr_t)SOCKET_POLICY_PIN;attr.file_flags = BPF_F_RDONLY;fd = syscall(SYS_bpf, BPF_OBJ_GET, &attr, sizeof(attr));if (fd < 0)@@ -104,15 +104,15 @@ static int read_socket_policy(int socket_fd, __u64 *active)attr.map_fd = fd;attr.key = (uintptr_t)&socket_fd;attr.value = (uintptr_t)&value;- rc = syscall(SYS_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));- if (rc && errno != ENOENT)- rc = -errno;+ ret = syscall(SYS_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));+ if (ret && errno != ENOENT)+ ret = -errno;else- rc = 0;+ ret = 0;close(fd);- if (!rc)+ if (!ret)*active = value;- return rc;+ return ret;}static int check_socket_policy(int socket_fd, __u64 expected)@@ -122,18 +122,29 @@ static int check_socket_policy(int socket_fd, __u64 expected)return read_socket_policy(socket_fd, &actual) || actual != expected;}-static int parse_policy(const char *text, __u64 *value);+static int parse_policy(const char *text, __u64 *value)+{+ __u64 parsed;+ char *end;++ errno = 0;+ parsed = strtoull(text, &end, 0);+ if (errno || end == text || *end)+ return -EINVAL;+ *value = parsed;+ return 0;+}static int accept_child(void){- __u64 active = 0;- __u64 task_bits, socket_bits;const char *listener_text = getenv("SHIRUDO_TEST_LISTENER");- const char *task_text = getenv("SHIRUDO_TEST_TASK_BITS");const char *socket_text = getenv("SHIRUDO_TEST_SOCKET_BITS");- char *end;+ const char *task_text = getenv("SHIRUDO_TEST_TASK_BITS");+ __u64 task_bits, socket_bits;+ __u64 active = 0;long listener;int accepted;+ char *end;if (!listener_text || !task_text || !socket_text ||parse_policy(task_text, &task_bits) ||@@ -141,7 +152,7 @@ static int accept_child(void)return 1;listener = strtol(listener_text, &end, 10);if (*end || listener < 0 ||- read_policy("/run/shirudo/bpffs/policy_map", &active) ||+ read_policy(POLICY_PIN, &active) ||active != task_bits)return 1;accepted = accept((int)listener, NULL, NULL);@@ -152,15 +163,15 @@ static int accept_child(void)static int socket_inheritance(const char *script, __u64 extra){- struct sockaddr_in addr = {};static const char contents[] ="#!/run/shirudo/runtime/usr/bin/shirudo-policy-probe accept-child\n";+ int listener, client, status, ret, length;char acceptor[PATH_MAX], number[32];+ struct sockaddr_in addr = {};__u64 active = 0;- int listener, client, status, rc, length;pid_t child;- if (read_policy("/run/shirudo/bpffs/policy_map", &active) || !active)+ if (read_policy(POLICY_PIN, &active) || !active)return 1;listener = socket(AF_INET, SOCK_STREAM, 0);if (listener < 0 || check_socket_policy(listener, active))@@ -170,9 +181,9 @@ static int socket_inheritance(const char *script, __u64 extra)if (bind(listener, (struct sockaddr *)&addr, sizeof(addr)) ||listen(listener, 1))return 1;- rc = getsockname(listener, (struct sockaddr *)&addr,- (socklen_t[]){ sizeof(addr) });- if (rc)+ ret = getsockname(listener, (struct sockaddr *)&addr,+ (socklen_t[]){ sizeof(addr) });+ if (ret)return 1;length = snprintf(acceptor, sizeof(acceptor), "%s.acceptor", script);if (length < 0 || (size_t)length >= sizeof(acceptor))@@ -227,19 +238,6 @@ static int socket_inheritance(const char *script, __u64 extra)return !WIFEXITED(status) || WEXITSTATUS(status);}-static int parse_policy(const char *text, __u64 *value)-{- char *end;- __u64 parsed;-- errno = 0;- parsed = strtoull(text, &end, 0);- if (errno || end == text || *end)- return -EINVAL;- *value = (__u64)parsed;- return 0;-}-static int set_policy(const char *path, const char *text){__u64 value;@@ -265,7 +263,7 @@ static int check_policy(const char *text)__u64 expected, active = 0;if (parse_policy(text, &expected) ||- read_policy("/run/shirudo/bpffs/policy_map", &active))+ read_policy(POLICY_PIN, &active))return 1;return active != expected;}diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 27b68e84..be9f6d63 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -361,10 +361,10 @@ static int build_target_ok(const char *target);static int build_parse_spec(struct bpm_build_spec *spec, char *body){struct bpm_build_upatch *upatch = NULL, *tree_owner = NULL;- struct bpm_build_rodata *rodata = NULL;- struct tree_selector *sel = NULL;struct bpm_build_policy *policy = NULL;+ struct bpm_build_rodata *rodata = NULL;struct bpm_build_file *file = NULL;+ struct tree_selector *sel = NULL;bool tree_section = false;char *save = NULL, *line;char section[48] = {};@@ -1243,13 +1243,13 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,for (size_t i = 0; i < spec->n_upatches; i++) {char source[PATH_MAX], load[PATH_MAX], unload[PATH_MAX];char object[PATH_MAX], name[UPATCH_NAME_MAX];+ struct upatch_policy_label_entry *labels = NULL;struct bpm_build_upatch *u = &spec->upatches[i];const char *bpf_o = source, *src_path = NULL;__u8 payload[CRYPTO_SHA256_LEN];struct upatch parsed = {};- int ret;- struct upatch_policy_label_entry *labels = NULL;__u32 nr_labels = 0;+ int ret;if (build_resolve_source(spec, u->source, source, sizeof(source),&(struct stat){}))diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex 2e689f5c..2624ef4e 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -1769,17 +1769,17 @@ static int check_upatch_name(const char *name)int build_package_load(const char *bpf_o, const char *src_path,const char *out_path, const char *upatch_name,- const struct upatch_policy_label_entry *policies,- __u32 nr_policies,- const char *pkey_path, const char *cert_path)+ const struct upatch_policy_label_entry *policies,+ __u32 nr_policies,+ const char *pkey_path, const char *cert_path){- struct bpf_object *obj;+ __u32 label_len = 0, policy_map_idx = 0, socket_policy_map_idx = 0;struct upatch_policy_label_header *label_blob = NULL;- struct upatch_policy_label_entry *label_entries;struct bpf_map *map, *policy_map, *socket_policy_map;- __u32 label_len = 0, policy_map_idx = 0, socket_policy_map_idx = 0;- __u32 map_idx = 0;+ struct upatch_policy_label_entry *label_entries;int handler_ids[2] = {-1, -1};+ struct bpf_object *obj;+ __u32 map_idx = 0;int err;err = check_upatch_name(upatch_name);diff --git a/util/image_guards.h b/util/image_guards.hindex 232d1ebc..2b5872f0 100644--- a/util/image_guards.h+++ b/util/image_guards.h@@ -13,6 +13,7 @@#define MOUNT_HOOK_COUNT 5#define OBSERVER_HOOK_COUNT 9#define IMAGE_OBSERVER_HOOK_COUNT 4+struct image_guard {const char *prog;const char *pin;