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"
|
|
|
|
|
|
2022-01-15 16:16:30 -05:00
|
|
|
/**
|
|
|
|
|
* https://github.com/torvalds/linux/blob/master/kernel/trace/trace_syscalls.c#L673
|
|
|
|
|
*/
|
|
|
|
|
struct sys_read_exit_ctx {
|
|
|
|
|
unsigned long long unused; //Pointer to pt_regs
|
|
|
|
|
int __syscall_nr;
|
|
|
|
|
long ret;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* https://github.com/torvalds/linux/blob/master/kernel/trace/trace_syscalls.c#L588
|
|
|
|
|
*/
|
|
|
|
|
struct sys_read_enter_ctx {
|
|
|
|
|
unsigned long long unused; //Pointer to pt_regs
|
|
|
|
|
int __syscall_nr;
|
|
|
|
|
unsigned int padding; //Alignment
|
|
|
|
|
unsigned long fd;
|
|
|
|
|
char* buf;
|
|
|
|
|
size_t count;
|
|
|
|
|
};
|
2022-01-14 21:18:51 -05:00
|
|
|
|
2022-01-15 16:16:30 -05:00
|
|
|
static __always_inline int handle_sys_read(struct sys_read_enter_ctx *ctx, int fd, char* buf){
|
2022-01-14 21:18:51 -05:00
|
|
|
__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-15 16:16:30 -05:00
|
|
|
//bpf_printk("IN 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-15 16:16:30 -05:00
|
|
|
SEC("tracepoint/syscalls/sys_enter_read")
|
|
|
|
|
int kprobe_ksys_read(struct sys_read_enter_ctx *ctx) {
|
|
|
|
|
struct sys_read_enter_ctx *rctx = ctx;
|
|
|
|
|
if (ctx == NULL){
|
|
|
|
|
bpf_printk("Error\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fd = (int) ctx->fd;
|
|
|
|
|
char *buf = (char*) ctx->buf;
|
2022-01-14 21:18:51 -05:00
|
|
|
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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-01-15 16:16:30 -05:00
|
|
|
SEC("tracepoint/syscalls/sys_exit_read")
|
|
|
|
|
int kretprobe_vfs_read(struct sys_read_exit_ctx *ctx){
|
2022-01-14 22:05:08 -05:00
|
|
|
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
2022-01-15 16:16:30 -05:00
|
|
|
if(pid_tgid<0){
|
|
|
|
|
bpf_printk("Out\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
//bpf_printk("OUT PID: %u\n", pid_tgid>>32);
|
|
|
|
|
|
2022-01-14 22:05:08 -05:00
|
|
|
struct fs_open_data *data = (struct fs_open_data*) bpf_map_lookup_elem(&fs_open, &pid_tgid);
|
2022-01-15 16:16:30 -05:00
|
|
|
if (data == NULL || data->buf == NULL){
|
2022-01-14 22:05:08 -05:00
|
|
|
//Not found
|
2022-01-15 16:16:30 -05:00
|
|
|
bpf_printk("Not found\n");
|
2022-01-14 22:05:08 -05:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Overwritting a byte of the buffer
|
|
|
|
|
char *buf = data->buf;
|
2022-01-15 16:16:30 -05:00
|
|
|
__u32 pid = data->pid;
|
|
|
|
|
char *msg = "OOOOOOOOOOOOO\0";
|
|
|
|
|
|
|
|
|
|
if(buf == NULL){
|
|
|
|
|
bpf_printk("Out\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int length = 0;
|
|
|
|
|
char c[7] = {0};
|
|
|
|
|
|
|
|
|
|
while(length<6){
|
|
|
|
|
if(bpf_probe_read_user(c+length, 1, buf+length)<0){
|
|
|
|
|
//bpf_printk("Error reading\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
//bpf_printk("%i\n", length);
|
|
|
|
|
length++;
|
|
|
|
|
};
|
|
|
|
|
c[6] = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int ii=0; ii<6; ii++){
|
|
|
|
|
if(!((c[ii] >= 'a' && c[ii] <= 'z') || (c[ii] >= 'A' && c[ii] <= 'Z'))){
|
|
|
|
|
//bpf_printk("Not a valid buf\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bpf_printk("Overwritting at pid %u, %s\n", pid, buf);
|
|
|
|
|
if(bpf_probe_write_user((void*)buf, (void*)msg, (__u32)1)<0){
|
|
|
|
|
bpf_printk("Error writing to user memory\n");
|
|
|
|
|
}
|
|
|
|
|
bpf_printk("NEW at pid %u, %s\n", pid, buf);
|
2022-01-14 22:05:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 20:09:59 -05:00
|
|
|
#endif
|