Core: Carry the packet-pointer kind on the type descriptor

The previous commit recognised a packet-pointer field only where it was
read directly in a binary operation, so d = ctx.data made d an ordinary
integer again and comparisons such as data + 34 > data_end were still
ranked u32 and emitted as 32-bit compares, which the verifier rejects.

PktPtrTy is a 64-bit unsigned IntTy tagged with the verifier's kind
(pkt, pkt_meta, pkt_end). It originates at the field read and travels
as the descriptor does: into a local's slot, through pointer +/- offset
(which yields the same kind), and into comparisons, which compare at
64 bits unsigned whenever either side is a packet pointer. It is handled
before the usual arithmetic conversions, which would re-rank it.

Two rules the verifier enforces become compile errors: a packet-end
pointer takes no offset, and convert() refuses to narrow a packet
pointer below 64 bits.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-09-25 13:03:30 +05:30
parent 6a859ac0cd
commit b9b582a7d8
6 changed files with 127 additions and 46 deletions

View File

@ -13,34 +13,50 @@ users never need the cast:
packet range whose offset stays within MAX_PACKET_OFF (0xffff);
- pointer - pointer (e.g. data_end - data) is an ordinary 64-bit length.
Only direct field reads (`ctx.data`) are recognised so far; a local copied
from one (`d = ctx.data`) is an ordinary integer again. Comparisons between
packet pointers are the next site of the same rule.
The field read gives its value a PktPtrTy descriptor (a 64-bit unsigned
IntTy tagged with the verifier's kind), and the rules key on that descriptor,
so they follow the pointer through locals (`d = ctx.data`), through
`d + 14`, and into comparisons (`ctx.data + 34 > ctx.data_end` is a 64-bit
unsigned compare). packet-end pointers take no offset at all, and nothing
narrows a packet pointer below 64 bits.
"""
import ast
# Context struct -> fields the verifier treats as packet pointers. Taken from
# the verifier's is_valid_access callbacks, restricted to the fields that
# vmlinux declares as 32-bit integers.
from pythonbpf.type_deducer import PktPtrTy
# Context struct -> {field: packet kind}. Taken from the verifier's
# is_valid_access callbacks, restricted to the fields vmlinux declares as
# 32-bit integers.
PACKET_POINTER_FIELDS = {
"struct_xdp_md": {"data", "data_end", "data_meta"},
"struct___sk_buff": {"data", "data_end", "data_meta"},
"struct_xdp_md": {"data": "pkt", "data_meta": "pkt_meta", "data_end": "pkt_end"},
"struct___sk_buff": {
"data": "pkt",
"data_meta": "pkt_meta",
"data_end": "pkt_end",
},
}
MAX_PACKET_OFF = 0xFFFF
def is_packet_pointer(expr, local_sym_tab) -> bool:
"""True for `ctx.<field>` where ctx is a context parameter of one of the
structs above and the field is one of its packet-pointer fields."""
def packet_kind(expr, local_sym_tab) -> "str | None":
"""The packet kind of `ctx.<field>` when ctx is a context parameter of one
of the structs above and the field is one of its packet-pointer fields;
None otherwise. This is the only place a PktPtrTy originates: from here it
travels as a descriptor, through locals, binop results and comparisons."""
if not (isinstance(expr, ast.Attribute) and isinstance(expr.value, ast.Name)):
return False
return None
sym = (local_sym_tab or {}).get(expr.value.id)
if sym is None:
return False
return None
meta = sym.metadata
struct_name = meta if isinstance(meta, str) else getattr(meta, "__name__", None)
if not isinstance(struct_name, str):
return False
return expr.attr in PACKET_POINTER_FIELDS.get(struct_name, set())
return None
return PACKET_POINTER_FIELDS.get(struct_name, {}).get(expr.attr)
def packet_type(expr, local_sym_tab) -> "PktPtrTy | None":
kind = packet_kind(expr, local_sym_tab)
return PktPtrTy(kind) if kind is not None else None