| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package cbpfc |
| 22 |
|
| 23 |
import ( |
| 24 |
"fmt" |
| 25 |
"sort" |
| 26 |
|
| 27 |
"github.com/pkg/errors" |
| 28 |
"golang.org/x/net/bpf" |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
const maxPacketOffset = 0xFFFF |
| 34 |
|
| 35 |
|
| 36 |
var condToInverse = map[bpf.JumpTest]bpf.JumpTest{ |
| 37 |
bpf.JumpEqual: bpf.JumpNotEqual, |
| 38 |
bpf.JumpNotEqual: bpf.JumpEqual, |
| 39 |
bpf.JumpGreaterThan: bpf.JumpLessOrEqual, |
| 40 |
bpf.JumpLessThan: bpf.JumpGreaterOrEqual, |
| 41 |
bpf.JumpGreaterOrEqual: bpf.JumpLessThan, |
| 42 |
bpf.JumpLessOrEqual: bpf.JumpGreaterThan, |
| 43 |
bpf.JumpBitsSet: bpf.JumpBitsNotSet, |
| 44 |
bpf.JumpBitsNotSet: bpf.JumpBitsSet, |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
type pos uint |
| 49 |
|
| 50 |
|
| 51 |
type skip uint |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
type instruction struct { |
| 56 |
bpf.Instruction |
| 57 |
id pos |
| 58 |
} |
| 59 |
|
| 60 |
func (i instruction) String() string { |
| 61 |
return fmt.Sprintf("%d: %v", i.id, i.Instruction) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
type block struct { |
| 73 |
|
| 74 |
insns []instruction |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
jumps map[pos]*block |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
id pos |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func newBlock(insns []instruction) *block { |
| 87 |
return &block{ |
| 88 |
insns: insns, |
| 89 |
jumps: make(map[pos]*block), |
| 90 |
id: insns[0].id, |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func (b *block) Label() string { |
| 95 |
return fmt.Sprintf("block_%d", b.id) |
| 96 |
} |
| 97 |
|
| 98 |
func (b *block) skipToPos(s skip) pos { |
| 99 |
return b.last().id + 1 + pos(s) |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
func (b *block) skipToBlock(s skip) *block { |
| 104 |
return b.jumps[b.skipToPos(s)] |
| 105 |
} |
| 106 |
|
| 107 |
func (b *block) last() instruction { |
| 108 |
return b.insns[len(b.insns)-1] |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
type packetGuard interface { |
| 114 |
bpf.Instruction |
| 115 |
|
| 116 |
|
| 117 |
extend(o packetGuard) packetGuard |
| 118 |
|
| 119 |
|
| 120 |
restrict(o packetGuard) packetGuard |
| 121 |
|
| 122 |
|
| 123 |
adjustInsns(insns []instruction) |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
type packetGuardAbsolute struct { |
| 129 |
|
| 130 |
end int32 |
| 131 |
} |
| 132 |
|
| 133 |
func newPacketGuardAbsolute(off uint32, size int) packetGuardAbsolute { |
| 134 |
if off > maxPacketOffset { |
| 135 |
panic("can't create absolute packet guard for offset") |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
return packetGuardAbsolute{int32(off) + int32(size)} |
| 140 |
} |
| 141 |
|
| 142 |
func (a packetGuardAbsolute) extend(o packetGuard) packetGuard { |
| 143 |
n := a |
| 144 |
if b := o.(packetGuardAbsolute); b.end > a.end { |
| 145 |
n.end = b.end |
| 146 |
} |
| 147 |
return n |
| 148 |
} |
| 149 |
|
| 150 |
func (a packetGuardAbsolute) restrict(o packetGuard) packetGuard { |
| 151 |
n := a |
| 152 |
if b := o.(packetGuardAbsolute); b.end < a.end { |
| 153 |
n.end = b.end |
| 154 |
} |
| 155 |
return n |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func (a packetGuardAbsolute) adjustInsns(insns []instruction) {} |
| 160 |
|
| 161 |
|
| 162 |
func (p packetGuardAbsolute) Assemble() (bpf.RawInstruction, error) { |
| 163 |
return bpf.RawInstruction{}, errors.Errorf("unsupported") |
| 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 |
type packetGuardIndirect struct { |
| 191 |
|
| 192 |
start int32 |
| 193 |
|
| 194 |
|
| 195 |
end int64 |
| 196 |
|
| 197 |
|
| 198 |
packetStartMaxOffset uint16 |
| 199 |
} |
| 200 |
|
| 201 |
func newPacketGuardIndirect(off uint32, size int, opts compileOpts) packetGuardIndirect { |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
return packetGuardIndirect{ |
| 207 |
start: int32(off), |
| 208 |
end: int64(int32(off)) + int64(size), |
| 209 |
|
| 210 |
packetStartMaxOffset: opts.packetStartMaxOffset, |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
func (a packetGuardIndirect) extend(o packetGuard) packetGuard { |
| 215 |
b := o.(packetGuardIndirect) |
| 216 |
|
| 217 |
|
| 218 |
if a == (packetGuardIndirect{}) { |
| 219 |
return b |
| 220 |
} |
| 221 |
if b == (packetGuardIndirect{}) { |
| 222 |
return a |
| 223 |
} |
| 224 |
|
| 225 |
n := a |
| 226 |
|
| 227 |
if b.start < a.start { |
| 228 |
n.start = b.start |
| 229 |
} |
| 230 |
if b.end > a.end { |
| 231 |
n.end = b.end |
| 232 |
} |
| 233 |
|
| 234 |
return n |
| 235 |
} |
| 236 |
|
| 237 |
func (a packetGuardIndirect) restrict(o packetGuard) packetGuard { |
| 238 |
b := o.(packetGuardIndirect) |
| 239 |
|
| 240 |
|
| 241 |
if a == (packetGuardIndirect{}) || b == (packetGuardIndirect{}) { |
| 242 |
return packetGuardIndirect{} |
| 243 |
} |
| 244 |
|
| 245 |
n := a |
| 246 |
|
| 247 |
if b.start > a.start { |
| 248 |
n.start = b.start |
| 249 |
} |
| 250 |
if b.end < a.end { |
| 251 |
n.end = b.end |
| 252 |
} |
| 253 |
|
| 254 |
return n |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
func (p packetGuardIndirect) maxStartOffset() int32 { |
| 261 |
m := maxPacketOffset - int64(p.packetStartMaxOffset) - (p.end - int64(p.start)) |
| 262 |
|
| 263 |
|
| 264 |
if m < 0 { |
| 265 |
return 0 |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
return int32(m) + 1 |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
func (p packetGuardIndirect) length() int32 { |
| 276 |
|
| 277 |
|
| 278 |
return int32(p.end - int64(p.start)) |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
func (p packetGuardIndirect) adjustInsns(insns []instruction) { |
| 284 |
for i := range insns { |
| 285 |
switch insn := insns[i].Instruction.(type) { |
| 286 |
case bpf.LoadIndirect: |
| 287 |
insns[i].Instruction = bpf.LoadIndirect{ |
| 288 |
Off: uint32(int32(insn.Off) - p.start), |
| 289 |
Size: insn.Size, |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
func (p packetGuardIndirect) Assemble() (bpf.RawInstruction, error) { |
| 297 |
return bpf.RawInstruction{}, errors.Errorf("unsupported") |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
type checkXNotZero struct { |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
func (c checkXNotZero) Assemble() (bpf.RawInstruction, error) { |
| 307 |
return bpf.RawInstruction{}, errors.Errorf("unsupported") |
| 308 |
} |
| 309 |
|
| 310 |
type compileOpts struct { |
| 311 |
|
| 312 |
packetStartMaxOffset uint16 |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
func compile(insns []bpf.Instruction, opts compileOpts) ([]*block, error) { |
| 320 |
err := validateInstructions(insns) |
| 321 |
if err != nil { |
| 322 |
return nil, err |
| 323 |
} |
| 324 |
|
| 325 |
instructions := toInstructions(insns) |
| 326 |
|
| 327 |
normalizeJumps(instructions) |
| 328 |
|
| 329 |
|
| 330 |
blocks, err := splitBlocks(instructions) |
| 331 |
if err != nil { |
| 332 |
return nil, errors.Wrapf(err, "unable to compute blocks") |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
err = initializeMemory(blocks) |
| 337 |
if err != nil { |
| 338 |
return nil, err |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
err = addDivideByZeroGuards(blocks) |
| 343 |
if err != nil { |
| 344 |
return nil, err |
| 345 |
} |
| 346 |
|
| 347 |
rewriteLargePacketOffsets(&blocks, opts) |
| 348 |
|
| 349 |
|
| 350 |
addAbsolutePacketGuards(blocks) |
| 351 |
addIndirectPacketGuards(blocks, opts) |
| 352 |
|
| 353 |
return blocks, nil |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
func validateInstructions(insns []bpf.Instruction) error { |
| 358 |
|
| 359 |
if len(insns) == 0 { |
| 360 |
return errors.New("can't compile 0 instructions") |
| 361 |
} |
| 362 |
|
| 363 |
for pc, insn := range insns { |
| 364 |
|
| 365 |
_, err := insn.Assemble() |
| 366 |
if err != nil { |
| 367 |
return errors.Errorf("can't assemble instruction %d: %v", pc, insn) |
| 368 |
} |
| 369 |
|
| 370 |
switch i := insn.(type) { |
| 371 |
case bpf.RawInstruction: |
| 372 |
return errors.Errorf("unsupported instruction %d: %v", pc, insn) |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
case bpf.LoadAbsolute: |
| 377 |
if int32(i.Off) < 0 { |
| 378 |
return errors.Errorf("LoadAbsolute negative offset %v", int32(i.Off)) |
| 379 |
} |
| 380 |
case bpf.LoadMemShift: |
| 381 |
if int32(i.Off) < 0 { |
| 382 |
return errors.Errorf("LoadMemShift negative offset %v", int32(i.Off)) |
| 383 |
} |
| 384 |
|
| 385 |
case bpf.LoadExtension: |
| 386 |
switch i.Num { |
| 387 |
case bpf.ExtLen: |
| 388 |
break |
| 389 |
default: |
| 390 |
return errors.Errorf("unsupported BPF extension %d: %v", pc, insn) |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
return nil |
| 396 |
} |
| 397 |
|
| 398 |
func toInstructions(insns []bpf.Instruction) []instruction { |
| 399 |
instructions := make([]instruction, len(insns)) |
| 400 |
|
| 401 |
for pc, insn := range insns { |
| 402 |
instructions[pc] = instruction{ |
| 403 |
Instruction: insn, |
| 404 |
id: pos(pc), |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
return instructions |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
func normalizeJumps(insns []instruction) { |
| 416 |
for pc := range insns { |
| 417 |
switch i := insns[pc].Instruction.(type) { |
| 418 |
case bpf.JumpIf: |
| 419 |
if !shouldInvert(i.SkipTrue, i.SkipFalse) { |
| 420 |
continue |
| 421 |
} |
| 422 |
|
| 423 |
insns[pc].Instruction = bpf.JumpIf{Cond: condToInverse[i.Cond], Val: i.Val, SkipTrue: i.SkipFalse, SkipFalse: i.SkipTrue} |
| 424 |
|
| 425 |
case bpf.JumpIfX: |
| 426 |
if !shouldInvert(i.SkipTrue, i.SkipFalse) { |
| 427 |
continue |
| 428 |
} |
| 429 |
|
| 430 |
insns[pc].Instruction = bpf.JumpIfX{Cond: condToInverse[i.Cond], SkipTrue: i.SkipFalse, SkipFalse: i.SkipTrue} |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
func shouldInvert(skipTrue, skipFalse uint8) bool { |
| 437 |
return skipTrue == 0 && skipFalse != 0 |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
func visitBlock(insns []instruction, target pos) (*block, []skip) { |
| 443 |
for pc, insn := range insns { |
| 444 |
|
| 445 |
var skips []skip |
| 446 |
|
| 447 |
switch i := insn.Instruction.(type) { |
| 448 |
case bpf.Jump: |
| 449 |
skips = []skip{skip(i.Skip)} |
| 450 |
case bpf.JumpIf: |
| 451 |
skips = []skip{skip(i.SkipTrue), skip(i.SkipFalse)} |
| 452 |
case bpf.JumpIfX: |
| 453 |
skips = []skip{skip(i.SkipTrue), skip(i.SkipFalse)} |
| 454 |
|
| 455 |
case bpf.RetA, bpf.RetConstant: |
| 456 |
|
| 457 |
|
| 458 |
default: |
| 459 |
|
| 460 |
continue |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
return newBlock(insns[:pc+1]), skips |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
return newBlock(insns), []skip{0} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
func splitBlocks(instructions []instruction) ([]*block, error) { |
| 477 |
|
| 478 |
blocks := []*block{} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
targets := map[pos][]*block{ |
| 483 |
0: nil, |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
for len(targets) > 0 { |
| 488 |
sortedTargets := sortTargets(targets) |
| 489 |
|
| 490 |
|
| 491 |
target := sortedTargets[0] |
| 492 |
|
| 493 |
end := len(instructions) |
| 494 |
|
| 495 |
if len(sortedTargets) > 1 { |
| 496 |
end = int(sortedTargets[1]) |
| 497 |
} |
| 498 |
|
| 499 |
next, nextSkips := visitBlock(instructions[target:end], target) |
| 500 |
|
| 501 |
|
| 502 |
for _, s := range nextSkips { |
| 503 |
|
| 504 |
t := next.skipToPos(s) |
| 505 |
|
| 506 |
if t >= pos(len(instructions)) { |
| 507 |
return nil, errors.Errorf("instruction %v flows past last instruction", next.last()) |
| 508 |
} |
| 509 |
|
| 510 |
targets[t] = append(targets[t], next) |
| 511 |
} |
| 512 |
|
| 513 |
jmpBlocks := targets[target] |
| 514 |
|
| 515 |
|
| 516 |
for _, jmpBlock := range jmpBlocks { |
| 517 |
jmpBlock.jumps[target] = next |
| 518 |
} |
| 519 |
|
| 520 |
blocks = append(blocks, next) |
| 521 |
|
| 522 |
|
| 523 |
delete(targets, target) |
| 524 |
} |
| 525 |
|
| 526 |
return blocks, nil |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
func sortTargets(targets map[pos][]*block) []pos { |
| 531 |
keys := make([]pos, len(targets)) |
| 532 |
|
| 533 |
i := 0 |
| 534 |
for k := range targets { |
| 535 |
keys[i] = k |
| 536 |
i++ |
| 537 |
} |
| 538 |
|
| 539 |
sort.Slice(keys, func(i, j int) bool { |
| 540 |
return keys[i] < keys[j] |
| 541 |
}) |
| 542 |
|
| 543 |
return keys |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
func addDivideByZeroGuards(blocks []*block) error { |
| 549 |
isDivision := func(op bpf.ALUOp) bool { |
| 550 |
return op == bpf.ALUOpDiv || op == bpf.ALUOpMod |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
xNotZero := make(map[*block]bool) |
| 556 |
|
| 557 |
for _, block := range blocks { |
| 558 |
notZero := xNotZero[block] |
| 559 |
|
| 560 |
|
| 561 |
newInsns := []instruction{} |
| 562 |
for _, insn := range block.insns { |
| 563 |
switch i := insn.Instruction.(type) { |
| 564 |
case bpf.ALUOpConstant: |
| 565 |
if isDivision(i.Op) && i.Val == 0 { |
| 566 |
return errors.Errorf("instruction %v divides by 0", insn) |
| 567 |
} |
| 568 |
case bpf.ALUOpX: |
| 569 |
if isDivision(i.Op) && !notZero { |
| 570 |
newInsns = append(newInsns, instruction{Instruction: checkXNotZero{}}) |
| 571 |
notZero = true |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
newInsns = append(newInsns, insn) |
| 576 |
|
| 577 |
|
| 578 |
if memWrites(insn.Instruction).regs[bpf.RegX] { |
| 579 |
notZero = false |
| 580 |
} |
| 581 |
} |
| 582 |
block.insns = newInsns |
| 583 |
|
| 584 |
|
| 585 |
for _, target := range block.jumps { |
| 586 |
targetNotZero, ok := xNotZero[target] |
| 587 |
if !ok { |
| 588 |
xNotZero[target] = notZero |
| 589 |
continue |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
xNotZero[target] = targetNotZero && notZero |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
return nil |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
func rewriteLargePacketOffsets(blocks *[]*block, opts compileOpts) { |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
blockRefs := make(map[*block]int) |
| 612 |
|
| 613 |
var newBlocks []*block |
| 614 |
|
| 615 |
for i, block := range *blocks { |
| 616 |
|
| 617 |
if i != 0 && blockRefs[block] == 0 { |
| 618 |
continue |
| 619 |
} |
| 620 |
newBlocks = append(newBlocks, block) |
| 621 |
|
| 622 |
for _, insn := range block.insns { |
| 623 |
var ( |
| 624 |
offset uint64 |
| 625 |
size uint64 |
| 626 |
) |
| 627 |
|
| 628 |
|
| 629 |
switch i := insn.Instruction.(type) { |
| 630 |
case bpf.LoadAbsolute: |
| 631 |
offset = uint64(i.Off) |
| 632 |
size = uint64(i.Size) |
| 633 |
case bpf.LoadMemShift: |
| 634 |
offset = uint64(i.Off) |
| 635 |
size = 1 |
| 636 |
default: |
| 637 |
continue |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
if offset+size+uint64(opts.packetStartMaxOffset) > maxPacketOffset { |
| 642 |
|
| 643 |
|
| 644 |
block.insns = []instruction{ |
| 645 |
{Instruction: bpf.RetConstant{Val: 0}}, |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
block.jumps = nil |
| 650 |
|
| 651 |
break |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
for _, target := range block.jumps { |
| 657 |
blockRefs[target]++ |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
*blocks = newBlocks |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
func addAbsolutePacketGuards(blocks []*block) { |
| 666 |
addPacketGuards(blocks, packetGuardOpts{ |
| 667 |
requiredGuard: func(insns []instruction) requiredGuard { |
| 668 |
var biggestGuard packetGuard = packetGuardAbsolute{} |
| 669 |
|
| 670 |
for _, insn := range insns { |
| 671 |
switch i := insn.Instruction.(type) { |
| 672 |
case bpf.LoadAbsolute: |
| 673 |
biggestGuard = biggestGuard.extend(newPacketGuardAbsolute(i.Off, i.Size)) |
| 674 |
case bpf.LoadMemShift: |
| 675 |
biggestGuard = biggestGuard.extend(newPacketGuardAbsolute(i.Off, 1)) |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
return requiredGuard{ |
| 681 |
guard: biggestGuard, |
| 682 |
alwaysValid: true, |
| 683 |
} |
| 684 |
}, |
| 685 |
|
| 686 |
zeroGuard: func() packetGuard { |
| 687 |
return packetGuardAbsolute{} |
| 688 |
}, |
| 689 |
}) |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
func addIndirectPacketGuards(blocks []*block, opts compileOpts) { |
| 694 |
addPacketGuards(blocks, packetGuardOpts{ |
| 695 |
requiredGuard: func(insns []instruction) requiredGuard { |
| 696 |
var ( |
| 697 |
insnCount int |
| 698 |
biggestGuard packetGuard = packetGuardIndirect{} |
| 699 |
) |
| 700 |
|
| 701 |
for _, insn := range insns { |
| 702 |
insnCount++ |
| 703 |
|
| 704 |
switch i := insn.Instruction.(type) { |
| 705 |
case bpf.LoadIndirect: |
| 706 |
biggestGuard = biggestGuard.extend(newPacketGuardIndirect(i.Off, i.Size, opts)) |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
if memWrites(insn.Instruction).regs[bpf.RegX] { |
| 711 |
return requiredGuard{ |
| 712 |
guard: biggestGuard, |
| 713 |
validForInsns: insnCount, |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
return requiredGuard{ |
| 719 |
guard: biggestGuard, |
| 720 |
alwaysValid: true, |
| 721 |
} |
| 722 |
}, |
| 723 |
|
| 724 |
zeroGuard: func() packetGuard { |
| 725 |
return packetGuardIndirect{} |
| 726 |
}, |
| 727 |
}) |
| 728 |
} |
| 729 |
|
| 730 |
type packetGuardOpts struct { |
| 731 |
|
| 732 |
requiredGuard func(insns []instruction) requiredGuard |
| 733 |
|
| 734 |
|
| 735 |
zeroGuard func() packetGuard |
| 736 |
} |
| 737 |
|
| 738 |
type requiredGuard struct { |
| 739 |
guard packetGuard |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
alwaysValid bool |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
validForInsns int |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
func addPacketGuards(blocks []*block, opts packetGuardOpts) { |
| 757 |
|
| 758 |
|
| 759 |
guards := make(map[*block][]packetGuard) |
| 760 |
|
| 761 |
for _, block := range blocks { |
| 762 |
blockGuard := addBlockGuards(block, leastGuard(opts.zeroGuard(), guards[block]), opts) |
| 763 |
|
| 764 |
for _, target := range block.jumps { |
| 765 |
guards[target] = append(guards[target], blockGuard) |
| 766 |
} |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
func addBlockGuards(block *block, currentGuard packetGuard, opts packetGuardOpts) packetGuard { |
| 772 |
insns := block.insns |
| 773 |
block.insns = nil |
| 774 |
|
| 775 |
for len(insns) != 0 { |
| 776 |
required := opts.requiredGuard(insns) |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
if newGuard := currentGuard.extend(required.guard); newGuard != currentGuard { |
| 781 |
currentGuard = newGuard |
| 782 |
|
| 783 |
|
| 784 |
if required.alwaysValid { |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
currentGuard = currentGuard.extend(guaranteedGuard(block.jumps, opts)) |
| 790 |
} |
| 791 |
|
| 792 |
block.insns = append(block.insns, instruction{Instruction: currentGuard}) |
| 793 |
} |
| 794 |
|
| 795 |
coveredInsns := insns |
| 796 |
if !required.alwaysValid { |
| 797 |
coveredInsns = insns[:required.validForInsns] |
| 798 |
} |
| 799 |
|
| 800 |
currentGuard.adjustInsns(coveredInsns) |
| 801 |
block.insns = append(block.insns, coveredInsns...) |
| 802 |
|
| 803 |
if required.alwaysValid { |
| 804 |
|
| 805 |
return currentGuard |
| 806 |
} else { |
| 807 |
|
| 808 |
currentGuard = opts.zeroGuard() |
| 809 |
insns = insns[required.validForInsns:] |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
return currentGuard |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
func guaranteedGuard(targets map[pos]*block, opts packetGuardOpts) packetGuard { |
| 832 |
|
| 833 |
|
| 834 |
return guaranteedGuardCached(targets, opts, make(map[*block]packetGuard)) |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
func guaranteedGuardCached(targets map[pos]*block, opts packetGuardOpts, cache map[*block]packetGuard) packetGuard { |
| 839 |
targetGuards := []packetGuard{} |
| 840 |
|
| 841 |
for _, target := range targets { |
| 842 |
|
| 843 |
if blockNeverMatches(target) { |
| 844 |
continue |
| 845 |
} |
| 846 |
if guard, ok := cache[target]; ok { |
| 847 |
targetGuards = append(targetGuards, guard) |
| 848 |
continue |
| 849 |
} |
| 850 |
|
| 851 |
required := opts.requiredGuard(target.insns) |
| 852 |
|
| 853 |
|
| 854 |
if !required.alwaysValid { |
| 855 |
targetGuards = append(targetGuards, required.guard) |
| 856 |
continue |
| 857 |
} |
| 858 |
|
| 859 |
guard := required.guard.extend(guaranteedGuardCached(target.jumps, opts, cache)) |
| 860 |
|
| 861 |
cache[target] = guard |
| 862 |
targetGuards = append(targetGuards, guard) |
| 863 |
} |
| 864 |
|
| 865 |
return leastGuard(opts.zeroGuard(), targetGuards) |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
func leastGuard(zero packetGuard, guards []packetGuard) packetGuard { |
| 871 |
least := zero |
| 872 |
|
| 873 |
for i, guard := range guards { |
| 874 |
if i == 0 { |
| 875 |
least = guard |
| 876 |
} else { |
| 877 |
least = least.restrict(guard) |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
return least |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
func blockNeverMatches(block *block) bool { |
| 886 |
for _, insn := range block.insns { |
| 887 |
switch i := insn.Instruction.(type) { |
| 888 |
case bpf.RetConstant: |
| 889 |
if i.Val == 0 { |
| 890 |
return true |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
return false |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
type memStatus struct { |
| 900 |
|
| 901 |
regs [2]bool |
| 902 |
scratch [16]bool |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
func (r memStatus) merge(other memStatus, policy func(this, other bool) bool) memStatus { |
| 907 |
newStatus := memStatus{} |
| 908 |
|
| 909 |
for i := range newStatus.regs { |
| 910 |
newStatus.regs[i] = policy(r.regs[i], other.regs[i]) |
| 911 |
} |
| 912 |
|
| 913 |
for i := range newStatus.scratch { |
| 914 |
newStatus.scratch[i] = policy(r.scratch[i], other.scratch[i]) |
| 915 |
} |
| 916 |
|
| 917 |
return newStatus |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
func (r memStatus) and(other memStatus) memStatus { |
| 922 |
return r.merge(other, func(this, other bool) bool { |
| 923 |
return this && other |
| 924 |
}) |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
func (r memStatus) or(other memStatus) memStatus { |
| 929 |
return r.merge(other, func(this, other bool) bool { |
| 930 |
return this || other |
| 931 |
}) |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
func initializeMemory(blocks []*block) error { |
| 936 |
|
| 937 |
statuses := make(map[*block]memStatus) |
| 938 |
|
| 939 |
|
| 940 |
uninitialized := memStatus{} |
| 941 |
|
| 942 |
for _, block := range blocks { |
| 943 |
status := statuses[block] |
| 944 |
|
| 945 |
for _, insn := range block.insns { |
| 946 |
insnUninitialized := memUninitializedReads(insn.Instruction, status) |
| 947 |
|
| 948 |
for scratch, uninit := range insnUninitialized.scratch { |
| 949 |
if uninit { |
| 950 |
return errors.Errorf("instruction %v reads potentially uninitialized scratch register M[%d]", insn, scratch) |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
uninitialized = uninitialized.or(insnUninitialized) |
| 955 |
status = status.or(memWrites(insn.Instruction)) |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
for _, target := range block.jumps { |
| 960 |
targetStatus, ok := statuses[target] |
| 961 |
if !ok { |
| 962 |
statuses[target] = status |
| 963 |
continue |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
statuses[target] = targetStatus.and(status) |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
initInsns := []instruction{} |
| 973 |
for reg, uninit := range uninitialized.regs { |
| 974 |
if !uninit { |
| 975 |
continue |
| 976 |
} |
| 977 |
|
| 978 |
initInsns = append(initInsns, instruction{ |
| 979 |
Instruction: bpf.LoadConstant{ |
| 980 |
Dst: bpf.Register(reg), |
| 981 |
Val: 0, |
| 982 |
}, |
| 983 |
}) |
| 984 |
} |
| 985 |
blocks[0].insns = append(initInsns, blocks[0].insns...) |
| 986 |
return nil |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
func memUninitializedReads(insn bpf.Instruction, initialized memStatus) memStatus { |
| 991 |
return memReads(insn).merge(initialized, func(read, init bool) bool { |
| 992 |
return read && !init |
| 993 |
}) |
| 994 |
} |
| 995 |
|
| 996 |
|
| 997 |
func memReads(insn bpf.Instruction) memStatus { |
| 998 |
read := memStatus{} |
| 999 |
|
| 1000 |
switch i := insn.(type) { |
| 1001 |
case bpf.ALUOpConstant: |
| 1002 |
read.regs[bpf.RegA] = true |
| 1003 |
case bpf.ALUOpX: |
| 1004 |
read.regs[bpf.RegA] = true |
| 1005 |
read.regs[bpf.RegX] = true |
| 1006 |
|
| 1007 |
case bpf.JumpIf: |
| 1008 |
read.regs[bpf.RegA] = true |
| 1009 |
case bpf.JumpIfX: |
| 1010 |
read.regs[bpf.RegA] = true |
| 1011 |
read.regs[bpf.RegX] = true |
| 1012 |
|
| 1013 |
case bpf.LoadIndirect: |
| 1014 |
read.regs[bpf.RegX] = true |
| 1015 |
case bpf.LoadScratch: |
| 1016 |
read.scratch[i.N] = true |
| 1017 |
|
| 1018 |
case bpf.NegateA: |
| 1019 |
read.regs[bpf.RegA] = true |
| 1020 |
|
| 1021 |
case bpf.RetA: |
| 1022 |
read.regs[bpf.RegA] = true |
| 1023 |
|
| 1024 |
case bpf.StoreScratch: |
| 1025 |
read.regs[i.Src] = true |
| 1026 |
|
| 1027 |
case bpf.TAX: |
| 1028 |
read.regs[bpf.RegA] = true |
| 1029 |
case bpf.TXA: |
| 1030 |
read.regs[bpf.RegX] = true |
| 1031 |
} |
| 1032 |
|
| 1033 |
return read |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
func memWrites(insn bpf.Instruction) memStatus { |
| 1038 |
write := memStatus{} |
| 1039 |
|
| 1040 |
switch i := insn.(type) { |
| 1041 |
case bpf.ALUOpConstant: |
| 1042 |
write.regs[bpf.RegA] = true |
| 1043 |
case bpf.ALUOpX: |
| 1044 |
write.regs[bpf.RegA] = true |
| 1045 |
|
| 1046 |
case bpf.LoadAbsolute: |
| 1047 |
write.regs[bpf.RegA] = true |
| 1048 |
case bpf.LoadConstant: |
| 1049 |
write.regs[i.Dst] = true |
| 1050 |
case bpf.LoadExtension: |
| 1051 |
write.regs[bpf.RegA] = true |
| 1052 |
case bpf.LoadIndirect: |
| 1053 |
write.regs[bpf.RegA] = true |
| 1054 |
case bpf.LoadMemShift: |
| 1055 |
write.regs[bpf.RegX] = true |
| 1056 |
case bpf.LoadScratch: |
| 1057 |
write.regs[i.Dst] = true |
| 1058 |
|
| 1059 |
case bpf.NegateA: |
| 1060 |
write.regs[bpf.RegA] = true |
| 1061 |
|
| 1062 |
case bpf.StoreScratch: |
| 1063 |
write.scratch[i.N] = true |
| 1064 |
|
| 1065 |
case bpf.TAX: |
| 1066 |
write.regs[bpf.RegX] = true |
| 1067 |
case bpf.TXA: |
| 1068 |
write.regs[bpf.RegA] = true |
| 1069 |
} |
| 1070 |
|
| 1071 |
return write |
| 1072 |
} |