forked from falcosecurity/driverkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentos.go
228 lines (203 loc) · 4.89 KB
/
centos.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
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
package builder
import (
"bytes"
"fmt"
"text/template"
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
)
// TargetTypeCentos identifies the Centos target.
const TargetTypeCentos Type = "centos"
func init() {
BuilderByTarget[TargetTypeCentos] = ¢os{}
}
// centos is a driverkit target.
type centos struct {
}
// Script compiles the script to build the kernel module and/or the eBPF probe.
func (c centos) Script(cfg Config) (string, error) {
t := template.New(string(TargetTypeCentos))
parsed, err := t.Parse(centosTemplate)
if err != nil {
return "", err
}
kr := kernelrelease.FromString(cfg.Build.KernelRelease)
// Check (and filter) existing kernels before continuing
urls, err := getResolvingURLs(fetchCentosKernelURLS(kr))
if err != nil {
return "", err
}
td := centosTemplateData{
DriverBuildDir: DriverDirectory,
ModuleDownloadURL: moduleDownloadURL(cfg),
KernelDownloadURL: urls[0],
GCCVersion: centosGccVersionFromKernelRelease(kr),
ModuleDriverName: cfg.DriverName,
ModuleFullPath: ModuleFullPath,
BuildModule: len(cfg.Build.ModuleFilePath) > 0,
BuildProbe: len(cfg.Build.ProbeFilePath) > 0,
}
buf := bytes.NewBuffer(nil)
err = parsed.Execute(buf, td)
if err != nil {
return "", err
}
return buf.String(), nil
}
func fetchCentosKernelURLS(kr kernelrelease.KernelRelease) []string {
vaultReleases := []string{
"6.0/os",
"6.0/updates",
"6.1/os",
"6.1/updates",
"6.2/os",
"6.2/updates",
"6.3/os",
"6.3/updates",
"6.4/os",
"6.4/updates",
"6.5/os",
"6.5/updates",
"6.6/os",
"6.6/updates",
"6.7/os",
"6.7/updates",
"6.8/os",
"6.8/updates",
"6.9/os",
"6.9/updates",
"6.10/os",
"6.10/updates",
"7.0.1406/os",
"7.0.1406/updates",
"7.1.1503/os",
"7.1.1503/updates",
"7.2.1511/os",
"7.2.1511/updates",
"7.3.1611/os",
"7.3.1611/updates",
"7.4.1708/os",
"7.4.1708/updates",
"7.5.1804/os",
"7.5.1804/updates",
"7.6.1810/os",
"7.6.1810/updates",
"7.7.1908/os",
"7.7.1908/updates",
"7.8.2003/os",
"7.8.2003/updates",
"7.9.2009/os",
"7.9.2009/updates",
"8.0.1905/os",
"8.0.1905/updates",
"8.1.1911/os",
"8.1.1911/updates",
}
centos8VaultReleases := []string{
"8.0.1905/BaseOS",
"8.1.1911/BaseOS",
"8.2.2004/BaseOS",
"8.3.2011/BaseOS",
"8.4.2105/BaseOS",
"8.5.2111/BaseOS",
}
edgeReleases := []string{
"6/os",
"6/updates",
"7/os",
"7/updates",
}
streamReleases := []string{
"8/BaseOS",
"8-stream/BaseOS",
}
urls := []string{}
for _, r := range edgeReleases {
urls = append(urls, fmt.Sprintf(
"https://mirrors.edge.kernel.org/centos/%s/x86_64/Packages/kernel-devel-%s%s.rpm",
r,
kr.Fullversion,
kr.FullExtraversion,
))
}
for _, r := range streamReleases {
urls = append(urls, fmt.Sprintf(
"https://mirrors.edge.kernel.org/centos/%s/x86_64/os/Packages/kernel-devel-%s%s.rpm",
r,
kr.Fullversion,
kr.FullExtraversion,
))
}
for _, r := range vaultReleases {
urls = append(urls, fmt.Sprintf(
"http://vault.centos.org/%s/x86_64/Packages/kernel-devel-%s%s.rpm",
r,
kr.Fullversion,
kr.FullExtraversion,
))
}
for _, r := range centos8VaultReleases {
urls = append(urls, fmt.Sprintf(
"http://vault.centos.org/%s/x86_64/os/Packages/kernel-devel-%s%s.rpm",
r,
kr.Fullversion,
kr.FullExtraversion,
))
}
return urls
}
type centosTemplateData struct {
DriverBuildDir string
ModuleDownloadURL string
KernelDownloadURL string
GCCVersion string
ModuleDriverName string
ModuleFullPath string
BuildModule bool
BuildProbe bool
}
const centosTemplate = `
#!/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
curl --silent -o kernel-devel.rpm -SL {{ .KernelDownloadURL }}
rpm2cpio kernel-devel.rpm | cpio --extract --make-directories
rm -Rf /tmp/kernel
mkdir -p /tmp/kernel
mv usr/src/kernels/*/* /tmp/kernel
# Change current gcc
ln -sf /usr/bin/gcc-{{ .GCCVersion }} /usr/bin/gcc
{{ if .BuildModule }}
# Build the module
cd {{ .DriverBuildDir }}
make KERNELDIR=/tmp/kernel
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .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 }}
`
func centosGccVersionFromKernelRelease(kr kernelrelease.KernelRelease) string {
switch kr.Version {
case "3":
return "5"
case "2":
return "4.8"
}
return "8"
}