fixed abs path check

This commit is contained in:
criyle 2019-07-14 17:53:29 -07:00
parent d260bf4657
commit b7b6f78f64
3 changed files with 33 additions and 30 deletions

View File

@ -1,9 +1,6 @@
package runconfig
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
)
@ -152,29 +149,6 @@ func dirname(path string) string {
return ""
}
// getProcCwd gets the process CWD
func getProcCwd(pid int) string {
fileName := "/proc/self/cwd"
if pid > 0 {
fileName = fmt.Sprintf("/proc/%d/cwd", pid)
}
s, err := os.Readlink(fileName)
if err != nil {
return ""
}
return s
}
// absPath calculates the absolute path for a process
// built-in function did the dirty works to resolve relative paths
func absPath(pid int, p string) string {
// if relative path
if path.IsAbs(p) {
return path.Join(getProcCwd(pid), p)
}
return path.Clean(p)
}
func realPath(p string) string {
f, err := filepath.EvalSymlinks(p)
if err != nil {

View File

@ -26,6 +26,7 @@ func (h *Handler) CheckRead(fn string) runprogram.TraceAction {
// CheckWrite checks whether the file have write permission
func (h *Handler) CheckWrite(fn string) runprogram.TraceAction {
fmt.Println("write: ", fn)
if !h.FileSet.IsWritableFile(fn) {
return h.onDgsFileDetect(fn)
}

View File

@ -3,6 +3,7 @@ package runprogram
import (
"fmt"
"os"
"path"
"syscall"
libseccomp "github.com/seccomp/libseccomp-golang"
@ -21,8 +22,12 @@ func (h *tracerHandler) Debug(v ...interface{}) {
}
}
func (h *tracerHandler) getString(ctx *tracer.Context, addr uint) string {
return absPath(ctx.Pid, ctx.GetString(uintptr(addr)))
}
func (h *tracerHandler) checkOpen(ctx *tracer.Context, addr uint, flags uint) TraceAction {
fn := ctx.GetString(uintptr(addr))
fn := h.getString(ctx, addr)
isReadOnly := (flags&syscall.O_ACCMODE == syscall.O_RDONLY) &&
(flags&syscall.O_CREAT == 0) &&
(flags&syscall.O_EXCL == 0) &&
@ -36,19 +41,19 @@ func (h *tracerHandler) checkOpen(ctx *tracer.Context, addr uint, flags uint) Tr
}
func (h *tracerHandler) checkRead(ctx *tracer.Context, addr uint) TraceAction {
fn := ctx.GetString(uintptr(addr))
fn := h.getString(ctx, addr)
h.Debug("check read: ", fn)
return h.Handler.CheckRead(fn)
}
func (h *tracerHandler) checkWrite(ctx *tracer.Context, addr uint) TraceAction {
fn := ctx.GetString(uintptr(addr))
fn := h.getString(ctx, addr)
h.Debug("check write: ", fn)
return h.Handler.CheckWrite(fn)
}
func (h *tracerHandler) checkStat(ctx *tracer.Context, addr uint) TraceAction {
fn := ctx.GetString(uintptr(addr))
fn := h.getString(ctx, addr)
h.Debug("check stat: ", fn)
return h.Handler.CheckStat(fn)
}
@ -138,3 +143,26 @@ func getFileMode(flags uint) string {
return "??"
}
}
// getProcCwd gets the process CWD
func getProcCwd(pid int) string {
fileName := "/proc/self/cwd"
if pid > 0 {
fileName = fmt.Sprintf("/proc/%d/cwd", pid)
}
s, err := os.Readlink(fileName)
if err != nil {
return ""
}
return s
}
// absPath calculates the absolute path for a process
// built-in function did the dirty works to resolve relative paths
func absPath(pid int, p string) string {
// if relative path
if !path.IsAbs(p) {
return path.Join(getProcCwd(pid), p)
}
return path.Clean(p)
}