2022-01-04 20:09:59 -05:00
|
|
|
#ifndef __FS_H
|
|
|
|
|
#define __FS_H
|
|
|
|
|
|
2022-01-14 21:18:51 -05:00
|
|
|
#include "headervmlinux.h"
|
2022-01-06 13:31:52 -05:00
|
|
|
/*#include <stdio.h>
|
2022-01-04 20:09:59 -05:00
|
|
|
#include <linux/types.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <linux/ptrace.h>
|
2022-01-06 13:31:52 -05:00
|
|
|
#include <linux/stat.h>*/
|
2022-01-04 20:09:59 -05:00
|
|
|
|
|
|
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
|
#include <bpf/bpf_core_read.h>
|
|
|
|
|
|
|
|
|
|
#include "../../../common/constants.h"
|
2022-01-14 21:18:51 -05:00
|
|
|
#include "../../../common/map_common.h"
|
2022-01-04 20:09:59 -05:00
|
|
|
#include "../data/ring_buffer.h"
|
2022-01-14 21:18:51 -05:00
|
|
|
#include "map_defs.h"
|
|
|
|
|
#include "../utils/strings.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static __always_inline int handle_sys_read(struct pt_regs *ctx, int fd, char* buf){
|
|
|
|
|
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
|
|
|
|
__u32 pid = pid_tgid >> 32;
|
|
|
|
|
struct fs_open_data data = {
|
|
|
|
|
.buf = buf,
|
|
|
|
|
.fd = fd,
|
|
|
|
|
.pid = pid
|
|
|
|
|
};
|
|
|
|
|
bpf_map_update_elem(&fs_open, &pid_tgid, &data, BPF_ANY);
|
2022-01-14 22:05:08 -05:00
|
|
|
//bpf_printk("PID: %u, FS:%u\n", pid, fd);
|
2022-01-14 21:18:51 -05:00
|
|
|
return 0;
|
2022-01-05 20:34:53 -05:00
|
|
|
}
|
2022-01-04 20:09:59 -05:00
|
|
|
|
2022-01-14 22:05:08 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Receives read event and stores the parameters into internal map
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-01-14 21:18:51 -05:00
|
|
|
SEC("kprobe/ksys_read")
|
2022-01-14 22:05:08 -05:00
|
|
|
int kprobe_ksys_read(struct pt_regs *ctx) {
|
2022-01-14 21:18:51 -05:00
|
|
|
struct pt_regs *rctx = ctx;
|
|
|
|
|
if (!rctx) return 0;
|
|
|
|
|
int fd = (int) PT_REGS_PARM1(ctx);
|
|
|
|
|
char *buf = (char *) PT_REGS_PARM2(ctx);
|
|
|
|
|
return handle_sys_read(ctx, fd, buf);
|
|
|
|
|
}
|
2022-01-04 20:09:59 -05:00
|
|
|
|
2022-01-14 22:05:08 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Called AFTER the ksys_read call, checks the internal
|
|
|
|
|
* map for the tgid+pid used and extracts the parameters.
|
|
|
|
|
* Uses the user-space buffer reference for overwritting the returned
|
|
|
|
|
* values.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
SEC("kretprobe/vfs_read")
|
|
|
|
|
int kretprobe_vfs_read(struct pt_regs *ctx){
|
|
|
|
|
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
|
|
|
|
|
|
|
|
|
struct fs_open_data *data = (struct fs_open_data*) bpf_map_lookup_elem(&fs_open, &pid_tgid);
|
|
|
|
|
if (data!=NULL){
|
|
|
|
|
//Not found
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Overwritting a byte of the buffer
|
|
|
|
|
char *buf = data->buf;
|
|
|
|
|
char *msg = "OOOOOOOOOOOOO";
|
|
|
|
|
bpf_printk("Overwritting at pid %u\n", data->pid);
|
|
|
|
|
//int err = bpf_probe_write_user((void*)buf, (void*)msg, (__u32)1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 20:09:59 -05:00
|
|
|
#endif
|