Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistences in arithmetic shift implementation of interpreter #411

Open
pcy190 opened this issue Mar 13, 2024 · 0 comments
Open

Inconsistences in arithmetic shift implementation of interpreter #411

pcy190 opened this issue Mar 13, 2024 · 0 comments

Comments

@pcy190
Copy link

pcy190 commented Mar 13, 2024

In the upstream kernel eBPF specification, the offset of the arithmetic shift should be mask 0

In the interpreter of ubpf, the mask is missing for EBPF_OP_ARSH64 and EBPF_OP_ARSH

ubpf/vm/ubpf_vm.c

Lines 572 to 577 in a6082a2

case EBPF_OP_ARSH64_IMM:
reg[inst.dst] = (int64_t)reg[inst.dst] >> inst.imm;
break;
case EBPF_OP_ARSH64_REG:
reg[inst.dst] = (int64_t)reg[inst.dst] >> reg[inst.src];
break;

ubpf/vm/ubpf_vm.c

Lines 475 to 482 in a6082a2

case EBPF_OP_ARSH_IMM:
reg[inst.dst] = (int32_t)reg[inst.dst] >> inst.imm;
reg[inst.dst] &= UINT32_MAX;
break;
case EBPF_OP_ARSH_REG:
reg[inst.dst] = (int32_t)reg[inst.dst] >> u32(reg[inst.src]);
reg[inst.dst] &= UINT32_MAX;
break;

We should mask the src/imm offset before performing shift operation.

The following PoC program implies the differences between the specification and the implementation, while the program executed in the ubpf will trigger undefined behavior since the shift offset overflows.

mov64 r8, 0x05454500
alsh64 r8, r8
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant