1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
6 * Author: Roberto Sassu <roberto.sassu@huawei.com>
11 #include <bpf/bpf_helpers.h>
12 #include <bpf/bpf_tracing.h>
14 #define MAX_DATA_SIZE (1024 * 1024)
15 #define MAX_SIG_SIZE 1024
17 extern struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym;
18 extern struct bpf_key *bpf_lookup_system_key(__u64 id) __ksym;
19 extern void bpf_key_put(struct bpf_key *key) __ksym;
20 extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
21 struct bpf_dynptr *sig_ptr,
22 struct bpf_key *trusted_keyring) __ksym;
25 __u32 user_keyring_serial;
26 __u64 system_keyring_id;
29 __u8 data[MAX_DATA_SIZE];
31 __u8 sig[MAX_SIG_SIZE];
36 __uint(type, BPF_MAP_TYPE_ARRAY);
37 __uint(max_entries, 1);
39 __type(value, struct data);
40 } data_input SEC(".maps");
42 char _license[] SEC("license") = "GPL";
45 int BPF_PROG(bpf, int cmd, union bpf_attr *attr, unsigned int size)
47 struct bpf_dynptr data_ptr, sig_ptr;
48 struct data *data_val;
49 struct bpf_key *trusted_keyring;
54 pid = bpf_get_current_pid_tgid() >> 32;
55 if (pid != monitored_pid)
58 data_val = bpf_map_lookup_elem(&data_input, &zero);
62 ret = bpf_probe_read_kernel(&value, sizeof(value), &attr->value);
66 ret = bpf_copy_from_user(data_val, sizeof(struct data),
67 (void *)(unsigned long)value);
71 if (data_val->data_len > sizeof(data_val->data))
74 bpf_dynptr_from_mem(data_val->data, data_val->data_len, 0, &data_ptr);
76 if (data_val->sig_len > sizeof(data_val->sig))
79 bpf_dynptr_from_mem(data_val->sig, data_val->sig_len, 0, &sig_ptr);
81 if (user_keyring_serial)
82 trusted_keyring = bpf_lookup_user_key(user_keyring_serial, 0);
84 trusted_keyring = bpf_lookup_system_key(system_keyring_id);
89 ret = bpf_verify_pkcs7_signature(&data_ptr, &sig_ptr, trusted_keyring);
91 bpf_key_put(trusted_keyring);