Skip to content

Commit 8ec396d

Browse files
lorenzo-stoakesakpm00
authored andcommitted
mm: reinstate ability to map write-sealed memfd mappings read-only
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]>
1 parent 4bbf902 commit 8ec396d

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
@@ -4101,6 +4101,37 @@ void mem_dump_obj(void *object);
41014101
static inline void mem_dump_obj(void *object) {}
41024102
#endif
41034103

4104+
static inline bool is_write_sealed(int seals)
4105+
{
4106+
return seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE);
4107+
}
4108+
4109+
/**
4110+
* is_readonly_sealed - Checks whether write-sealed but mapped read-only,
4111+
* in which case writes should be disallowing moving
4112+
* forwards.
4113+
* @seals: the seals to check
4114+
* @vm_flags: the VMA flags to check
4115+
*
4116+
* Returns whether readonly sealed, in which case writess should be disallowed
4117+
* going forward.
4118+
*/
4119+
static inline bool is_readonly_sealed(int seals, vm_flags_t vm_flags)
4120+
{
4121+
/*
4122+
* Since an F_SEAL_[FUTURE_]WRITE sealed memfd can be mapped as
4123+
* MAP_SHARED and read-only, take care to not allow mprotect to
4124+
* revert protections on such mappings. Do this only for shared
4125+
* mappings. For private mappings, don't need to mask
4126+
* VM_MAYWRITE as we still want them to be COW-writable.
4127+
*/
4128+
if (is_write_sealed(seals) &&
4129+
((vm_flags & (VM_SHARED | VM_WRITE)) == VM_SHARED))
4130+
return true;
4131+
4132+
return false;
4133+
}
4134+
41044135
/**
41054136
* seal_check_write - Check for F_SEAL_WRITE or F_SEAL_FUTURE_WRITE flags and
41064137
* handle them.
@@ -4112,24 +4143,15 @@ static inline void mem_dump_obj(void *object) {}
41124143
*/
41134144
static inline int seal_check_write(int seals, struct vm_area_struct *vma)
41144145
{
4115-
if (seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
4116-
/*
4117-
* New PROT_WRITE and MAP_SHARED mmaps are not allowed when
4118-
* write seals are active.
4119-
*/
4120-
if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
4121-
return -EPERM;
4122-
4123-
/*
4124-
* Since an F_SEAL_[FUTURE_]WRITE sealed memfd can be mapped as
4125-
* MAP_SHARED and read-only, take care to not allow mprotect to
4126-
* revert protections on such mappings. Do this only for shared
4127-
* mappings. For private mappings, don't need to mask
4128-
* VM_MAYWRITE as we still want them to be COW-writable.
4129-
*/
4130-
if (vma->vm_flags & VM_SHARED)
4131-
vm_flags_clear(vma, VM_MAYWRITE);
4132-
}
4146+
if (!is_write_sealed(seals))
4147+
return 0;
4148+
4149+
/*
4150+
* New PROT_WRITE and MAP_SHARED mmaps are not allowed when
4151+
* write seals are active.
4152+
*/
4153+
if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
4154+
return -EPERM;
41334155

41344156
return 0;
41354157
}

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)