-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from trailofbits/kernel-abstraction
Freebsd integration
- Loading branch information
Showing
188 changed files
with
823 additions
and
427 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,8 @@ | |
*.gen.h | ||
*.gen.c | ||
*.cmd | ||
*~ | ||
krfexec | ||
krfctl | ||
*.bak | ||
example/* |
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
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,16 @@ | ||
proto: struct thread *td, struct accept_args *uap | ||
parms: td, uap | ||
errors: | ||
- EBADF | ||
- EINTR | ||
- EMFILE | ||
- ENFILE | ||
- ENOTSOCK | ||
- EINVAL | ||
- EFAULT | ||
- EWOULDBLOCK | ||
- EAGAIN | ||
- ECONNABORTED | ||
profiles: | ||
- net | ||
|
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,15 @@ | ||
proto: struct thread *td, struct read_args *uap | ||
parms: td, uap | ||
errors: | ||
- ECONNRESET | ||
- EFAULT | ||
- EIO | ||
- EBUSY | ||
- EINTR | ||
- EINVAL | ||
- EAGAIN | ||
- EISDIR | ||
- EOPNOTSUPP | ||
- EOVERFLOW | ||
profiles: | ||
- io |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,5 @@ | ||
SYSCALL_C_FILES!=ls syscalls/*.gen.c | ||
SRCS=krf.c syscalls.c ../config.c ../krf.c ${SYSCALL_C_FILES} | ||
KMOD=krf | ||
|
||
.include <bsd.kmod.mk> |
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,20 @@ | ||
|
||
# -*- mode: ruby; -*- | ||
|
||
# Force Virtualbox for those people who have installed vagrant-lxc (e.g.) | ||
#ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox' | ||
|
||
Vagrant.configure("2") do |config| | ||
config.vm.guest = :freebsd | ||
config.vm.network "private_network", ip: "10.0.1.10" | ||
|
||
# Use NFS as a shared folder | ||
# config.vm.synced_folder ".", "/vagrant", type: "nfs", id: "vagrant-root" | ||
config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true | ||
# Set to a shell that actually exists | ||
config.ssh.shell = 'sh' | ||
|
||
config.vm.box = "freebsd/FreeBSD-12.0-CURRENT" | ||
|
||
|
||
end |
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,15 @@ | ||
#pragma once | ||
// FreeBSD specific definitions | ||
#include "syscalls.h" | ||
|
||
#define KRF_SAFE_WRITE(x) x // ??? | ||
#define KRF_LOG(...) uprintf(__VA_ARGS__) | ||
#define KRF_SYSCALL_TABLE sysent | ||
#define KRF_TARGETING_PROTO struct thread *td | ||
#define KRF_TARGETING_PARMS td | ||
#define KRF_PERSONALITY() (td->td_proc->p_flag2) | ||
#define KRF_PID() (td->td_proc->p_pid) | ||
#define KRF_UID() \ | ||
(td->td_proc->p_ucred->cr_ruid) // Currently using real UID but could use effective UID (cr_uid) | ||
#define KRF_GID() (td->td_proc->p_ucred->cr_rgid) | ||
#define KRF_EXTRACT_SYSCALL(x) ((x).sy_call) |
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,120 @@ | ||
#include <sys/types.h> | ||
#include <sys/param.h> | ||
#include <sys/proc.h> | ||
#include <sys/module.h> | ||
#include <sys/sysproto.h> | ||
#include <sys/sysent.h> | ||
#include <sys/kernel.h> | ||
#include <sys/systm.h> | ||
#include <sys/sysctl.h> | ||
#include <sys/conf.h> | ||
|
||
#include <sys/syscall.h> | ||
|
||
#include "../config.h" | ||
#include "../krf.h" | ||
#include "syscalls.h" | ||
|
||
static struct sysctl_ctx_list clist; | ||
static struct sysctl_oid *krf_sysctl_root; | ||
static unsigned int krf_control; | ||
static char krf_targetings[13]; | ||
|
||
static int control_file_sysctl(SYSCTL_HANDLER_ARGS) { | ||
int syscall = -1; | ||
int err = 0; | ||
|
||
if (sysctl_handle_int(oidp, &syscall, 0, req)) { | ||
return -1; | ||
} else if (req->newptr) { | ||
err = control_file_handler(syscall); | ||
if (err < 0) | ||
return -err; | ||
} else { | ||
// read request? | ||
} | ||
return err; | ||
} | ||
|
||
static int targeting_file_sysctl(SYSCTL_HANDLER_ARGS) { | ||
int err = 0; | ||
krf_target_mode_t mode; | ||
unsigned int data; | ||
|
||
err = sysctl_handle_string(oidp, &krf_targetings, 13, req); | ||
if (err) { | ||
return -err; | ||
} else if (req->newptr) { | ||
if (sscanf(krf_targetings, "%u %u", &mode, &data) != 2) { | ||
return EINVAL; | ||
} | ||
if (targeting_file_write_handler(mode, data) < 0) { | ||
return EINVAL; | ||
} | ||
} else { | ||
// read request? | ||
} | ||
return err; | ||
} | ||
|
||
static int krf_init() { | ||
int err = 0; | ||
sysctl_ctx_init(&clist); | ||
if (!(krf_sysctl_root = | ||
SYSCTL_ADD_ROOT_NODE(&clist, OID_AUTO, "krf", CTLFLAG_RW, 0, "krf sysctl root node"))) { | ||
uprintf("krf error: Failed to add root sysctl node.\n"); | ||
return -1; | ||
} | ||
|
||
memset(krf_faultable_table, 0, KRF_NR_SYSCALLS * sizeof(struct sysent)); | ||
memcpy(krf_sys_call_table, sysent, KRF_NR_SYSCALLS * sizeof(struct sysent)); | ||
|
||
SYSCTL_ADD_UINT(&clist, SYSCTL_CHILDREN(krf_sysctl_root), OID_AUTO, KRF_PROBABILITY_FILENAME, | ||
CTLFLAG_RW, &krf_probability, krf_probability, | ||
"Reciprocal of the probability of a fault"); | ||
SYSCTL_ADD_UINT(&clist, SYSCTL_CHILDREN(krf_sysctl_root), OID_AUTO, KRF_RNG_STATE_FILENAME, | ||
CTLFLAG_RW, &krf_rng_state, krf_rng_state, "Sets the current RNG state"); | ||
SYSCTL_ADD_UINT(&clist, SYSCTL_CHILDREN(krf_sysctl_root), OID_AUTO, KRF_LOG_FAULTS_FILENAME, | ||
CTLFLAG_RW, &krf_log_faults, krf_log_faults, "Toggle logging faults to syslog"); | ||
SYSCTL_ADD_PROC(&clist, SYSCTL_CHILDREN(krf_sysctl_root), OID_AUTO, KRF_CONTROL_FILENAME, | ||
CTLTYPE_UINT | CTLFLAG_WR, &krf_control, krf_control, control_file_sysctl, "IU", | ||
"Enables specific syscall faults"); | ||
SYSCTL_ADD_PROC(&clist, SYSCTL_CHILDREN(krf_sysctl_root), OID_AUTO, KRF_TARGETING_FILENAME, | ||
CTLTYPE_STRING | CTLFLAG_WR, &krf_targetings, 13, targeting_file_sysctl, "A", | ||
"Enables specific targeting options"); | ||
return err; | ||
} | ||
|
||
static int krf_teardown() { | ||
krf_flush_table(); | ||
sysctl_remove_oid(krf_sysctl_root, 1, 0); | ||
sysctl_ctx_free(&clist); | ||
return 0; | ||
} | ||
|
||
static int krf_loader(struct module *m, int what, void *arg) { | ||
int err = 0; | ||
switch (what) { | ||
case MOD_LOAD: | ||
err = krf_init(); | ||
if (err != 0) | ||
uprintf("krf_init failed with %d\n", err); | ||
|
||
#include "krf.gen.x" | ||
|
||
uprintf("krf: loaded\n"); | ||
break; | ||
case MOD_UNLOAD: | ||
krf_teardown(); | ||
uprintf("krf: unloaded\n"); | ||
break; | ||
default: | ||
err = EOPNOTSUPP; | ||
break; | ||
} | ||
return (err); | ||
} | ||
|
||
static moduledata_t krf_mod = {"krf", krf_loader, NULL}; | ||
|
||
DECLARE_MODULE(krf, krf_mod, SI_SUB_EXEC, SI_ORDER_ANY); |
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,19 @@ | ||
#include <sys/types.h> | ||
#include <sys/param.h> | ||
#include <sys/proc.h> | ||
#include <sys/module.h> | ||
#include <sys/sysproto.h> | ||
#include <sys/syscall.h> | ||
#include <sys/sysent.h> | ||
#include <sys/kernel.h> | ||
#include <sys/systm.h> | ||
|
||
#include "syscalls.h" | ||
#include "syscalls/internal.h" | ||
#include "../targeting.h" | ||
#include "freebsd.h" | ||
|
||
struct sysent krf_faultable_table[KRF_MAX_SYSCALL] = {}; | ||
struct sysent krf_sys_call_table[KRF_MAX_SYSCALL] = {}; | ||
|
||
#include "syscalls.gen.x" |
Oops, something went wrong.