Skip to contentSkip to conversation

Make mmap() with PROT_WRITE imply PROT_READ

PATCH In review Andrew Bresticker 3 participants 5 messages Sep 15, 2022 – Oct 13, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover Make mmap() with PROT_WRITE imply PROT_READ

Commit 2139619bcad7 ("riscv: mmap with PROT_WRITE but no PROT_READ is invalid") made mmap() reject mappings with only PROT_WRITE set in an attempt to fix an observed inconsistency in behavior when attempting to read from a PROT_WRITE-only mapping. The root cause of this behavior was actually that while RISC-V's protection_map maps VM_WRITE to readable PTE permissions (since write-only PTEs are considered reserved by the privileged spec), the page fault handler considered loads from VM_WRITE-only VMAs illegal accesses. Fix the underlying cause by handling faults in VM_WRITE-only VMAs (patch 1) and then re-enable use of mmap(PROT_WRITE) (patch 2), making RISC-V's behavior consistent with all other architectures that don't support write-only PTEs.

Both patches are tagged as fixes for the aforementioned commit since that commit made a userspace visible change that will break any software relying on mmap(PROT_WRITE). (Also cc: stable since the offending commit was itself backported to stable).

v1 -> v2: Allow handling of load faults in VM_WRITE VMAs v2 -> v3: Split into two pathces
v3 -> v4: Fixes tags (+ this cover letter)

Andrew Bresticker (2):
riscv: Make VM_WRITE imply VM_READ
riscv: Allow PROT_WRITE-only mmap()

 arch/riscv/kernel/sys_riscv.c | 3 ---
 arch/riscv/mm/fault.c         | 3 ++-
 2 files changed, 2 insertions(+), 4 deletions(-)
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

PATCH v4 1/2 riscv: Make VM_WRITE imply VM_READ

RISC-V does not presently have write-only mappings as that PTE bit pattern is considered reserved in the privileged spec, so allow handling of read faults in VMAs that have VM_WRITE without VM_READ in order to be consistent with other architectures that have similar limitations.

Fixes 2139619bcad7 ("riscv: mmap with PROT_WRITE but no PROT_READ is invalid")
Cc <[email protected]> # v4.19+

---
new in v3
v3 -> v4: add Fixes tag

---
 arch/riscv/mm/fault.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/riscv/mm/fault.c +2 -1
index f2fbd1400b7c..d86f7cebd4a7 100644
@@ -184,7 +184,8 @@ static inline bool access_error(unsigned long cause, struct vm_area_struct *vma)
 		}
 		break;
 	case EXC_LOAD_PAGE_FAULT:
-		if (!(vma->vm_flags & VM_READ)) {
+		/* Write implies read */
+		if (!(vma->vm_flags & (VM_READ | VM_WRITE))) {
 			return true;
 		}
 		break;
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#2 lore raw

PATCH v4 2/2 riscv: Allow PROT_WRITE-only mmap()

In reply to Andrew Bresticker

Commit 2139619bcad7 ("riscv: mmap with PROT_WRITE but no PROT_READ is invalid") made mmap() return EINVAL if PROT_WRITE was set wihtout PROT_READ with the justification that a write-only PTE is considered a reserved PTE permission bit pattern in the privileged spec. This check is unnecessary since we let VM_WRITE imply VM_READ on RISC-V, and it is inconsistent with other architectures that don't support write-only PTEs, creating a potential software portability issue. Just remove the check altogether and let PROT_WRITE imply PROT_READ as is the case on other architectures.

Note that this also allows PROT_WRITE|PROT_EXEC mappings which were disallowed prior to the aforementioned commit; PROT_READ is implied in such mappings as well.

Fixes 2139619bcad7 ("riscv: mmap with PROT_WRITE but no PROT_READ is invalid")
Cc <[email protected]> # v4.19+

---
v1 -> v2: Update access_error() to account for write-implies-read v2 -> v3: Separate into two commits

---
 arch/riscv/kernel/sys_riscv.c | 3 ---
 1 file changed, 3 deletions(-)
arch/riscv/kernel/sys_riscv.c +0 -3
index 571556bb9261..5d3f2fbeb33c 100644
@@ -18,9 +18,6 @@ static long riscv_sys_mmap(unsigned long addr, unsigned long len,
 	if (unlikely(offset & (~PAGE_MASK >> page_shift_offset)))
 		return -EINVAL;
 
-	if (unlikely((prot & PROT_WRITE) && !(prot & PROT_READ)))
-		return -EINVAL;
-
 	return ksys_mmap_pgoff(addr, len, prot, flags, fd,
 			       offset >> (PAGE_SHIFT - page_shift_offset));
 }
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#3 lore raw
In reply to Andrew Bresticker

Hey Palmer,

Quoted 12 lines · Andrew Bresticker

Commit 2139619bcad7 ("riscv: mmap with PROT_WRITE but no PROT_READ is invalid") made mmap() reject mappings with only PROT_WRITE set in an attempt to fix an observed inconsistency in behavior when attempting to read from a PROT_WRITE-only mapping. The root cause of this behavior was actually that while RISC-V's protection_map maps VM_WRITE to readable PTE permissions (since write-only PTEs are considered reserved by the privileged spec), the page fault handler considered loads from VM_WRITE-only VMAs illegal accesses. Fix the underlying cause by handling faults in VM_WRITE-only VMAs (patch 1) and then re-enable use of mmap(PROT_WRITE) (patch 2), making RISC-V's behavior consistent with all other architectures that don't support write-only PTEs.

Quoted 4 lines

Both patches are tagged as fixes for the aforementioned commit since that commit made a userspace visible change that will break any software relying on mmap(PROT_WRITE). (Also cc: stable since the offending commit was itself backported to stable).

The patch that these commits fix has hit the distros & manifests as a userspace breakage for openJDK:
https://lore.kernel.org/linux-riscv/[email protected]/
https://lore.kernel.org/linux-riscv/[email protected]/

Eva tested these patches and reported that their problem was fixed:
https://lore.kernel.org/linux-riscv/[email protected]/

I asked them for a T-b but I don't see one on lore etc, but it would be from Eva Kotova <[email protected]> if you consider their comments their sufficient for a T-B

Thanks,
Conor.

Quoted 5 lines

v1 -> v2: Allow handling of load faults in VM_WRITE VMAs v2 -> v3: Split into two pathces
v3 -> v4: Fixes tags (+ this cover letter)

Quoted 16 lines

Andrew Bresticker (2):
riscv: Make VM_WRITE imply VM_READ
riscv: Allow PROT_WRITE-only mmap()

 arch/riscv/kernel/sys_riscv.c | 3 ---
 arch/riscv/mm/fault.c         | 3 ++-
 2 files changed, 2 insertions(+), 4 deletions(-)
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#4 lore raw
In reply to Andrew Bresticker
Quoted 21 lines · On Thu, 15 Sep 2022 12:37:00 PDT (-0700), [email protected] wrote:

Commit 2139619bcad7 ("riscv: mmap with PROT_WRITE but no PROT_READ is invalid") made mmap() reject mappings with only PROT_WRITE set in an attempt to fix an observed inconsistency in behavior when attempting to read from a PROT_WRITE-only mapping. The root cause of this behavior was actually that while RISC-V's protection_map maps VM_WRITE to readable PTE permissions (since write-only PTEs are considered reserved by the privileged spec), the page fault handler considered loads from VM_WRITE-only VMAs illegal accesses. Fix the underlying cause by handling faults in VM_WRITE-only VMAs (patch 1) and then re-enable use of mmap(PROT_WRITE) (patch 2), making RISC-V's behavior consistent with all other architectures that don't support write-only PTEs.

Both patches are tagged as fixes for the aforementioned commit since that commit made a userspace visible change that will break any software relying on mmap(PROT_WRITE). (Also cc: stable since the offending commit was itself backported to stable).

v1 -> v2: Allow handling of load faults in VM_WRITE VMAs v2 -> v3: Split into two pathces
v3 -> v4: Fixes tags (+ this cover letter)

Quoted 7 lines

Andrew Bresticker (2):
riscv: Make VM_WRITE imply VM_READ
riscv: Allow PROT_WRITE-only mmap()

 arch/riscv/kernel/sys_riscv.c | 3 ---
 arch/riscv/mm/fault.c         | 3 ++-
 2 files changed, 2 insertions(+), 4 deletions(-)

Thanks, these are on for-next.