forked from falcosecurity/driverkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazonlinux.go
More file actions
318 lines (277 loc) · 7.88 KB
/
Copy pathamazonlinux.go
File metadata and controls
318 lines (277 loc) · 7.88 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package builder
import (
"bufio"
"bytes"
"compress/bzip2"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"text/template"
"database/sql"
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
_ "github.com/mattn/go-sqlite3" // Why do you want me to justify? Leave me alone :)
logger "github.com/sirupsen/logrus"
)
type amazonlinux2 struct {
}
type amazonlinux struct {
}
// TargetTypeAmazonLinux2 identifies the AmazonLinux2 target.
const TargetTypeAmazonLinux2 Type = "amazonlinux2"
// TargetTypeAmazonLinux identifies the AmazonLinux target.
const TargetTypeAmazonLinux Type = "amazonlinux"
func init() {
BuilderByTarget[TargetTypeAmazonLinux2] = &amazonlinux2{}
BuilderByTarget[TargetTypeAmazonLinux] = &amazonlinux{}
}
const amazonlinuxTemplate = `
#!/bin/bash
set -xeuo pipefail
rm -Rf {{ .DriverBuildDir }}
mkdir {{ .DriverBuildDir }}
rm -Rf /tmp/module-download
mkdir -p /tmp/module-download
curl --silent -SL {{ .ModuleDownloadURL }} | tar -xzf - -C /tmp/module-download
mv /tmp/module-download/*/driver/* {{ .DriverBuildDir }}
cp /driverkit/module-Makefile {{ .DriverBuildDir }}/Makefile
cp /driverkit/module-driver-config.h {{ .DriverBuildDir }}/driver_config.h
# Fetch the kernel
mkdir /tmp/kernel-download
cd /tmp/kernel-download
{{ range $url := .KernelDownloadURLs }}
curl --silent -o kernel.rpm -SL {{ $url }}
rpm2cpio kernel.rpm | cpio --extract --make-directories
rm -rf kernel.rpm
{{ end }}
rm -Rf /tmp/kernel
mkdir -p /tmp/kernel
mv usr/src/kernels/*/* /tmp/kernel
{{ if .BuildModule }}
# Build the kernel module
cd {{ .DriverBuildDir }}
make KERNELDIR=/tmp/kernel
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
{{ end }}
{{ if .BuildProbe }}
# Build the eBPF probe
cd {{ .DriverBuildDir }}/bpf
make LLC=/usr/bin/llc-7 CLANG=/usr/bin/clang-7 CC=/usr/bin/gcc KERNELDIR=/tmp/kernel
ls -l probe.o
{{ end }}
`
type amazonlinuxTemplateData struct {
DriverBuildDir string
ModuleDownloadURL string
KernelDownloadURLs []string
ModuleDriverName string
ModuleFullPath string
BuildModule bool
BuildProbe bool
}
// Script compiles the script to build the kernel module and/or the eBPF probe.
func (a amazonlinux2) Script(c Config) (string, error) {
return script(c, TargetTypeAmazonLinux2)
}
// Script compiles the script to build the kernel module and/or the eBPF probe.
func (a amazonlinux) Script(c Config) (string, error) {
return script(c, TargetTypeAmazonLinux)
}
func script(c Config, targetType Type) (string, error) {
t := template.New(string(targetType))
parsed, err := t.Parse(amazonlinuxTemplate)
if err != nil {
return "", err
}
kv := kernelrelease.FromString(c.Build.KernelRelease)
// Check (and filter) existing kernels before continuing
packages, err := fetchAmazonLinuxPackagesURLs(kv, c.Build.Architecture, targetType)
if err != nil {
return "", err
}
if len(packages) != 2 {
return "", fmt.Errorf("target %s needs to find both kernel and kernel-devel packages", targetType)
}
urls, err := getResolvingURLs(packages)
if err != nil {
return "", err
}
td := amazonlinuxTemplateData{
DriverBuildDir: DriverDirectory,
ModuleDownloadURL: moduleDownloadURL(c),
KernelDownloadURLs: urls,
ModuleDriverName: c.DriverName,
ModuleFullPath: ModuleFullPath,
BuildModule: len(c.Build.ModuleFilePath) > 0,
BuildProbe: len(c.Build.ProbeFilePath) > 0,
}
buf := bytes.NewBuffer(nil)
err = parsed.Execute(buf, td)
if err != nil {
return "", err
}
return buf.String(), nil
}
var reposByTarget = map[Type][]string{
TargetTypeAmazonLinux2: []string{
"core/2.0",
"core/latest",
"extras/kernel-5.4/latest",
},
TargetTypeAmazonLinux: []string{
"latest/updates",
"latest/main",
"2017.03/updates",
"2017.03/main",
"2017.09/updates",
"2017.09/main",
"2018.03/updates",
"2018.03/main",
},
}
var baseByTarget = map[Type]string{
TargetTypeAmazonLinux: "http://repo.us-east-1.amazonaws.com/%s",
TargetTypeAmazonLinux2: "http://amazonlinux.us-east-1.amazonaws.com/2/core/%s/%s",
}
func fetchAmazonLinuxPackagesURLs(kv kernelrelease.KernelRelease, arch string, targetType Type) ([]string, error) {
urls := []string{}
visited := map[string]bool{}
amazonlinux2baseURL := "http://amazonlinux.us-east-1.amazonaws.com"
for _, v := range reposByTarget[targetType] {
var baseURL string
switch targetType {
case TargetTypeAmazonLinux:
baseURL = fmt.Sprintf("http://repo.us-east-1.amazonaws.com/%s", v)
case TargetTypeAmazonLinux2:
baseURL = fmt.Sprintf("%s/2/%s/%s", amazonlinux2baseURL, v, arch)
default:
return nil, fmt.Errorf("unsupported target")
}
mirror := fmt.Sprintf("%s/%s", baseURL, "mirror.list")
logger.WithField("url", mirror).WithField("version", v).Debug("looking for repo...")
// Obtain the repo URL by getting mirror URL content
mirrorRes, err := http.Get(mirror)
if err != nil {
return nil, err
}
defer mirrorRes.Body.Close()
var repo string
scanner := bufio.NewScanner(mirrorRes.Body)
if scanner.Scan() {
repo = scanner.Text()
}
if repo == "" {
return nil, fmt.Errorf("repository not found")
}
repo = strings.ReplaceAll(strings.TrimSuffix(string(repo), "\n"), "$basearch", arch)
ext := "gz"
if targetType == TargetTypeAmazonLinux {
ext = "bz2"
}
repoDatabaseURL := fmt.Sprintf("%s/repodata/primary.sqlite.%s", repo, ext)
if _, ok := visited[repoDatabaseURL]; ok {
continue
}
// Download the repo database
repoRes, err := http.Get(repoDatabaseURL)
logger.WithField("url", repoDatabaseURL).Debug("downloading...")
if err != nil {
return nil, err
}
defer repoRes.Body.Close()
visited[repoDatabaseURL] = true
// Decompress the database
var unzipFunc func(io.Reader) ([]byte, error)
if targetType == TargetTypeAmazonLinux {
unzipFunc = bunzip
} else {
unzipFunc = gunzip
}
dbBytes, err := unzipFunc(repoRes.Body)
if err != nil {
return nil, err
}
// Create the temporary database file
dbFile, err := ioutil.TempFile(os.TempDir(), fmt.Sprintf("%s-*.sqlite", targetType))
if err != nil {
return nil, err
}
defer os.Remove(dbFile.Name())
if _, err := dbFile.Write(dbBytes); err != nil {
return nil, err
}
// Open the database
db, err := sql.Open("sqlite3", dbFile.Name())
if err != nil {
return nil, err
}
defer db.Close()
logger.WithField("db", dbFile.Name()).Debug("connecting to database...")
// Query the database
rel := strings.TrimPrefix(strings.TrimSuffix(kv.FullExtraversion, fmt.Sprintf(".%s", arch)), "-")
q := fmt.Sprintf("SELECT location_href FROM packages WHERE name LIKE 'kernel%%' AND name NOT LIKE 'kernel-livepatch%%' AND name NOT LIKE '%%doc%%' AND name NOT LIKE '%%tools%%' AND name NOT LIKE '%%headers%%' AND version='%s' AND release='%s'", kv.Fullversion, rel)
stmt, err := db.Prepare(q)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query()
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var href string
err = rows.Scan(&href)
if err != nil {
log.Fatal(err)
}
base := repo
if targetType == TargetTypeAmazonLinux2 {
base = amazonlinux2baseURL
}
href = strings.ReplaceAll(href, "../", "")
urls = append(urls, fmt.Sprintf("%s/%s", base, href))
}
if err := dbFile.Close(); err != nil {
return nil, err
}
// Found, do not continue
// todo > verify amazonlinux always needs 2 packages (kernel and kernel-devel) too
if len(urls) == 2 {
break
}
}
return urls, nil
}
func gunzip(data io.Reader) (res []byte, err error) {
var r io.Reader
r, err = gzip.NewReader(data)
if err != nil {
return
}
var b bytes.Buffer
_, err = b.ReadFrom(r)
if err != nil {
return
}
res = b.Bytes()
return
}
func bunzip(data io.Reader) (res []byte, err error) {
var r io.Reader
r = bzip2.NewReader(data)
var b bytes.Buffer
_, err = b.ReadFrom(r)
if err != nil {
return
}
res = b.Bytes()
return
}