From 0c79e83a9ff8da14eb65a758b72e9054d4569d88 Mon Sep 17 00:00:00 2001 From: criyle Date: Mon, 8 Jul 2019 23:31:05 -0700 Subject: [PATCH] apply go11 module & code clean up --- README.md | 49 ++++++++++--------- tracee/tracee_fork.go => forkexec/fork.go | 2 +- tracee/tracee_fork.s => forkexec/fork.s | 0 tracee/tracee.go => forkexec/runner.go | 9 +--- go.mod | 8 +++ go.sum | 3 ++ runprogram/rlimit.go | 16 +++--- runprogram/runprogram_run.go | 10 ++-- secutil/secutil.go => seccomp/seccomp.go | 4 +- .../seccomp_test.go | 2 +- 10 files changed, 55 insertions(+), 48 deletions(-) rename tracee/tracee_fork.go => forkexec/fork.go (99%) rename tracee/tracee_fork.s => forkexec/fork.s (100%) rename tracee/tracee.go => forkexec/runner.go (81%) create mode 100644 go.mod create mode 100644 go.sum rename secutil/secutil.go => seccomp/seccomp.go (95%) rename secutil/secutil_test.go => seccomp/seccomp_test.go (99%) diff --git a/README.md b/README.md index 3e01e99..fed0864 100644 --- a/README.md +++ b/README.md @@ -6,47 +6,48 @@ Goal is to reimplement [uoj-judger/run_program](https://github.com/vfleaking/uoj Install: -+ install go compiler: `apt install golang-go` -+ install libseccomp-dev: `apt install libseccomp-dev` -+ install: `go install github.com/criyle/go-judger/...` +- install go compiler: `apt install golang-go` +- install libseccomp-dev: `apt install libseccomp-dev` +- install: `go install github.com/criyle/go-judger/...` Features (same as uoj-judger/run_program): -1. Restricted computing resource: Time & Memory (Stack) & Output -1. Restricted syscall access (by libseccomp & ptrace) -1. Restricted file access (read & write & access & exec) +1. Restricted computing resource: Time & Memory (Stack) & Output +2. Restricted syscall access (by libseccomp & ptrace) +3. Restricted file access (read & write & access & exec) New Features: -1. Percise resource limits (s -> ms, mb -> kb) -1. More architectures (arm32, arm64, x86) -1. Allow multiple traced programs in different threads -1. Allow pipes as input / output files +1. Percise resource limits (s -> ms, mb -> kb) +2. More architectures (arm32, arm64, x86) +3. Allow multiple traced programs in different threads +4. Allow pipes as input / output files Default file access action: -+ check file read / write: `open`, `openat` -+ check file read: `readlink`, `readlinkat` -+ check file write: `unlink`, `unlinkat`, `chmod`, `rename` -+ check file access: `stat`, `lstat`, `access`, `faccessat` -+ check file exec: `execve` +- check file read / write: `open`, `openat` +- check file read: `readlink`, `readlinkat` +- check file write: `unlink`, `unlinkat`, `chmod`, `rename` +- check file access: `stat`, `lstat`, `access`, `faccessat` +- check file exec: `execve` Packages: -+ secutil: provides common utility function that wrappers libseccomp -+ tracee: ptraced fork-exec with seccomp loaded -+ tracer: ptrace tracee and provides syscall trap context -+ runprogram: wrapper to call trecee and trecer -+ runconfig: defines arch & language specified trace condition +- seccomp: provides utility function that wrappers libseccomp +- forkexec: fork-exec provides ptrace and seccomp before exec +- tracer: ptrace tracer and provides syscall trap filter context +- runprogram: wrapper to call forkexec and trecer +- runconfig: defines arch & language specified trace condition Executable: -+ run_program: under construction +- run_program: under construction Configuations: -+ run_program/config.go: all configs toward running specs +- run_program/config.go: all configs toward running specs TODO: -1. Use Linux Namespace to isolate file access (elimilate ptrace) -1. Use Linux Control Groups to limit & acct CPU & memory (elimilate wait4 rusage) + +1. Use Linux Namespace to isolate file access (elimilate ptrace) +2. Use Linux Control Groups to limit & acct CPU & memory (elimilate wait4 rusage) diff --git a/tracee/tracee_fork.go b/forkexec/fork.go similarity index 99% rename from tracee/tracee_fork.go rename to forkexec/fork.go index ec333a6..06322f4 100644 --- a/tracee/tracee_fork.go +++ b/forkexec/fork.go @@ -1,4 +1,4 @@ -package tracee +package forkexec import ( "syscall" diff --git a/tracee/tracee_fork.s b/forkexec/fork.s similarity index 100% rename from tracee/tracee_fork.s rename to forkexec/fork.s diff --git a/tracee/tracee.go b/forkexec/runner.go similarity index 81% rename from tracee/tracee.go rename to forkexec/runner.go index f0f7427..e73c2cf 100644 --- a/tracee/tracee.go +++ b/forkexec/runner.go @@ -1,6 +1,6 @@ -// Package tracee provides interface to run a seccomp filtered, rlimited +// Package forkexec provides interface to run a seccomp filtered, rlimited // executable and ptraced -package tracee +package forkexec import "syscall" @@ -31,8 +31,3 @@ type RLimit struct { // Rlim is the limit applied to that resource Rlim syscall.Rlimit } - -// NewRunner creates new runner struct -func NewRunner() Runner { - return Runner{} -} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..173c4bb --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/criyle/go-judger + +go 1.12 + +require ( + github.com/seccomp/libseccomp-golang v0.9.1 + golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5bd7052 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo= +golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k= +golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/runprogram/rlimit.go b/runprogram/rlimit.go index 0c37b83..1c71f37 100644 --- a/runprogram/rlimit.go +++ b/runprogram/rlimit.go @@ -3,7 +3,7 @@ package runprogram import ( "syscall" - "github.com/criyle/go-judger/tracee" + "github.com/criyle/go-judger/forkexec" ) // RLimits defines the rlimit applied by setrlimit syscall to traced process @@ -22,39 +22,39 @@ func getRlimit(cur, max uint64) syscall.Rlimit { // prepareRLimit creates rlimit structures for tracee // TimeLimit in s, SizeLimit in byte -func (r *RLimits) prepareRLimit() []tracee.RLimit { - var ret []tracee.RLimit +func (r *RLimits) prepareRLimit() []forkexec.RLimit { + var ret []forkexec.RLimit if r.CPU > 0 { cpuHard := r.CPUHard if cpuHard < r.CPU { cpuHard = r.CPU } - ret = append(ret, tracee.RLimit{ + ret = append(ret, forkexec.RLimit{ Res: syscall.RLIMIT_CPU, Rlim: getRlimit(uint64(r.CPU), uint64(cpuHard)), }) } if r.Data > 0 { - ret = append(ret, tracee.RLimit{ + ret = append(ret, forkexec.RLimit{ Res: syscall.RLIMIT_DATA, Rlim: getRlimit(uint64(r.Data)<<10, uint64(r.Data)<<10), }) } if r.FileSize > 0 { - ret = append(ret, tracee.RLimit{ + ret = append(ret, forkexec.RLimit{ Res: syscall.RLIMIT_FSIZE, Rlim: getRlimit(uint64(r.FileSize)<<10, uint64(r.FileSize)<<10), }) } if r.Stack > 0 { - ret = append(ret, tracee.RLimit{ + ret = append(ret, forkexec.RLimit{ Res: syscall.RLIMIT_STACK, Rlim: getRlimit(uint64(r.Stack)<<10, uint64(r.Stack)<<10), }) } if r.AddressSpace > 0 { - ret = append(ret, tracee.RLimit{ + ret = append(ret, forkexec.RLimit{ Res: syscall.RLIMIT_AS, Rlim: getRlimit(uint64(r.AddressSpace)<<10, uint64(r.AddressSpace)<<10), }) diff --git a/runprogram/runprogram_run.go b/runprogram/runprogram_run.go index fb52e51..57f6e30 100644 --- a/runprogram/runprogram_run.go +++ b/runprogram/runprogram_run.go @@ -3,8 +3,8 @@ package runprogram import ( libseccomp "github.com/seccomp/libseccomp-golang" - "github.com/criyle/go-judger/secutil" - "github.com/criyle/go-judger/tracee" + "github.com/criyle/go-judger/forkexec" + "github.com/criyle/go-judger/seccomp" "github.com/criyle/go-judger/tracer" ) @@ -18,13 +18,13 @@ func (r *RunProgram) Start() (rt tracer.TraceResult, err error) { } defer filter.Release() - bpf, err := secutil.FilterToBPF(filter) + bpf, err := seccomp.FilterToBPF(filter) if err != nil { println(err) return } - ch := &tracee.Runner{ + ch := &forkexec.Runner{ Args: r.Args, Env: r.Env, RLimits: r.RLimits.prepareRLimit(), @@ -51,5 +51,5 @@ func buildFilter(showDetails bool, allow, trace []string) (*libseccomp.ScmpFilte } else { defaultAction = libseccomp.ActKill } - return secutil.BuildFilter(defaultAction, libseccomp.ActTrace.SetReturnCode(tracer.MsgHandle), allow, trace) + return seccomp.BuildFilter(defaultAction, libseccomp.ActTrace.SetReturnCode(tracer.MsgHandle), allow, trace) } diff --git a/secutil/secutil.go b/seccomp/seccomp.go similarity index 95% rename from secutil/secutil.go rename to seccomp/seccomp.go index bb2b1ff..013ec43 100644 --- a/secutil/secutil.go +++ b/seccomp/seccomp.go @@ -1,6 +1,6 @@ -// Package secutil provides utility functions to manipulate seccomp filters +// Package seccomp provides utility functions to manipulate seccomp filters // provided by libseccomp -package secutil +package seccomp import ( "io/ioutil" diff --git a/secutil/secutil_test.go b/seccomp/seccomp_test.go similarity index 99% rename from secutil/secutil_test.go rename to seccomp/seccomp_test.go index ba95b39..ea7e4df 100644 --- a/secutil/secutil_test.go +++ b/seccomp/seccomp_test.go @@ -1,4 +1,4 @@ -package secutil +package seccomp import ( "testing"