-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlaffun.c
278 lines (214 loc) · 5.64 KB
/
laffun.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
LAF - Linux Application Firewall (for linux Intel 32 and 64bits and ARM 32 bits)
laffun (user space LAF common functions)
Copyright 2015-2016 by @sha0coder and @capi_x
Licensed under GNU General Public License 3.0 or later.
Some rights reserved. See COPYING, AUTHORS.
@license GPL-3.0 <http://www.gnu.org/licenses/gpl-3.0.txt>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/sysctl.h>
#include <linux/netlink.h>
#include "laf.h"
#include "laffun.h"
int open_netlink(void)
{
int sock;
struct sockaddr_nl addr;
int group = NETLINK_LAF_GRP;
sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_LAF_USR);
if (sock < 0) {
fprintf(stderr, "error: can't open socket.\n");
return -1;
}
memset((void *) &addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
printf("bind < 0.\n");
return -1;
}
if (setsockopt(sock, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) < 0) {
fprintf(stderr, "error: can't open netlink.\n");
close(sock);
return -1;
}
return sock;
}
void send_event(int sock, char *buffer)
{
struct sockaddr_nl nladdr;
struct msghdr msg;
struct iovec iov;
struct nlmsghdr *nlh = NULL;
int buflen = NLMSG_SPACE(strlen(buffer) + 1);
memset(&msg, 0, sizeof(msg));
memset(&nlh, 0, sizeof(nlh));
memset(&nladdr, 0, sizeof(nladdr));
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
nladdr.nl_pid = 0; /* 0 = send to Linux kernel */
nladdr.nl_groups = 0; /* 0 = unicast */
nlh = (struct nlmsghdr *)malloc(buflen);
nlh->nlmsg_len = NLMSG_SPACE(buflen);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nlh), buffer);
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *) &(nladdr);
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if (DEBUG)
printf("sending...\n");
sendmsg(sock, &msg, 0);
return;
}
void read_event(int sock, int wait)
{
struct sockaddr_nl nladdr;
struct msghdr msg;
struct iovec iov;
char buffer[MAX_WL_NUMB];
int ret;
iov.iov_base = (void *) buffer;
iov.iov_len = sizeof(buffer);
msg.msg_name = (void *) &(nladdr);
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if (DEBUG)
printf("listening...\n");
ret = recvmsg(sock, &msg, wait);
if (ret < 0)
fprintf(stderr, "error: can't recv data.\n");
else
printf("%s\n", (char *) NLMSG_DATA((struct nlmsghdr *) &buffer));
}
void read_event_buf(int sock, int wait, char *buf_in, size_t buf_in_len)
{
struct sockaddr_nl nladdr;
struct msghdr msg;
struct iovec iov;
char buffer[MAX_WL_NUMB];
int ret;
iov.iov_base = (void *) buffer;
iov.iov_len = sizeof(buffer);
msg.msg_name = (void *) &(nladdr);
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if (DEBUG)
printf("listening...\n");
ret = recvmsg(sock, &msg, wait);
if (ret < 0)
fprintf(stderr, "error: can't recv data.\n");
else
strncpy(buf_in,NLMSG_DATA((struct nlmsghdr *) &buffer),buf_in_len - 1);
}
int read_config (char *path, char *whitelist_exact, char *whitelist_similar) {
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
char flag_exact = 0;
char flag_similar = 0;
int wl_size_e = 0;
int wl_size_s = 0;
fp = fopen(path, "r");
if (fp == NULL) {
fprintf(stderr, "error: can't read the file %s\n", path);
return -1;
}
while ((read = getline(&line, &len, fp)) != -1) {
if (line[0] == ' ' || line[0] == ';' || line[0] == '\n')
continue;
if (line[0] == '[') {
line[read - 2] = '\0';
line++;
if (strcmp(line,"whitelist_exact") == 0) {
flag_exact = 1;
flag_similar = 0;
}
if (strcmp(line,"whitelist_similar") == 0) {
flag_exact = 0;
flag_similar = 1;
}
continue;
}
line[read - 1] = '/';
if (wl_size_e + read >= MAX_WL_NUMB - 1 || wl_size_e + read >= MAX_WL_NUMB - 1) {
fprintf(stderr, "error: the section in the config file is more than %i bytes.\n", MAX_WL_NUMB);
fclose(fp);
return -1;
}
if (flag_exact) {
wl_size_e += read;
strcat(whitelist_exact,line);
}
if (flag_similar) {
wl_size_s += read;
strcat(whitelist_similar,line);
}
}
fclose(fp);
return 0;
}
int laf_set_sysctl(int status) {
FILE *sys_enable_fp;
sys_enable_fp = fopen("/proc/sys/kernel/laf/enabled", "w");
if (sys_enable_fp) {
fprintf(sys_enable_fp, "%i\n%c", status, '\0');
fclose (sys_enable_fp);
} else
return -1;
return 0;
}
int laf_add_whitelist(int wl_type, char *path, char *cmd) {
FILE *fp_r, *fp_w;
char *line = NULL;
char *buffer;
size_t len = 0;
ssize_t read;
char flag_search = 0;
int f_size = 0;
int f_seek = 0;
int cmd_len = 0, i = 0, ret = 0;
if (wl_type > 1)
return -1;
fp_r = fopen(path, "r+");
if (fp_r == NULL) {
fprintf(stderr, "error: can't read the file %s\n", path);
return -1;
}
cmd_len = strlen(cmd);
while ((read = getline(&line, &len, fp_r)) != -1) {
if (cmd_len == (read -1) && (strncmp(line,cmd,read -1) == 0)) {
fclose(fp_r);
return 1;
}
if ((strcmp(line,"[whitelist_exact]\n") == 0 && wl_type == 0) ||
(strcmp(line,"[whitelist_similar]\n") == 0 && wl_type == 1)) {
f_seek = ftell(fp_r);
}
}
fseek(fp_r, 0, SEEK_END);
f_size = ftell(fp_r);
fseek(fp_r, 0, SEEK_SET);
buffer = malloc((f_size + 1) * sizeof(char));
ret = fread(buffer, f_size, 1, fp_r);
fclose(fp_r);
fp_w = fopen(path, "w+");
for (i=0; i < f_seek; i++)
fputc(buffer[i],fp_w);
fprintf(fp_w,"%s\n",cmd);
for (i=f_seek; i < f_size; i++)
fputc(buffer[i],fp_w);
fclose(fp_w);
free(buffer);
return 0;
}