-
Notifications
You must be signed in to change notification settings - Fork 64
/
runner_linuxu.go
186 lines (160 loc) · 5.2 KB
/
runner_linuxu.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package run
import (
"context"
"debug/elf"
"fmt"
"os"
"path/filepath"
machineapi "kraftkit.sh/api/machine/v1alpha1"
"kraftkit.sh/config"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/tui/paraprogress"
"kraftkit.sh/unikraft/elfloader"
)
// https://github.com/file/file/blob/FILE5_44/src/readelf.h#L543
const DF_1_PIE elf.DynFlag = 0x08000000 //nolint:stylecheck
// runnerLinuxu is a runner that allows for the instantiation of a Linux
// Userspace binary in "binary compatibility"-mode which in turn utilizes the
// Unikraft ELFLoader application to enable its runtime. The binary (and
// desired root filesystem) are mounted to this application to enable its
// runtime. E.g.:
//
// $ kraft run path/to/bin
type runnerLinuxu struct {
exePath string
args []string
}
// String implements Runner.
func (runner *runnerLinuxu) String() string {
return "linuxu"
}
// Runnable implements Runner.
func (runner *runnerLinuxu) Runnable(ctx context.Context, opts *Run, args ...string) (bool, error) {
if len(args) == 0 {
return false, fmt.Errorf("no arguments supplied")
}
runner.exePath = args[0]
runner.args = args[1:]
fs, err := os.Stat(runner.exePath)
if err != nil {
return false, err
} else if fs.IsDir() {
return false, fmt.Errorf("first positional argument is a directory: %s", runner.exePath)
}
fi, err := os.Open(runner.exePath)
if err != nil {
return false, fmt.Errorf("opening file %s: %w", runner.exePath, err)
}
defer fi.Close()
ef, err := elf.NewFile(fi)
if err != nil {
return false, fmt.Errorf("reading ELF file %s: %w", runner.exePath, err)
}
// Both static and dynamic PIEs have this type.
if ef.Type != elf.ET_DYN {
return false, fmt.Errorf("ELF file is shared object")
}
// Based on file(1) and elf.(*File).DynString.
// https://github.com/file/file/blob/FILE5_44/src/readelf.c#L1141-L1147
// https://cs.opensource.google/go/go/+/refs/tags/go1.20.4:src/debug/elf/file.go;l=1602-1643
ds := ef.SectionByType(elf.SHT_DYNAMIC)
if ds == nil {
return false, fmt.Errorf("ELF file has type %s but no dynamic section", ef.Type)
}
d, err := ds.Data()
if err != nil {
return false, fmt.Errorf("reading ELF section %s of type %s", ds.Name, ds.Type)
}
for len(d) > 0 {
var t elf.DynTag
var v uint64
switch ef.Class {
case elf.ELFCLASS32:
t = elf.DynTag(ef.ByteOrder.Uint32(d[0:4]))
v = uint64(ef.ByteOrder.Uint32(d[4:8]))
d = d[8:]
case elf.ELFCLASS64:
t = elf.DynTag(ef.ByteOrder.Uint64(d[0:8]))
v = ef.ByteOrder.Uint64(d[8:16])
d = d[16:]
}
if t == elf.DT_FLAGS_1 && elf.DynFlag(v)&DF_1_PIE != 0 {
return true, nil
}
}
// Fallback
// This logic originates from Debian's hardening-check devscript.
// https://salsa.debian.org/debian/devscripts/-/blob/v2.23.3/scripts/hardening-check.pl?ref_type=tags#L293-303
for _, p := range ef.Progs {
if p.Type == elf.PT_PHDR {
return true, nil
}
}
return false, fmt.Errorf("file is not ELF executable")
}
// Prepare implements Runner.
func (runner *runnerLinuxu) Prepare(ctx context.Context, opts *Run, machine *machineapi.Machine, args ...string) error {
loader, err := elfloader.NewELFLoaderFromPrebuilt(ctx, runner.exePath)
if err != nil {
return err
}
// Create a temporary directory where the image can be stored
dir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
// TODO(nderjung): For now, there is no proper support for using the --initrd
// flag since we set Linux userspace application as the initramfs. In the
// future when we have better volume/filesystem support, we can re-think its
// use here.
if len(opts.InitRd) > 0 {
log.G(ctx).Warnf("ignoring --initrd in favour of Linux userspace binary")
}
paramodel, err := paraprogress.NewParaProgress(
ctx,
[]*paraprogress.Process{paraprogress.NewProcess(
fmt.Sprintf("pulling %s", loader.Name()),
func(ctx context.Context, w func(progress float64)) error {
popts := []pack.PullOption{
pack.WithPullWorkdir(dir),
pack.WithPullPlatform(opts.platform.String()),
}
if log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) == log.FANCY {
popts = append(popts, pack.WithPullProgressFunc(w))
}
return loader.Pull(
ctx,
popts...,
)
},
)},
paraprogress.IsParallel(false),
paraprogress.WithRenderer(
log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY,
),
paraprogress.WithFailFast(true),
)
if err != nil {
return err
}
if err := paramodel.Start(); err != nil {
return err
}
machine.Spec.Architecture = loader.Architecture().Name()
machine.Spec.Platform = loader.Platform().Name()
machine.Spec.Kernel = fmt.Sprintf("elfloader://%s:%s", loader.Name(), loader.Version())
machine.Spec.ApplicationArgs = append([]string{filepath.Base(runner.exePath)}, runner.args...)
machine.Status.InitrdPath = runner.exePath
// Use the symbolic debuggable kernel image?
if opts.WithKernelDbg {
machine.Status.KernelPath = loader.KernelDbg()
} else {
machine.Status.KernelPath = loader.Kernel()
}
return nil
}