The vulnerability CVE-2024-31852 describes a situation where LLVM's ARM backend generates code where the Link Register (LR) can be overwritten because it wasn't properly saved to the stack. This is due to a miscompilation. The provided commit 0e16af8e4cf3a66ad5d078d52744ae2776f9c4b2 addresses this issue.
The analysis of the commit reveals that the core of the problem lay within the ARMLoadStoreOptimizer.
- The function
llvm::ARMLoadStoreOpt::MergeReturnIntoLDM previously contained logic that would incorrectly mark the LR as not needing to be restored (Info.setRestored(false)) based on a local optimization. This logic was removed in the patch, indicating it was faulty.
- The main pass function
llvm::ARMLoadStoreOpt::runOnMachineFunction calls MergeReturnIntoLDM. Before the patch, it didn't ensure that the LR's restoration status was globally correct after such optimizations. The patch introduces a call to a new function ARMFrameLowering::updateLRRestored (which correctly determines LR restoration status based on all return instructions) if MergeReturnIntoLDM modified any return instructions.
These two functions in ARMLoadStoreOptimizer.cpp are identified as vulnerable because their pre-patch logic directly contributed to the incorrect assessment of whether LR needed to be saved, leading to the generation of faulty code where LR could be clobbered. The functions in ARMFrameLowering.cpp were modified to introduce the fix (updateLRRestored) and to call this fix, but the origin of the incorrect state was the optimizer.