r/programming • u/soap94 • Sep 05 '26
The NX bit is not just about security
https://purplesyringa.moe/blog/guest/the-nx-bit-is-not-just-about-security/
128
Upvotes
r/programming • u/soap94 • Sep 05 '26
75
u/creeper6530 Sep 05 '26
Section "Hypothesis #2: out-of-spec hardware", the link to kernel code really made me LOL.
First I thought it was an insult of the CPUs before realising they meant Apple (a fruit).
48
u/Aeyeoelle Sep 05 '26
Speculative execution bugs have to be the nadir of hardware debugging. To have thehe system fail out doing something it technically never actually does is brain-breaking. This is an impressively good article to explain it so well.
25
u/matthieum Sep 05 '26
I am so glad I don't have to debug at this level, sounds nightmarish.
8
u/pohahaha Sep 05 '26
Very nice!! 👍 interesting read
6
u/happyscrappy Sep 05 '26 edited Sep 05 '26
Good read. I had a little experience with a similar issue. The code I was working on had been used for a long time on systems with no speculative execution capability. It had started on 32-bit machines. Not that 32-bit machines are prohibited from speculating but I guess no one that used this code ran into any ARM ones that did.
But on 64-bit it was having problems like this. We ran into accesses to memory which caused aborts due to unexpected fetch sizes, areas which we were pretty sure we never accessed. The unexpected fetch sizes were because while we always accessed those areas of memory using accesses which were the same width as the device bus, the code fetcher was doing larger loads.
I never got it down to code as clean as hers here with just changing a single instruction to make it work or not. But it was clear after a while it was a speculative fetch. And in fixing all that we ended up making the same transition she speaks of here, which was to modify our system so that the default was to mark areas as non-executable except the windows to code. It was a good idea anyway, but of course no one had even considered implementing it because we simply were not to the level of implementing security improvements yet.
It actually paid off pretty quickly by making our crash dumps more useful. When we had bugs where we jumped into and started executing data we'd have to slightly hand pick the dumps apart to find out which instruction jumped us there. But after the change the instruction that tried to jump into data would be the one highlighted in the crash dump and so it was quicker to find out what went wrong.
3
u/ShinyHappyREM Sep 05 '26
IIRC, executing code from MMIO registers (i.e. device control/status bits) is what's used in certain TASes (tool-assisted speedruns) to execute code from the SNES controller registers.
2
u/indigo945 Sep 07 '26
What I do not understand is why the speculative execution of an invalid function address causes the CPU to trap. Is that the hardware bug? I understand how at
blr x0, even though it is afteradr x0, get_ctr_el0,x0is still filled with prior garbage data as far as the branch predictor is concerned. I also understand how it might therefore choose to speculatively read non-executable memory. But certainly a speculative memory read should not crash the CPU, but rather discard the speculative branch?3
u/747101350e0972dccde2 Sep 05 '26
very insightful