Skip to contentSkip to conversation

ARM: prctl: Reject PR_SET_MDWE where not supported

PATCH RESEND Applied Zev Weiss 3 participants 6 messages Feb 27, 2024 – Mar 19, 2024

Subsystems (3)
lore
Share & tools
lore

Zev Weiss

author
#0 lore raw

cover ARM: prctl: Reject PR_SET_MDWE where not supported

[Resending as per Russell's request...]

Hello,

I noticed after a recent kernel update that my ARM926 system started segfaulting on any execve() after calling prctl(PR_SET_MDWE). After some investigation it appears that ARMv5 is incapable of providing the appropriate protections for MDWE, since any readable memory is also implicitly executable.

(Note that I'm not an expert in either ARM arch details or the mm subsystem, so please bear with me if I've botched something in the above analysis.)

The prctl_set_mdwe() function already had some special-case logic added disabling it on PARISC (commit 793838138c15, "prctl: Disable prctl(PR_SET_MDWE) on parisc"); this patch series (1) generalizes that check to use an arch_*() function, and (2) adds a corresponding override for ARM to disable MDWE on pre-ARMv6 CPUs.

With the series applied, prctl(PR_SET_MDWE) is rejected on ARMv5 and subsequent execve() calls (as well as mmap(PROT_READ|PROT_WRITE)) can succeed instead of unconditionally failing; on ARMv6 the prctl works as it did previously.

Thanks,
Zev

[0] https://lore.kernel.org/all/2023112456-linked-nape-bf19@gregkh/

Zev Weiss (2):
prctl: Generalize PR_SET_MDWE support check to be per-arch
ARM: prctl: Reject PR_SET_MDWE on pre-ARMv6

 arch/arm/include/asm/mman.h    | 14 ++++++++++++++
 arch/parisc/include/asm/mman.h | 14 ++++++++++++++
 include/linux/mman.h           |  8 ++++++++
 kernel/sys.c                   |  7 +++++--
 4 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm/include/asm/mman.h
 create mode 100644 arch/parisc/include/asm/mman.h
Signature · 1 line
2.43.2
author
#1 lore raw

PATCH RESEND 1/2 prctl: Generalize PR_SET_MDWE support check to be per-arch

There exist systems other than PARISC where MDWE may not be feasible to support; rather than cluttering up the generic code with additional arch-specific logic let's add a generic function for checking MDWE support and allow each arch to override it as needed.

---
 arch/parisc/include/asm/mman.h | 14 ++++++++++++++
 include/linux/mman.h           |  8 ++++++++
 kernel/sys.c                   |  7 +++++--
 3 files changed, 27 insertions(+), 2 deletions(-)
 create mode 100644 arch/parisc/include/asm/mman.h
arch/parisc/include/asm/mman.h new file +14 -0
new file mode 100644
index 000000000000..47c5a1991d10
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_MMAN_H__
+#define __ASM_MMAN_H__
+
+#include <uapi/asm/mman.h>
+
+/* PARISC cannot allow mdwe as it needs writable stacks */
+static inline bool arch_memory_deny_write_exec_supported(void)
+{
+	return false;
+}
+#define arch_memory_deny_write_exec_supported arch_memory_deny_write_exec_supported
+
+#endif /* __ASM_MMAN_H__ */
include/linux/mman.h +8 -0
index dc7048824be8..bcb201ab7a41 100644
@@ -162,6 +162,14 @@ calc_vm_flag_bits(unsigned long flags)
 
 unsigned long vm_commit_limit(void);
 
+#ifndef arch_memory_deny_write_exec_supported
+static inline bool arch_memory_deny_write_exec_supported(void)
+{
+	return true;
+}
+#define arch_memory_deny_write_exec_supported arch_memory_deny_write_exec_supported
+#endif
+
 /*
  * Denies creating a writable executable mapping or gaining executable permissions.
  *
kernel/sys.c +5 -2
index f8e543f1e38a..8bb106a56b3a 100644
@@ -2408,8 +2408,11 @@ static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3,
 	if (bits & PR_MDWE_NO_INHERIT && !(bits & PR_MDWE_REFUSE_EXEC_GAIN))
 		return -EINVAL;
 
-	/* PARISC cannot allow mdwe as it needs writable stacks */
-	if (IS_ENABLED(CONFIG_PARISC))
+	/*
+	 * EOPNOTSUPP might be more appropriate here in principle, but
+	 * existing userspace depends on EINVAL specifically.
+	 */
+	if (!arch_memory_deny_write_exec_supported())
 		return -EINVAL;
 
 	current_bits = get_current_mdwe();
Signature · 1 line
2.43.2
#2 lore raw

Re: [PATCH RESEND 1/2] prctl: Generalize PR_SET_MDWE support check to be per-arch

Quoted 7 lines · Zev Weiss

There exist systems other than PARISC where MDWE may not be feasible to support; rather than cluttering up the generic code with additional arch-specific logic let's add a generic function for checking MDWE support and allow each arch to override it as needed.

PA-RISC folk need to ack/review-by this patch. Alternatively, it needs to be restructured to add the arch_memory_deny_write_exec_supported() override without touching the PA-RISC code, which then makes the Arm patch independent of the status of the PA-RISC patch. That will allow the Arm issue to be solved even if an ack is not forthcoming for the PA-RISC parts.

Alternatively, I wonder whether akpm would be willing to pick up this patch set as-is.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
maintainer Acked-by
#3 lore raw

Re: [PATCH RESEND 1/2] prctl: Generalize PR_SET_MDWE support check to be per-arch

Quoted 10 lines · Russell King (Oracle)
Quoted 7 lines · Zev Weiss

There exist systems other than PARISC where MDWE may not be feasible to support; rather than cluttering up the generic code with additional arch-specific logic let's add a generic function for checking MDWE support and allow each arch to override it as needed.

PA-RISC folk need to ack/review-by this patch.

I'm fine with patch 1/2:

Quoted 8 lines

Alternatively, it needs
to be restructured to add the arch_memory_deny_write_exec_supported() override without touching the PA-RISC code, which then makes the Arm patch independent of the status of the PA-RISC patch. That will allow the Arm issue to be solved even if an ack is not forthcoming for the PA-RISC parts.

Quoted 1 line

Alternatively, I wonder whether akpm would be willing to pick up this

patch set as-is.

I have no preference, but I think both patches should be pushed together via arm tree or akpm.

Helge

author
#4 lore raw

Re: [PATCH RESEND 1/2] prctl: Generalize PR_SET_MDWE support check to be per-arch

Quoted 21 lines · Helge Deller
Quoted 10 lines · Russell King (Oracle)
Quoted 7 lines · Zev Weiss

There exist systems other than PARISC where MDWE may not be feasible to support; rather than cluttering up the generic code with additional arch-specific logic let's add a generic function for checking MDWE support and allow each arch to override it as needed.

PA-RISC folk need to ack/review-by this patch.

I'm fine with patch 1/2:

Quoted 6 lines

Alternatively, it needs
to be restructured to add the arch_memory_deny_write_exec_supported() override without touching the PA-RISC code, which then makes the Arm patch independent of the status of the PA-RISC patch. That will allow the Arm issue to be solved even if an ack is not forthcoming for the PA-RISC parts.

Quoted 7 lines
Quoted 2 lines
Quoted 1 line

Alternatively, I wonder whether akpm would be willing to pick up this

patch set as-is.

I have no preference, but I think both patches should be pushed together via arm tree or akpm.

Helge

Ping...Russell, Andrew, any thoughts on how this could move forward?

Thanks,
Zev

author
#5 lore raw

PATCH RESEND 2/2 ARM: prctl: Reject PR_SET_MDWE on pre-ARMv6

In reply to Zev Weiss

On v5 and lower CPUs we can't provide MDWE protection, so ensure we fail any attempt to enable it via prctl(PR_SET_MDWE).

Previously such an attempt would misleadingly succeed, leading to any subsequent mmap(PROT_READ|PROT_WRITE) or execve() failing unconditionally (the latter somewhat violently via force_fatal_sig(SIGSEGV) due to READ_IMPLIES_EXEC).

---
 arch/arm/include/asm/mman.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
 create mode 100644 arch/arm/include/asm/mman.h
arch/arm/include/asm/mman.h new file +14 -0
new file mode 100644
index 000000000000..2189e507c8e0
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_MMAN_H__
+#define __ASM_MMAN_H__
+
+#include <asm/system_info.h>
+#include <uapi/asm/mman.h>
+
+static inline bool arch_memory_deny_write_exec_supported(void)
+{
+	return cpu_architecture() >= CPU_ARCH_ARMv6;
+}
+#define arch_memory_deny_write_exec_supported arch_memory_deny_write_exec_supported
+
+#endif /* __ASM_MMAN_H__ */
Signature · 1 line
2.43.2