This is an archive of the discontinued LLVM Phabricator instance.

[BPF] Check jump and memory offsets to avoid truncation
ClosedPublic

Authored by eddyz87 on Aug 21 2023, 7:59 AM.

Details

Summary

The following assembly code should issue two errors specifying that
both jump and load offsets are out of range:

if r1 > r2 goto +100500
r1 = *(u64 *)(r1 - 100500)

This commit updates BPFAsmParser to check that:

  • offset specified for jump is either identifier (label) or a 16-bit signed constant;
  • offset specified for memory operations is a signed 16-bit constant.

(Which matches expectations in the BPFELFObjectWriter and
BPFMCCodeEmitter).

Diff Detail

Event Timeline

eddyz87 created this revision.Aug 21 2023, 7:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 21 2023, 7:59 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
eddyz87 requested review of this revision.Aug 21 2023, 7:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 21 2023, 7:59 AM

Hi Yonghong,

Could you please take a look?
This is a follow-up for this kernel mailing list thread. I diverged from the suggested implementation and used approach described in this thread. I used the following reasoning:

  • range checks are needed for offsets and immediates, which are used in many instructions;
  • if check is done in BPFMCCodeEmitter::encodeInstruction correct per-instruction operand indexes are needed to extract opreand from MCInst => big switch which duplicates information already encoded in the BPFInstrInfo.td;
  • on the other hand, adjusting operand declarations in BPFInstrInfo.td is much more concise.

What do you think?

yonghong-song accepted this revision.Aug 21 2023, 11:07 PM

Thanks. This is indeed much better to detect during insn matching stage.

This revision is now accepted and ready to land.Aug 21 2023, 11:07 PM
eddyz87 updated this revision to Diff 557267.Sep 23 2023, 6:25 AM

Rebase, will merge this in after CI run.

This revision was automatically updated to reflect the committed changes.