Skip to content

Commit

Permalink
Merge pull request opencontainers#2841 from AdamKorcz/fuzz1
Browse files Browse the repository at this point in the history
tests: Move fuzzers upstream
  • Loading branch information
kolyshkin authored Mar 10, 2021
2 parents 097062a + 2ae5665 commit 249bca0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions libcontainer/configs/configs_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build gofuzz

package configs

func FuzzUnmarshalJSON(data []byte) int {
hooks := Hooks{}
_ = hooks.UnmarshalJSON(data)
return 1
}
15 changes: 15 additions & 0 deletions libcontainer/system/system_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build gofuzz

package system

import (
"strings"

"github.com/opencontainers/runc/libcontainer/user"
)

func FuzzUIDMap(data []byte) int {
uidmap, _ := user.ParseIDMap(strings.NewReader(string(data)))
_ = UIDMapInUserNS(uidmap)
return 1
}
42 changes: 42 additions & 0 deletions libcontainer/user/user_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// +build gofuzz

package user

import (
"io"
"strings"
)

func IsDivisbleBy(n int, divisibleby int) bool {
return (n % divisibleby) == 0
}

func FuzzUser(data []byte) int {
if len(data) == 0 {
return -1
}
if !IsDivisbleBy(len(data), 5) {
return -1
}

var divided [][]byte

chunkSize := len(data) / 5

for i := 0; i < len(data); i += chunkSize {
end := i + chunkSize

divided = append(divided, data[i:end])
}

_, _ = ParsePasswdFilter(strings.NewReader(string(divided[0])), nil)

var passwd, group io.Reader

group = strings.NewReader(string(divided[1]))
_, _ = GetAdditionalGroups([]string{string(divided[2])}, group)

passwd = strings.NewReader(string(divided[3]))
_, _ = GetExecUser(string(divided[4]), nil, passwd, group)
return 1
}
13 changes: 13 additions & 0 deletions tests/fuzzing/oss_fuzz_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# This file is only meant to be run by OSS-fuzz and will not work
# if run outside of it.
# The api, compile_go_fuzzer() is provided by the OSS-fuzz
# environment and is a high level helper function for a series
# of compilation and linking steps to build the fuzzers in the
# OSS-fuzz environment.
# More info about compile_go_fuzzer() can be found here:
# https://google.github.io/oss-fuzz/getting-started/new-project-guide/go-lang/#buildsh
compile_go_fuzzer ./libcontainer/system FuzzUIDMap id_map_fuzzer linux
compile_go_fuzzer ./libcontainer/user FuzzUser user_fuzzer
compile_go_fuzzer ./libcontainer/configs FuzzUnmarshalJSON configs_fuzzer

0 comments on commit 249bca0

Please sign in to comment.