[PATCH v2] powerpc/rtas_flash: allow user copy to flash block cache objects

From: Nathan Lynch via B4 Relay
Date: Thu Aug 10 2023 - 23:38:14 EST


From: Nathan Lynch <nathanl@xxxxxxxxxxxxx>

With hardened usercopy enabled (CONFIG_HARDENED_USERCOPY=y), using the
/proc/powerpc/rtas/firmware_update interface to prepare a system
firmware update yields a BUG():

kernel BUG at mm/usercopy.c:102!
Oops: Exception in kernel mode, sig: 5 [#1]
LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries
Modules linked in:
CPU: 0 PID: 2232 Comm: dd Not tainted 6.5.0-rc3+ #2
Hardware name: IBM,8408-E8E POWER8E (raw) 0x4b0201 0xf000004 of:IBM,FW860.50 (SV860_146) hv:phyp pSeries
NIP: c0000000005991d0 LR: c0000000005991cc CTR: 0000000000000000
REGS: c0000000148c76a0 TRAP: 0700 Not tainted (6.5.0-rc3+)
MSR: 8000000000029033 <SF,EE,ME,IR,DR,RI,LE> CR: 24002242 XER: 0000000c
CFAR: c0000000001fbd34 IRQMASK: 0
[ ... GPRs omitted ... ]
NIP [c0000000005991d0] usercopy_abort+0xa0/0xb0
LR [c0000000005991cc] usercopy_abort+0x9c/0xb0
Call Trace:
[c0000000148c7940] [c0000000005991cc] usercopy_abort+0x9c/0xb0 (unreliable)
[c0000000148c79b0] [c000000000536814] __check_heap_object+0x1b4/0x1d0
[c0000000148c79f0] [c000000000599080] __check_object_size+0x2d0/0x380
[c0000000148c7a30] [c000000000045ed4] rtas_flash_write+0xe4/0x250
[c0000000148c7a80] [c00000000068a0fc] proc_reg_write+0xfc/0x160
[c0000000148c7ab0] [c0000000005a381c] vfs_write+0xfc/0x4e0
[c0000000148c7b70] [c0000000005a3e10] ksys_write+0x90/0x160
[c0000000148c7bc0] [c00000000002f2c8] system_call_exception+0x178/0x320
[c0000000148c7e50] [c00000000000d520] system_call_common+0x160/0x2c4
--- interrupt: c00 at 0x7fff9f17e5e4

The blocks of the firmware image are copied directly from user memory
to objects allocated from flash_block_cache, so flash_block_cache must
be created using kmem_cache_create_usercopy() to mark it safe for user
access.

Fixes: 6d07d1cd300f ("usercopy: Restrict non-usercopy caches to size 0")
Signed-off-by: Nathan Lynch <nathanl@xxxxxxxxxxxxx>
---
I believe it's much more common to update Power system firmware
without involving a Linux partition, which may explain why this has
gone unreported for so long.
---
Changes in v2:
- Drop excessive local const variables. No functional change.
- Link to v1: https://lore.kernel.org/r/20230801-rtas-flash-vs-hardened-usercopy-v1-1-2e99cf9e2bed@xxxxxxxxxxxxx
---
arch/powerpc/kernel/rtas_flash.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/kernel/rtas_flash.c b/arch/powerpc/kernel/rtas_flash.c
index 4caf5e3079eb..359577ec1680 100644
--- a/arch/powerpc/kernel/rtas_flash.c
+++ b/arch/powerpc/kernel/rtas_flash.c
@@ -709,9 +709,9 @@ static int __init rtas_flash_init(void)
if (!rtas_validate_flash_data.buf)
return -ENOMEM;

- flash_block_cache = kmem_cache_create("rtas_flash_cache",
- RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
- NULL);
+ flash_block_cache = kmem_cache_create_usercopy("rtas_flash_cache",
+ RTAS_BLK_SIZE, RTAS_BLK_SIZE,
+ 0, 0, RTAS_BLK_SIZE, NULL);
if (!flash_block_cache) {
printk(KERN_ERR "%s: failed to create block cache\n",
__func__);

---
base-commit: c3cad890877f59aeeaf5a638aa7a7c0612c16fa1
change-id: 20230731-rtas-flash-vs-hardened-usercopy-09a6d236b011

Best regards,
--
Nathan Lynch <nathanl@xxxxxxxxxxxxx>