▶ Cinematic fable · Watch on YouTube ▶ 影片版寓言 · 在 YouTube 观看

云隐阁凿在青崖半腰,藏书七万卷,向以”快”闻名。

阁里有一条传了不知多少代的老规矩:不许等人开口。

掌门还没说要哪一卷,掌阁的陆老先生已经把人打发下去了。他站在门口,听脚步、看灯影、数风向,赌掌门下一步要开哪一扇门。赌对了,掌门话音未落,卷子已摊在案上,快得像本来就在那里;赌错了,就把人喊回来,卷子放回原处,案上什么也不留。

阁里还有第二条规矩,比第一条更要紧:没开口的事,算没发生过。 拿错了卷子不算过失,账册上一个字都不记——那些卷子根本没”到过”案上。

多少年来,云隐阁的名声是快,从没人说过乱。

直到那年秋天,对家派来一个抄书人。

他叫玄七,持着正经荐书,每日在阁里抄书,抄得极慢。他不问机密,不碰禁区,只是午后日头正好时,提着一支烛在架子之间慢慢走,走到哪一格,便把烛凑近,看焰子歪不歪。

他做的事只有三步。

第一步,把陆老先生的赌法喂熟。连着三日,他求掌门每日午后必问”玄字第七卷”。老先生果然记住了这个时辰。第四日午后,又有一封盖着假印的密札,被人混进了掌门案上的文书堆——那密札第一行只写一个字。

第二步,他在密室到书架之间的廊下,摆了两百五十六个一模一样的空木匣,一个挨一个,匣底压着不同的青砖。

第三步,他挑了个最老实的小厮,把一枚假符塞进他手里,说:等会儿老先生叫你,你照他的话走。

午后,掌门果然开口:”取玄字第七卷。”

陆老先生没有犹豫——这个时辰,这个语气,他听过三遍了。他一挥手,小厮便朝最深的那扇封条门跑去。

那扇门后的规矩是:外面的架上有的,可以取;封条门后的,一步不许进。 这是阁里唯一的铁律,掌门亲口立的。

可老先生喊得太快。小厮的脚步已经迈出去了,门上的封条在他眼里还没来得及变成一个”止”字。他跑了进去,一眼看见那封密札,看见了第一行那个字。

那是个”柒”字。

小厮是识数的。他脚步没停,转身就出来,径直走到廊下那两百五十六个空木匣前面,在第七个匣子跟前站了半瞬,又走回门口。

这时陆老先生的第二声喊才追上来:”错了!回来!”

小厮回来了。卷子放回原处。密札重新封好。账册上一个字都没有——他没取过任何东西,他只是跑错了一趟。

事情到这里本该过去。

可玄七还提着那支烛在廊下走。

他一个匣子一个匣子地凑过去,看焰子。二百五十五个匣子前的焰子纹丝不动;走到第七个匣子跟前,焰子忽然歪了一下——那里的青砖,还是温的。

那半瞬的站立,把那个”柒”字,原原本本地留在了砖上。

——到这儿你大概已经认出来了:陆老先生那”不许等人开口”的抢跑,是 speculative execution;他凭时辰和语气赌的那一扇门,是 branch prediction;小厮穿过封条门的那一步,是乱序执行越过了边界检查;账册上的空白,是错误推测被回滚后的 architectural state;而那块还温着的青砖,是没有被回滚的缓存状态。玄七提着烛挨个量焰子,就是 Flush+Reload 计时;他三天里喂熟的那个时辰,是 branch training。

这一整套,叫推测执行的侧信道,最出名的一支是 Spectre。

这是什么

现代 CPU 不肯让流水线空着。遇到一个分支,它不等你算出条件,先猜——用历史记录(branch predictor)猜你往哪走,然后把猜的那条路上的指令提前执行掉,这叫 speculative execution。猜对了,白赚一大截时间;猜错了,就把结果全部丢弃,寄存器、内存、标志位一概恢复原状。对软件来说,错误推测过的那条路从未发生——这被称为 architectural state 的精确回滚。

问题在于:回滚只发生在架构状态这一层。微架构里的东西——缓存里被填进来的那一行、TLB 里新换的表项、branch predictor 自己更新过的历史——没有回滚机制,也从来没人打算做。它们只是”状态”。于是错误推测执行过的那条路,会在缓存里留下一道浅浅的印子。

Spectre 就是拿这道印子当信道。攻击者先训练分支预测器(反复让它走同一条路,喂出一个稳定的猜测),再让受害代码带着一个越界的下标走到那条路上:if (x < len) y = arr2[arr1[x] * 4096];。x < len 本该是护栏,但预测器已经笃定它成立,于是 arr1[x] 被推测性地读出来——它是一个受害者本来无权访问的字节——然后这个字节被用来算地址,把 arr2 里对应那一条缓存行拉进缓存。整段很快被判定为推测错误,全部撤销,程序继续若无其事地跑。

可缓存里那一条,还在。

攻击者只需再来一轮,把 arr2 的每一条缓存行都清掉,然后逐个计时访问:快的那个,就是被秘密字节拉进来的那一条。一次一个字节,重复几千次,一个密钥就漏完了。整个过程没有任何越权访问记录,没有异常,没有日志——因为架构上它真的什么都没做。

为什么重要

因为它把一句本来很结实的话打穿了:“没有日志,就是没发生。”

三十年来,隔离建立在”代码不可能执行到那一步”之上——边界检查、权限位、page table 的 user/supervisor 位、KVM 的地址空间、容器的 namespace。而 Spectre 攻击的恰恰是执行到那一步之前的猜测:真正的指令从未提交,权限从未被违反,操作系统看不见任何异常,可数据已经借着缓存的可观测性溜了出去。你的隔离边界还在,只是有人在边界外测了测温度。

这也解释了它在云上为什么格外刺眼。多租户的物理机上,同一颗核的超线程(SMT)与共享的 L3 缓存,就是那块”公共廊道”——同一台机器上另一个租户的缓存时序,原则上就是可观测的。这就是为什么当年的补丁链条那么难看:

  • 微码 / 固件补丁(microcode)改的是推测器本身,比如 IBRS/STIBP 之类的间接分支预测限制;
  • 内核里的 retpoline 把间接跳转换成一段”捕获并返回”的循环,让推测走进一条死路;
  • 关掉超线程(disable SMT)——最有效,也最贵,吞吐直接掉一截;
  • 站点隔离 / 进程隔离、给敏感代码加 lfence / speculation barrier,以及把代码写成常数时间(constant-time,无论秘密是什么都走一样的路,比如在密码学库里用无分支选择替代 if)。

代价是实打实的:这些补丁普遍带来几位数百分比的吞吐损失,而它们防的不是”漏洞”这种可以补完的东西,是现代 CPU 为了快而做的一个根本取舍。这一类攻击(Spectre 家族)被反复重新发明,因为每一代新微架构都在同一处偷速度。

工程上真正能落地的三条,故事里都给了:

  • 在封条门前喊停——在信任边界的两侧插入序列化点(lfence、屏障、constant-time 比较)。位置要对:喊在门后毫无意义。
  • 各院各烛——把真正敏感的东西和不可信的输入放进不同的进程、不同的核、不同的机器。租户隔离别只靠 namespace。
  • 让人把每个匣子都走一遍——敏感代码里不许有依赖秘密的分支和内存下标。这条最难,也最根本。

最后一条冷知识:你自己的代码通常不需要写 lfence。真正会中招的是编译器、JIT 和密码学库——它们会替你生成边界检查与间接跳转。所以修这件事的顺序是:先补微码和内核,看好你的运行时,最后才是自己的代码。

隐喻对应表

  • 陆老先生”不许等人开口”的抢跑 —— speculative execution
  • 凭时辰、语气赌那扇门 —— branch prediction
  • 三天里喂熟的”午后必问玄字第七卷” —— branch training(训练预测器)
  • 封条门”外面有就可以取,门后一步不许进” —— 边界检查 / 权限检查(if (x < len))
  • 喊得太快,脚步先迈了出去 —— 推测执行越过未完成的条件判断
  • 小厮看见密札第一行的字 —— 推测性地读出一个越权字节
  • 他走到第七个空匣子前站了半瞬 —— 用秘密字节计算地址,把对应缓存行拉入缓存
  • 二百五十六个一模一样的匣子 —— 256 个候选值对应 256 条缓存行
  • “错了!回来!”,卷子放回,账册无字 —— 错误推测的精确回滚(architectural state 无痕)
  • 青砖还是温的 —— 微架构状态(缓存)不回滚
  • 玄七提烛挨个量焰子 —— Flush+Reload 计时侧信道
  • “他从未读过”却字字皆知 —— 隔离边界仍然完好,数据却借可观测性外泄
  • 同一廊道、同一盏灯 —— 共享 L3 缓存 / 同一颗核的 SMT 线程
  • 在封条门前喊一声”停” —— 序列化 / lfence / speculation barrier
  • 让推测走进死路(原地默念再返回) —— retpoline
  • 各家分独立小院、各点各的烛 —— 站点隔离 / 进程与核隔离 / 关掉 SMT
  • 无论取哪一卷都把每个匣子走一遍 —— constant-time、无分支代码
  • 补丁带来的吞吐下降 —— 安全与性能的硬取舍,不是可以补完的 bug

The Cloud-Hidden Pavilion is cut into the flank of a green cliff. Seventy thousand scrolls, and a reputation built entirely on one word: fast.

It has an old rule, older than anyone can trace: never wait for the master to speak.

Before the master has named a single scroll, old Lu, the keeper of the archive, has already sent a boy running. He stands at the doorway reading footsteps, lamp shadows, the turn of the wind, and guesses which door the master will open next. Guess right, and the scroll is laid on the desk before the sentence ends — so quick it seems it was always there. Guess wrong, and the boy is called back, the scroll returned, the desk left bare.

And there is a second rule, more important than the first: what was never spoken never happened. A wrong scroll is not a fault. Not one character goes into the ledger — those scrolls never “reached” the desk at all.

For generations the Pavilion’s name meant fast. Nobody ever said careless.

Then one autumn, a rival house sent a copyist.

His name was Xuan Qi. He carried proper letters of introduction and copied slowly, every day. He asked nothing confidential and touched nothing forbidden. He simply walked the aisles in the good afternoon light with a candle, and wherever he stopped, he held the flame close and watched whether it leaned.

He did three things.

First, he taught old Lu’s guessing habit. For three days running he begged the master to ask, every afternoon at that hour, for “the seventh scroll of the Xuan set.” The old man learned the hour. On the fourth afternoon, a sealed letter bearing a forged stamp was slipped in among the papers on the master’s desk — and the first line of that letter held a single character.

Second, in the corridor between the sealed chamber and the shelves, he set out two hundred and fifty-six identical empty wooden boxes, side by side, each resting on a different blue brick.

Third, he picked the most honest boy in the place, pressed a counterfeit tally into his hand, and said: when the old man calls you, do exactly as he says.

That afternoon the master spoke: “Fetch the seventh scroll of the Xuan set.”

Old Lu did not hesitate. That hour, that tone — he had heard it three times. He waved, and the boy ran for the deepest door in the archive.

The rule behind that door was simple: what is on the outer shelves may be taken; what lies behind the seal may not be entered, not one step. It was the only iron law in the Pavilion, and the master himself had set it.

But the old man had shouted too fast. The boy’s foot was already in the air, and the seal on the door had not yet become the character for stop in his eyes. He went in. He saw the sealed letter. He saw the first character of its first line.

It was the character for seven.

The boy could count. He did not break stride. He turned, came back out, walked straight to the two hundred and fifty-six empty boxes in the corridor, stood for half a breath in front of the seventh — and went back to the doorway.

Only then did old Lu’s second shout catch up with him: “Wrong! Come back!”

The boy came back. The scroll was returned to its place. The letter was resealed. Not one character went into the ledger — he had taken nothing. He had merely run the wrong way.

That should have been the end of it.

But Xuan Qi was still walking the corridor with his candle.

He went box by box, holding the flame close. In front of two hundred and fifty-five of them the flame stood perfectly still. In front of the seventh, it leaned — the blue brick there was still warm.

Half a breath of standing had left that character seven on the stone, whole and legible.

— By now you’ve probably recognised it: old Lu’s rush to run before the order was spoken is speculative execution; the door he guessed from hour and tone is branch prediction; the boy’s step through the sealed door is out-of-order execution passing a bounds check; the blank in the ledger is architectural state after a rolled-back misprediction; and the brick still warm is cache state that is never rolled back. Xuan Qi measuring flame after flame with a candle is Flush+Reload timing; the hour he spent three days teaching is branch training.

The whole arrangement is a speculative-execution side channel, and its most famous member is Spectre.

What it is

A modern CPU refuses to let its pipeline sit idle. Faced with a branch, it does not wait for the condition to resolve — it guesses, using history (the branch predictor), and executes down the guessed path ahead of time. That is speculative execution. Guess right and you win a large chunk of time for free. Guess wrong and everything is discarded: registers, memory, flags all restored. To the software, the mispredicted path never happened — this is the precise rollback of architectural state.

The trouble is that the rollback stops at the architectural layer. Things in the microarchitecture — the line just pulled into cache, the TLB entry just installed, the predictor’s own updated history — have no rollback mechanism, and were never designed to have one. They are simply state. So the wrongly speculated path leaves a faint mark in the cache.

Spectre uses that mark as a channel. The attacker first trains the predictor — repeatedly taking one path until its guess is stable — then leads the victim’s code down that path with an out-of-range index: if (x < len) y = arr2[arr1[x] * 4096];. The check x < len is the guard rail, but the predictor is now certain it holds, so arr1[x] is read speculatively — a byte the victim has no right to touch — and that byte is then used to compute an address, pulling the corresponding line of arr2 into cache. Moments later the whole thing is judged a misprediction and undone, and the program carries on as if nothing occurred.

Except that one cache line is still there.

The attacker needs one more round: evict every line of arr2, then time access to each. The fast one is the line the secret byte pulled in. One byte at a time, a few thousand repetitions, and a key is gone. And there is no record of any unauthorised access, no exception, no log entry — because architecturally nothing happened.

Why it matters

Because it breaks a sentence that used to be solid: “no log means it never happened.”

For thirty years, isolation has rested on “the code cannot get to that step” — bounds checks, permission bits, the user/supervisor bit in the page table, KVM’s address spaces, container namespaces. Spectre attacks the guess that happens before that step: the real instruction never retires, no permission is ever violated, the operating system sees no anomaly — yet the data has already leaked through the observability of the cache. Your isolation boundary is intact. Someone just took its temperature from outside.

Which is why it is especially sharp in the cloud. On a multi-tenant physical host, the hyperthreads of a single core (SMT) and the shared L3 cache are precisely that “public corridor” — another tenant’s cache timing on the same machine is, in principle, observable. It’s why the patch chain looked so ugly:

  • Microcode and firmware changes target the speculation machinery itself, such as IBRS/STIBP restrictions on indirect-branch prediction;
  • retpoline in the kernel turns indirect jumps into a “capture and return” loop so speculation walks into a dead end;
  • Disabling hyperthreading (SMT) is the most effective measure and the most expensive — throughput drops outright;
  • Site isolation / process isolation, lfence or other speculation barriers around sensitive code, and writing code constant-time — where every path looks identical no matter the secret, as crypto libraries do when they replace if with a branchless select.

The cost is real: these mitigations routinely take a double-digit-percent bite out of throughput, and what they defend against is not a “vulnerability” that can be patched away, but a fundamental trade-off modern CPUs make in order to be fast. The Spectre family keeps being reinvented, because every new microarchitecture cuts the same corner.

Three things that actually land in engineering practice, all of them in the story:

  • Shout stop at the sealed door — insert a serialisation point (lfence, a barrier, a constant-time comparison) on both sides of the trust boundary. Placement matters: shouting behind the door is useless.
  • A separate courtyard and its own candle per house — put genuinely sensitive material and untrusted input in different processes, different cores, different machines. Don’t rest tenant isolation on namespaces alone.
  • Make the boy walk every box — no secret-dependent branches and no secret-dependent memory indices in sensitive code. Hardest, and most fundamental.

One last counter-intuitive note: your own code usually does not need an lfence. The things that get hit are compilers, JITs, and crypto libraries — they generate the bounds checks and indirect jumps for you. So the order of repair is: microcode and kernel first, then know your runtime, and your own code last.

Metaphor mapping

  • Old Lu’s rush to run before the order is spoken — speculative execution
  • Guessing the door from hour and tone — branch prediction
  • The three days spent teaching “the Xuan set, every afternoon” — branch training (teaching the predictor)
  • “What is outside may be taken; behind the seal, not one step” — a bounds check / permission check (if (x < len))
  • Shouting too late, the foot already in the air — speculative execution crossing an unresolved condition
  • The boy seeing the first character of the sealed letter — speculatively reading an out-of-bounds byte
  • Standing half a breath before the seventh empty box — computing an address from the secret byte, pulling that cache line in
  • Two hundred and fifty-six identical boxes — 256 candidate values mapping to 256 cache lines
  • “Wrong! Come back!”, scroll returned, ledger blank — precise rollback of architectural state on a misprediction
  • The blue brick still warm — microarchitectural state (cache) is never rolled back
  • Xuan Qi measuring flame after flame by candle — the Flush+Reload timing side channel
  • “He never read it”, yet every character known — the isolation boundary stays intact while data leaks through observability
  • The same corridor, the same lamp — shared L3 cache / SMT threads on one core
  • Shouting “stop” at the sealed door — serialisation / lfence / speculation barrier
  • Making speculation walk into a dead end (chant in place, then return) — retpoline
  • A courtyard and its own candle for each house — site isolation / process and core isolation / disabling SMT
  • Walking every box no matter which scroll is asked for — constant-time, branchless code
  • The throughput lost to the patches — a hard security-versus-speed trade-off, not a bug you can finish fixing
Daily Fables每日寓言 2026-11-15