Skip to content

Commit 464770d

Browse files
lorenzo-stoakesgregkh
authored andcommitted
mm: reinstate ability to map write-sealed memfd mappings read-only
commit 8ec396d upstream. Patch series "mm: reinstate ability to map write-sealed memfd mappings read-only". In commit 1589789 ("mm: perform the mapping_map_writable() check after call_mmap()") (and preceding changes in the same series) it became possible to mmap() F_SEAL_WRITE sealed memfd mappings read-only. Commit 5de1950 ("mm: resolve faulty mmap_region() error path behaviour") unintentionally undid this logic by moving the mapping_map_writable() check before the shmem_mmap() hook is invoked, thereby regressing this change. This series reworks how we both permit write-sealed mappings being mapped read-only and disallow mprotect() from undoing the write-seal, fixing this regression. We also add a regression test to ensure that we do not accidentally regress this in future. Thanks to Julian Orth for reporting this regression. This patch (of 2): In commit 1589789 ("mm: perform the mapping_map_writable() check after call_mmap()") (and preceding changes in the same series) it became possible to mmap() F_SEAL_WRITE sealed memfd mappings read-only. This was previously unnecessarily disallowed, despite the man page documentation indicating that it would be, thereby limiting the usefulness of F_SEAL_WRITE logic. We fixed this by adapting logic that existed for the F_SEAL_FUTURE_WRITE seal (one which disallows future writes to the memfd) to also be used for F_SEAL_WRITE. For background - the F_SEAL_FUTURE_WRITE seal clears VM_MAYWRITE for a read-only mapping to disallow mprotect() from overriding the seal - an operation performed by seal_check_write(), invoked from shmem_mmap(), the f_op->mmap() hook used by shmem mappings. By extending this to F_SEAL_WRITE and critically - checking mapping_map_writable() to determine if we may map the memfd AFTER we invoke shmem_mmap() - the desired logic becomes possible. This is because mapping_map_writable() explicitly checks for VM_MAYWRITE, which we will have cleared. Commit 5de1950 ("mm: resolve faulty mmap_region() error path behaviour") unintentionally undid this logic by moving the mapping_map_writable() check before the shmem_mmap() hook is invoked, thereby regressing this change. We reinstate this functionality by moving the check out of shmem_mmap() and instead performing it in do_mmap() at the point at which VMA flags are being determined, which seems in any case to be a more appropriate place in which to make this determination. In order to achieve this we rework memfd seal logic to allow us access to this information using existing logic and eliminate the clearing of VM_MAYWRITE from seal_check_write() which we are performing in do_mmap() instead. Link: https://lkml.kernel.org/r/99fc35d2c62bd2e05571cf60d9f8b843c56069e0.1732804776.git.lorenzo.stoakes@oracle.com Fixes: 5de1950 ("mm: resolve faulty mmap_region() error path behaviour") Signed-off-by: Lorenzo Stoakes <[email protected]> Reported-by: Julian Orth <[email protected]> Closes: https://lore.kernel.org/all/CAHijbEUMhvJTN9Xw1GmbM266FXXv=U7s4L_Jem5x3AaPZxrYpQ@mail.gmail.com/ Cc: Jann Horn <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 58d0d02 commit 464770d

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

include/linux/memfd.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifdef CONFIG_MEMFD_CREATE
88
extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg);
99
struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx);
10+
unsigned int *memfd_file_seals_ptr(struct file *file);
1011
#else
1112
static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a)
1213
{
@@ -16,6 +17,19 @@ static inline struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
1617
{
1718
return ERR_PTR(-EINVAL);
1819
}
20+
21+
static inline unsigned int *memfd_file_seals_ptr(struct file *file)
22+
{
23+
return NULL;
24+
}
1925
#endif
2026

27+
/* Retrieve memfd seals associated with the file, if any. */
28+
static inline unsigned int memfd_file_seals(struct file *file)
29+
{
30+
unsigned int *sealsp = memfd_file_seals_ptr(file);
31+
32+
return sealsp ? *sealsp : 0;
33+
}
34+
2135
#endif /* __LINUX_MEMFD_H */

include/linux/mm.h

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,6 +4079,37 @@ void mem_dump_obj(void *object);
40794079
static inline void mem_dump_obj(void *object) {}
40804080
#endif
40814081

4082+
static inline bool is_write_sealed(int seals)
4083+
{
4084+
return seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE);
4085+
}
4086+
4087+
/**
4088+
* is_readonly_sealed - Checks whether write-sealed but mapped read-only,
4089+
* in which case writes should be disallowing moving
4090+
* forwards.
4091+
* @seals: the seals to check
4092+
* @vm_flags: the VMA flags to check
4093+
*
4094+
* Returns whether readonly sealed, in which case writess should be disallowed
4095+
* going forward.
4096+
*/
4097+
static inline bool is_readonly_sealed(int seals, vm_flags_t vm_flags)
4098+
{
4099+
/*
4100+
* Since an F_SEAL_[FUTURE_]WRITE sealed memfd can be mapped as
4101+
* MAP_SHARED and read-only, take care to not allow mprotect to
4102+
* revert protections on such mappings. Do this only for shared
4103+
* mappings. For private mappings, don't need to mask
4104+
* VM_MAYWRITE as we still want them to be COW-writable.
4105+
*/
4106+
if (is_write_sealed(seals) &&
4107+
((vm_flags & (VM_SHARED | VM_WRITE)) == VM_SHARED))
4108+
return true;
4109+
4110+
return false;
4111+
}
4112+
40824113
/**
40834114
* seal_check_write - Check for F_SEAL_WRITE or F_SEAL_FUTURE_WRITE flags and
40844115
* handle them.
@@ -4090,24 +4121,15 @@ static inline void mem_dump_obj(void *object) {}
40904121
*/
40914122
static inline int seal_check_write(int seals, struct vm_area_struct *vma)
40924123
{
4093-
if (seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
4094-
/*
4095-
* New PROT_WRITE and MAP_SHARED mmaps are not allowed when
4096-
* write seals are active.
4097-
*/
4098-
if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
4099-
return -EPERM;
4100-
4101-
/*
4102-
* Since an F_SEAL_[FUTURE_]WRITE sealed memfd can be mapped as
4103-
* MAP_SHARED and read-only, take care to not allow mprotect to
4104-
* revert protections on such mappings. Do this only for shared
4105-
* mappings. For private mappings, don't need to mask
4106-
* VM_MAYWRITE as we still want them to be COW-writable.
4107-
*/
4108-
if (vma->vm_flags & VM_SHARED)
4109-
vm_flags_clear(vma, VM_MAYWRITE);
4110-
}
4124+
if (!is_write_sealed(seals))
4125+
return 0;
4126+
4127+
/*
4128+
* New PROT_WRITE and MAP_SHARED mmaps are not allowed when
4129+
* write seals are active.
4130+
*/
4131+
if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
4132+
return -EPERM;
41114133

41124134
return 0;
41134135
}

mm/memfd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static int memfd_wait_for_pins(struct address_space *mapping)
170170
return error;
171171
}
172172

173-
static unsigned int *memfd_file_seals_ptr(struct file *file)
173+
unsigned int *memfd_file_seals_ptr(struct file *file)
174174
{
175175
if (shmem_file(file))
176176
return &SHMEM_I(file_inode(file))->seals;

mm/mmap.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <linux/oom.h>
4848
#include <linux/sched/mm.h>
4949
#include <linux/ksm.h>
50+
#include <linux/memfd.h>
5051

5152
#include <linux/uaccess.h>
5253
#include <asm/cacheflush.h>
@@ -368,6 +369,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
368369

369370
if (file) {
370371
struct inode *inode = file_inode(file);
372+
unsigned int seals = memfd_file_seals(file);
371373
unsigned long flags_mask;
372374

373375
if (!file_mmap_ok(file, inode, pgoff, len))
@@ -408,6 +410,8 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
408410
vm_flags |= VM_SHARED | VM_MAYSHARE;
409411
if (!(file->f_mode & FMODE_WRITE))
410412
vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
413+
else if (is_readonly_sealed(seals, vm_flags))
414+
vm_flags &= ~VM_MAYWRITE;
411415
fallthrough;
412416
case MAP_PRIVATE:
413417
if (!(file->f_mode & FMODE_READ))

0 commit comments

Comments
 (0)