Skip to content

Commit e8f17b5

Browse files
committed
Reorganize functions
1 parent 53f40d5 commit e8f17b5

File tree

2 files changed

+57
-60
lines changed

2 files changed

+57
-60
lines changed

lemon.c

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdlib.h>
12
#include <unistd.h>
23
#include <argp.h>
34
#include <arpa/inet.h>
@@ -7,14 +8,12 @@
78
#include <sys/capability.h>
89

910
#include "lemon.h"
10-
#include "ebpf/mem.ebpf.skel.h"
1111

12-
extern int init_translation(struct ram_regions *restrict ram_regions, struct mem_ebpf *restrict skel);
12+
extern int load_ebpf_mem_progs(void);
13+
extern int init_translation(struct ram_regions *restrict ram_regions);
1314
extern int dump_on_disk(const struct options *restrict opts, const struct ram_regions *restrict ram_regions);
1415
extern int dump_on_net(const struct options *restrict opts, const struct ram_regions *restrict ram_regions);
1516
extern int increase_priority_and_launch_stealers(void);
16-
extern int init_mmap(struct mem_ebpf *restrict skel);
17-
extern void cleanup_mmap(void);
1817
extern int join_cpu_stealers(void);
1918
extern int check_capability(const cap_value_t cap);
2019
extern int toggle_kptr(void);
@@ -32,48 +31,6 @@ static const struct argp_option options[] = {
3231
};
3332
static const char doc[] = "Lemon - An eBPF Memory Dump Tool for x64 and ARM64 Linux and Android";
3433

35-
/*
36-
* load_ebpf_mem_progs() - Initialize and attach eBPF programs for memory access
37-
* @skel: Output pointer to the initialized mem_ebpf skeleton
38-
*
39-
* Opens, loads, attaches the eBPF programs, and sets up shared memory.
40-
* Returns 0 on success or a negative error code on failure.
41-
*/
42-
static int load_ebpf_mem_progs(struct mem_ebpf **restrict skel) {
43-
int ret;
44-
45-
/* Check if we have sufficient capabilities to set RLIMIT_MEMLOCK (required by libbpf...)*/
46-
if((check_capability(CAP_PERFMON) <= 0) && (check_capability(CAP_SYS_ADMIN) <= 0)) {
47-
WARN("LEMON does not have CAP_PERFMON needed to modify RLIMIT_MEMLOCK");
48-
}
49-
50-
/* Open the BPF object file */
51-
*skel = mem_ebpf__open();
52-
if(!skel) {
53-
perror("Failed to open BPF skeleton");
54-
return errno;
55-
}
56-
57-
/* Load the BPF objectes */
58-
if (mem_ebpf__load(*skel)) {
59-
perror("Failed to load BPF object");
60-
return errno;
61-
}
62-
63-
/* Attach the uprobe to the 'read_kernel_memory' function in the current executable */
64-
if (mem_ebpf__attach(*skel)) {
65-
fprintf(stderr, "Failed to attach program\n");
66-
return errno;
67-
}
68-
69-
/* Create the mmap */
70-
if((ret = init_mmap(*skel))) {
71-
return ret;
72-
}
73-
74-
return 0;
75-
}
76-
7734
/*
7835
* parse_opt() - Argument parser callback for argp
7936
* @key: Option key
@@ -163,7 +120,6 @@ static int check_kernel_version() {
163120

164121
int main(int argc, char **argv)
165122
{
166-
struct mem_ebpf *skel = NULL;
167123
struct ram_regions ram_regions;
168124
struct options opts = {0};
169125
struct argp argp = {options, parse_opt, "", doc};
@@ -214,13 +170,13 @@ int main(int argc, char **argv)
214170
}
215171

216172
/* Load eBPF progs that read memory */
217-
if((ret = load_ebpf_mem_progs(&skel))) return ret;
173+
if((ret = load_ebpf_mem_progs())) return ret;
218174

219175
/* Disable kptr_restrict if needed */
220176
if((ret = toggle_kptr())) return ret;
221177

222178
/* Determine the memory dumpable regions */
223-
if((ret = init_translation(&ram_regions, skel))) goto cleanup;
179+
if((ret = init_translation(&ram_regions))) goto cleanup;
224180

225181
/* Dump on a file */
226182
if(opts.disk_mode) {
@@ -234,10 +190,6 @@ int main(int argc, char **argv)
234190

235191
/* Cleanup: close BPF object */
236192
cleanup:
237-
if(skel) {
238-
cleanup_mmap();
239-
mem_ebpf__destroy(skel);
240-
}
241193
join_cpu_stealers();
242194

243195
/* Restore kptr_restrict if needed */

mem.c

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ struct resource {
2727

2828
extern int check_capability(const cap_value_t cap);
2929

30+
/* eBPF memory read program skeleton */
31+
struct mem_ebpf *mem_ebpf_skel;
32+
3033
/* File descriptor and mmap() pointer associated to the eBPF map.*/
3134
int read_mem_result_fd;
3235
struct read_mem_result *read_mem_result;
@@ -46,14 +49,13 @@ static uintptr_t iomem_resource;
4649

4750
/*
4851
* init_mmap() - Initializes a shared memory mapping for reading memory results from eBPF
49-
* @skel: eBPF skeleton containing the map to be used
5052
*
5153
* Retrieves the file descriptor for the BPF map and creates a shared memory mapping
5254
* to allow user space to access the memory read results.
5355
*/
54-
int init_mmap(struct mem_ebpf *restrict skel) {
56+
static int init_mmap() {
5557

56-
read_mem_result_fd = bpf_map__fd(skel->maps.read_mem_array_map);
58+
read_mem_result_fd = bpf_map__fd(mem_ebpf_skel->maps.read_mem_array_map);
5759
if(read_mem_result_fd < 0)
5860
return read_mem_result_fd;
5961

@@ -66,10 +68,54 @@ int init_mmap(struct mem_ebpf *restrict skel) {
6668
}
6769

6870
/*
69-
* cleanup_mmap() - Unmaps the shared memory region used for memory memory.
71+
* load_ebpf_mem_progs() - Initialize and attach eBPF programs for memory access
72+
*
73+
* Opens, loads, attaches the eBPF programs, and sets up shared memory.
74+
* Returns 0 on success or a negative error code on failure.
75+
*/
76+
int load_ebpf_mem_progs() {
77+
int ret;
78+
79+
/* Check if we have sufficient capabilities to set RLIMIT_MEMLOCK (required by libbpf...)*/
80+
if((check_capability(CAP_PERFMON) <= 0) && (check_capability(CAP_SYS_ADMIN) <= 0)) {
81+
WARN("LEMON does not have CAP_PERFMON needed to modify RLIMIT_MEMLOCK");
82+
}
83+
84+
/* Open the BPF object file */
85+
mem_ebpf_skel = mem_ebpf__open();
86+
if(!mem_ebpf_skel) {
87+
perror("Failed to open BPF skeleton");
88+
return errno;
89+
}
90+
91+
/* Load the BPF objectes */
92+
if (mem_ebpf__load(mem_ebpf_skel)) {
93+
perror("Failed to load BPF object");
94+
return errno;
95+
}
96+
97+
/* Attach the uprobe to the 'read_kernel_memory' function in the current executable */
98+
if (mem_ebpf__attach(mem_ebpf_skel)) {
99+
fprintf(stderr, "Failed to attach program\n");
100+
return errno;
101+
}
102+
103+
/* Create the mmap */
104+
if((ret = init_mmap())) {
105+
return ret;
106+
}
107+
108+
return 0;
109+
}
110+
111+
/*
112+
* cleanup_mem_ebpf() - Unmaps the shared memory region used to access map and free eBPF resources.
70113
*/
71114
void cleanup_mmap() {
72-
if(read_mem_result) munmap(read_mem_result, sizeof(struct read_mem_result));
115+
if(mem_ebpf_skel) {
116+
if(read_mem_result) munmap(read_mem_result, sizeof(struct read_mem_result));
117+
mem_ebpf__destroy(mem_ebpf_skel);
118+
}
73119
}
74120

75121
/*
@@ -523,13 +569,12 @@ static int get_iomem_regions_kernel(struct ram_regions *restrict ram_regions)
523569
/*
524570
* init_translation() - Initialize phys-to-virt translation and extract System RAM regions
525571
* @ram_regions: Output pointer for storing valid memory regions
526-
* @skel: eBPF skeleton used for memory access
527572
*
528573
* Initializes the physical-to-virtual address mapping and retrieves System RAM virtual address ranges
529574
* from kernel or /proc/iomem.
530575
* Returns 0 on success or an error code on failure.
531576
*/
532-
int init_translation(struct ram_regions *restrict ram_regions, struct mem_ebpf *restrict skel) {
577+
int init_translation(struct ram_regions *restrict ram_regions) {
533578
int err;
534579

535580
/* Parse kallsyms looking for symbols needed to initialize translatation system */

0 commit comments

Comments
 (0)