EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
io.c File Reference
#include "io.h"
#include "config.h"
Include dependency graph for io.c:

Go to the source code of this file.

Functions

int _read_file (const char *path, char **out_buf)
 
int _write_file (const char *path, const char *buf, size_t len)
 
void build_cfg_path (const char *fname, char *out, size_t sz)
 

Function Documentation

◆ _read_file()

int _read_file ( const char *  path,
char **  out_buf 
)

_read_file - Reads the contents of a file into a dynamically allocated buffer.

Parameters
pathPath to the file to be read.
out_bufPointer to a char pointer that will receive the allocated buffer containing the file contents. Return: On success, returns the number of bytes read (excluding the null terminator). On failure, returns a negative error code.

Definition at line 12 of file io.c.

12 {
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}
#define STD_BUFFER_SIZE
Definition config.h:68
static struct dentry * file
Definition epikeylog.c:145

◆ _write_file()

int _write_file ( const char *  path,
const char *  buf,
size_t  len 
)

_write_file - Write a buffer to a file in the kernel space.

Parameters
pathPath to the file to write.
bufPointer to the buffer containing data to write.
lenNumber of bytes to write from the buffer. Return: Number of bytes written on success, or a negative error code on failure.

Definition at line 67 of file io.c.

67 {
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}

◆ build_cfg_path()

void build_cfg_path ( const char *  fname,
char *  out,
size_t  sz 
)

build_cfg_path - Constructs a full path for a configuration file in the hidden directory.

Parameters
fnameName of the configuration file.
outOutput buffer to store the full path.
szSize of the output buffer.

Definition at line 87 of file io.c.

87 {
88 snprintf(out, sz, "%s/%s", HIDDEN_DIR_PATH, fname);
89}
#define HIDDEN_DIR_PATH
Definition config.h:56