Discrepancy in netlink_sock offsets between pahole and Kernel Module (offsetof)

2026-05-01

Stack Overflow: View Question

Tags: c, debugging, kernel, reverse-engineering

Score: 1 | Views: 72

The asker is doing kernel security research and needs to know the exact byte offset of the portid field within struct netlink_sock. They're using two different methods to determine this offset and getting conflicting results: pahole (a DWARF debug info tool) reports offset 792, while offsetof() in a loaded kernel module reports a different value.

This is a deceptively tricky problem, and the discrepancy almost certainly comes from one of these sources:

The approach to resolving this:

A subtle gotcha: if the struct definition includes a flexible array member or uses __randomize_layout annotation, the compiler is free to reorder fields. The DWARF info will reflect the actual compiled layout, but the source-level field order becomes meaningless.

The challenge: Kernel struct layout is not stable across builds, configs, or compiler versions, making hardcoded offsets unreliable — and features like RANDSTRUCT exist specifically to enforce this.

All newsletters