Skip to content

Commit 4d34627

Browse files
committed
[WIP] fuzzer: add security context generator
Added the security context generator in the fuzzer package which will contain the implementation for loading a binary security policy, parse it and then generate an appropiate security context based on the syscalls that are fuzzed in a program. Signed-off-by: Rares Constantin <[email protected]>
1 parent 982a6f0 commit 4d34627

File tree

7 files changed

+41
-36
lines changed

7 files changed

+41
-36
lines changed

pkg/fuzzer/fuzzer.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424

2525
type Fuzzer struct {
2626
Stats
27-
Config *Config
28-
Cover *Cover
27+
Config *Config
28+
Cover *Cover
29+
SecContextGen *SecContextGenerator
2930

3031
ctx context.Context
3132
mu sync.Mutex
@@ -43,16 +44,17 @@ type Fuzzer struct {
4344
}
4445

4546
func NewFuzzer(ctx context.Context, cfg *Config, rnd *rand.Rand,
46-
target *prog.Target) *Fuzzer {
47+
target *prog.Target, seclabelgen *SecContextGenerator) *Fuzzer {
4748
if cfg.NewInputFilter == nil {
4849
cfg.NewInputFilter = func(call string) bool {
4950
return true
5051
}
5152
}
5253
f := &Fuzzer{
53-
Stats: newStats(target),
54-
Config: cfg,
55-
Cover: newCover(),
54+
Stats: newStats(target),
55+
Config: cfg,
56+
Cover: newCover(),
57+
SecContextGen: seclabelgen,
5658

5759
ctx: ctx,
5860
rnd: rnd,
@@ -219,8 +221,6 @@ type Config struct {
219221
EnabledCalls map[*prog.Syscall]bool
220222
NoMutateCalls map[int]bool
221223
FetchRawCover bool
222-
Sandbox string
223-
SandboxArg int64
224224
AttachSecContext bool
225225
NewInputFilter func(call string) bool
226226
PatchTest bool
@@ -374,16 +374,8 @@ func (fuzzer *Fuzzer) AddCandidates(candidates []Candidate) {
374374
Stat: fuzzer.statExecCandidate,
375375
Important: true,
376376
}
377-
req.Prog.SecContext = ""
378-
if fuzzer.Config.AttachSecContext {
379-
req.Prog.SecContext = "user_u:user_r:user_t:s0"
380-
if fuzzer.Config.Sandbox == "android" {
381-
if fuzzer.Config.SandboxArg == 0 {
382-
req.Prog.SecContext = "u:r:untrusted_app:s0:c512,c768"
383-
} else {
384-
req.Prog.SecContext = ""
385-
}
386-
}
377+
if fuzzer.SecContextGen != nil {
378+
req.Prog.SecContext = fuzzer.SecContextGen.getSecLabel()
387379
}
388380
fuzzer.enqueue(fuzzer.candidateQueue, req, candidate.Flags|progCandidate, 0)
389381
}

pkg/fuzzer/fuzzer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestFuzz(t *testing.T) {
5959
EnabledCalls: map[*prog.Syscall]bool{
6060
target.SyscallMap["syz_test_fuzzer1"]: true,
6161
},
62-
}, rand.New(testutil.RandSource(t)), target)
62+
}, rand.New(testutil.RandSource(t)), target, nil)
6363

6464
go func() {
6565
for {
@@ -108,7 +108,7 @@ func BenchmarkFuzzer(b *testing.B) {
108108
Corpus: corpus.NewCorpus(ctx),
109109
Coverage: true,
110110
EnabledCalls: calls,
111-
}, rand.New(rand.NewSource(time.Now().UnixNano())), target)
111+
}, rand.New(rand.NewSource(time.Now().UnixNano())), target, nil)
112112

113113
b.ResetTimer()
114114
b.RunParallel(func(pb *testing.PB) {

pkg/fuzzer/job.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,7 @@ func genProgRequest(fuzzer *Fuzzer, rnd *rand.Rand) *queue.Request {
4545
p := fuzzer.target.Generate(rnd,
4646
fuzzer.RecommendedCalls(),
4747
fuzzer.ChoiceTable())
48-
p.SecContext = ""
49-
if fuzzer.Config.AttachSecContext {
50-
p.SecContext = "user_u:user_r:user_t:s0"
51-
if fuzzer.Config.Sandbox == "android" {
52-
if fuzzer.Config.SandboxArg == 0 {
53-
p.SecContext = "u:r:untrusted_app:s0:c512,c768"
54-
} else {
55-
p.SecContext = ""
56-
}
57-
}
58-
}
48+
p.SecContext = fuzzer.SecContextGen.getSecLabel()
5949
return &queue.Request{
6050
Prog: p,
6151
ExecOpts: setFlags(flatrpc.ExecFlagCollectSignal),

pkg/fuzzer/seccontextgen.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2025 syzkaller project authors. All rights reserved.
2+
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3+
4+
package fuzzer
5+
6+
type SecContextGenerator struct {
7+
Sandbox string
8+
SandboxArg int64
9+
AttachSecLabels bool
10+
}
11+
12+
func (secContextGenerator *SecContextGenerator) getSecLabel() string {
13+
var secContext string = ""
14+
if secContextGenerator.AttachSecLabels {
15+
secContext = "user_u:user_r:user_t:s0"
16+
if secContextGenerator.Sandbox == "android" {
17+
if secContextGenerator.SandboxArg == 0 {
18+
secContext = "u:r:untrusted_app:s0:c512,c768"
19+
} else {
20+
secContext = ""
21+
}
22+
}
23+
}
24+
return secContext
25+
}

pkg/kfuzztest-manager/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func NewKFuzzTestManager(ctx context.Context, cfg Config) (*kFuzzTestManager, er
114114
// Don't filter anything.
115115
return true
116116
},
117-
}, rnd, target)
117+
}, rnd, target, nil)
118118

119119
// TODO: Sufficient for startup, but not ideal that we are passing a
120120
// manager config here. Would require changes to pkg/fuzzer if we wanted to

pkg/manager/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ func (kc *kernelContext) setupFuzzer(features flatrpc.Feature, syscalls map[*pro
571571
}
572572
log.Logf(level, msg, args...)
573573
},
574-
}, rnd, kc.cfg.Target)
574+
}, rnd, kc.cfg.Target, nil)
575575

576576
if kc.http != nil {
577577
kc.http.Fuzzer.Store(fuzzerObj)

syz-manager/manager.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,8 +1176,6 @@ func (mgr *Manager) MachineChecked(features flatrpc.Feature,
11761176
NoMutateCalls: mgr.cfg.NoMutateCalls,
11771177
FetchRawCover: mgr.cfg.RawCover,
11781178
AttachSecContext: mgr.cfg.Experimental.AttachSecContexts,
1179-
Sandbox: mgr.cfg.Sandbox,
1180-
SandboxArg: mgr.cfg.SandboxArg,
11811179
Logf: func(level int, msg string, args ...interface{}) {
11821180
if level != 0 {
11831181
return
@@ -1190,7 +1188,7 @@ func (mgr *Manager) MachineChecked(features flatrpc.Feature,
11901188
return !mgr.saturatedCalls[call]
11911189
},
11921190
ModeKFuzzTest: mgr.cfg.Experimental.EnableKFuzzTest,
1193-
}, rnd, mgr.target)
1191+
}, rnd, mgr.target, &fuzzer.SecContextGenerator{ Sandbox: mgr.cfg.Sandbox, SandboxArg: mgr.cfg.SandboxArg, AttachSecLabels: mgr.cfg.Experimental.AttachSecContexts })
11941192
fuzzerObj.AddCandidates(candidates)
11951193
mgr.fuzzer.Store(fuzzerObj)
11961194
mgr.http.Fuzzer.Store(fuzzerObj)

0 commit comments

Comments
 (0)