Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp448834rdg; Thu, 12 Oct 2023 10:04:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpcJMsyxg+taln9r2T84wZEuUo1qbr9jePbYEeqHWrnAFw0NL2CBoAnnLOr2EdfEk4e7Du X-Received: by 2002:a05:6a20:324d:b0:14d:7511:1c2 with SMTP id hm13-20020a056a20324d00b0014d751101c2mr23239674pzc.55.1697130292910; Thu, 12 Oct 2023 10:04:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697130292; cv=none; d=google.com; s=arc-20160816; b=Rujk7MGdQQ8NkySnVL6KDlpig4R85camHw7qMPAA3jYFIz58muIuAtlAeQBgQmVwdZ eiVeM26NWewRUPxR539cVgutNZA5hjF6W4emtxCj2vCnxLl5Snn2ahsxeBEzzxaL94IE 9vqkTR+Ikye9kewWXA0n/tJBFm+KblKjo1GeQ4XlLBGK6PKcyXBJgTKBswKUDSl4sRrm xe12xKxfjy+ygDo/Ua6NTVaxx0Tok5ZMh4JW+1Z5uzKpZBBBb74/AcPCWX9z9ed22Efl U75XM8dKtoJMUJtDw8xxbTXMlRV3I4ZJX6OX1eAIrDuDVATflUprHlNolrvML/w+3BOF XExg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=s8vXm3Rb5eaL+HF5MglErGcxxNG7EAs28QhToMLXn+Y=; fh=On8i7MftmLnii/x6szJvRZF6tmYBE7/T6K0SDqpksdA=; b=zem2Kjwj2Mwbn3lrs9TaeH9YdnTPAer85UinrvO9S1q53xeekR5uyCWuR0JWnPUfbu aRj4v8PlIl+rlSr+dIOLN31wZk+4xptSn++8S8guXbG5wAxAbzed/o98ZcwHbKKhM2Ae ZziFPenO2X5UctxNpzhAKYbgbd2OwO2y+TxxxuThTZMfBvVwcZE4/dLYC+eTtvjXWKNk DFurASHzpiSA54wnhtFWq+eShE/AkxehstpOvEI8KKSE6yugGXGAdGNQ2Xo05n7dM7Ep 8cIBvqHWJFzogWq+qZa5aQMthkioa94hYdZvezcV324XiYAzYw8FeGSOBrn+ZvGj74fx l1qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="SbOBK/93"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id f11-20020a63dc4b000000b00578da80ac3dsi2746241pgj.80.2023.10.12.10.04.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 10:04:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="SbOBK/93"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 051318251388; Thu, 12 Oct 2023 10:04:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379570AbjJLREl (ORCPT + 99 others); Thu, 12 Oct 2023 13:04:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344085AbjJLREi (ORCPT ); Thu, 12 Oct 2023 13:04:38 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39C94C0; Thu, 12 Oct 2023 10:04:36 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-405361bb9f7so12870435e9.2; Thu, 12 Oct 2023 10:04:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697130274; x=1697735074; darn=vger.kernel.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=s8vXm3Rb5eaL+HF5MglErGcxxNG7EAs28QhToMLXn+Y=; b=SbOBK/93R8uby/C8s3MGMTuyLh9015npr3m05JONHsW1aAvsHJpaFg01F3fS5lkWBV w1sodH1N+PIMms6l29KFNhflzLRN7NFvvDDbYT1A+CErJZ2PhvDA3V9SSTmua1azkQpg Z7K19KjZ2Rz00XeQ6OUPeqFLmC2ttu0XvpCv/6l/x+nd8b4BRGsXfA9/JFZwEZjzJ636 jFGhj4uj/3KlHdZsJMDCQ8U89MbjrXMkfFVOiwcWh+Qs3oYi0CZn252rB0yf+k6xBVzK /yl0KXNTzRbjP/5bXNyLoKx3L7NWi7Fsy8Ea/7n4OE4LrHhcKh3qkwbovmfli0NM/k/7 rEZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697130274; x=1697735074; 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=s8vXm3Rb5eaL+HF5MglErGcxxNG7EAs28QhToMLXn+Y=; b=cnzvxujuFG7NDyy9tmpCxPJiCZRqRkgPM5fgicUn2D8bkNjHwdlsWSepKjyDXFo7Uz jDkvzslJNjX4jrh8pO6dgnc1a1j8gfx9rUHIX/Ch7Ls55z/hrU5HhOO7nsYHoXf/WRWc Us4g3G1MGqDY/Xh9qv+ecNjH/NQuP1RaGSUvyKgfomZ0QIyNH0xO5xl04o325VQz1Y5V oRfCHHrRziLTr/DfARCIinso+lbwMFNlx9sxIhuTqsLAb1YJoV3SMNbHw03NsHlOxldY FVfCL4ibW0aqh/yPdICgxXQ1lBELWBwi+Kyu6bZJxJgTJbGf2susQ7K7sVfM3NCVt030 Qe2w== X-Gm-Message-State: AOJu0Yxj4TxCMxuw0IS9c8lchR1Vwin92UwsEC3vJXMr1PVM+4Vs73md LJm7ARaBVOkFUSJ/FCTECzM= X-Received: by 2002:a5d:458f:0:b0:321:6ff5:9256 with SMTP id p15-20020a5d458f000000b003216ff59256mr19911164wrq.58.1697130274280; Thu, 12 Oct 2023 10:04:34 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id h16-20020adffd50000000b003197869bcd7sm18875418wrs.13.2023.10.12.10.04.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 10:04:33 -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 v4 1/3] mm: drop the assumption that VM_SHARED always implies writable Date: Thu, 12 Oct 2023 18:04:28 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 12 Oct 2023 10:04:52 -0700 (PDT) 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 74d7547ffb70..bae234d18d81 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 9ef49255f1a5..9710f43a89ac 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3618,7 +3618,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 70dafc99ff1e..6214a1ab5654 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -981,7 +981,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 3ea52451623b..0041e3631f6c 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); @@ -2846,7 +2846,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; @@ -2920,7 +2920,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); @@ -2937,7 +2937,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); @@ -2985,7 +2985,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); -- 2.42.0