From patchwork Sat Oct 7 20:50:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13412496 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F99DE95A6A for ; Sat, 7 Oct 2023 20:51:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0298C8D0012; Sat, 7 Oct 2023 16:51:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EA5188D0001; Sat, 7 Oct 2023 16:51:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA9FA8D0012; Sat, 7 Oct 2023 16:51:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B81AC8D0001 for ; Sat, 7 Oct 2023 16:51:13 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 8DDAD1CA632 for ; Sat, 7 Oct 2023 20:51:13 +0000 (UTC) X-FDA: 81319860426.16.EEB9920 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) by imf04.hostedemail.com (Postfix) with ESMTP id 9F60B4000A for ; Sat, 7 Oct 2023 20:51:11 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=c+hiYEG3; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.50 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696711871; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3Eqt+lFhGqyhgA/GIcCG4Sd1WLEsHw06nfkpMalVsW4=; b=6PjfljZiNE4+AOzqjye6fUQJvnmwoLuPZ1/qVdqes8zVBlfUcNnFcCQhuUWZllhjtolXyC i6ILSYl4io3gnadmfG9Sk8S/fk/rjaHXi62zfNw3Bt+2WB6NflORE/kk3JsnSQedOg1jEa KaMEYBmeqqp8wl/IaIRj7cbCahqZMBU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=c+hiYEG3; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.50 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696711871; a=rsa-sha256; cv=none; b=17GF58Z2S7XXWD51P/OX6eNwqk+DIw8htAWEJdkpZRoaablr7/+wPA76FIphluxPW2ctOO 06v2FbQNSSY70h17oznC6Gx/TiGrVokpLLEeGpgfvgPtd1uoALkoS3MQvMF2SePwAWACWc ao3lquBk2I2u24Rzj8l8mGVcJfgPiyE= Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-32483535e51so3238729f8f.0 for ; Sat, 07 Oct 2023 13:51:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696711870; x=1697316670; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3Eqt+lFhGqyhgA/GIcCG4Sd1WLEsHw06nfkpMalVsW4=; b=c+hiYEG3N4WWGOvIP4Kxyrmt8/c1HaCctQO/FhMV81SHN7qOq2iY89uYhiFD1uHdps YLPszkioVla1307MQDqe5c513uo/P9riL8hzwu4Ma8BmK10WiG9SdU+F+VirY1g7vEIa Ibx+QKtjYxJTWWpymI8hfZnFsRuONQKyyNodIEOXDDq6hqUMGJ94L8MTuh9kQgXXGotb wPbuoEUa7tXyHUXJDdt26bT5YRtWVFwT7bqQrM+kmJGEWJzUmE5yy8Mxcihs5VV6xrue tVDKfW8ND3dubFeP6CnODSYoO7Rbv/3MofaQGDGVuq8hxZiqb//Pd0hdWIRChcvA4QFZ f2ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696711870; x=1697316670; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3Eqt+lFhGqyhgA/GIcCG4Sd1WLEsHw06nfkpMalVsW4=; b=gFEmlBa3a54ujV5jyU3Jx6UqIpu+UZUjAUiF4cqlU3GvCvQg2KByTWKFhhmOhIWlpS Ch8L9qi/YHhTHkBYG8zgiLYN51rmt5891vHS25BknBuVSLakZZB5DsxJRBcCO29uuDO2 vtbKitf3TgDvni6PCtpZOh0wTGxFUl5e1AeehPZCeOQLMuOE64vWIHmSdCu9yjkNyXyD cotod7iPLo87ZGsof5covycti22cscx138QGDXzUrmKxoqvpyDugXK905J0446LgNhVB nqGVhDFdPJl+HnovHrIpGCd+mD/qnJH209P46SFw5bVUtjFv6QVZsfy2zwcDGJZP9wpi 6MzQ== X-Gm-Message-State: AOJu0YyL5ijgAiQk0vdOQwgbZzYsTw9Qqxjk3F0DECOupQLePnQSAXGr 9Fw5CoCCali8wYZ/D0aKnGjI3I+utiM= X-Google-Smtp-Source: AGHT+IGDGw56aAVS4Ps+h8icv4jAQgm4Hk3HtBxKyknFOFfb//OHVHdrVHz4nilvcQwrSS5yFdaaXw== X-Received: by 2002:a5d:4c8a:0:b0:313:ecd3:7167 with SMTP id z10-20020a5d4c8a000000b00313ecd37167mr9918903wrs.42.1696711869667; Sat, 07 Oct 2023 13:51:09 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id g7-20020adfe407000000b003232d122dbfsm5120550wrm.66.2023.10.07.13.51.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 13:51:08 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Matthew Wilcox , Hugh Dickins , Andy Lutomirski , Jan Kara , linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v3 1/3] mm: drop the assumption that VM_SHARED always implies writable Date: Sat, 7 Oct 2023 21:50:59 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9F60B4000A X-Stat-Signature: qg8c193yix7uf5xusa4jdqzurfdkzc5z X-HE-Tag: 1696711871-460677 X-HE-Meta: U2FsdGVkX18KnPaIy/HrywM9qww7B4X0b53sDtxgkRF8Y9zDtID7XHZn9+tC8FhAWF8TpIYa4NeX3hQgXDH06sIoF9wAKWzDzZYjSUzdcrAAPZt46FoJplP9LNhtutnCpTNt2rb8v6qY9n1c5xc69h+SIpx8WZ4EFt4L2jPRX/q6pRCcTlq4GylyFjlwyeNqQv+G3xnOxfIPICJA+vBZEC989r66Gp2t70X39W7Wb81gR7slCbKYGGatvZN/VtA/CaiYDHCV2kt7BndOLFpD8Z6cmaQSthJnV7po7DaYTSVLZEKcZma20OdE5Nb4VZi9O9YAulM6KNxud+Gfs5LjcPVTqSa0+ss0vA1hNASUiP0LjGFBvOVHbjxWoY6jI3fVWRg24cyskwCY6HidM10y5et0K5ZQmIEWw8Dzoxp6LAzxiOo7f2OxV47+pZC3p6Rzn+mhVXIPgWAhaCgz7hrkFoqr76vZ0nMWXNYaQNhe4Xoc3bu2Dpe3I+4enEr8p7n1ieygWSAG2bj1wSG3VPrINjHgj7y4XfLEGuCjyYPPyfI+15PIjIdN/R/dhU/VI01p2s2sMESGI1qAFQhuIFVYE3ReGjhI7LJGc7C8mcBpJjGJHMujUcxOc7CuFHcPU2AHAeBEgHy3i1zbD0ybecsp7mzkCfv1TpJQL6uR5+dN5XZmesFarwUysu2EXC4FvOa/t/Mv6VGXHa3xVD+ztj7xicUfrIe7NOfZqmTDDN+QmByOHdiNhkkXrs4fKxrP2jPdo4O1ocFQGHGzbFDmxxMai2GmGb4xboNP5fjynk88NCBEeYPvKS9l9yyZ8eEDNRHzvrbFHAPsXzLQlXj2Qz+4xIXoe9lghFN1eTnfezaNzIv2BisJlJ+I7VvWAmh1+XvIYLaUyI/ycwPlxj3luJlFhx0Ay8/3Kix7cneQk+8LrHvsg/DraB5MrWAfSZlFYqSO6EB/CKv1CQ2wqYqupm5 bCcQD/j+ QXo8VizpEOaaqQB23V67wy2fZhWQ9lgQ/TZ6zWcI+HMq1IB0M+VR7aJBdUfYRPb7ejt39bqNhin9Q/9L604qK3JAt6duYLvfTRLVusWMjZ7DTjPSyrnx2w9VuVo01xDrvevAYdWm0PmMH58pGcP6jcz5HU+5D7iA96w7rtcRE2BvW1p1aSkcqOFJbbis1EjdDEsT2YJeoAqm4j5IJa1yJXrUkeU72tBmteN72XuW4j2ReH6Aq9rx0Zj7RO4ltd6Yqj1pNROLh95CYs+nnJoYv8Si5MLmkIsQLJN/mi+RYGgrQy4KmWeGdS0Y/+FHCHSSeNEMnW91uRf/cwf6OPOA6gjwOpp29lMmh9z8M546LAIxzq5ajBnJlf4wpgEUeNzKxLbJUBDTfssqTw0SxdsygInnXbbViB1x9yIK4CCpz50YogPIfPuCih3xuEaAKROC4gjfJyrjpJb92+IfKnq5fTkE0kXp1ZMvg0eO910vIvnZzaf9Daz3UfyoEbV4iZ3JmOxPE1iJwksLPZoPZ9aQyEwbuC1hgFbfjC0qd X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: There is a general assumption that VMAs with the VM_SHARED flag set are writable. If the VM_MAYWRITE flag is not set, then this is simply not the case. Update those checks which affect the struct address_space->i_mmap_writable field to explicitly test for this by introducing [vma_]is_shared_maywrite() helper functions. This remains entirely conservative, as the lack of VM_MAYWRITE guarantees that the VMA cannot be written to. Suggested-by: Andy Lutomirski Signed-off-by: Lorenzo Stoakes --- include/linux/fs.h | 4 ++-- include/linux/mm.h | 11 +++++++++++ kernel/fork.c | 2 +- mm/filemap.c | 2 +- mm/madvise.c | 2 +- mm/mmap.c | 12 ++++++------ 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index 92a9c6157de1..e9c03fb00d5c 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -454,7 +454,7 @@ extern const struct address_space_operations empty_aops; * It is also used to block modification of page cache contents through * memory mappings. * @gfp_mask: Memory allocation flags to use for allocating pages. - * @i_mmap_writable: Number of VM_SHARED mappings. + * @i_mmap_writable: Number of VM_SHARED, VM_MAYWRITE mappings. * @nr_thps: Number of THPs in the pagecache (non-shmem only). * @i_mmap: Tree of private and shared mappings. * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable. @@ -557,7 +557,7 @@ static inline int mapping_mapped(struct address_space *mapping) /* * Might pages of this file have been modified in userspace? - * Note that i_mmap_writable counts all VM_SHARED vmas: do_mmap + * Note that i_mmap_writable counts all VM_SHARED, VM_MAYWRITE vmas: do_mmap * marks vma as VM_SHARED if it is shared, and the file was opened for * writing i.e. vma may be mprotected writable even if now readonly. * diff --git a/include/linux/mm.h b/include/linux/mm.h index a7b667786cde..c9e9628addc4 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -937,6 +937,17 @@ static inline bool vma_is_accessible(struct vm_area_struct *vma) return vma->vm_flags & VM_ACCESS_FLAGS; } +static inline bool is_shared_maywrite(vm_flags_t vm_flags) +{ + return (vm_flags & (VM_SHARED | VM_MAYWRITE)) == + (VM_SHARED | VM_MAYWRITE); +} + +static inline bool vma_is_shared_maywrite(struct vm_area_struct *vma) +{ + return is_shared_maywrite(vma->vm_flags); +} + static inline struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max) { diff --git a/kernel/fork.c b/kernel/fork.c index e45a4457ba83..1e6c656e0857 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -733,7 +733,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, get_file(file); i_mmap_lock_write(mapping); - if (tmp->vm_flags & VM_SHARED) + if (vma_is_shared_maywrite(tmp)) mapping_allow_writable(mapping); flush_dcache_mmap_lock(mapping); /* insert tmp into the share list, just after mpnt */ diff --git a/mm/filemap.c b/mm/filemap.c index 48cd16c54e86..ad559f94e125 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3637,7 +3637,7 @@ int generic_file_mmap(struct file *file, struct vm_area_struct *vma) */ int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) { - if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) + if (vma_is_shared_maywrite(vma)) return -EINVAL; return generic_file_mmap(file, vma); } diff --git a/mm/madvise.c b/mm/madvise.c index a4a20de50494..5c74fb645de8 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -999,7 +999,7 @@ static long madvise_remove(struct vm_area_struct *vma, return -EINVAL; } - if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE)) + if (!vma_is_shared_maywrite(vma)) return -EACCES; offset = (loff_t)(start - vma->vm_start) diff --git a/mm/mmap.c b/mm/mmap.c index 673429ee8a9e..6f6856b3267a 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -107,7 +107,7 @@ void vma_set_page_prot(struct vm_area_struct *vma) static void __remove_shared_vm_struct(struct vm_area_struct *vma, struct file *file, struct address_space *mapping) { - if (vma->vm_flags & VM_SHARED) + if (vma_is_shared_maywrite(vma)) mapping_unmap_writable(mapping); flush_dcache_mmap_lock(mapping); @@ -384,7 +384,7 @@ static unsigned long count_vma_pages_range(struct mm_struct *mm, static void __vma_link_file(struct vm_area_struct *vma, struct address_space *mapping) { - if (vma->vm_flags & VM_SHARED) + if (vma_is_shared_maywrite(vma)) mapping_allow_writable(mapping); flush_dcache_mmap_lock(mapping); @@ -2767,7 +2767,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_pgoff = pgoff; if (file) { - if (vm_flags & VM_SHARED) { + if (is_shared_maywrite(vm_flags)) { error = mapping_map_writable(file->f_mapping); if (error) goto free_vma; @@ -2842,7 +2842,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, mm->map_count++; if (vma->vm_file) { i_mmap_lock_write(vma->vm_file->f_mapping); - if (vma->vm_flags & VM_SHARED) + if (vma_is_shared_maywrite(vma)) mapping_allow_writable(vma->vm_file->f_mapping); flush_dcache_mmap_lock(vma->vm_file->f_mapping); @@ -2859,7 +2859,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, /* Once vma denies write, undo our temporary denial count */ unmap_writable: - if (file && vm_flags & VM_SHARED) + if (file && is_shared_maywrite(vm_flags)) mapping_unmap_writable(file->f_mapping); file = vma->vm_file; ksm_add_vma(vma); @@ -2907,7 +2907,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, unmap_region(mm, &vmi.mas, vma, prev, next, vma->vm_start, vma->vm_end, vma->vm_end, true); } - if (file && (vm_flags & VM_SHARED)) + if (file && is_shared_maywrite(vm_flags)) mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma); From patchwork Sat Oct 7 20:51:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13412497 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17DD2E95A62 for ; Sat, 7 Oct 2023 20:51:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58B678D0013; Sat, 7 Oct 2023 16:51:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E3908D0001; Sat, 7 Oct 2023 16:51:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BEC08D0013; Sat, 7 Oct 2023 16:51:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0D04A8D0001 for ; Sat, 7 Oct 2023 16:51:15 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C9A8BB469B for ; Sat, 7 Oct 2023 20:51:14 +0000 (UTC) X-FDA: 81319860468.10.CC8113A Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by imf29.hostedemail.com (Postfix) with ESMTP id F15AC120003 for ; Sat, 7 Oct 2023 20:51:12 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AvvUmf7r; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696711873; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OgUbWzhgbQTh2Tsrre/VRjjQqlOhd22889mYfV2Y6PM=; b=0HnDHFgU/mUz3RDKM5lx+RryB1mTShaQIO2JfPyhw7H7O3Hcp6IMnaiG8T/xHTBjdOJliu bgDBnMKB6P7eyirybbLNd29CG/rB53QbA8qaaXarb0kgsuhNc1WJG1+VuGxRvgCjhOgHdF XwZqqwm/dcqSQUjR+/q+Abpdx/Sow+M= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AvvUmf7r; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696711873; a=rsa-sha256; cv=none; b=YQ02eC0Ep3Un194ykFcyw8fw46lPsDzwePOGniqU6wF4UCudjVqzCVmt0yxFiFQ1Yh7cbA dj4GIJDiHbSPbibOQERgFlw+VhjiyjPl6muhyYNfE+EQNXtkb8886j27ZdfC5PL5IlS9S7 7JKWcSdLR/1+A8cH+V1ObBKSojJW0mE= Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-3231d6504e1so3016983f8f.2 for ; Sat, 07 Oct 2023 13:51:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696711871; x=1697316671; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OgUbWzhgbQTh2Tsrre/VRjjQqlOhd22889mYfV2Y6PM=; b=AvvUmf7rrV2/pWxViW3ezxfmXuk/yUS92gdBh1+Bxp1okJND3dPw44hRSec+9rjqYO dquar6zAvuoeXmGyz2gGTReQqvg/4tLtGQh9WznkbSCB1614u/iwrwr8GFUf9/PXj/S9 /ed07//r00pdl3j1baLhUsr5ip0QZJN5aD1P9w/QZb/H5xKYSFNSvT31JCSggOFF3zDL DUsOuiiNZsDY0XCl/fTJJ/0OgJjPRJBhD2KdLvaRG7EGnK6JFzehu2u2DDX300XbSvdo XTUvnVMD3mMU0/rL2aDU49o14JhjPpXs12H8jgPl50wWaV/ssoRJJvw0IgDltC63RI+k 9+YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696711871; x=1697316671; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OgUbWzhgbQTh2Tsrre/VRjjQqlOhd22889mYfV2Y6PM=; b=GgomrMi78az4hZl5zp6Vwn804TPt2ywnmfJlOG3CeZEZ8fbqf0i+tcNcdumM//8Mma OC2rLWbjhWYGwhZ8IrH0LqLqW7GBW1+Se7AN/5MTmwHJX6FKzGbfdUYYjCoA+h1nqgh6 E0iEW8mR5jFVzjL2LcBzfbT1wqJR1/sb/uBcEmd0bZ6DZ+z3wtszfVgN1GJS6gSDZyfS bVzyyFkbgYEX+Fxf0CiuGb5vCfAw6uEYrNcjgc8jLcJOuf6qg0FxM44m6veEWkKBmJJz +lmRWVZ3JIQTJLv3MTt9oVCAoteyjjxvNXcThBzo34i2CbS9Te0Ty0F96tcnC20V/qHX Z0XQ== X-Gm-Message-State: AOJu0Yz/KwNl9wBgzCjbeT/4U3aUN828JT+QiInBcQ4c7M+AnH9hWSsX XjEtLlCVC4uCTrAoDD/8bWBAQBRBxfY= X-Google-Smtp-Source: AGHT+IEXmITz4RMxLO9GqG7a0wyR3fIBUWZzSpv/XJYSngX65HmU/52/H9zjUYWovmJKhPLVZlC06A== X-Received: by 2002:adf:f005:0:b0:326:c623:3bbf with SMTP id j5-20020adff005000000b00326c6233bbfmr11078774wro.26.1696711871254; Sat, 07 Oct 2023 13:51:11 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id g7-20020adfe407000000b003232d122dbfsm5120550wrm.66.2023.10.07.13.51.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 13:51:10 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Matthew Wilcox , Hugh Dickins , Andy Lutomirski , Jan Kara , linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v3 2/3] mm: update memfd seal write check to include F_SEAL_WRITE Date: Sat, 7 Oct 2023 21:51:00 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: F15AC120003 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: f6a8cfat6kndqbpyrmnebyf9giutt657 X-HE-Tag: 1696711872-684908 X-HE-Meta: U2FsdGVkX1+GDm1xu4U0cSjc24EL5qXnBuf2sEny8Ufjx0G+dQ0oAqzZ6XK7FFGzWTEG1bUffoV2+VVZGfS6IG4Fn/ovjfO9Luev10oCUD5ohtEokD7use9A4N2A/qk5s1dlr4mWUR66m6BBZ3Noy/obrEIpyaOlVg9pv6yR2zeglfXkD7VvGBrUEloAFYQ2K1sCPYeOE6b0NKLgDM2a5nUwrmiBC3g/8l//Us5jg8uNbXX6txRVUkj7mHFxq/wG2J0q7HFYLOU52K/JdJyzTMDnbcFscf0B6HEXJmZpTQl6XO86dn/dZ/rLZ6RJ9WchK8X5+2Lw0KXOoXU6TP07oBT3wcV79fwhtW0jb252PtfojT/L3Er6M8HzIv4sDjHSbNWgeuZD1jjyHogwJ9eA7V0RxadO8U1VqbQh/3RHtMpQ0gK1cScGr7DqQDrJcSuPDMI4+UujzV+bImUOV/wYBVt7pReBhg4yPg1MpKXWOuV+p4lafP+Gwi6VLb9BnHqbVGaB9JaHuDuGIx8wGWmAGmVe/PTF4WPYi4MTE0TXDQKNeTtNEG4GieOc3mjA6aURylUc+awGL/iRwUw9E5GKeSmE5CJugrXnQqMXpGnYT1AhvoWfnrhKnjAFbY1fLpfk2yy9KXHQhGPZjz+6HDLvcKSMOwsgirx9LYWqn64AKqYHZeJLq4ydv4Ic2y533J4zUcQ+Z2YDDwxgdYZ9vQ7GXxqP4ovCXKTUsDC8m1WPttBGyaAUJc+Bf8LFIJegKhu98T9uFukuO+V3IOnEG9rUMyOBebtJoTIa8PVmdEP8e+fGYA3b1uBXOSGK20yMnDYWGRctDPbgkMJQaUzRpQJvUPbdFligGSpooxATgfxgjRtMp1L4IjZHAzjDun39S2iPWGLwNTHITzH3QdkRP0oOnCyhE32I75DNscZKET7p9sAz5xH+T5L0J6PCTOR8JAJ5lDBdEPn3w/P0eq3pX/T i9lXDsgR Ca20sCbKGFZ3dTRa7dXIeIozxhY+pvXMXXkUO5pEJoh/RyZwSUgN5jZsN5BkdDTN8kS5URSqxB4PLZ2xla928pORVMaE37w01qtGguGKPxYG7XHVKM4m/s7FUsW1r+kAFzlsIH2a3uaud/NEUmzRq37x6A8RaU4Y6+l2WXB3zheCQ2YXkJ9/eeOb5plgikQ9c4hcHdQCxRrqWme/zE9GPQ8lHzuN+J3UkUE7WsBobHCQ+bwsWfzxWLfZNyORoN2eVfLGxrrHGi6anIkrNT6vKue/04KwRTsu2ltEGKQM6rj8EFXsQVBTh85BxbzL2/zTV+PdRGibVgZJehDlmjw22rWTLMldyeKJqZdVHpJ8ly8i9vJ/ztV1eNWBh39e/ZT9Jf6PGHhkgEK24mhW2/NKNL+GnkzQSmzUoZKnItaLANF+UlfDbmMOS29d2Re2rgQ/gg/oxNreahcXtCHlr9gdhDYqgGfMbTycTQHJ8 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The seal_check_future_write() function is called by shmem_mmap() or hugetlbfs_file_mmap() to disallow any future writable mappings of an memfd sealed this way. The F_SEAL_WRITE flag is not checked here, as that is handled via the mapping->i_mmap_writable mechanism and so any attempt at a mapping would fail before this could be run. However we intend to change this, meaning this check can be performed for F_SEAL_WRITE mappings also. The logic here is equally applicable to both flags, so update this function to accommodate both and rename it accordingly. Signed-off-by: Lorenzo Stoakes --- fs/hugetlbfs/inode.c | 2 +- include/linux/mm.h | 15 ++++++++------- mm/shmem.c | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 06693bb1153d..5c333373dcc9 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -112,7 +112,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) vm_flags_set(vma, VM_HUGETLB | VM_DONTEXPAND); vma->vm_ops = &hugetlb_vm_ops; - ret = seal_check_future_write(info->seals, vma); + ret = seal_check_write(info->seals, vma); if (ret) return ret; diff --git a/include/linux/mm.h b/include/linux/mm.h index c9e9628addc4..51a217ed4d1b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4027,25 +4027,26 @@ static inline void mem_dump_obj(void *object) {} #endif /** - * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it + * seal_check_write - Check for F_SEAL_WRITE or F_SEAL_FUTURE_WRITE flags and + * handle them. * @seals: the seals to check * @vma: the vma to operate on * - * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on - * the vma flags. Return 0 if check pass, or <0 for errors. + * Check whether F_SEAL_WRITE or F_SEAL_FUTURE_WRITE are set; if so, do proper + * check/handling on the vma flags. Return 0 if check pass, or <0 for errors. */ -static inline int seal_check_future_write(int seals, struct vm_area_struct *vma) +static inline int seal_check_write(int seals, struct vm_area_struct *vma) { - if (seals & F_SEAL_FUTURE_WRITE) { + if (seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { /* * New PROT_WRITE and MAP_SHARED mmaps are not allowed when - * "future write" seal active. + * write seals are active. */ if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE)) return -EPERM; /* - * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as + * Since an F_SEAL_[FUTURE_]WRITE sealed memfd can be mapped as * MAP_SHARED and read-only, take care to not allow mprotect to * revert protections on such mappings. Do this only for shared * mappings. For private mappings, don't need to mask diff --git a/mm/shmem.c b/mm/shmem.c index 6503910b0f54..cab053831fea 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2405,7 +2405,7 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma) struct shmem_inode_info *info = SHMEM_I(inode); int ret; - ret = seal_check_future_write(info->seals, vma); + ret = seal_check_write(info->seals, vma); if (ret) return ret; From patchwork Sat Oct 7 20:51:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13412498 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 077C5E95A6A for ; Sat, 7 Oct 2023 20:51:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFB2E8D0014; Sat, 7 Oct 2023 16:51:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C36D58D0001; Sat, 7 Oct 2023 16:51:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C4D48D0014; Sat, 7 Oct 2023 16:51:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 877148D0001 for ; Sat, 7 Oct 2023 16:51:16 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5DEEEA02A6 for ; Sat, 7 Oct 2023 20:51:16 +0000 (UTC) X-FDA: 81319860552.30.8235DB3 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by imf19.hostedemail.com (Postfix) with ESMTP id 78CF91A0004 for ; Sat, 7 Oct 2023 20:51:14 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZboWskx5; spf=pass (imf19.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696711874; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; b=oseJuOnpTrJkQWthxd/UVI/21p++drYvPfVMtJnMKebwb1zKhWoMhKZSA2SatA88CkmVsE lCdwcz4/PA5XaFDRx7YLnwB8VQtg333bV8z2yNl/9AuAecog3qHTSn3pyxxX8IhyldCCtk Cd9grEGzsTec/2ADWysGN8AiBBmX+Qw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696711874; a=rsa-sha256; cv=none; b=x8MCCM0eNNZTN3bt8lVr7wPbWGg5HVAyo3idA15j5+m0rDTDNiggfNUHFjMq+QzX73hrJQ xXbe/uEcO+NVLU9C/gsJg4QCJkhzccaDwdyOAEtS4M07bDTBo5rF5VJXopEv90Ppik5PLj f99V1a0JzkEXznxtyk1xsP4/gAUtmVA= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZboWskx5; spf=pass (imf19.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-313e742a787so1948645f8f.1 for ; Sat, 07 Oct 2023 13:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696711873; x=1697316673; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; b=ZboWskx50rWOvbG2acGhcgPy7QEkYmPxfXmhMRqfKr8JkfrfKY/2vSK4nboARt7ToG qvHK250rNDZxbiFe7HPhMoXHbhuuO2F7m6pyG6HN/r0pTBPAFudQx2xMqTBvpfbZS3l3 YjiIDqVKQc0nYfl6pz5+fD8KZaBEFpGQm6oukguaunkzOsAyQkHiUIRmHTCZwA8GOHRg QsSOwPqWd3PuoOu1yXWf79EgNDd5RPYWumnQjf6XNotBt6YPUN/04mQso/qkn+/6yVyE SIAncfNnOQc9UVO67LPe235aOoEaNLNcmf2mkSO+8AgNd9xhIomC9/8Zfs2PVI3OCgxr nMlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696711873; x=1697316673; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; b=uqMbF7W/KZym29X7D8zgQwH6bbuH6FH5Jx+Sb0P5iM85ch8mwLkjr9ahbF8gHAYuxW gYROdKpCKc+TZbEZsImjSm6vOgvLi6zIDB5hzBiIfYhdhBa61n2GRkC90zpS1XNhZnqO HwY7z0YxGeOsXWqHtaEODSIydZ25357RYGlUo2mqrfORQe5pDeKQVYDJEu++Kqmx8lZG AS42HjgcZxnta3dYt1GyeJMXD/UGK2V2aj4D2/gQx+94yeKA08EipQkwEsF2vSEtTFoV pe4cGI1IbEZ64Nw4XQFrboq/RTqvS+Q32cB2lm5DBT7zzyuMuT+m03NLYckf2sGrRNYz jP1g== X-Gm-Message-State: AOJu0Yx8haoxG0R5RzdzieKJPwFkF8nC7XiMlMxNGQd5C3lXIcLhK4nj kbUAV9bGbUwrqVB4fRje/MfeUJuHe7k= X-Google-Smtp-Source: AGHT+IEgDkt6GkwlLJ9aqmEH7QCFmGhtBxHyfp4ayq2b9oWhbpAMnmJ+7jpgidaqVh/T1j6p5PGJPg== X-Received: by 2002:a5d:4805:0:b0:314:3369:df57 with SMTP id l5-20020a5d4805000000b003143369df57mr6970810wrq.5.1696711872784; Sat, 07 Oct 2023 13:51:12 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id g7-20020adfe407000000b003232d122dbfsm5120550wrm.66.2023.10.07.13.51.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 13:51:11 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Matthew Wilcox , Hugh Dickins , Andy Lutomirski , Jan Kara , linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v3 3/3] mm: enforce the mapping_map_writable() check after call_mmap() Date: Sat, 7 Oct 2023 21:51:01 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 78CF91A0004 X-Rspam-User: X-Stat-Signature: x7d839ff98zjinttgyqemij37ag5xox3 X-Rspamd-Server: rspam03 X-HE-Tag: 1696711874-101869 X-HE-Meta: U2FsdGVkX18bLhksPwQI521j7+aPxECG24wzrdNFdROxmQWHW/EqTykkdqP4wxxXzFHnElEY74tu85iHeieiq/NcrXKs9Jxjc418e0emW/If97s8HmrUvcW8NNgad6XVarB0rVtITEmlHN1ZtCPem98W6OgSIKGNgtUXwmpnwzXxVP+OeGs+0wtHFnvOzWhXhhqFPl6G3Hk2L5odkFhirtJuc33rsBTVvmX2DGxlTL6gFxCtNzqyvG8M/XwmMDMJflnwmuuNYlPuOL9rHzIoHH64J7QzeReZ1q8tBxbdfae757YVofTctcC60zmyhkna5UTx0J4ML/3B5dyfLYyGI/GMaLElHR80o7TO+10vW2OkTSxf8yrNnG7kBSkF3l3XBlb8lMVKAIu8vOENg+u/8++9jxZCnNRw34adhY5yGZSXS6qiipwGMyNVAUUsB8SvLskH6mliLo5EGmo7+jLOszxXnsQkhF+m7GGxFGgN2mhjAzvwAph0htv/HTTV8NvTcprqIKRWG3C/vPnfV8QJuYzHGgiKv3+a7G3DVVnmlxbd3+ppzwLJfjmhFp3E6xQTuEpDqsKx3TCv6tsxqFb8T6ZpKRrw9OqjIwuZS5dsgh9/DIdn6iwDKu+VhSSLnpzm75Zjkz5z6aJKPBLJWnU668Dy273guTR70HrwgXVHDS7MgSTx5uMXvuy6RHu5l0pLOT32uwpl66Slq0uaOzR3hEsuou/xwZv7bYUgCCNGMdZAdZhho369ryWY+VHdkS/W215bikM9trMFsav2MSbOE0EUgyzVPMtOS5QCeYf6QhSb/BT5en7SS33xJgyRmBVzO/n0M7tH0FIoay5QaoAePhQaccWZa7hITl2vsOJOjdqvCqNmn3kji06iNdEQ/uPr07cBGiRR0dYr6M1/a+x4E1lINDt8a3kyY6fdm42O3WtMI5ViBLYk8JiPG46fP1LR2b8XUtOaPOmltuFVCdA TIrv5al+ dXE4LghTtebwnKVWbs8bem7kvUWaMTSeXe+G/q9iccqEqF+NbQC65W5evgcT/HW4NmUkC0OZhPJXxyBBAuyHJufRjL/z1rUfeeS9p7gqWu1Jq4BU+glVGOtBZsmNuSEJaVcLBWJ9czj1njw+6ev5H4RtNcEettUntX+OiaWNxOsuG/mTg0uW/PX5xPqRUioNhvzfdDjd97dVZ1pPx9EPJVBeSag5Ppp/N9pluCjRpBvCmjE11vwLEBmfZ/enNFnZttP9GANdX1tGOdR84g58iwqrqBnSg8mR+LMM5P43x10zXtaR8VApwAG7LWwVxoiEGYMwgfTTMmuJ+mKMY71uYwsq6pUjOe2X7OQ2HMmOD9mke90LLHFQD7Jp28t0+kKzEACmdRkOGDO1DGPvs3Lzt6O5D+YgE/YGnEmWOKAL06GNy0ZHis/jj20okAzuKDlF1RbBdLPC85c/jIjbE2qz6faC6txhWlR8qln3RZuzZwEDx6LN2sThp5d8d5az8U71fHupXKto5cLOWvwqfYX5LdbDo0ee5NAhJnrPU X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In order for an F_SEAL_WRITE sealed memfd mapping to have an opportunity to clear VM_MAYWRITE in seal_check_write() we must be able to invoke either the shmem_mmap() or hugetlbfs_file_mmap() f_ops->mmap() handler to do so. We would otherwise fail the mapping_map_writable() check before we had the opportunity to clear VM_MAYWRITE. However, the existing logic in mmap_region() performs this check BEFORE calling call_mmap() (which invokes file->f_ops->mmap()). We must enforce this check AFTER the function call. In order to avoid any risk of breaking call_mmap() handlers which assume this will have been done first, we continue to mark the file writable first, simply deferring enforcement of it failing until afterwards. This enables mmap(..., PROT_READ, MAP_SHARED, fd, 0) mappings for memfd's sealed via F_SEAL_WRITE to succeed, whereas previously they were not permitted. Link: https://bugzilla.kernel.org/show_bug.cgi?id=217238 Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 6f6856b3267a..9fbee92aaaee 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2767,17 +2767,25 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_pgoff = pgoff; if (file) { - if (is_shared_maywrite(vm_flags)) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } + int writable_error = 0; + + if (vma_is_shared_maywrite(vma)) + writable_error = mapping_map_writable(file->f_mapping); vma->vm_file = get_file(file); error = call_mmap(file, vma); if (error) goto unmap_and_free_vma; + /* + * call_mmap() may have changed VMA flags, so retry this check + * if it failed before. + */ + if (writable_error && vma_is_shared_maywrite(vma)) { + error = writable_error; + goto close_and_free_vma; + } + /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA.