Skip to content

Commit ab3948f

Browse files
joelagneltorvalds
authored andcommitted
mm/memfd: add an F_SEAL_FUTURE_WRITE seal to memfd
Android uses ashmem for sharing memory regions. We are looking forward to migrating all usecases of ashmem to memfd so that we can possibly remove the ashmem driver in the future from staging while also benefiting from using memfd and contributing to it. Note staging drivers are also not ABI and generally can be removed at anytime. One of the main usecases Android has is the ability to create a region and mmap it as writeable, then add protection against making any "future" writes while keeping the existing already mmap'ed writeable-region active. This allows us to implement a usecase where receivers of the shared memory buffer can get a read-only view, while the sender continues to write to the buffer. See CursorWindow documentation in Android for more details: https://developer.android.com/reference/android/database/CursorWindow This usecase cannot be implemented with the existing F_SEAL_WRITE seal. To support the usecase, this patch adds a new F_SEAL_FUTURE_WRITE seal which prevents any future mmap and write syscalls from succeeding while keeping the existing mmap active. A better way to do F_SEAL_FUTURE_WRITE seal was discussed [1] last week where we don't need to modify core VFS structures to get the same behavior of the seal. This solves several side-effects pointed by Andy. self-tests are provided in later patch to verify the expected semantics. [1] https://lore.kernel.org/lkml/[email protected]/ Thanks a lot to Andy for suggestions to improve code. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Joel Fernandes (Google) <[email protected]> Acked-by: John Stultz <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Jann Horn <[email protected]> Cc: Al Viro <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: J. Bruce Fields <[email protected]> Cc: Jeff Layton <[email protected]> Cc: Marc-Andr Lureau <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Shuah Khan <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7f18825 commit ab3948f

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

fs/hugetlbfs/inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
530530
inode_lock(inode);
531531

532532
/* protected by i_mutex */
533-
if (info->seals & F_SEAL_WRITE) {
533+
if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
534534
inode_unlock(inode);
535535
return -EPERM;
536536
}

include/uapi/linux/fcntl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
4242
#define F_SEAL_GROW 0x0004 /* prevent file from growing */
4343
#define F_SEAL_WRITE 0x0008 /* prevent writes */
44+
#define F_SEAL_FUTURE_WRITE 0x0010 /* prevent future writes while mapped */
4445
/* (1U << 31) is reserved for signed error codes */
4546

4647
/*

mm/memfd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ static unsigned int *memfd_file_seals_ptr(struct file *file)
131131
#define F_ALL_SEALS (F_SEAL_SEAL | \
132132
F_SEAL_SHRINK | \
133133
F_SEAL_GROW | \
134-
F_SEAL_WRITE)
134+
F_SEAL_WRITE | \
135+
F_SEAL_FUTURE_WRITE)
135136

136137
static int memfd_add_seals(struct file *file, unsigned int seals)
137138
{

mm/shmem.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,24 @@ int shmem_lock(struct file *file, int lock, struct user_struct *user)
21902190

21912191
static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
21922192
{
2193+
struct shmem_inode_info *info = SHMEM_I(file_inode(file));
2194+
2195+
if (info->seals & F_SEAL_FUTURE_WRITE) {
2196+
/*
2197+
* New PROT_WRITE and MAP_SHARED mmaps are not allowed when
2198+
* "future write" seal active.
2199+
*/
2200+
if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
2201+
return -EPERM;
2202+
2203+
/*
2204+
* Since the F_SEAL_FUTURE_WRITE seals allow for a MAP_SHARED
2205+
* read-only mapping, take care to not allow mprotect to revert
2206+
* protections.
2207+
*/
2208+
vma->vm_flags &= ~(VM_MAYWRITE);
2209+
}
2210+
21932211
file_accessed(file);
21942212
vma->vm_ops = &shmem_vm_ops;
21952213
if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
@@ -2440,8 +2458,9 @@ shmem_write_begin(struct file *file, struct address_space *mapping,
24402458
pgoff_t index = pos >> PAGE_SHIFT;
24412459

24422460
/* i_mutex is held by caller */
2443-
if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
2444-
if (info->seals & F_SEAL_WRITE)
2461+
if (unlikely(info->seals & (F_SEAL_GROW |
2462+
F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) {
2463+
if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
24452464
return -EPERM;
24462465
if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
24472466
return -EPERM;
@@ -2704,7 +2723,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
27042723
DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
27052724

27062725
/* protected by i_mutex */
2707-
if (info->seals & F_SEAL_WRITE) {
2726+
if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
27082727
error = -EPERM;
27092728
goto out;
27102729
}

0 commit comments

Comments
 (0)