forked from LTD-Beget/tcpsecrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpsecrets.c
executable file
·165 lines (141 loc) · 4.3 KB
/
tcpsecrets.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/kallsyms.h>
#include <linux/cryptohash.h>
#include <linux/ftrace.h>
#include <linux/version.h>
#include <net/tcp.h>
#include "system_map.inc"
#ifndef SYNCOOKIE_SECRET_ADDR
#define SYNCOOKIE_SECRET_ADDR 0x0
#endif
static void *cookie_v4_check_ptr;
static u32 (*syncookie_secret_ptr)[2][16-4+SHA_DIGEST_WORDS] = (void*)SYNCOOKIE_SECRET_ADDR;
static struct proc_dir_entry *proc_entry;
static int tcp_secrets_show(struct seq_file *m, void *v)
{
int i, j;
seq_printf(m, "%lu %lu %d\n", (unsigned long) jiffies, (unsigned long)tcp_cookie_time(), HZ);
for(i = 0; i < 2; i++) {
for(j = 0; j < 16-4+SHA_DIGEST_WORDS; j++) {
seq_printf(m, "%.8x.", (*syncookie_secret_ptr)[i][j]);
}
seq_printf(m, "\n");
}
return 0;
}
static int tcp_secrets_open(struct inode *inode, struct file *file)
{
return single_open(file, tcp_secrets_show, NULL);
}
static const struct file_operations tcp_secrets_fops = {
.open = tcp_secrets_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int symbol_walk_callback(void *data, const char *name,
struct module *mod, unsigned long addr) {
if (mod)
return 0;
if (strcmp(name, "cookie_v4_check") == 0) {
cookie_v4_check_ptr = (void *)addr;
}
if (strcmp(name, "syncookie_secret") == 0) {
syncookie_secret_ptr = (void *)addr;
}
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
static struct sock *cookie_v4_check_wrapper(struct sock *sk,
struct sk_buff *skb,
struct ip_options *opt)
{
struct sock* (*old_func)(struct sock *sk, struct sk_buff *skb, struct ip_options *opt) =
(void*)((unsigned long)cookie_v4_check_ptr + MCOUNT_INSN_SIZE);
extern int sysctl_tcp_syncookies;
if (sysctl_tcp_syncookies == 2) {
tcp_synq_overflow(sk);
}
return old_func(sk, skb, opt);
}
#else
static struct sock *cookie_v4_check_wrapper(struct sock *sk,
struct sk_buff *skb) {
struct sock* (*old_func)(struct sock *sk, struct sk_buff *skb) =
(void*)((unsigned long)cookie_v4_check_ptr + MCOUNT_INSN_SIZE);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
extern int sysctl_tcp_syncookies;
if (sysctl_tcp_syncookies == 2) {
#else
if (sock_net(sk)->ipv4.sysctl_tcp_syncookies == 2) {
#endif
tcp_synq_overflow(sk);
}
return old_func(sk, skb);
}
#endif
static void notrace
tcpsecrets_ftrace_handler(unsigned long ip, unsigned long parent_ip,
struct ftrace_ops *fops, struct pt_regs *regs)
{
regs->ip = (unsigned long)cookie_v4_check_wrapper;
}
static struct ftrace_ops tcpsecrets_ftrace_ops __read_mostly = {
.func = tcpsecrets_ftrace_handler,
.flags = FTRACE_OPS_FL_SAVE_REGS,
};
static void fix_cookie_v4_check(void) {
int ret;
ret = ftrace_set_filter_ip(&tcpsecrets_ftrace_ops, (unsigned long)cookie_v4_check_ptr, 0, 0);
if (ret) {
printk("cant set ftrace filter\n");
}
ret = register_ftrace_function(&tcpsecrets_ftrace_ops);
if (ret) {
printk("cant set ftrace function\n");
}
}
static int __init tcp_secrets_init(void)
{
int rc = kallsyms_on_each_symbol(symbol_walk_callback, NULL);
if (rc)
return rc;
if (cookie_v4_check_ptr) {
fix_cookie_v4_check();
} else {
printk("tcp_secrets: can't find cookie_v4_check function!\n");
return -1;
}
if (!syncookie_secret_ptr) {
printk("tcp_secrets: can't find syncookie secret!\n");
return -2;
}
return (proc_entry = proc_create("tcp_secrets", 0, NULL, &tcp_secrets_fops)) == NULL;
}
module_init(tcp_secrets_init);
static void __exit tcp_secrets_exit(void)
{
int ret;
if (cookie_v4_check_ptr) {
ret = unregister_ftrace_function(&tcpsecrets_ftrace_ops);
if (ret) {
printk("can't unregister ftrace\n");
}
ret = ftrace_set_filter_ip(&tcpsecrets_ftrace_ops, (unsigned long)cookie_v4_check_ptr, 1, 0);
if (ret) {
printk("can't unregister filter\n");
}
cookie_v4_check_ptr = 0;
}
syncookie_secret_ptr = 0;
if (proc_entry)
remove_proc_entry("tcp_secrets", 0);
}
module_exit(tcp_secrets_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alexander Polyakov <[email protected]>");
MODULE_DESCRIPTION("Provide access to tcp syncookie secrets via /proc/tcp_secrets");
MODULE_VERSION("1.1");