aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ifaceprobe/ifaceprobe.go
blob: c5bb6363943daf54bd84173f2e704b57833a4e8e (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
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
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

// Package ifaceprobe implements dynamic component of automatic kernel interface extraction.
// Currently it discovers all /{dev,sys,proc} files, and collects coverage for open/read/write/mmap/ioctl
// syscalls on these files. Later this allows to build file path <-> file_operations mapping.
package ifaceprobe

import (
	"context"
	"fmt"
	"path/filepath"
	"slices"
	"strings"
	"sync"

	"github.com/google/syzkaller/pkg/csource"
	"github.com/google/syzkaller/pkg/flatrpc"
	"github.com/google/syzkaller/pkg/fuzzer/queue"
	"github.com/google/syzkaller/pkg/log"
	"github.com/google/syzkaller/pkg/mgrconfig"
	"github.com/google/syzkaller/pkg/symbolizer"
	"github.com/google/syzkaller/prog"
)

// Info represents information about dynamically extracted information.
type Info struct {
	Files []FileInfo
	PCs   []PCInfo
}

type FileInfo struct {
	Name  string // Full file name, e.g. /dev/null.
	Cover []int  // Combined coverage for operations on the file.
}

type PCInfo struct {
	Func string
	File string
}

// Run does dynamic analysis and returns dynamic info.
// As it runs it will submit some test program requests to the exec queue.
func Run(ctx context.Context, cfg *mgrconfig.Config, features flatrpc.Feature, exec queue.Executor) (*Info, error) {
	return (&prober{
		ctx:      ctx,
		cfg:      cfg,
		features: features,
		exec:     exec,
		done:     make(chan *fileDesc, 100),
		errc:     make(chan error, 1),
	}).run()
}

type prober struct {
	ctx      context.Context
	cfg      *mgrconfig.Config
	features flatrpc.Feature
	exec     queue.Executor
	wg       sync.WaitGroup
	done     chan *fileDesc
	errc     chan error
}

type fileDesc struct {
	file    string
	results []*queue.Result
}

func (pr *prober) run() (*Info, error) {
	symb := symbolizer.Make(pr.cfg.SysTarget)
	defer symb.Close()

	for _, glob := range globList() {
		pr.submitGlob(glob)
	}

	go func() {
		pr.wg.Wait()
		close(pr.done)
	}()

	info := &Info{}
	pcIndexes := make(map[uint64]int)
	kernelObj := filepath.Join(pr.cfg.KernelObj, pr.cfg.SysTarget.KernelObject)
	sourceBase := filepath.Clean(pr.cfg.KernelSrc) + string(filepath.Separator)
	i := 0
	for desc := range pr.done {
		i++
		if i%500 == 0 {
			log.Logf(0, "done file %v", i)
		}
		fi := FileInfo{
			Name: desc.file,
		}
		fileDedup := make(map[uint64]bool)
		for _, res := range desc.results {
			cover := append(res.Info.Calls[0].Cover, res.Info.Calls[1].Cover...)
			for _, pc := range cover {
				if fileDedup[pc] {
					continue
				}
				fileDedup[pc] = true
				pcIndex, ok := pcIndexes[pc]
				if !ok {
					pcIndex = -1
					frames, err := symb.Symbolize(kernelObj, pc)
					if err != nil {
						return nil, err
					}
					if len(frames) != 0 {
						// Look only at the non-inline frame,
						// callbacks we are looking for can't be inlined.
						frame := frames[len(frames)-1]
						pcIndex = len(info.PCs)
						info.PCs = append(info.PCs, PCInfo{
							Func: frame.Func,
							File: strings.TrimPrefix(filepath.Clean(frame.File), sourceBase),
						})
					}
					pcIndexes[pc] = pcIndex
				}
				if pcIndex >= 0 {
					fi.Cover = append(fi.Cover, pcIndex)
				}
			}
		}
		slices.Sort(fi.Cover)
		info.Files = append(info.Files, fi)
	}
	slices.SortFunc(info.Files, func(a, b FileInfo) int {
		return strings.Compare(a.Name, b.Name)
	})
	select {
	case err := <-pr.errc:
		return nil, err
	default:
		return info, nil
	}
}

func (pr *prober) noteError(err error) {
	select {
	case pr.errc <- err:
	default:
	}
}

func (pr *prober) submitGlob(glob string) {
	pr.wg.Add(1)
	req := &queue.Request{
		Type:        flatrpc.RequestTypeGlob,
		GlobPattern: glob,
		ExecOpts: flatrpc.ExecOpts{
			EnvFlags: flatrpc.ExecEnvSandboxNone | csource.FeaturesToFlags(pr.features, nil),
		},
		Important: true,
	}
	req.OnDone(pr.onGlobDone)
	pr.exec.Submit(req)
}

func (pr *prober) onGlobDone(req *queue.Request, res *queue.Result) bool {
	defer pr.wg.Done()
	if res.Status != queue.Success {
		pr.noteError(fmt.Errorf("failed to execute glob: %w (%v)\n%s\n%s",
			res.Err, res.Status, req.GlobPattern, res.Output))
	}
	files := res.GlobFiles()
	log.Logf(0, "glob %v expanded to %v files", req.GlobPattern, len(files))
	for _, file := range files {
		if extractFileFilter(file) {
			pr.submitFile(file)
		}
	}
	return true
}

func (pr *prober) submitFile(file string) {
	pr.wg.Add(1)
	var fops = []struct {
		mode string
		call string
	}{
		{mode: "O_RDONLY", call: "read(r0, &AUTO=' ', AUTO)"},
		{mode: "O_WRONLY", call: "write(r0, &AUTO=' ', AUTO)"},
		{mode: "O_RDONLY", call: "ioctl(r0, 0x0, 0x0)"},
		{mode: "O_WRONLY", call: "ioctl(r0, 0x0, 0x0)"},
		{mode: "O_RDONLY", call: "mmap(0x0, 0x1000, 0x1, 0x2, r0, 0)"},
		{mode: "O_WRONLY", call: "mmap(0x0, 0x1000, 0x2, 0x2, r0, 0)"},
	}
	desc := &fileDesc{
		file: file,
	}
	var reqs []*queue.Request
	for _, desc := range fops {
		text := fmt.Sprintf("r0 = openat(0x%x, &AUTO='%s', 0x%x, 0x0)\n%v",
			pr.constVal("AT_FDCWD"), file, pr.constVal(desc.mode), desc.call)
		p, err := pr.cfg.Target.Deserialize([]byte(text), prog.StrictUnsafe)
		if err != nil {
			panic(fmt.Sprintf("failed to deserialize: %v\n%v", err, text))
		}
		req := &queue.Request{
			Prog: p,
			ExecOpts: flatrpc.ExecOpts{
				EnvFlags: flatrpc.ExecEnvSandboxNone | flatrpc.ExecEnvSignal |
					csource.FeaturesToFlags(pr.features, nil),
				ExecFlags: flatrpc.ExecFlagCollectCover,
			},
			Important: true,
		}
		reqs = append(reqs, req)
		pr.exec.Submit(req)
	}
	go func() {
		defer pr.wg.Done()
		for _, req := range reqs {
			res := req.Wait(pr.ctx)
			if res.Status != queue.Success {
				pr.noteError(fmt.Errorf("failed to execute prog: %w (%v)\n%s\n%s",
					res.Err, res.Status, req.Prog.Serialize(), res.Output))
				continue
			}
			desc.results = append(desc.results, res)
		}
		pr.done <- desc
	}()
}

func (pr *prober) constVal(name string) uint64 {
	val, ok := pr.cfg.Target.ConstMap[name]
	if !ok {
		panic(fmt.Sprintf("const %v is not present", name))
	}
	return val
}

// globList returns a list of glob's we are interested in.
func globList() []string {
	var globs []string
	// /selinux is mounted by executor, we probably should mount it at the standard /sys/fs/selinux,
	// but this is where it is now.
	// Also query the test cwd, executor creates some links in there.
	for _, path := range []string{"/dev", "/sys", "/proc", "/selinux", "."} {
		// Our globs currently do not support recursion (#4906),
		// so we append N "/*" parts manully. Some of the paths can be very deep, e.g. try:
		// sudo find /sys -ls 2>/dev/null | sed "s#[^/]##g" | sort | uniq -c
		for i := 1; i < 15; i++ {
			globs = append(globs, path+strings.Repeat("/*", i))
		}
	}
	return globs
}

func extractFileFilter(file string) bool {
	if strings.HasPrefix(file, "/dev/") ||
		strings.HasPrefix(file, "/selinux/") ||
		strings.HasPrefix(file, "./") {
		return true
	}
	if proc := "/proc/"; strings.HasPrefix(file, proc) {
		// These won't be present in the test process.
		if strings.HasPrefix(file, "/proc/self/fdinfo/") ||
			strings.HasPrefix(file, "/proc/thread-self/fdinfo/") {
			return false
		}
		// It contains actual pid number that will be different in the test.
		if strings.HasPrefix(file, "/proc/self/task/") {
			return false
		}
		// Ignore all actual processes.
		c := file[len(proc)]
		return c < '0' || c > '9'
	}
	if strings.HasPrefix(file, "/sys/") {
		// There are too many tracing events, so leave just one of them.
		if strings.HasPrefix(file, "/sys/kernel/tracing/events/") &&
			!strings.HasPrefix(file, "/sys/kernel/tracing/events/vmalloc/") ||
			strings.HasPrefix(file, "/sys/kernel/debug/tracing/events/") &&
				!strings.HasPrefix(file, "/sys/kernel/debug/tracing/events/vmalloc/") {
			return false
		}
		// There are too many slabs, so leave just one of them.
		if strings.HasPrefix(file, "/sys/kernel/slab/") &&
			!strings.HasPrefix(file, "/sys/kernel/slab/kmalloc-64") {
			return false
		}
		// There are too many of these, leave just one of them.
		if strings.HasPrefix(file, "/sys/fs/selinux/class/") &&
			!strings.HasPrefix(file, "/sys/fs/selinux/class/file/") {
			return false
		}
		return true
	}
	panic(fmt.Sprintf("unhandled file %q", file))
}