forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_crypto_kern.c
146 lines (128 loc) · 3.83 KB
/
test_crypto_kern.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: GPL-2.0
// test crypto
//
// Copyright (C) 2024 Haojian Zhuang <haojian.zhuang@linaro.org>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <string.h>
volatile int my_pid_var = 0;
volatile int res_var = 0;
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, int);
__type(value, int);
__uint(max_entries, 2);
} hash_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
__uint(max_entries, 256 * 1024);
} user_rb SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 4096);
} kern_rb SEC(".maps");
static int dump_md5_digest(void *digest, size_t digest_sz)
{
int i, offset;
char out[64] = {};
__u64 tmp;
unsigned char c;
for (i = 0, offset = 0; i < digest_sz; i++) {
if (i % 8 == 0)
tmp = *(__u64 *)(digest + i);
c = (tmp & 0xf0) >> 4;
if ((c >= 0) && (c <= 9)) {
out[offset++] = 0x30 + c;
} else if ((c >= 0xa) && (c <= 0xf)) {
out[offset++] = 0x61 + c - 10;
} else {
bpf_printk("wc[%d]:%x, whole value:0x%llx\n", i, c, *(__u64 *)digest);
}
c = tmp & 0x0f;
if ((c >= 0) && (c <= 9)) {
out[offset++] = 0x30 + c;
} else if ((c >= 0xa) && (c <= 0xf)) {
out[offset++] = 0x61 + c - 10;
} else {
bpf_printk("wc[%d]:%x, whole value:0x%llx\n", i, c, *(__u64 *)digest);
}
tmp = tmp >> 8;
if (i < digest_sz - 1)
out[offset++] = '-';
}
out[offset] = '\0';
bpf_printk("digest:%s\n", out);
return 0;
}
char src[4096];
#define USER_DATA_LEN 22
#define MD5_DIGEST_LEN 16
static long do_md5(struct bpf_dynptr *dynptr, void *ctx)
{
long ret;
char digest[MD5_DIGEST_LEN] = {};
__u64 handle;
char fmt[] = "tfm:0x%llx, ret:%d\n";
bpf_printk("entering do_md5()\n");
ret = bpf_dynptr_read(&src, USER_DATA_LEN, dynptr, 0, 0);
if (ret) {
bpf_printk("fail to read, ret:%d\n", ret);
return 1;
}
/* trigger MD5 */
ret = bpf_crypto_alloc_shash("md5", 0, 0, &handle);
bpf_trace_printk(fmt, sizeof(fmt), handle, ret);
if (ret != 0) {
bpf_printk("exit do_md5(), ret:%d\n", ret);
return 1;
}
ret = bpf_crypto_shash_digest(handle, src, USER_DATA_LEN,
(void *)&digest, MD5_DIGEST_LEN);
dump_md5_digest(digest, MD5_DIGEST_LEN);
bpf_ringbuf_output(&kern_rb, digest, MD5_DIGEST_LEN, 0);
bpf_crypto_free_shash(handle);
return 0;
}
SEC("tracepoint/syscalls/sys_enter_getpgid")
int handle_user_ringbuf()
{
long status = 0;
long cons_pos, prod_pos, avail_data, rb_size;
char fmt3[] = "rb_size:%d, avail_data:%d\n";
char fmt4[] = "cons_pos:%d, prod_pos:%d\n";
bpf_printk("entering handle_user_ringbuf()\n");
cons_pos = bpf_ringbuf_query(&kern_rb, BPF_RB_CONS_POS);
prod_pos = bpf_ringbuf_query(&kern_rb, BPF_RB_PROD_POS);
avail_data = bpf_ringbuf_query(&kern_rb, BPF_RB_AVAIL_DATA);
rb_size = bpf_ringbuf_query(&kern_rb, BPF_RB_RING_SIZE);
bpf_trace_printk(fmt3, sizeof(fmt3), rb_size, avail_data);
bpf_trace_printk(fmt4, sizeof(fmt4), cons_pos, prod_pos);
status = bpf_user_ringbuf_drain(&user_rb, do_md5, NULL, 0);
bpf_printk("status:%ld\n", status);
if (status < 0) {
bpf_printk("drain user ringbuf returned %ld\n", status);
return -1;
}
return 0;
}
int bpf_md5()
{
__u64 handle;
int ret;
long cons_pos, prod_pos, avail_data, rb_size;
char fmt[] = "tfm:0x%llx, ret:%x\n";
char fmt3[] = "rb_size:%d, avail_data:%d\n";
char fmt4[] = "cons_pos:%d, prod_pos:%d\n";
cons_pos = bpf_ringbuf_query(&kern_rb, BPF_RB_CONS_POS);
prod_pos = bpf_ringbuf_query(&kern_rb, BPF_RB_PROD_POS);
avail_data = bpf_ringbuf_query(&kern_rb, BPF_RB_AVAIL_DATA);
rb_size = bpf_ringbuf_query(&kern_rb, BPF_RB_RING_SIZE);
bpf_trace_printk(fmt3, sizeof(fmt3), rb_size, avail_data);
bpf_trace_printk(fmt4, sizeof(fmt4), cons_pos, prod_pos);
ret = bpf_crypto_alloc_shash("md5", 0, 0, &handle);
bpf_trace_printk(fmt, sizeof(fmt), handle, ret);
bpf_crypto_free_shash(handle);
return ret;
}
char _license[] SEC("license") = "GPL";