git.sophuwu.com > gophuwu
added memory file exec
sophuwu sophie@sophuwu.com
Mon, 28 Jul 2025 06:22:50 +0200
commit

cfa0ff2c2cc3878a965ca9886f551e91e8060725

parent

26e29b0c923a9b8eb52d77e0c05277bc2edca83b

1 files changed, 68 insertions(+), 0 deletions(-)

jump to
A mexec/mexec.go

@@ -0,0 +1,68 @@

+package mexec + +import ( + "errors" + "fmt" + "os" + "os/exec" + "syscall" + "unsafe" +) + +type Bin struct { + Data *[]byte + Fd *os.File +} + +func (b *Bin) Close() error { + if b.Fd != nil { + err := b.Fd.Close() + b.Fd = nil + return err + } + return nil +} + +func (b *Bin) Open() error { + err := b.Close() + if err != nil { + return err + } + a := make([]byte, 1) + r, _, e := syscall.Syscall(319, uintptr(unsafe.Pointer(&a[0])), uintptr(0x1), 0) + if e != 0 { + return errors.New("memfd_create failed") + } + b.Fd = os.NewFile(r, fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), int(r))) + if _, err = b.Fd.Write(*b.Data); err != nil { + _ = b.Fd.Close() + return err + } + return nil +} + +func (b *Bin) Command(args ...string) *exec.Cmd { + return exec.Command(b.Fd.Name(), args...) +} + +func NewBin(data *[]byte) (*Bin, error) { + if len(*data) == 0 { + return nil, errors.New("binary data is empty") + } + bin := &Bin{Data: data} + if err := bin.Open(); err != nil { + return nil, err + } + return bin, nil +} + +func OpenBin(file string) (*Bin, error) { + b, err := os.ReadFile(file) + if err != nil { + return nil, fmt.Errorf("failed to read binary file %s: %w", file, err) + } + if len(b) == 0 { + return nil, errors.New("binary file is empty") + } + return NewBin(&b) +}