-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
30,331 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Blocking all bash commands */ | ||
/* Require: "CONFIG_BPF_LSM=y" and CONFIG_LSM="bpf,..." */ | ||
#include "vmlinux.h" | ||
#include <bpf/bpf_core_read.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
#define EPERM 1 | ||
#define NULL 0 | ||
|
||
static __always_inline int handle_new_process(struct task_struct *parent, | ||
struct task_struct *child) | ||
{ | ||
char bash[] = "bash"; | ||
|
||
pid_t pid = BPF_CORE_READ(child, pid); | ||
char comm[16]; | ||
bpf_get_current_comm(&comm, sizeof(comm)); | ||
u64 pidns = child->nsproxy->pid_ns_for_children->ns.inum; | ||
|
||
for (int i = 0; i < sizeof(bash); i++) { | ||
if (comm[i] != bash[i]) { | ||
return 0; | ||
} | ||
} | ||
|
||
bpf_printk("lsm: blocking %s (pid: %d) in pidns %ld\n", comm, pid, | ||
pidns); | ||
return -EPERM; | ||
} | ||
|
||
SEC("lsm/task_alloc") | ||
int BPF_PROG(task_alloc, struct task_struct *task, unsigned long clone_flags, | ||
int ret_prev) | ||
{ | ||
struct task_struct *parent = BPF_CORE_READ(task, real_parent); | ||
if (parent == NULL) { | ||
return -EPERM; /* Shouldn't happen */ | ||
} | ||
|
||
/* Handle results of previous programs */ | ||
if (ret_prev != 0) { | ||
return ret_prev; | ||
} | ||
|
||
return handle_new_process(parent, task); | ||
} | ||
|
||
char LICENSE[] SEC("license") = "Dual BSD/GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* Blocking all bash commands */ | ||
/* Require: "CONFIG_BPF_LSM=y" and CONFIG_LSM="bpf,..." */ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/resource.h> | ||
#include <bpf/libbpf.h> | ||
#include "block_shell.skel.h" | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct block_shell_bpf *obj; | ||
int err = 0; | ||
|
||
struct rlimit rlim_new = { | ||
.rlim_cur = RLIM_INFINITY, | ||
.rlim_max = RLIM_INFINITY, | ||
}; | ||
|
||
err = setrlimit(RLIMIT_MEMLOCK, &rlim_new); | ||
if (err) { | ||
fprintf(stderr, "failed to change rlimit\n"); | ||
return 1; | ||
} | ||
obj = block_shell_bpf__open(); | ||
if (!obj) { | ||
fprintf(stderr, "failed to open and/or load BPF object\n"); | ||
return 1; | ||
} | ||
|
||
err = block_shell_bpf__load(obj); | ||
if (err) { | ||
fprintf(stderr, "failed to load BPF object %d\n", err); | ||
goto cleanup; | ||
} | ||
|
||
err = block_shell_bpf__attach(obj); | ||
if (err) { | ||
fprintf(stderr, "failed to attach BPF programs\n"); | ||
goto cleanup; | ||
} | ||
|
||
printf | ||
("Successfully started! Tracing /sys/kernel/debug/tracing/trace_pipe...\n"); | ||
|
||
system("cat /sys/kernel/debug/tracing/trace_pipe"); | ||
|
||
cleanup: | ||
block_shell_bpf__destroy(obj); | ||
return err != 0; | ||
} |
Oops, something went wrong.