summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/test_verify_pkcs7_sig.c
blob: 7748cc23de8a903ec30f3cd654577775ccfb4983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
 *
 * Author: Roberto Sassu <roberto.sassu@huawei.com>
 */

#include "vmlinux.h"
#include <errno.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#define MAX_DATA_SIZE (1024 * 1024)
#define MAX_SIG_SIZE 1024

extern struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym;
extern struct bpf_key *bpf_lookup_system_key(__u64 id) __ksym;
extern void bpf_key_put(struct bpf_key *key) __ksym;
extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
				      struct bpf_dynptr *sig_ptr,
				      struct bpf_key *trusted_keyring) __ksym;

__u32 monitored_pid;
__u32 user_keyring_serial;
__u64 system_keyring_id;

struct data {
	__u8 data[MAX_DATA_SIZE];
	__u32 data_len;
	__u8 sig[MAX_SIG_SIZE];
	__u32 sig_len;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, __u32);
	__type(value, struct data);
} data_input SEC(".maps");

char _license[] SEC("license") = "GPL";

SEC("lsm.s/bpf")
int BPF_PROG(bpf, int cmd, union bpf_attr *attr, unsigned int size)
{
	struct bpf_dynptr data_ptr, sig_ptr;
	struct data *data_val;
	struct bpf_key *trusted_keyring;
	__u32 pid;
	__u64 value;
	int ret, zero = 0;

	pid = bpf_get_current_pid_tgid() >> 32;
	if (pid != monitored_pid)
		return 0;

	data_val = bpf_map_lookup_elem(&data_input, &zero);
	if (!data_val)
		return 0;

	ret = bpf_probe_read_kernel(&value, sizeof(value), &attr->value);
	if (ret)
		return ret;

	ret = bpf_copy_from_user(data_val, sizeof(struct data),
				 (void *)(unsigned long)value);
	if (ret)
		return ret;

	if (data_val->data_len > sizeof(data_val->data))
		return -EINVAL;

	bpf_dynptr_from_mem(data_val->data, data_val->data_len, 0, &data_ptr);

	if (data_val->sig_len > sizeof(data_val->sig))
		return -EINVAL;

	bpf_dynptr_from_mem(data_val->sig, data_val->sig_len, 0, &sig_ptr);

	if (user_keyring_serial)
		trusted_keyring = bpf_lookup_user_key(user_keyring_serial, 0);
	else
		trusted_keyring = bpf_lookup_system_key(system_keyring_id);

	if (!trusted_keyring)
		return -ENOENT;

	ret = bpf_verify_pkcs7_signature(&data_ptr, &sig_ptr, trusted_keyring);

	bpf_key_put(trusted_keyring);

	return ret;
}