From patchwork Tue Jun 22 12:30:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12337183 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7CFFC48BDF for ; Tue, 22 Jun 2021 12:30:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2591D6135D for ; Tue, 22 Jun 2021 12:30:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2591D6135D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=szeredi.hu Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B2D0E6B0087; Tue, 22 Jun 2021 08:30:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADC8D6B0088; Tue, 22 Jun 2021 08:30:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A4DF6B0089; Tue, 22 Jun 2021 08:30:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0029.hostedemail.com [216.40.44.29]) by kanga.kvack.org (Postfix) with ESMTP id 6937D6B0087 for ; Tue, 22 Jun 2021 08:30:10 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 0A0D71180A for ; Tue, 22 Jun 2021 12:30:10 +0000 (UTC) X-FDA: 78281292180.22.7AA75F6 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by imf26.hostedemail.com (Postfix) with ESMTP id F35114224948 for ; Tue, 22 Jun 2021 12:30:08 +0000 (UTC) Received: by mail-wr1-f43.google.com with SMTP id a13so1653689wrf.10 for ; Tue, 22 Jun 2021 05:30:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=szeredi.hu; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=0eja4Ft9+IhKqhZ+5g9OE9fSmKDzEpLJlLWbkDAUBg0=; b=IJEDprYdBC4k0O57DwT6ozE+TtnVYsKJ8/3y72d2VukcmoPJ7ZxMb4D4Up2QXpE3VL 2zXEAGChuMmMVosYsubFpySIDJ9pAdEkd+ZJADLBhlc5P8ALKeCLmfT+hOx+I6OL5u/g 0lnx/vBdRd1laZbnF0X4MtW7WU29B4O+0O1Ug= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=0eja4Ft9+IhKqhZ+5g9OE9fSmKDzEpLJlLWbkDAUBg0=; b=tZqhSIb18yryElauPFoeGcUQxgV6ZD7+wqHYyCzinCv9i4p8qIpFwWrzCQ2AvgTAc0 HuFm0h+bsgFQ5pb4uXCux33c7bts1gsY+DUK8holX53hdU1uZyBmMWEwI64SybBlW+98 mLLxCDHjt2l2nXjS/EcVVI2DhfmG1+SbFrUAQy40gcV0NoUmsiu4CuhrrJxCdP2qypnB m1wofjw3YWVkEfhcoWj5TPo41uofmE7voVhmrEjy4/1vcBOcCNxl2boWF20DdtzMdO6p a3WTrsHfnGc45nBa0Nich5PhLdj1YvK9wZiInM5+7KTUIIP585iJgUeGJypoHSc8Nt2n GZhg== X-Gm-Message-State: AOAM532mpc/7d7p8cdykBwpUbWQL1uiRZELWfGb16FHjRmnZ/dUCvfki 8L6Tloz8n7ZRCQERNQ17WsRJRw== X-Google-Smtp-Source: ABdhPJwk+a4Um+CD2IMM4W8sfXkD+OlPlmUe6MhNapFLqM3YHogBRctyy6VBAsWvDWoXC/SzSSHN7w== X-Received: by 2002:adf:f68a:: with SMTP id v10mr4483160wrp.366.1624365007418; Tue, 22 Jun 2021 05:30:07 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (catv-86-101-169-67.catv.broadband.hu. [86.101.169.67]) by smtp.gmail.com with ESMTPSA id t9sm2400812wmq.14.2021.06.22.05.30.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Jun 2021 05:30:07 -0700 (PDT) Date: Tue, 22 Jun 2021 14:30:04 +0200 From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: Chengguang Xu , Christian =?utf-8?b?S8O2bmln?= , linux-unionfs@vger.kernel.org Subject: [PATCH] ovl: fix mmap denywrite Message-ID: MIME-Version: 1.0 Content-Disposition: inline Authentication-Results: imf26.hostedemail.com; dkim=none ("invalid DKIM record") header.d=szeredi.hu header.s=google header.b=IJEDprYd; spf=pass (imf26.hostedemail.com: domain of miklos@szeredi.hu designates 209.85.221.43 as permitted sender) smtp.mailfrom=miklos@szeredi.hu; dmarc=none X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: F35114224948 X-Stat-Signature: w9ib167x7t3i4ybiabzuocfgfk6ccspe X-HE-Tag: 1624365008-675800 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: Overlayfs did not honor positive i_writecount on realfile for VM_DENYWRITE mappings. Similarly negative i_mmap_writable counts were ignored for VM_SHARED mappings. Fix by making vma_set_file() switch the temporary counts obtained and released by mmap_region(). Reported-by: Chengguang Xu Signed-off-by: Miklos Szeredi Reported-by: Chengguang Xu Signed-off-by: Miklos Szeredi Reviewed-by: Christian König --- fs/overlayfs/file.c | 4 +++- include/linux/mm.h | 1 + mm/mmap.c | 2 +- mm/util.c | 38 +++++++++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -430,7 +430,9 @@ static int ovl_mmap(struct file *file, s if (WARN_ON(file != vma->vm_file)) return -EIO; - vma_set_file(vma, realfile); + ret = vma_set_file_checkwrite(vma, realfile); + if (ret) + return ret; old_cred = ovl_override_creds(file_inode(file)->i_sb); ret = call_mmap(vma->vm_file, vma); --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2751,6 +2751,7 @@ static inline void vma_set_page_prot(str #endif void vma_set_file(struct vm_area_struct *vma, struct file *file); +int vma_set_file_checkwrite(struct vm_area_struct *vma, struct file *file); #ifdef CONFIG_NUMA_BALANCING unsigned long change_prot_numa(struct vm_area_struct *vma, --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1809,6 +1809,7 @@ unsigned long mmap_region(struct file *f */ vma->vm_file = get_file(file); error = call_mmap(file, vma); + file = vma->vm_file; if (error) goto unmap_and_free_vma; @@ -1870,7 +1871,6 @@ unsigned long mmap_region(struct file *f if (vm_flags & VM_DENYWRITE) allow_write_access(file); } - file = vma->vm_file; out: perf_event_mmap(vma); --- a/mm/util.c +++ b/mm/util.c @@ -314,12 +314,48 @@ int vma_is_stack_for_current(struct vm_a /* * Change backing file, only valid to use during initial VMA setup. */ -void vma_set_file(struct vm_area_struct *vma, struct file *file) +int vma_set_file_checkwrite(struct vm_area_struct *vma, struct file *file) { + vm_flags_t vm_flags = vma->vm_flags; + int err = 0; + /* Changing an anonymous vma with this is illegal */ get_file(file); + + /* Get temporary denial counts on replacement */ + if (vm_flags & VM_DENYWRITE) { + err = deny_write_access(file); + if (err) + goto out_put; + } + if (vm_flags & VM_SHARED) { + err = mapping_map_writable(file->f_mapping); + if (err) + goto out_allow; + } + swap(vma->vm_file, file); + + /* Undo temporary denial counts on replaced */ + if (vm_flags & VM_SHARED) + mapping_unmap_writable(file->f_mapping); +out_allow: + if (vm_flags & VM_DENYWRITE) + allow_write_access(file); +out_put: fput(file); + return err; +} +EXPORT_SYMBOL(vma_set_file_checkwrite); + +/* + * Change backing file, only valid to use during initial VMA setup. + */ +void vma_set_file(struct vm_area_struct *vma, struct file *file) +{ + int err = vma_set_file_checkwrite(vma, file); + + WARN_ON_ONCE(err); } EXPORT_SYMBOL(vma_set_file);