跳到主要內容

eBPF 為什麼不能隨便讀記憶體?看懂 Linux Verifier 的安全檢查 / Why the Linux eBPF Verifier Refuses Unsafe Memory Access

daily-tech:2026-11-16

現象:一個指標,為什麼不能直接讀? / Symptom: Why can’t one pointer simply be read?

寫 eBPF 時,最容易踩到的坑不是語法,而是「我明明先算出位址,為什麼載入核心就被拒絕?」原因是 eBPF 程式會在核心內執行;Linux 不能等錯誤發生後才補救,因此載入前先由 verifier 檢查指標型別、數值範圍、對齊與控制流程。

When writing eBPF, the frustrating failure is often not syntax: “I calculated the address, so why did the kernel reject the program?” eBPF runs inside the kernel. Linux therefore checks pointer types, value ranges, alignment, and control flow before loading instead of repairing a fault afterward.

機制:verifier 追蹤的是狀態,不是猜測 / Mechanism: the verifier tracks states, not guesses

官方文件把暫存器分成 scalar 與多種 pointer。scalar 會保存最小、最大值與未知位元;指標則帶著來源與可存取範圍。以封包為例,先取出 data 與 data_end,再確認 data 加上標頭長度沒有越過 data_end,之後那個分支內的讀取才被視為安全。若中間把指標做成無法證明範圍的運算,狀態會退化成 scalar,後面的記憶體讀取就會失效。

The kernel documentation describes registers as scalars or several pointer types. A scalar carries bounds and unknown bits; a pointer carries its origin and an accessible range. For packet data, the program loads data and data_end, proves that the header fits before data_end, and only then reads it. If an operation destroys the verifier’s range knowledge, the pointer becomes a scalar and a later memory read is rejected.

eBPF 從程式載入、verifier 檢查到掛載執行點的安全流程圖 / eBPF safety pipeline from loading through verifier checks to runtime hooks
圖一|原創流程圖:verifier 是載入時的安全閘門,不是執行後的錯誤處理器。 / Figure 1 | Original diagram: the verifier is a load-time safety gate, not post-failure error handling.

影響:可觀測性換來額外約束 / Impact: observability comes with constraints

這種設計讓 eBPF 能接近核心熱路徑,卻也讓「看起來合理」不等於「可證明安全」。NULL 檢查會改變後續分支的型別;map value、socket reference 與封包指標也各有生命週期規則。工程上不要只盯著 verifier log 的最後一行,應回頭找哪個分支讓範圍或 reference 狀態遺失。

This design lets eBPF observe hot kernel paths, but “looks reasonable” is not the same as “provably safe.” A NULL check changes the type known in the following branch, while map values, socket references, and packet pointers have different lifetime rules. When a load fails, do not read only the last verifier line; find the branch where range or reference knowledge was lost.

可操作步驟:先縮小,再驗證 / Practical steps: narrow first, then verify

先把程式縮成一個最小讀取:明確檢查 data_end,再讀固定長度;接著一次只加入一個 helper、map lookup 或分支。用 bpftool 或 loader 保留完整 verifier log,並在與目標核心相同版本的測試環境跑過。這不是保證程式永遠安全,而是讓每個安全假設都能被核心的規則檢查。

Reduce the program to one minimal read: check data_end explicitly, then read a fixed length. Add one helper, map lookup, or branch at a time. Keep the complete verifier log with bpftool or your loader, and test on a kernel version matching the target. This does not promise universal safety; it makes each safety assumption visible to the kernel’s rules.

void *data = (void *)(long)skb->data;
void *end  = (void *)(long)skb->data_end;
if (data + sizeof(struct ethhdr) > end) return 0;
/* only after the proof: read the header */

來源 / Sources:Linux Kernel eBPF verifier;Linux BPF documentation。查核日:2026-11-16(Asia/Taipei)。

留言

這個網誌中的熱門文章

text-to-cad: 讓 AI 驅動的參數化 CAD 建模成為現實 / text-to-cad: Bringing AI-Driven Parametric CAD Modeling to Reality

一個讓 AI 代理能生成、預覽並迭代 3D CAD 模型的開源框架 / An open-source harness for AI agents to generate, preview, and iterate 3D CAD models. 🔎 工具速覽 / AT A GLANCE Category AI-Powered CAD Tooling / AI 驅動 CAD 工具 Pricing Free (Open Source) / 免費(開源) BestFor Robotics engineers and product designers using AI coding agents / 使用 AI 程式碼代理的機器人工程師與產品設計師 GitHub Stars ⭐ 93 🚀 引言 / Introduction text-to-cad 是一個強大的開源框架,旨在填補大型語言模型(LLM)與專業 3D 建模之間的鴻溝。它允許開發者利用 Claude Code 或 Codex 等 AI 代理,透過程式碼生成精確的參數化幾何模型,並提供即時預覽與迭代能力。 text-to-cad is a powerful open-source harness designed to bridge the gap between Large Language Models (LLMs) and professional 3D modeling. It enables developers to leverage AI agents like Claude Code or Codex to generate precise parametric geometry via code, providing real-time preview and iteration capabilities. 🛠️ 核心功能 / Key Features 與 Codex 或 Claude Code 等代理...

kimiflare:將 Moonshot Kimi-K2.6 直接部署於 Cloudflare 的終端 AI 編碼助手 | kimiflare: A Terminal Coding Agent Powered by Moonshot Kimi-K2.6 on Cloudflare

無需中繼,在你的 Cloudflare 帳戶中直接運行強大的 Kimi 終端編碼智能體。 | No middleman, run the powerful Kimi terminal coding agent directly on your own Cloudflare account. 🔎 工具速覽 / AT A GLANCE Category AI Coding Agent / Developer Tool Pricing Free (Open-weight model / Cloudflare Workers AI pricing) BestFor Developers seeking a private, high-context terminal AI assistant with direct infrastructure control. GitHub Stars ⭐ 36 🚀 引言 / Introduction kimiflare 是一款為開發者量身打造的終端編碼智能體,它將 Moonshot 的 1 兆參數開源模型 Kimi-K2.6 與 Cloudflare Workers AI 完美結合。不同於傳統 AI 工具,kimiflare 讓流量直接在你的 Cloudflare 帳戶中流動,徹底擺脫第三方代理或 AI 閘道的束縛,實現最高等級的控制力與隱私。 | kimiflare is a terminal coding agent tailored for developers, seamlessly combining Moonshot's 1T-parameter open-source model Kimi-K2.6 with Cloudflare Workers AI. Unlike traditional AI tools, kimiflare routes traffic directly through your Cloudflare account, eliminating the n...

Orca Skills: 開源賦能,加速雲安全工作流

加速雲安全工作流 - Orca Skills 整合多種工具與插件,打造高效安全環境 | Accelerate Cloud Security Workflows with Orca Skills, a Suite of Tools and Plugins 🔎 工具速覽 / AT A GLANCE Category 开源安全工具 Pricing 开源免费 BestFor 雲安全团队、DevSecOps工程师 GitHub Stars ⭐ 33 🚀 引言 / Introduction 在雲端環境中,確保安全不僅是一種技術挑戰,更是對工作效率的一場考驗。Orca Skills 是一款專為加速 Orca 云平台安全工作流而設計的开源插件工具包。透過整合多種工具和插件,幫助我們在複雜的安全環境下保持高效率。 🛠️ 核心功能 / Key Features Orca Skills 提供了一系列功能強大的安全工作流工具,包括警報處理、影響分析、配置源頭追溯等。這些工具不僅能夠幫助我們快速定位問題,還能確保在解決問題時避免造成不必要的後續問題。 💡 技術亮點 / Tech Highlights Orca Skills 真正的亮點在于其開放性與兼容性。它不僅支持Anthropic Claude Code、Cursor 和 Codex,還能夠與 MCP(Orca Security)集成,為安全團隊提供全面的支持。 📦 快速上手 / Quick Start 要快速開始使用 Orca Skills,首先需要安裝所需環境和工具。接下來,配置 MCP 以實現與 Orca Cloud Platform 的集成。最後,測試各個技能插件以確保其正常運行。 中英文對照: 要快速開始使用 Orca Skills 首先需要安裝所需環境和工具 接著配置 MCP 以實現與 Orca 云平台的集成 最...