2017-01-25 15:18:05 +00:00
|
|
|
// Copyright 2017 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 prog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/syzkaller/sys"
|
|
|
|
)
|
|
|
|
|
2017-04-27 18:31:00 +00:00
|
|
|
type CsumKind int
|
2017-01-25 15:18:05 +00:00
|
|
|
|
2017-04-27 18:31:00 +00:00
|
|
|
const (
|
|
|
|
CsumInet CsumKind = iota
|
|
|
|
)
|
2017-01-25 15:18:05 +00:00
|
|
|
|
2017-04-27 18:31:00 +00:00
|
|
|
type CsumChunkKind int
|
2017-01-25 15:18:05 +00:00
|
|
|
|
2017-04-27 18:31:00 +00:00
|
|
|
const (
|
|
|
|
CsumChunkArg CsumChunkKind = iota
|
|
|
|
CsumChunkConst
|
|
|
|
)
|
2017-01-25 15:18:05 +00:00
|
|
|
|
2017-04-27 18:31:00 +00:00
|
|
|
type CsumInfo struct {
|
|
|
|
Kind CsumKind
|
|
|
|
Chunks []CsumChunk
|
2017-01-25 15:18:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 18:31:00 +00:00
|
|
|
type CsumChunk struct {
|
|
|
|
Kind CsumChunkKind
|
2017-07-11 14:49:08 +00:00
|
|
|
Arg Arg // for CsumChunkArg
|
2017-04-27 18:31:00 +00:00
|
|
|
Value uintptr // for CsumChunkConst
|
|
|
|
Size uintptr // for CsumChunkConst
|
2017-01-25 15:18:05 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
func getFieldByName(arg Arg, name string) Arg {
|
|
|
|
for _, field := range arg.(*GroupArg).Inner {
|
|
|
|
if field.Type().FieldName() == name {
|
2017-01-30 18:17:38 +00:00
|
|
|
return field
|
2017-01-25 15:18:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
panic(fmt.Sprintf("failed to find %v field in %v", name, arg.Type().Name()))
|
2017-01-30 18:17:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
func extractHeaderParamsIPv4(arg Arg) (Arg, Arg) {
|
2017-01-30 18:17:38 +00:00
|
|
|
srcAddr := getFieldByName(arg, "src_ip")
|
|
|
|
if srcAddr.Size() != 4 {
|
2017-07-11 14:49:08 +00:00
|
|
|
panic(fmt.Sprintf("src_ip field in %v must be 4 bytes", arg.Type().Name()))
|
2017-01-30 18:17:38 +00:00
|
|
|
}
|
|
|
|
dstAddr := getFieldByName(arg, "dst_ip")
|
|
|
|
if dstAddr.Size() != 4 {
|
2017-07-11 14:49:08 +00:00
|
|
|
panic(fmt.Sprintf("dst_ip field in %v must be 4 bytes", arg.Type().Name()))
|
2017-01-30 18:17:38 +00:00
|
|
|
}
|
2017-02-02 14:58:15 +00:00
|
|
|
return srcAddr, dstAddr
|
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
func extractHeaderParamsIPv6(arg Arg) (Arg, Arg) {
|
2017-02-02 14:58:15 +00:00
|
|
|
srcAddr := getFieldByName(arg, "src_ip")
|
|
|
|
if srcAddr.Size() != 16 {
|
2017-07-11 14:49:08 +00:00
|
|
|
panic(fmt.Sprintf("src_ip field in %v must be 4 bytes", arg.Type().Name()))
|
2017-02-02 14:58:15 +00:00
|
|
|
}
|
|
|
|
dstAddr := getFieldByName(arg, "dst_ip")
|
|
|
|
if dstAddr.Size() != 16 {
|
2017-07-11 14:49:08 +00:00
|
|
|
panic(fmt.Sprintf("dst_ip field in %v must be 4 bytes", arg.Type().Name()))
|
2017-01-30 18:17:38 +00:00
|
|
|
}
|
2017-02-02 14:58:15 +00:00
|
|
|
return srcAddr, dstAddr
|
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
func composePseudoCsumIPv4(tcpPacket, srcAddr, dstAddr Arg, protocol uint8, pid int) CsumInfo {
|
2017-04-27 18:31:00 +00:00
|
|
|
info := CsumInfo{Kind: CsumInet}
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkArg, srcAddr, 0, 0})
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkArg, dstAddr, 0, 0})
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkConst, nil, uintptr(swap16(uint16(protocol))), 2})
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkConst, nil, uintptr(swap16(uint16(tcpPacket.Size()))), 2})
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkArg, tcpPacket, 0, 0})
|
|
|
|
return info
|
2017-01-30 18:17:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
func composePseudoCsumIPv6(tcpPacket, srcAddr, dstAddr Arg, protocol uint8, pid int) CsumInfo {
|
2017-04-27 18:31:00 +00:00
|
|
|
info := CsumInfo{Kind: CsumInet}
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkArg, srcAddr, 0, 0})
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkArg, dstAddr, 0, 0})
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkConst, nil, uintptr(swap32(uint32(tcpPacket.Size()))), 4})
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkConst, nil, uintptr(swap32(uint32(protocol))), 4})
|
|
|
|
info.Chunks = append(info.Chunks, CsumChunk{CsumChunkArg, tcpPacket, 0, 0})
|
|
|
|
return info
|
2017-01-30 18:17:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
func findCsummedArg(arg Arg, typ *sys.CsumType, parentsMap map[Arg]Arg) Arg {
|
2017-02-08 14:49:03 +00:00
|
|
|
if typ.Buf == "parent" {
|
|
|
|
if csummedArg, ok := parentsMap[arg]; ok {
|
|
|
|
return csummedArg
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("parent for %v is not in parents map", typ.Name()))
|
|
|
|
} else {
|
|
|
|
for parent := parentsMap[arg]; parent != nil; parent = parentsMap[parent] {
|
2017-07-11 14:49:08 +00:00
|
|
|
if typ.Buf == parent.Type().Name() {
|
2017-02-08 14:49:03 +00:00
|
|
|
return parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("csum field '%v' references non existent field '%v'", typ.FieldName(), typ.Buf))
|
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
func calcChecksumsCall(c *Call, pid int) map[Arg]CsumInfo {
|
|
|
|
var inetCsumFields []Arg
|
|
|
|
var pseudoCsumFields []Arg
|
2017-02-06 18:27:17 +00:00
|
|
|
|
2017-02-08 14:49:03 +00:00
|
|
|
// Find all csum fields.
|
2017-07-11 14:49:08 +00:00
|
|
|
foreachArgArray(&c.Args, nil, func(arg, base Arg, _ *[]Arg) {
|
|
|
|
if typ, ok := arg.Type().(*sys.CsumType); ok {
|
2017-02-08 14:49:03 +00:00
|
|
|
switch typ.Kind {
|
|
|
|
case sys.CsumInet:
|
|
|
|
inetCsumFields = append(inetCsumFields, arg)
|
|
|
|
case sys.CsumPseudo:
|
|
|
|
pseudoCsumFields = append(pseudoCsumFields, arg)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown csum kind %v\n", typ.Kind))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Return if no csum fields found.
|
|
|
|
if len(inetCsumFields) == 0 && len(pseudoCsumFields) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build map of each field to its parent struct.
|
2017-07-11 14:49:08 +00:00
|
|
|
parentsMap := make(map[Arg]Arg)
|
|
|
|
foreachArgArray(&c.Args, nil, func(arg, base Arg, _ *[]Arg) {
|
|
|
|
if _, ok := arg.Type().(*sys.StructType); ok {
|
|
|
|
for _, field := range arg.(*GroupArg).Inner {
|
|
|
|
parentsMap[InnerArg(field)] = arg
|
2017-02-06 18:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
csumMap := make(map[Arg]CsumInfo)
|
2017-02-08 14:49:03 +00:00
|
|
|
|
2017-04-27 18:31:00 +00:00
|
|
|
// Calculate generic inet checksums.
|
2017-02-08 14:49:03 +00:00
|
|
|
for _, arg := range inetCsumFields {
|
2017-07-11 14:49:08 +00:00
|
|
|
typ, _ := arg.Type().(*sys.CsumType)
|
2017-02-08 14:49:03 +00:00
|
|
|
csummedArg := findCsummedArg(arg, typ, parentsMap)
|
2017-04-27 18:31:00 +00:00
|
|
|
chunk := CsumChunk{CsumChunkArg, csummedArg, 0, 0}
|
|
|
|
info := CsumInfo{Kind: CsumInet, Chunks: make([]CsumChunk, 0)}
|
|
|
|
info.Chunks = append(info.Chunks, chunk)
|
|
|
|
csumMap[arg] = info
|
2017-02-08 14:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No need to continue if there are no pseudo csum fields.
|
|
|
|
if len(pseudoCsumFields) == 0 {
|
|
|
|
return csumMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract ipv4 or ipv6 source and destination addresses.
|
|
|
|
ipv4HeaderParsed := false
|
|
|
|
ipv6HeaderParsed := false
|
2017-07-11 14:49:08 +00:00
|
|
|
var ipSrcAddr Arg
|
|
|
|
var ipDstAddr Arg
|
|
|
|
foreachArgArray(&c.Args, nil, func(arg, base Arg, _ *[]Arg) {
|
2017-02-02 18:19:32 +00:00
|
|
|
// syz_csum_* structs are used in tests
|
2017-07-11 14:49:08 +00:00
|
|
|
switch arg.Type().Name() {
|
2017-02-02 18:19:32 +00:00
|
|
|
case "ipv4_header", "syz_csum_ipv4_header":
|
2017-02-02 14:58:15 +00:00
|
|
|
ipSrcAddr, ipDstAddr = extractHeaderParamsIPv4(arg)
|
2017-01-30 18:17:38 +00:00
|
|
|
ipv4HeaderParsed = true
|
2017-02-02 18:19:32 +00:00
|
|
|
case "ipv6_packet", "syz_csum_ipv6_header":
|
2017-02-02 14:58:15 +00:00
|
|
|
ipSrcAddr, ipDstAddr = extractHeaderParamsIPv6(arg)
|
|
|
|
ipv6HeaderParsed = true
|
2017-01-25 15:18:05 +00:00
|
|
|
}
|
|
|
|
})
|
2017-02-08 14:49:03 +00:00
|
|
|
if !ipv4HeaderParsed && !ipv6HeaderParsed {
|
|
|
|
panic("no ipv4 nor ipv6 header found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate pseudo checksums.
|
|
|
|
for _, arg := range pseudoCsumFields {
|
2017-07-11 14:49:08 +00:00
|
|
|
typ, _ := arg.Type().(*sys.CsumType)
|
2017-02-08 14:49:03 +00:00
|
|
|
csummedArg := findCsummedArg(arg, typ, parentsMap)
|
|
|
|
protocol := uint8(typ.Protocol)
|
2017-04-27 18:31:00 +00:00
|
|
|
var info CsumInfo
|
2017-02-08 14:49:03 +00:00
|
|
|
if ipv4HeaderParsed {
|
2017-04-27 18:31:00 +00:00
|
|
|
info = composePseudoCsumIPv4(csummedArg, ipSrcAddr, ipDstAddr, protocol, pid)
|
2017-02-08 14:49:03 +00:00
|
|
|
} else {
|
2017-04-27 18:31:00 +00:00
|
|
|
info = composePseudoCsumIPv6(csummedArg, ipSrcAddr, ipDstAddr, protocol, pid)
|
2017-02-08 14:49:03 +00:00
|
|
|
}
|
2017-04-27 18:31:00 +00:00
|
|
|
csumMap[arg] = info
|
2017-02-08 14:49:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 18:17:38 +00:00
|
|
|
return csumMap
|
2017-01-25 15:18:05 +00:00
|
|
|
}
|