aboutsummaryrefslogtreecommitdiffhomepage
path: root/ldd/exec.go
blob: 45d33ec0e7a5962bfd52e0523bb8911f36e05ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ldd

import (
	"bytes"
	"context"
	"errors"
	"io"
	"os"
	"os/exec"
	"time"

	"hakurei.app/check"
	"hakurei.app/container"
	"hakurei.app/container/fhs"
	"hakurei.app/container/seccomp"
	"hakurei.app/container/std"
	"hakurei.app/message"
)

const (
	// msgStaticSuffix is the suffix of message printed to stderr by musl on a
	// statically linked program.
	msgStaticSuffix = ": Not a valid dynamic program"
	// msgStaticGlibc is a substring of the message printed to stderr by glibc
	// on a statically linked program.
	msgStaticGlibc = "not a dynamic executable"

	// lddName is the file name of ldd(1) passed to exec.LookPath.
	lddName = "ldd"
	// lddTimeout is the maximum duration ldd(1) is allowed to ran for before it
	// is terminated.
	lddTimeout = 4 * time.Second
)

// Resolve runs ldd(1) in a strict sandbox and connects its stdout to a [Decoder].
//
// The returned error has concrete type
// [exec.Error] or [check.AbsoluteError] for fault during lookup of ldd(1),
// [os.SyscallError] for fault creating the stdout pipe,
// [container.StartError] for fault during either stage of container setup.
// Otherwise, it passes through the return values of [Decoder.Decode].
func Resolve(
	ctx context.Context,
	msg message.Msg,
	pathname *check.Absolute,
) ([]*Entry, error) {
	if pathname == nil {
		if p, err := os.Executable(); err != nil {
			return nil, err
		} else if pathname, err = check.NewAbs(p); err != nil {
			return nil, err
		}
	}

	c, cancel := context.WithTimeout(ctx, lddTimeout)
	defer cancel()

	var toolPath *check.Absolute
	if s, err := exec.LookPath(lddName); err != nil {
		return nil, err
	} else if toolPath, err = check.NewAbs(s); err != nil {
		return nil, err
	}

	z := container.NewCommand(c, msg, toolPath, lddName, pathname.String())
	z.Hostname = "hakurei-" + lddName
	z.SeccompFlags |= seccomp.AllowMultiarch
	z.SeccompPresets |= std.PresetStrict
	stderr := new(bytes.Buffer)
	z.Stderr = stderr
	z.
		Bind(fhs.AbsRoot, fhs.AbsRoot, 0).
		Proc(fhs.AbsProc).
		Dev(fhs.AbsDev, false)

	var d *Decoder
	if r, err := z.StdoutPipe(); err != nil {
		return nil, err
	} else {
		d = NewDecoder(r)
	}

	if err := z.Start(); err != nil {
		return nil, err
	}
	defer func() { _, _ = io.Copy(os.Stderr, stderr) }()
	if err := z.Serve(); err != nil {
		return nil, err
	}

	entries, decodeErr := d.Decode()
	if decodeErr != nil {
		// do not cancel on successful decode to avoid racing with ldd(1) termination
		cancel()
	}

	if err := z.Wait(); err != nil {
		m := stderr.Bytes()
		if bytes.Contains(m, []byte(msgStaticSuffix)) || bytes.Contains(m, []byte(msgStaticGlibc)) {
			return nil, nil
		}

		if decodeErr != nil {
			return nil, errors.Join(decodeErr, err)
		}
		return nil, err
	}
	return entries, decodeErr
}