aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-02-08 13:03:45 +0900
committerOphestra <cat@gensokyo.uk>2025-02-08 13:03:45 +0900
commite14923ae530dda0601c204fe17ac1956f54a45b0 (patch)
tree1578bbc4c08d9afc94c4b009cd2bf64260574f05 /helper
parent7aff3ead3afaa7461e0054fbef23fb571b4add0a (diff)
helper/proc: move package out of internal
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'helper')
-rw-r--r--helper/bwrap/arg.go2
-rw-r--r--helper/pipe.go2
-rw-r--r--helper/proc/fd.go45
-rw-r--r--helper/proc/files.go17
-rw-r--r--helper/proc/self.go26
5 files changed, 90 insertions, 2 deletions
diff --git a/helper/bwrap/arg.go b/helper/bwrap/arg.go
index d419ba50..d69c597c 100644
--- a/helper/bwrap/arg.go
+++ b/helper/bwrap/arg.go
@@ -6,7 +6,7 @@ import (
"slices"
"strconv"
- "git.gensokyo.uk/security/fortify/internal/proc"
+ "git.gensokyo.uk/security/fortify/helper/proc"
)
type Builder interface {
diff --git a/helper/pipe.go b/helper/pipe.go
index c87d4f64..f17b74fb 100644
--- a/helper/pipe.go
+++ b/helper/pipe.go
@@ -6,7 +6,7 @@ import (
"os"
"os/exec"
- "git.gensokyo.uk/security/fortify/internal/proc"
+ "git.gensokyo.uk/security/fortify/helper/proc"
)
type pipes struct {
diff --git a/helper/proc/fd.go b/helper/proc/fd.go
new file mode 100644
index 00000000..e99abb38
--- /dev/null
+++ b/helper/proc/fd.go
@@ -0,0 +1,45 @@
+package proc
+
+import (
+ "encoding/gob"
+ "errors"
+ "os"
+ "strconv"
+)
+
+var (
+ ErrNotSet = errors.New("environment variable not set")
+ ErrInvalid = errors.New("bad file descriptor")
+)
+
+// Setup appends the read end of a pipe for payload transmission and returns its fd.
+func Setup(extraFiles *[]*os.File) (int, *gob.Encoder, error) {
+ if r, w, err := os.Pipe(); err != nil {
+ return -1, nil, err
+ } else {
+ fd := 3 + len(*extraFiles)
+ *extraFiles = append(*extraFiles, r)
+ return fd, gob.NewEncoder(w), nil
+ }
+}
+
+// Receive retrieves payload pipe fd from the environment,
+// receives its payload and returns the Close method of the pipe.
+func Receive(key string, e any) (func() error, error) {
+ var setup *os.File
+
+ if s, ok := os.LookupEnv(key); !ok {
+ return nil, ErrNotSet
+ } else {
+ if fd, err := strconv.Atoi(s); err != nil {
+ return nil, err
+ } else {
+ setup = os.NewFile(uintptr(fd), "setup")
+ if setup == nil {
+ return nil, ErrInvalid
+ }
+ }
+ }
+
+ return func() error { return setup.Close() }, gob.NewDecoder(setup).Decode(e)
+}
diff --git a/helper/proc/files.go b/helper/proc/files.go
new file mode 100644
index 00000000..712713a2
--- /dev/null
+++ b/helper/proc/files.go
@@ -0,0 +1,17 @@
+package proc
+
+import (
+ "os"
+ "os/exec"
+)
+
+func ExtraFile(cmd *exec.Cmd, f *os.File) (fd uintptr) {
+ return ExtraFileSlice(&cmd.ExtraFiles, f)
+}
+
+func ExtraFileSlice(extraFiles *[]*os.File, f *os.File) (fd uintptr) {
+ // ExtraFiles: If non-nil, entry i becomes file descriptor 3+i.
+ fd = uintptr(3 + len(*extraFiles))
+ *extraFiles = append(*extraFiles, f)
+ return
+}
diff --git a/helper/proc/self.go b/helper/proc/self.go
new file mode 100644
index 00000000..6dc92eb7
--- /dev/null
+++ b/helper/proc/self.go
@@ -0,0 +1,26 @@
+package proc
+
+import (
+ "os"
+ "sync"
+
+ "git.gensokyo.uk/security/fortify/internal/fmsg"
+)
+
+var (
+ executable string
+ executableOnce sync.Once
+)
+
+func copyExecutable() {
+ if name, err := os.Executable(); err != nil {
+ fmsg.Fatalf("cannot read executable path: %v", err)
+ } else {
+ executable = name
+ }
+}
+
+func MustExecutable() string {
+ executableOnce.Do(copyExecutable)
+ return executable
+}