forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
kmodule_linux.go
279 lines (232 loc) · 6.83 KB
/
kmodule_linux.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
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
// Copyright 2017-2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kmodule
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
// Flags to finit_module(2) / FileInit.
const (
// Ignore symbol version hashes.
MODULE_INIT_IGNORE_MODVERSIONS = 0x1
// Ignore kernel version magic.
MODULE_INIT_IGNORE_VERMAGIC = 0x2
)
// SyscallError contains an error message as well as the actual syscall Errno
type SyscallError struct {
Msg string
Errno syscall.Errno
}
func (s *SyscallError) Error() string {
if s.Errno != 0 {
return fmt.Sprintf("%s: %v", s.Msg, s.Errno)
}
return s.Msg
}
// Init loads the kernel module given by image with the given options.
func Init(image []byte, opts string) error {
optsNull, err := unix.BytePtrFromString(opts)
if err != nil {
return &SyscallError{Msg: fmt.Sprintf("kmodule.Init: could not convert %q to C string: %v", opts, err)}
}
if _, _, e := unix.Syscall(unix.SYS_INIT_MODULE, uintptr(unsafe.Pointer(&image[0])), uintptr(len(image)), uintptr(unsafe.Pointer(optsNull))); e != 0 {
return &SyscallError{
Msg: fmt.Sprintf("init_module(%v, %q) failed", image, opts),
Errno: e,
}
}
return nil
}
// FileInit loads the kernel module contained by `f` with the given opts and
// flags.
//
// FileInit falls back to Init when the finit_module(2) syscall is not available.
func FileInit(f *os.File, opts string, flags uintptr) error {
optsNull, err := unix.BytePtrFromString(opts)
if err != nil {
return &SyscallError{Msg: fmt.Sprintf("kmodule.Init: could not convert %q to C string: %v", opts, err)}
}
if _, _, e := unix.Syscall(unix.SYS_FINIT_MODULE, f.Fd(), uintptr(unsafe.Pointer(optsNull)), flags); e == unix.ENOSYS {
if flags != 0 {
return &SyscallError{Msg: fmt.Sprintf("finit_module unavailable"), Errno: e}
}
// Fall back to regular init_module(2).
img, err := ioutil.ReadAll(f)
if err != nil {
return &SyscallError{Msg: fmt.Sprintf("kmodule.FileInit: %v", err)}
}
return Init(img, opts)
} else if e != 0 {
return &SyscallError{
Msg: fmt.Sprintf("finit_module(%v, %q, %#x) failed", f, opts, flags),
Errno: e,
}
}
return nil
}
// Delete removes a kernel module.
func Delete(name string, flags uintptr) error {
modnameptr, err := unix.BytePtrFromString(name)
if err != nil {
return &SyscallError{Msg: fmt.Sprintf("could not delete module %q: %v", name, err)}
}
if _, _, e := unix.Syscall(unix.SYS_DELETE_MODULE, uintptr(unsafe.Pointer(modnameptr)), flags, 0); e != 0 {
return &SyscallError{Msg: fmt.Sprintf("could not delete module %q", name), Errno: e}
}
return nil
}
type modState uint8
const (
unloaded modState = iota
loading
loaded
)
type dependency struct {
state modState
deps []string
}
type depMap map[string]*dependency
// ProbeOpts contains optional parameters to Probe.
//
// An empty ProbeOpts{} should lead to the default behavior.
type ProbeOpts struct {
DryRunCB func(string)
RootDir string
KVer string
}
// Probe loads the given kernel module and its dependencies.
// It is calls ProbeOptions with the default ProbeOpts.
func Probe(name string, modParams string) error {
return ProbeOptions(name, modParams, ProbeOpts{})
}
// ProbeOptions loads the given kernel module and its dependencies.
// This functions takes ProbeOpts.
func ProbeOptions(name, modParams string, opts ProbeOpts) error {
deps, err := genDeps(opts)
if err != nil {
return &SyscallError{Msg: fmt.Sprintf("could not generate dependency map %v", err)}
}
modPath, err := findModPath(name, deps)
if err != nil {
return &SyscallError{Msg: fmt.Sprintf("could not find module path %q: %v", name, err)}
}
if opts.DryRunCB == nil {
// if the module is already loaded or does not have deps, or all of them are loaded
// then this succeeds and we are done
if err := loadModule(modPath, modParams, opts); err == nil {
return nil
}
// okay, we have to try the hard way and load dependencies first.
}
deps[modPath].state = loading
for _, d := range deps[modPath].deps {
if err := loadDeps(d, deps, opts); err != nil {
return err
}
}
if err := loadModule(modPath, modParams, opts); err != nil {
return err
}
// we don't care to set the state to loaded
// deps[modPath].state = loaded
return nil
}
func genDeps(opts ProbeOpts) (depMap, error) {
deps := make(depMap)
rel := opts.KVer
if rel == "" {
var u unix.Utsname
if err := unix.Uname(&u); err != nil {
return nil, fmt.Errorf("could not get release (uname -r): %v", err)
}
rel = string(u.Release[:bytes.IndexByte(u.Release[:], 0)])
}
var moduleDir string
for _, n := range []string{"/lib/modules", "/usr/lib/modules"} {
moduleDir = filepath.Join(opts.RootDir, n, strings.TrimSpace(rel))
if _, err := os.Stat(moduleDir); err == nil {
break
}
}
f, err := os.Open(filepath.Join(moduleDir, "modules.dep"))
if err != nil {
return nil, fmt.Errorf("could not open dependency file: %v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
txt := scanner.Text()
nameDeps := strings.Split(txt, ":")
modPath, modDeps := nameDeps[0], nameDeps[1]
modPath = filepath.Join(moduleDir, strings.TrimSpace(modPath))
var dependency dependency
if len(modDeps) > 0 {
for _, dep := range strings.Split(strings.TrimSpace(modDeps), " ") {
dependency.deps = append(dependency.deps, filepath.Join(moduleDir, dep))
}
}
deps[modPath] = &dependency
}
if err := scanner.Err(); err != nil {
return nil, err
}
return deps, nil
}
func findModPath(name string, m depMap) (string, error) {
for mp := range m {
if path.Base(mp) == name+".ko" {
return mp, nil
}
}
return "", fmt.Errorf("Could not find path for module %q", name)
}
func loadDeps(path string, m depMap, opts ProbeOpts) error {
dependency, ok := m[path]
if !ok {
return &SyscallError{Msg: fmt.Sprintf("could not find dependency %q", path)}
}
if dependency.state == loading {
return &SyscallError{Msg: fmt.Sprintf("circular dependency! %q already LOADING", path)}
} else if dependency.state == loaded {
return nil
}
m[path].state = loading
for _, dep := range dependency.deps {
if err := loadDeps(dep, m, opts); err != nil {
return err
}
}
// done with dependencies, load module
if err := loadModule(path, "", opts); err != nil {
return err
}
m[path].state = loaded
return nil
}
func loadModule(path, modParams string, opts ProbeOpts) error {
if opts.DryRunCB != nil {
opts.DryRunCB(path)
return nil
}
f, err := os.Open(path)
if err != nil {
return &SyscallError{Msg: fmt.Sprintf("could not open %q: %v", path, err)}
}
defer f.Close()
if err := FileInit(f, modParams, 0); err != nil {
if serr, ok := err.(*SyscallError); !ok || (ok && serr.Errno != unix.EEXIST) {
return err
}
}
return nil
}