clean up address limit helpers v2
Subsystems (26)
Hi all,
in preparation for eventually phasing out direct use of set_fs(), this series removes the segment_eq() arch helper that is only used to implement or duplicate the uaccess_kernel() API, and then adds descriptive helpers to force the kernel address limit.
Changes since v1:
- drop to incorrect hunks
- fix a commit log typo
Diffstat:
arch/alpha/include/asm/uaccess.h | 2 +- arch/arc/include/asm/segment.h | 3 +-- arch/arm/include/asm/uaccess.h | 4 ++-- arch/arm64/include/asm/uaccess.h | 2 +- arch/arm64/kernel/sdei.c | 2 +- arch/csky/include/asm/segment.h | 2 +- arch/h8300/include/asm/segment.h | 2 +- arch/ia64/include/asm/uaccess.h | 2 +- arch/m68k/include/asm/segment.h | 2 +- arch/m68k/include/asm/tlbflush.h | 6 +++--- arch/microblaze/include/asm/uaccess.h | 2 +- arch/mips/include/asm/uaccess.h | 2 +- arch/mips/kernel/unaligned.c | 27 +++++++++++++-------------- arch/nds32/include/asm/uaccess.h | 2 +- arch/nds32/kernel/process.c | 2 +- arch/nds32/mm/alignment.c | 7 +++---- arch/nios2/include/asm/uaccess.h | 2 +- arch/openrisc/include/asm/uaccess.h | 2 +- arch/parisc/include/asm/uaccess.h | 2 +- arch/powerpc/include/asm/uaccess.h | 3 +-- arch/riscv/include/asm/uaccess.h | 6 +++--- arch/s390/include/asm/uaccess.h | 2 +- arch/sh/include/asm/segment.h | 3 +-- arch/sh/kernel/traps_32.c | 12 +++++------- arch/sparc/include/asm/uaccess_32.h | 2 +- arch/sparc/include/asm/uaccess_64.h | 2 +- arch/x86/include/asm/uaccess.h | 2 +- arch/xtensa/include/asm/uaccess.h | 2 +- drivers/firmware/arm_sdei.c | 5 ++--- fs/exec.c | 7 ++++++- include/asm-generic/uaccess.h | 4 ++-- include/linux/syscalls.h | 2 +- include/linux/uaccess.h | 20 ++++++++++++++++++-- kernel/events/callchain.c | 5 ++--- kernel/events/core.c | 5 ++--- kernel/exit.c | 2 +- kernel/kthread.c | 5 ++--- kernel/stacktrace.c | 5 ++--- mm/maccess.c | 22 ++++++++++------------ 39 files changed, 99 insertions(+), 92 deletions(-)
PATCH 1/6 syscalls: use uaccess_kernel in addr_limit_user_check
Use the uaccess_kernel helper instead of duplicating it.
--- include/linux/syscalls.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/syscalls.h +1 -1
@@ -263,7 +263,7 @@ static inline void addr_limit_user_check(void)return ;#endif -if (CHECK_DATA_CORRUPTION (!segment_eq (get_fs (),USER_DS ), +if (CHECK_DATA_CORRUPTION (uaccess_kernel (),"Invalid address limit on user-mode return" ))force_sig (SIGKILL );
Signature · 1 line
2.26.2
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Hi,
Quoted 3 lines · Christoph Hellwig
Use the
uaccess_kernelhelper instead of duplicating it.Signed-off-by Christoph Hellwig <[email protected]>
This patch causes a severe hiccup with my mps2-an385 boot test.
[ 8.545195] ------------[ cut here ]------------ [ 8.545294] WARNING: CPU: 0 PID: 1 at ./include/linux/syscalls.h:267 addr_limit_check_failed+0x11/0x24 [ 8.545376] Invalid address limit on user-mode return [ 8.545487] CPU: 0 PID: 1 Comm: init Not tainted 5.8.0-rc5-next-20200717 #1 [ 8.545603] Hardware name: MPS2 (Device Tree Support) [ 8.546053] [<2100af9d>] (unwind_backtrace) from [<2100a353>] (show_stack+0xb/0xc) [ 8.546240] [<2100a353>] (show_stack) from [<2100dadb>] (__warn+0x6f/0x80) [ 8.546311] [<2100dadb>] (__warn) from [<2100db1d>] (warn_slowpath_fmt+0x31/0x60) [ 8.546383] [<2100db1d>] (warn_slowpath_fmt) from [<2100a159>] (addr_limit_check_failed+0x11/0x24) [ 8.546464] [<2100a159>] (addr_limit_check_failed) from [<210080f3>] (ret_to_user_from_irq+0xf/0x18) [ 8.546567] Exception stack(0x21427fb0 to 0x21427ff8) [ 8.546649] 7fa0: 00000000 00000000 00000000 00000000 [ 8.546729] 7fc0: 00000000 00000000 00000000 00000000 00000000 00000000 21744f80 00000000 [ 8.546800] 7fe0: 00000000 21757fa8 00000000 21700b6c 01000000 00000000 [ 8.546910] ---[ end trace f1b0cd10cc3456dc ]---
This keeps happening until qemu aborts.
Reverting the patch isn't possible since segment_eq() is gone in next-20200717.
Guenter
---
# bad: [aab7ee9f8ff0110bfcd594b33dc33748dc1baf46] Add linux-next specific files for 20200717
# good: [11ba468877bb23f28956a35e896356252d63c983] Linux 5.8-rc5
git bisect start 'HEAD' 'v5.8-rc5'
# good: [4d55a7a1298d197755c1a0f4512f56917e938a83] Merge remote-tracking branch 'crypto/master'
git bisect good 4d55a7a1298d197755c1a0f4512f56917e938a83
# good: [e63bf5dcce255302e355cb2277a3a39c83752c92] Merge remote-tracking branch 'devicetree/for-next' git bisect good e63bf5dcce255302e355cb2277a3a39c83752c92
# good: [94d932ec3afb923efd8c736974f8316413175a5b] Merge remote-tracking branch 'thunderbolt/next'
git bisect good 94d932ec3afb923efd8c736974f8316413175a5b
# good: [5ddd2e0dbe8fceb80b0b36bd38a32217be7a04a5] Merge remote-tracking branch 'livepatching/for-next' git bisect good 5ddd2e0dbe8fceb80b0b36bd38a32217be7a04a5
# bad: [40346f79983caf46fb92f779b0353422d43580a9] ipc/shm.c: Remove the superfluous break
git bisect bad 40346f79983caf46fb92f779b0353422d43580a9
# good: [0b917599517f71ddef5f7274a8199a33cecd49b2] kasan: update required compiler versions in documentation git bisect good 0b917599517f71ddef5f7274a8199a33cecd49b2
# good: [701571ae06641cc0632d113a6d25f54ce651e723] mm,hwpoison: rework soft offline for free pages
git bisect good 701571ae06641cc0632d113a6d25f54ce651e723
# bad: [1c21deffe923b068d2d297c248845ec93531d1bf] lib/test_bits.c: add tests of GENMASK
git bisect bad 1c21deffe923b068d2d297c248845ec93531d1bf
# bad: [9549375184b2cb4f63fa7917665acf9c44114499] uaccess: add force_uaccess_{begin,end} helpers
git bisect bad 9549375184b2cb4f63fa7917665acf9c44114499
# good: [233d009c15719e43c53b73296144664e0bd59a2e] mm/memory_hotplug: introduce default dummy memory_add_physaddr_to_nid() git bisect good 233d009c15719e43c53b73296144664e0bd59a2e
# good: [42889ca325dd735ce964838cff81a444637d9d01] mm: drop duplicated words in <linux/mm.h>
git bisect good 42889ca325dd735ce964838cff81a444637d9d01
# bad: [3b17e98704eedeeff41672b2f64cef1bbefbb8b2] nds32: use uaccess_kernel in show_regs
git bisect bad 3b17e98704eedeeff41672b2f64cef1bbefbb8b2
# bad: [02dc30b876b111276fe7d83d492ddfc2b39b80e3] syscalls: use uaccess_kernel in addr_limit_user_check git bisect bad 02dc30b876b111276fe7d83d492ddfc2b39b80e3
# first bad commit: [02dc30b876b111276fe7d83d492ddfc2b39b80e3] syscalls: use uaccess_kernel in addr_limit_user_check
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 8 lines · Guenter Roeck
Hi,
Quoted 3 lines · Christoph Hellwig
Use the
uaccess_kernelhelper instead of duplicating it.Signed-off-by Christoph Hellwig <[email protected]>This patch causes a severe hiccup with my mps2-an385 boot test.
I guess that is a nommu config?
Can you try this patch?
arch/arm/include/asm/uaccess.h +1 -1
@@ -263,7 +263,7 @@ extern int __put_user_8(void *, unsigned long long);*/ #define USER_DS KERNEL_DS -#define uaccess_kernel () (true) +#define uaccess_kernel () (false)#define __addr_ok (addr) ((void)(addr), 1)#define __range_ok (addr, size) ((void)(addr), 0)#define get_fs () (KERNEL_DS)
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 12 lines · Christoph Hellwig
Quoted 8 lines · Guenter Roeck
Hi,
Quoted 3 lines · Christoph Hellwig
Use the
uaccess_kernelhelper instead of duplicating it.Signed-off-by Christoph Hellwig <[email protected]>This patch causes a severe hiccup with my mps2-an385 boot test.
I guess that is a nommu config?
Correct.
Quoted 8 lines
Can you try this patch?
arch/arm/include/asm/uaccess.h +0
-0
arch/arm/include/asm/uaccess.h extern int __put_user_8(void *, unsigned long long); · line 269
Quoted 8 lines
arch/arm/include/asm/uaccess.h +1
-1@@ -263,7 +263,7 @@ extern int __put_user_8(void *, unsigned long long);*/ #define USER_DS KERNEL_DS -#define uaccess_kernel () (true) +#define uaccess_kernel () (false)#define __addr_ok (addr) ((void)(addr), 1)#define __range_ok (addr, size) ((void)(addr), 0)#define get_fs () (KERNEL_DS)
With this change, the error message is gone. However, it still does not work. The image just hangs instead.
Guenter
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
To try to reproduce your report I built a mps2_defconfig kernel and then run the qemu command line manually extraced from your script below, using a mainline qemu built for arm-softmmu, but it crashes with the following message even for the baseline kernel.
qemu: fatal: Lockup: can't escalate 3 to HardFault (current priority -1)
R00=00000000 R01=00000000 R02=00000000 R03=00000000 R04=00000000 R05=00000000 R06=00000000 R07=00000000 R08=00000000 R09=00000000 R10=00000000 R11=00000000 R12=00000000 R13=ffffffe0 R14=fffffff9 R15=00000000 XPSR=40000003 -Z-- A handler
FPSCR: 00000000
Does anyone have an idea what this means?
---
/opt/qemu/bin/qemu-system-arm \
-M mps2-an385 \
-cpu cortex-m3 \
-dtb arch/arm/boot/dts/mps2-an385.dtb \
-kernel vmlinux \
-no-reboot \
-snapshot -m 16 \
-initrd ~/images/rootfs-arm-m3.cpio \
-append 'panic=-1' \
-bios ~/images/mps2-boot.axf \
-nographic -monitor null -serial stdioRe: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 7 lines · Christoph Hellwig
To try to reproduce your report I built a
mps2_defconfigkernel and then run the qemu command line manually extraced from your script below, using a mainline qemu built for arm-softmmu, but it crashes with the following message even for the baseline kernel.qemu: fatal: Lockup: can't escalate 3 to HardFault (current priority -1)
Quoted 9 lines
R00=00000000 R01=00000000 R02=00000000 R03=00000000 R04=00000000 R05=00000000 R06=00000000 R07=00000000 R08=00000000 R09=00000000 R10=00000000 R11=00000000 R12=00000000 R13=ffffffe0 R14=fffffff9 R15=00000000 XPSR=40000003 -Z-- A handler
FPSCR: 00000000Does anyone have an idea what this means?
Ah, sorry, you can't use the upstream version of qemu to test mps2-an385 Linux images. You'll have to use a version from https://github.com/groeck/qemu.
I'd recommend to use the v5.0.0-local branch.
I had to make some changes to qemu to be able to boot mps2-an385. I tried to submit those changes into upstream qemu, but that was rejected because, as I was told, the qemu implementation would no longer reflect the real hardware with those changes in place.
Guenter
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 9 lines · Guenter Roeck
Ah, sorry, you can't use the upstream version of qemu to test mps2-an385 Linux images. You'll have to use a version from https://github.com/groeck/qemu.
I'd recommend to use the v5.0.0-local branch.I had to make some changes to qemu to be able to boot mps2-an385. I tried to submit those changes into upstream qemu, but that was rejected because, as I was told, the qemu implementation would no longer reflect the real hardware with those changes in place.
Yes; the rationale is that if you wanted to boot a kernel on an actual MPS2 board you'd need a bit of guest code to start it up (and to bundle the initrd/dtb in with it), so since you need to write that code anyway you could use it for booting the kernel in QEMU too.
I appreciate that this is awkward for kernel developers (and perhaps for some other users too), but QEMU's handling of -kernel and built-in-bootloader code is already a morass of special cases and do-what-I-mean behaviour that I'm not enthusiastic about further complicating :-)
(https://lists.gnu.org/archive/html/qemu-arm/2018-06/msg00393.html
has the archive of our original discussion on the point, for other readers of this post interested in further context and discussion.)
thanks
-- PMM
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
In reply to Christoph HellwigQuoted 10 lines · Christoph Hellwig
Quoted 8 lines · Guenter Roeck
Hi,
Quoted 3 lines · Christoph Hellwig
Use the
uaccess_kernelhelper instead of duplicating it.Signed-off-by Christoph Hellwig <[email protected]>This patch causes a severe hiccup with my mps2-an385 boot test.
I had another look into the code. Right after this patch, I see
#define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
Yet, this patch is:
- if (CHECK_DATA_CORRUPTION(!segment_eq(get_fs(), USER_DS),
+ if (CHECK_DATA_CORRUPTION(uaccess_kernel(),
So there is a negation in the condition. Indeed, the following change on top of next-20200720 fixes the problem for mps2-an385.
- if (CHECK_DATA_CORRUPTION(uaccess_kernel(),
+ if (CHECK_DATA_CORRUPTION(!uaccess_kernel(),
How does this work anywhere ?
Thanks,
Guenter
Quoted 17 lines
I guess that is a nommu config?
Can you try this patch?
arch/arm/include/asm/uaccess.h +1
-1@@ -263,7 +263,7 @@ extern int __put_user_8(void *, unsigned long long);*/ #define USER_DS KERNEL_DS -#define uaccess_kernel () (true) +#define uaccess_kernel () (false)#define __addr_ok (addr) ((void)(addr), 1)#define __range_ok (addr, size) ((void)(addr), 0)#define get_fs () (KERNEL_DS)
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 15 lines · Guenter Roeck
I had another look into the code. Right after this patch, I see
#define
uaccess_kernel()segment_eq(get_fs(), KERNEL_DS)Yet, this patch is:
(quoted diff) +1
-1- if (CHECK_DATA_CORRUPTION(!segment_eq(get_fs(), USER_DS), + if (CHECK_DATA_CORRUPTION(uaccess_kernel(),So there is a negation in the condition. Indeed, the following change on top of next-20200720 fixes the problem for mps2-an385.
(quoted diff) +1
-1- if (CHECK_DATA_CORRUPTION(uaccess_kernel(), + if (CHECK_DATA_CORRUPTION(!uaccess_kernel(),
Quoted 1 line
How does this work anywhere ?
No, that is the wrong check - we want to make sure the address space override doesn't leak to userspace. The problem is that armnommu (and m68knommu, but that doesn't call the offending function) pretends to not have a kernel address space, which doesn't really work. Here is the fix I sent out yesterday, which I should have Cc'ed you on, sorry:
---
From 2bb889b2d99a2d978e90640ade8fe02359287092 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Mon, 20 Jul 2020 17:46:50 +0200
Subject: arm: don't call addr_limit_user_check for nommu
On arm nommu kernel use the same constant for USER_DS and KERNEL_DS, and seqment_eq always returns false. With the current check in addr_limit_user_check that works by accident, but when replacing seqment_eq with uaccess_kerne it will fail. Just remove the not needed check entirely.
--- arch/arm/kernel/signal.c | 2 ++ 1 file changed, 2 insertions(+)
arch/arm/kernel/signal.c +2 -0
@@ -713,7 +713,9 @@ struct page *get_signal_page(void)/* Defer to generic check */ asmlinkage void addr_limit_check_failed(void) { +#ifdef CONFIG_MMU addr_limit_user_check (); +#endif }#ifdef CONFIG_DEBUG_RSEQ
Signature · 1 line
2.27.0
Guenter Roeck
Tested-byRe: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 16 lines · Christoph Hellwig
Quoted 15 lines · Guenter Roeck
I had another look into the code. Right after this patch, I see
#define
uaccess_kernel()segment_eq(get_fs(), KERNEL_DS)Yet, this patch is:
(quoted diff) +1
-1- if (CHECK_DATA_CORRUPTION(!segment_eq(get_fs(), USER_DS), + if (CHECK_DATA_CORRUPTION(uaccess_kernel(),So there is a negation in the condition. Indeed, the following change on top of next-20200720 fixes the problem for mps2-an385.
(quoted diff) +1
-1- if (CHECK_DATA_CORRUPTION(uaccess_kernel(), + if (CHECK_DATA_CORRUPTION(!uaccess_kernel(),
Quoted 9 lines
Quoted 1 line
How does this work anywhere ?
No, that is the wrong check - we want to make sure the address space override doesn't leak to userspace. The problem is that armnommu (and m68knommu, but that doesn't call the offending function) pretends to not have a kernel address space, which doesn't really work. Here is the fix I sent out yesterday, which I should have Cc'ed you on, sorry:
The patch below makes sense, and it does work, but I still suspect that something with your original patch is wrong, or at least suspicious.
Reason: My change above (Adding the "!") works for _all_ of my arm boot tests. Or, in other words, it doesn't make a difference if true or false is passed as first parameter of CHECK_DATA_CORRUPTION(), except for nommu systems. Also, unless I am really missing something, your original patch _does_ reverse the logic.
I didn't track this down further.
Thanks,
Guenter
Quoted 6 lines
---
Quoted 1 line
From
2bb889b2d99a2d978e90640ade8fe02359287092Mon Sep 17 00:00:00 2001From: Christoph Hellwig <[email protected]>
Date: Mon, 20 Jul 2020 17:46:50 +0200
Subject: arm: don't calladdr_limit_user_checkfor nommu
Quoted 8 lines
On arm nommu kernel use the same constant for
USER_DSandKERNEL_DS, andseqment_eqalways returns false. With the current check inaddr_limit_user_checkthat works by accident, but when replacingseqment_eqwithuaccess_kerneit will fail. Just remove the not needed check entirely.Signed-off-by Christoph Hellwig <[email protected]>Reported-by Guenter Roeck <[email protected]>
Quoted 19 lines
--- arch/arm/kernel/signal.c | 2 ++ 1 file changed, 2 insertions(+)arch/arm/kernel/signal.c +2
-0@@ -713,7 +713,9 @@ struct page *get_signal_page(void)/* Defer to generic check */ asmlinkage void addr_limit_check_failed(void) { +#ifdef CONFIG_MMU addr_limit_user_check (); +#endif }#ifdef CONFIG_DEBUG_RSEQ
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 13 lines · Guenter Roeck
Quoted 12 lines
Quoted 4 lines
(quoted diff) +1
-1- if (CHECK_DATA_CORRUPTION(uaccess_kernel(), + if (CHECK_DATA_CORRUPTION(!uaccess_kernel(),How does this work anywhere ?
No, that is the wrong check - we want to make sure the address space override doesn't leak to userspace. The problem is that armnommu (and m68knommu, but that doesn't call the offending function) pretends to not have a kernel address space, which doesn't really work. Here is the fix I sent out yesterday, which I should have Cc'ed you on, sorry:
Quoted 7 lines
The patch below makes sense, and it does work, but I still suspect that something with your original patch is wrong, or at least suspicious.
Reason: My change above (Adding the "!") works for _all_ of my arm boot tests. Or, in other words, it doesn't make a difference if true or false is passed as first parameter ofCHECK_DATA_CORRUPTION(), except for nommu systems. Also, unless I am really missing something, your original patch _does_ reverse the logic.
Well. segment_eq is in current mainline used in two places:
1) to implement uaccess_kernel
2) in addr_limit_user_check to implement uaccess_kernel-like semantics using a strange reverse notation
I think the explanation for your observation is how addr_limit_user_check is called on arm. The addr_limit_check_failed wrapper for it is called from assembly code, but only after already checking the addr_limit, basically duplicating the segment_eq check. So for mmu builds it won't get called unless we leak the kernel address space override, which is a pretty fatal error and won't show up in your boot tests. The only good way to test it is by explicit injecting it using the lkdtm module.
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 28 lines · Christoph Hellwig
Quoted 13 lines · Guenter Roeck
Quoted 12 lines
Quoted 4 lines
(quoted diff) +1
-1- if (CHECK_DATA_CORRUPTION(uaccess_kernel(), + if (CHECK_DATA_CORRUPTION(!uaccess_kernel(),How does this work anywhere ?
No, that is the wrong check - we want to make sure the address space override doesn't leak to userspace. The problem is that armnommu (and m68knommu, but that doesn't call the offending function) pretends to not have a kernel address space, which doesn't really work. Here is the fix I sent out yesterday, which I should have Cc'ed you on, sorry:
Quoted 7 lines
The patch below makes sense, and it does work, but I still suspect that something with your original patch is wrong, or at least suspicious.
Reason: My change above (Adding the "!") works for _all_ of my arm boot tests. Or, in other words, it doesn't make a difference if true or false is passed as first parameter ofCHECK_DATA_CORRUPTION(), except for nommu systems. Also, unless I am really missing something, your original patch _does_ reverse the logic.Well.
segment_eqis in current mainline used in two places:1) to implement
uaccess_kernel
2) inaddr_limit_user_checkto implement uaccess_kernel-like semantics using a strange reverse notation
Quoted 9 lines
I think the explanation for your observation is how
addr_limit_user_checkis called on arm. Theaddr_limit_check_failedwrapper for it is called from assembly code, but only after already checking theaddr_limit, basically duplicating thesegment_eqcheck. So for mmu builds it won't get called unless we leak the kernel address space override, which is a pretty fatal error and won't show up in your boot tests. The only good way to test it is by explicit injecting it using the lkdtm module.
Guess I lost it somewhere. Are you saying the check was wrong all along and your patch fixed it ?
Thanks,
Guenter
Re: [PATCH 1/6] syscalls: use uaccess_kernel in addr_limit_user_check
Quoted 2 lines · Guenter Roeck
Guess I lost it somewhere. Are you saying the check was wrong all along and your patch fixed it ?
Oh, it is a little complicated.
Normally we have two address space limits, KERNEL_DS and USER_DS, and they are supposed to be different. armnommu and m68k define them to the same value for no good reason. That leads to:
uaccess_kernel always returning true as it does a positive check agains KERNEL_DS, which disables a bunch of drivers like sg and rdma, and could also lead to really strange and probably broken results in a few places.
It also leads to the SIGKILL in addr_limit_user_check never triggering due to the negatіve check, which is ok as the limits never are different.
PATCH 2/6 nds32: use uaccess_kernel in show_regs
In reply to Christoph HellwigUse the uaccess_kernel helper instead of duplicating it.
--- arch/nds32/kernel/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/nds32/kernel/process.c +1 -1
@@ -121,7 +121,7 @@ void show_regs(struct pt_regs *regs) regs->uregs[3], regs->uregs[2], regs->uregs[1], regs->uregs[0]);pr_info (" IRQs o%s Segment %s\n" ,interrupts_enabled (regs) ?"n" :"ff" , -segment_eq (get_fs (),KERNEL_DS )?"kernel" :"user" ); +uaccess_kernel () ?"kernel" :"user" ); }EXPORT_SYMBOL (show_regs);
Signature · 1 line
2.26.2
PATCH 3/6 riscv: include <asm/pgtable.h> in <asm/uaccess.h>
In reply to Christoph HellwigTo ensure TASK_SIZE is defined for USER_DS.
--- arch/riscv/include/asm/uaccess.h | 2 ++ 1 file changed, 2 insertions(+)
arch/riscv/include/asm/uaccess.h +2 -0
@@ -8,6 +8,8 @@#ifndef _ASM_RISCV_UACCESS_H#define _ASM_RISCV_UACCESS_H +#include <asm/pgtable.h> /* for TASK_SIZE */ +/* * User space memory access functions */
Signature · 1 line
2.26.2
PATCH 4/6 uaccess: remove segment_eq
In reply to Christoph Hellwigsegment_eq is only used to implement uaccess_kernel. Just open code uaccess_kernel in the arch uaccess headers and remove one layer of indirection.
--- arch/alpha/include/asm/uaccess.h | 2 +- arch/arc/include/asm/segment.h | 3 +-- arch/arm/include/asm/uaccess.h | 4 ++-- arch/arm64/include/asm/uaccess.h | 2 +- arch/csky/include/asm/segment.h | 2 +- arch/h8300/include/asm/segment.h | 2 +- arch/ia64/include/asm/uaccess.h | 2 +- arch/m68k/include/asm/segment.h | 2 +- arch/microblaze/include/asm/uaccess.h | 2 +- arch/mips/include/asm/uaccess.h | 2 +- arch/nds32/include/asm/uaccess.h | 2 +- arch/nios2/include/asm/uaccess.h | 2 +- arch/openrisc/include/asm/uaccess.h | 2 +- arch/parisc/include/asm/uaccess.h | 2 +- arch/powerpc/include/asm/uaccess.h | 3 +-- arch/riscv/include/asm/uaccess.h | 4 +--- arch/s390/include/asm/uaccess.h | 2 +- arch/sh/include/asm/segment.h | 3 +-- arch/sparc/include/asm/uaccess_32.h | 2 +- arch/sparc/include/asm/uaccess_64.h | 2 +- arch/x86/include/asm/uaccess.h | 2 +- arch/xtensa/include/asm/uaccess.h | 2 +- include/asm-generic/uaccess.h | 4 ++-- include/linux/uaccess.h | 2 -- 24 files changed, 25 insertions(+), 32 deletions(-)
arch/alpha/include/asm/uaccess.h +1 -1
@@ -20,7 +20,7 @@#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)/* * Is a address valid? This does a straightforward calculation rather
arch/arc/include/asm/segment.h +1 -2
@@ -14,8 +14,7 @@ typedef unsigned long mm_segment_t;#define KERNEL_DS MAKE_MM_SEG(0)#define USER_DS MAKE_MM_SEG(TASK_SIZE) - -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)#endif /* __ASSEMBLY__ */ #endif /* __ASMARC_SEGMENT_H */
arch/arm/include/asm/uaccess.h +2 -2
@@ -76,7 +76,7 @@ static inline void set_fs(mm_segment_t fs)modify_domain (DOMAIN_KERNEL , fs ?DOMAIN_CLIENT :DOMAIN_MANAGER ); } -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)/* We use 33-bit arithmetic here... */ #define __range_ok (addr, size) ({ \
@@ -263,7 +263,7 @@ extern int __put_user_8(void *, unsigned long long);*/ #define USER_DS KERNEL_DS -#define segment_eq (a, b) (1) +#define uaccess_kernel () (true)#define __addr_ok (addr) ((void)(addr), 1)#define __range_ok (addr, size) ((void)(addr), 0)#define get_fs () (KERNEL_DS)
arch/arm64/include/asm/uaccess.h +1 -1
@@ -49,7 +49,7 @@ static inline void set_fs(mm_segment_t fs) CONFIG_ARM64_UAO)); } -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)/* * Test whether a block of memory is a valid user space address.
arch/csky/include/asm/segment.h +1 -1
@@ -13,6 +13,6 @@ typedef struct {#define USER_DS ((mm_segment_t) { 0x80000000UL })#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#endif /* __ASM_CSKY_SEGMENT_H */
arch/h8300/include/asm/segment.h +1 -1
@@ -33,7 +33,7 @@ static inline mm_segment_t get_fs(void) return USER_DS; } -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#endif /* __ASSEMBLY__ */
arch/ia64/include/asm/uaccess.h +1 -1
@@ -50,7 +50,7 @@#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)/* * When accessing user memory, we need to make sure the entire area really is in
arch/m68k/include/asm/segment.h +1 -1
@@ -52,7 +52,7 @@ static inline void set_fs(mm_segment_t val)#define set_fs (x) (current_thread_info()->addr_limit = (x))#endif -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#endif /* __ASSEMBLY__ */
arch/microblaze/include/asm/uaccess.h +1 -1
@@ -41,7 +41,7 @@# define get_fs () (current_thread_info()->addr_limit)# define set_fs (val) (current_thread_info()->addr_limit = (val)) -# define segment_eq (a, b) ((a).seg == (b).seg) +# define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#ifndef CONFIG_MMU
arch/mips/include/asm/uaccess.h +1 -1
@@ -72,7 +72,7 @@ extern u64 __ua_limit;#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)/* * eva_kernel_access() - determine whether kernel memory access on an EVA system
arch/nds32/include/asm/uaccess.h +1 -1
@@ -44,7 +44,7 @@ static inline void set_fs(mm_segment_t fs)current_thread_info ()->addr_limit = fs; } -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)#define __range_ok (addr, size) (size <= get_fs() && addr <= (get_fs() -size))
arch/nios2/include/asm/uaccess.h +1 -1
@@ -30,7 +30,7 @@#define get_fs () (current_thread_info()->addr_limit)#define set_fs (seg) (current_thread_info()->addr_limit = (seg)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define __access_ok (addr, len) \ (((signed long)(((long)get_fs().seg) & \
arch/openrisc/include/asm/uaccess.h +1 -1
@@ -43,7 +43,7 @@#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)/* Ensure that the range from addr to addr+size is all within the process' * address space
arch/parisc/include/asm/uaccess.h +1 -1
@@ -14,7 +14,7 @@#define KERNEL_DS ((mm_segment_t){0})#define USER_DS ((mm_segment_t){1}) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x))
arch/powerpc/include/asm/uaccess.h +1 -2
@@ -38,8 +38,7 @@ static inline void set_fs(mm_segment_t fs)set_thread_flag (TIF_FSCHECK ); } -#define segment_eq (a, b) ((a).seg == (b).seg) - +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define user_addr_max () (get_fs().seg)#ifdef __powerpc64__
arch/riscv/include/asm/uaccess.h +1 -3
@@ -64,11 +64,9 @@ static inline void set_fs(mm_segment_t fs)current_thread_info ()->addr_limit = fs; } -#define segment_eq (a, b) ((a).seg == (b).seg) - +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define user_addr_max () (get_fs().seg) -/** * access_ok: - Checks if a user space pointer is valid * @addr: User space pointer to start of block to check
arch/s390/include/asm/uaccess.h +1 -1
@@ -32,7 +32,7 @@#define USER_DS_SACF (3)#define get_fs () (current->thread.mm_segment) -#define segment_eq (a,b) (((a) & 2) == ((b) & 2)) +#define uaccess_kernel () ((get_fs() & 2) == KERNEL_DS)void set_fs (mm_segment_t fs);
arch/sh/include/asm/segment.h +1 -2
@@ -24,8 +24,7 @@ typedef struct {#define USER_DS KERNEL_DS#endif -#define segment_eq (a, b) ((a).seg == (b).seg) - +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x))
arch/sparc/include/asm/uaccess_32.h +1 -1
@@ -28,7 +28,7 @@#define get_fs () (current->thread.current_ds)#define set_fs (val) ((current->thread.current_ds) = (val)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)/* We have there a nice not-mapped page at PAGE_OFFSET - PAGE_SIZE, so that this test * can be fairly lightweight.
arch/sparc/include/asm/uaccess_64.h +1 -1
@@ -32,7 +32,7 @@#define get_fs () ((mm_segment_t){(current_thread_info()->current_ds)}) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define set_fs (val) \ do { \
arch/x86/include/asm/uaccess.h +1 -1
@@ -33,7 +33,7 @@ static inline void set_fs(mm_segment_t fs)set_thread_flag (TIF_FSCHECK ); } -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define user_addr_max () (current->thread.addr_limit.seg)/*
arch/xtensa/include/asm/uaccess.h +1 -1
@@ -35,7 +35,7 @@#define get_fs () (current->thread.current_ds)#define set_fs (val) (current->thread.current_ds = (val)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define __kernel_ok (uaccess_kernel())#define __user_ok (addr, size) \
include/asm-generic/uaccess.h +2 -2
@@ -86,8 +86,8 @@ static inline void set_fs(mm_segment_t fs) }#endif -#ifndef segment_eq -#define segment_eq (a, b) ((a).seg == (b).seg) +#ifndef uaccess_kernel +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#endif #define access_ok (addr, size) __access_ok((unsigned long)(addr),(size))
include/linux/uaccess.h +0 -2
@@ -6,8 +6,6 @@#include <linux/sched.h> #include <linux/thread_info.h> -#define uaccess_kernel () segment_eq(get_fs(), KERNEL_DS) -#include <asm/uaccess.h> /*
Signature · 1 line
2.26.2
Re: [PATCH 4/6] uaccess: remove segment_eq
Ack, just with a note:
arch/x86/include/asm/uaccess.h static inline void set_fs(mm_segment_t fs) · line 37
Quoted 10 lines · Christoph Hellwig
arch/x86/include/asm/uaccess.h +1
-1@@ -33,7 +33,7 @@ static inline void set_fs(mm_segment_t fs)set_thread_flag (TIF_FSCHECK ); } -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define user_addr_max () (current->thread.addr_limit.seg)
This "uaccess_kernel()" interface is a better model anyway, because at least on x86 (and from a quick glance at others), we might avoid the exact equality comparison, and instead do simpler/better things.
On x86-64, for example, checking whether the limit has the high bit set is not only more flexible and correct, it's much cheaper too.
Of course, trying to get rid of all this means that it doesn't matter so much, but it would probably have been good to do this part years ago regardless.
Linus
PATCH 5/6 uaccess: add force_uaccess_{begin,end} helpers
In reply to Christoph HellwigAdd helpers to wrap the get_fs/set_fs magic for undoing any damange done by set_fs(KERNEL_DS). There is no real functional benefit, but this documents the intent of these calls better, and will allow stubbing the functions out easily for kernels builds that do not allow address space overrides in the future.
--- arch/arm64/kernel/sdei.c | 2 +- arch/m68k/include/asm/tlbflush.h | 6 +++--- arch/mips/kernel/unaligned.c | 27 +++++++++++++-------------- arch/nds32/mm/alignment.c | 7 +++---- arch/sh/kernel/traps_32.c | 12 +++++------- drivers/firmware/arm_sdei.c | 5 ++--- include/linux/uaccess.h | 18 ++++++++++++++++++ kernel/events/callchain.c | 5 ++--- kernel/events/core.c | 5 ++--- kernel/kthread.c | 5 ++--- kernel/stacktrace.c | 5 ++--- mm/maccess.c | 22 ++++++++++------------ 12 files changed, 63 insertions(+), 56 deletions(-)
arch/arm64/kernel/sdei.c +1 -1
@@ -180,7 +180,7 @@ static __kprobes unsigned long _sdei_handler(struct pt_regs *regs,/* * We didn't take an exception to get here, set PAN. UAO will be cleared -* by sdei_event_handler()s set_fs(USER_DS) call. +* by sdei_event_handler()s force_uaccess_begin() call. */ __uaccess_enable_hw_pan ();
arch/m68k/include/asm/tlbflush.h +3 -3
@@ -85,10 +85,10 @@ static inline void flush_tlb_mm(struct mm_struct *mm)static inline void flush_tlb_page (struct vm_area_struct *vma,unsigned long addr) {if (vma->vm_mm == current->active_mm) { -mm_segment_t old_fs =get_fs (); -set_fs (USER_DS ); +mm_segment_t old_fs =force_uaccess_begin (); +__flush_tlb_one (addr); -set_fs (old_fs); +force_uaccess_end (old_fs); } }
arch/mips/kernel/unaligned.c +13 -14
@@ -191,17 +191,16 @@ static void emulate_load_store_insn(struct pt_regs *regs,* memory, so we need to "switch" the address limit to * user space, so that address check can work properly. */ - seg =get_fs (); -set_fs (USER_DS ); + seg =force_uaccess_begin ();switch (insn.spec3_format.func) {case lhe_op:if (!access_ok (addr,2 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }LoadHWE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }compute_return_epc (regs);
@@ -209,12 +208,12 @@ static void emulate_load_store_insn(struct pt_regs *regs,break ;case lwe_op:if (!access_ok (addr,4 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }LoadWE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }compute_return_epc (regs);
@@ -222,12 +221,12 @@ static void emulate_load_store_insn(struct pt_regs *regs,break ;case lhue_op:if (!access_ok (addr,2 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }LoadHWUE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }compute_return_epc (regs);
@@ -235,35 +234,35 @@ static void emulate_load_store_insn(struct pt_regs *regs,break ;case she_op:if (!access_ok (addr,2 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }compute_return_epc (regs); value = regs->regs[insn.spec3_format.rt];StoreHWE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }break ;case swe_op:if (!access_ok (addr,4 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }compute_return_epc (regs); value = regs->regs[insn.spec3_format.rt];StoreWE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }break ;default : -set_fs (seg); +force_uaccess_end (seg); goto sigill; } -set_fs (seg); +force_uaccess_end (seg); }#endif break ;
arch/nds32/mm/alignment.c +3 -4
@@ -512,7 +512,7 @@ int do_unaligned_access(unsigned long addr, struct pt_regs *regs) {unsigned long inst;int ret = -EFAULT ; -mm_segment_t seg =get_fs (); +mm_segment_t seg; inst =get_inst (regs->ipc);
@@ -520,13 +520,12 @@ int do_unaligned_access(unsigned long addr, struct pt_regs *regs)"Faulting addr: 0x%08lx, pc: 0x%08lx [inst: 0x%08lx ]\n" , addr, regs->ipc, inst); -set_fs (USER_DS ); - + seg =force_uaccess_begin ();if (inst &NDS32_16BIT_INSTRUCTION ) ret =do_16 ((inst >>16 ) &0xffff , regs);else ret =do_32 (inst, regs); -set_fs (seg); +force_uaccess_end (seg);return ret; }
arch/sh/kernel/traps_32.c +5 -7
@@ -482,8 +482,6 @@ asmlinkage void do_address_error(struct pt_regs *regs, error_code =lookup_exception_vector ();#endif - oldfs =get_fs (); -if (user_mode (regs)) {int si_code =BUS_ADRERR ;unsigned int user_action;
@@ -491,13 +489,13 @@ asmlinkage void do_address_error(struct pt_regs *regs,local_irq_enable ();inc_unaligned_user_access (); -set_fs (USER_DS ); + oldfs =force_uaccess_begin ();if (copy_from_user (&instruction, (insn_size_t *)(regs->pc & ~1 ),sizeof (instruction))) { -set_fs (oldfs); +force_uaccess_end (oldfs); goto uspace_segv; } -set_fs (oldfs); +force_uaccess_end (oldfs);/* shout about userspace fixups */ unaligned_fixups_notify (current, instruction, regs);
@@ -520,11 +518,11 @@ asmlinkage void do_address_error(struct pt_regs *regs, goto uspace_segv; } -set_fs (USER_DS ); + oldfs =force_uaccess_begin (); tmp =handle_unaligned_access (instruction, regs, &user_mem_access,0 , address); -set_fs (oldfs); +force_uaccess_end (oldfs);if (tmp ==0 )return ;/* sorted */
drivers/firmware/arm_sdei.c +2 -3
@@ -1136,15 +1136,14 @@ int sdei_event_handler(struct pt_regs *regs,* access kernel memory. * Do the same here because this doesn't come via the same entry code. */ - orig_addr_limit =get_fs (); -set_fs (USER_DS ); + orig_addr_limit =force_uaccess_begin (); err = arg->callback (event_num, regs, arg->callback_arg);if (err)pr_err_ratelimited ("event %u on CPU %u failed with error: %d\n" , event_num,smp_processor_id (), err); -set_fs (orig_addr_limit); +force_uaccess_end (orig_addr_limit);return err; }
include/linux/uaccess.h +18 -0
@@ -8,6 +8,24 @@#include <asm/uaccess.h> +/* +* Force the uaccess routines to be wired up for actual userspace access, +* overriding any possible set_fs(KERNEL_DS) still lingering around. Undone +* using force_uaccess_end below. +*/ +static inline mm_segment_t force_uaccess_begin (void ) +{ +mm_segment_t fs =get_fs (); + +set_fs (USER_DS ); +return fs; +} + +static inline void force_uaccess_end (mm_segment_t oldfs) +{ +set_fs (oldfs); +} +/* * Architectures should provide two primitives (raw_copy_{to,from}_user()) * and get rid of their private instances of copy_{to,from}_user() and
kernel/events/callchain.c +2 -3
@@ -218,10 +218,9 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,if (add_mark)perf_callchain_store_context (&ctx,PERF_CONTEXT_USER ); - fs =get_fs (); -set_fs (USER_DS ); + fs =force_uaccess_begin ();perf_callchain_user (&ctx, regs); -set_fs (fs); +force_uaccess_end (fs); } }
kernel/events/core.c +2 -3
@@ -6436,10 +6436,9 @@ perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,/* Data. */ sp =perf_user_stack_pointer (regs); - fs =get_fs (); -set_fs (USER_DS ); + fs =force_uaccess_begin (); rem =__output_copy_user (handle, (void *) sp, dump_size); -set_fs (fs); +force_uaccess_end (fs); dyn_size = dump_size - rem;perf_output_skip (handle, rem);
kernel/kthread.c +2 -3
@@ -1254,8 +1254,7 @@ void kthread_use_mm(struct mm_struct *mm)if (active_mm != mm)mmdrop (active_mm); -to_kthread (tsk)->oldfs =get_fs (); -set_fs (USER_DS ); +to_kthread (tsk)->oldfs =force_uaccess_begin (); }EXPORT_SYMBOL_GPL (kthread_use_mm);
@@ -1270,7 +1269,7 @@ void kthread_unuse_mm(struct mm_struct *mm)WARN_ON_ONCE (!(tsk->flags &PF_KTHREAD ));WARN_ON_ONCE (!tsk->mm); -set_fs (to_kthread (tsk)->oldfs); +force_uaccess_end (to_kthread (tsk)->oldfs);task_lock (tsk);sync_mm_rss (mm);
kernel/stacktrace.c +2 -3
@@ -233,10 +233,9 @@ unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)if (current->flags &PF_KTHREAD )return 0 ; - fs =get_fs (); -set_fs (USER_DS ); + fs =force_uaccess_begin ();arch_stack_walk_user (consume_entry, &c,task_pt_regs (current)); -set_fs (fs); +force_uaccess_end (fs);return c.len; }
mm/maccess.c +10 -12
@@ -205,15 +205,14 @@ long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)long copy_from_user_nofault (void *dst,const void __user *src,size_t size) {long ret = -EFAULT ; -mm_segment_t old_fs =get_fs (); +mm_segment_t old_fs =force_uaccess_begin (); -set_fs (USER_DS );if (access_ok (src, size)) {pagefault_disable (); ret =__copy_from_user_inatomic (dst, src, size);pagefault_enable (); } -set_fs (old_fs); +force_uaccess_end (old_fs);if (ret)return -EFAULT ;
@@ -233,15 +232,14 @@ EXPORT_SYMBOL_GPL(copy_from_user_nofault);long copy_to_user_nofault (void __user *dst,const void *src,size_t size) {long ret = -EFAULT ; -mm_segment_t old_fs =get_fs (); +mm_segment_t old_fs =force_uaccess_begin (); -set_fs (USER_DS );if (access_ok (dst, size)) {pagefault_disable (); ret =__copy_to_user_inatomic (dst, src, size);pagefault_enable (); } -set_fs (old_fs); +force_uaccess_end (old_fs);if (ret)return -EFAULT ;
@@ -270,17 +268,17 @@ EXPORT_SYMBOL_GPL(copy_to_user_nofault);long strncpy_from_user_nofault (char *dst,const void __user *unsafe_addr,long count) { -mm_segment_t old_fs =get_fs (); +mm_segment_t old_fs;long ret;if (unlikely (count <=0 ))return 0 ; -set_fs (USER_DS ); + old_fs =force_uaccess_begin ();pagefault_disable (); ret =strncpy_from_user (dst, unsafe_addr, count);pagefault_enable (); -set_fs (old_fs); +force_uaccess_end (old_fs);if (ret >= count) { ret = count;
@@ -310,14 +308,14 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,*/ long strnlen_user_nofault (const void __user *unsafe_addr,long count) { -mm_segment_t old_fs =get_fs (); +mm_segment_t old_fs;int ret; -set_fs (USER_DS ); + old_fs =force_uaccess_begin ();pagefault_disable (); ret =strnlen_user (unsafe_addr, count);pagefault_enable (); -set_fs (old_fs); +force_uaccess_end (old_fs);return ret; }
Signature · 1 line
2.26.2
Re: [PATCH 5/6] uaccess: add force_uaccess_{begin,end} helpers
Quoted 6 lines · Christoph Hellwig
Add helpers to wrap the get_fs/set_fs magic for undoing any damange done by
set_fs(KERNEL_DS). There is no real functional benefit, but this documents the intent of these calls better, and will allow stubbing the functions out easily for kernels builds that do not allow address space overrides in the future.
It would perhaps have been nicer to rename the save variabel too (neither "seg" nor "oldfs" make much sense once you get rid of the old x86-inspired name).
But from a greppability standpoint and a doc standpoint, I guess just renaming the function is sufficient (and certainly easier).
Linus
PATCH 6/6 exec: use force_uaccess_begin during exec and exit
In reply to Christoph HellwigBoth exec and exit want to ensure that the uaccess routines actually do access user pointers. Use the newly added force_uaccess_begin helper instead of an open coded set_fs for that to prepare for kernel builds where set_fs() does not exist.
--- fs/exec.c | 7 ++++++- kernel/exit.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-)
fs/exec.c +6 -1
@@ -1380,7 +1380,12 @@ int begin_new_exec(struct linux_binprm * bprm)if (retval) goto out_unlock; -set_fs (USER_DS ); +/* +* Ensure that the uaccess routines can actually operate on userspace +* pointers: +*/ +force_uaccess_begin (); + me->flags &= ~(PF_RANDOMIZE |PF_FORKNOEXEC |PF_KTHREAD |PF_NOFREEZE |PF_NO_SETAFFINITY );flush_thread ();
kernel/exit.c +1 -1
@@ -731,7 +731,7 @@ void __noreturn do_exit(long code)* mm_release()->clear_child_tid() from writing to a user-controlled * kernel address. */ -set_fs (USER_DS ); +force_uaccess_begin ();if (unlikely (in_atomic ())) { pr_info("note: %s[%d] exited with preempt_count %d\n",
Signature · 1 line
2.26.2
Re: [PATCH 6/6] exec: use force_uaccess_begin during exec and exit
Quoted 4 lines · Christoph Hellwig
Both exec and exit want to ensure that the uaccess routines actually do access user pointers. Use the newly added
force_uaccess_beginhelper instead of an open codedset_fsfor that to prepare for kernel builds whereset_fs()does not exist.
Have you played with a tree with all of your patches and placing force_uaccess_begin in init/main.c:start_kernel?
Somewhere deep in the arch code we seem to have it all backwards and kernel threads are all set_fs(KERNEL_DS). So just putting a force_uaccess_begin somewhere very early should be enough to switch things around.
Quoted 37 lines
Signed-off-by Christoph Hellwig <[email protected]>--- fs/exec.c | 7 ++++++- kernel/exit.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-)fs/exec.c +6
-1@@ -1380,7 +1380,12 @@ int begin_new_exec(struct linux_binprm * bprm)if (retval) goto out_unlock; -set_fs (USER_DS ); +/* +* Ensure that the uaccess routines can actually operate on userspace +* pointers: +*/ +force_uaccess_begin (); + me->flags &= ~(PF_RANDOMIZE |PF_FORKNOEXEC |PF_KTHREAD |PF_NOFREEZE |PF_NO_SETAFFINITY );flush_thread ();kernel/exit.c +1
-1@@ -731,7 +731,7 @@ void __noreturn do_exit(long code)* mm_release()->clear_child_tid() from writing to a user-controlled * kernel address. */ -set_fs (USER_DS ); +force_uaccess_begin ();if (unlikely (in_atomic ())) { pr_info("note: %s[%d] exited with preempt_count %d\n",
Re: [PATCH 6/6] exec: use force_uaccess_begin during exec and exit
Quoted 11 lines · Eric W. Biederman
Quoted 4 lines · Christoph Hellwig
Both exec and exit want to ensure that the uaccess routines actually do access user pointers. Use the newly added
force_uaccess_beginhelper instead of an open codedset_fsfor that to prepare for kernel builds whereset_fs()does not exist.Have you played with a tree with all of your patches and placing
force_uaccess_beginin init/main.c:start_kernel?
No, I have converted the early boot code to not require KERNEL_DS and except for a pending network item remove set_fs entirely. Older series here, will need some rework:
http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/set_fs-removal
Andrew Morton
maintainerQuoted 11 lines · On Tue, 14 Jul 2020 12:54:59 +0200 Christoph Hellwig <[email protected]> wrote:
Hi all,
in preparation for eventually phasing out direct use of
set_fs(), this series removes thesegment_eq()arch helper that is only used to implement or duplicate theuaccess_kernel()API, and then adds descriptive helpers to force the kernel address limit.Changes since v1:
- drop to incorrect hunks
- fix a commit log typo
I think this *is* v1. I can't find any differences in the patches and I was unable to eyeball any changelog alterations?
Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Tue, 14 Jul 2020 12:54:59 +0200 Christoph Hellwig <[email protected]> wrote:
Hi all,
in preparation for eventually phasing out direct use of
set_fs(), this series removes thesegment_eq()arch helper that is only used to implement or duplicate theuaccess_kernel()API, and then adds descriptive helpers to force the kernel address limit.
Quoted 6 lines
Quoted 3 lines
Changes since v1:
- drop to incorrect hunks
- fix a commit log typoI think this *is* v1. I can't find any differences in the patches and I was unable to eyeball any changelog alterations?
No, this actuall is v2.
"[PATCH 5/6] uaccess: add force_uaccess_{begin,end} helpers" dropped two incorrect hunks in the m68k and sh arch code, and lost and "er" in the commit log.
cover arm: don't call addr_limit_user_check for nommu
In reply to Christoph HellwigOn arm nommu kernel use the same constant for USER_DS and KERNEL_DS, and seqment_eq always returns false. With the current check in addr_limit_user_check that works by accident, but when replacing seqment_eq with uaccess_kerne it will fail. Just remove the not needed check entirely.
---
Andrew: this should preferably go before the other patches in this series.
arch/arm/kernel/signal.c | 2 ++ 1 file changed, 2 insertions(+)
arch/arm/kernel/signal.c +2 -0
@@ -713,7 +713,9 @@ struct page *get_signal_page(void)/* Defer to generic check */ asmlinkage void addr_limit_check_failed(void) { +#ifdef CONFIG_MMU addr_limit_user_check (); +#endif }#ifdef CONFIG_DEBUG_RSEQ
Signature · 1 line
2.27.0