From mboxrd@z Thu Jan 1 00:00:00 1970 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.8 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 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 4F5C6C07E9E for ; Fri, 9 Jul 2021 13:48:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 71F5F613B6 for ; Fri, 9 Jul 2021 13:48:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 71F5F613B6 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 3D17D6B0072; Fri, 9 Jul 2021 09:48:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 380616B0073; Fri, 9 Jul 2021 09:48:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 223C38D0001; Fri, 9 Jul 2021 09:48:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0053.hostedemail.com [216.40.44.53]) by kanga.kvack.org (Postfix) with ESMTP id E7CC06B0072 for ; Fri, 9 Jul 2021 09:48:35 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 2685323E51 for ; Fri, 9 Jul 2021 13:48:35 +0000 (UTC) X-FDA: 78343179390.19.D8B005D Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by imf18.hostedemail.com (Postfix) with ESMTP id 3F079400208F for ; Fri, 9 Jul 2021 13:48:34 +0000 (UTC) Received: by mail-wr1-f43.google.com with SMTP id f17so12239152wrt.6 for ; Fri, 09 Jul 2021 06:48:33 -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:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=cGO7q0CzBiMwAEMQOQcpzzsMxPBtts0cw9J7gCinjOc=; b=HA28AZwk1QxU15vNuMwGLjd7BQxghhrbMgcRPGR7LdmKl0brV6wpdSic4P6ZU82lwv 68rjNJOqU2cDfpXnHZRB05MryL74ZEEJruttd8UvSfLErHq0GekH4DM1jxBeHIc8dA+4 CUYmRtRkAVsr01ziI8nW06hbl1TqNoNzdqTYQ= 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:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=cGO7q0CzBiMwAEMQOQcpzzsMxPBtts0cw9J7gCinjOc=; b=VDlySUTaZxaR9aGhHucgh8lRoKRv5p3evXMwQb1CUby6CV0rYpjirlrwYTpL/2iP/0 eNm3WckyH0Os3h0AXwbyOcpnzSyatLvfs4CtNp4VWFPVQblpj0GJ3cfbKox9LeizBMfN sWvOoT3I2gFVnqOAKL5r7V70lphBkfDI8ti7CzZuVLbR8j2E2XMGpDqvzFNjo1kmaHFt sN85aFfof47EcXVCr/eTtfrh3RxmrT8hcd0P6M+cIafu6VVhtIgsHT5HXYF4Us3vIg7j hohtW/oLoPS6DQJ5RF5HJzEvtCuUZ1RTgJVceIpV5Z9m1A0Sqw1Q0DDYZ53LkyL+CKM3 jr8w== X-Gm-Message-State: AOAM532B4kJs0f25JdcJRruXau8Ki5VOFBSI6CX0WU4HFYU1Y9on0KCv 3BxU8P206ezb40LwMPr5AtkD6g== X-Google-Smtp-Source: ABdhPJxzkKe5sDw+XtCuQ8ZW+5ymZQba3kF6FPPYqBVxtmf7h56dlf5m5EqszW9INRO9qRAxjmofDw== X-Received: by 2002:adf:e7c1:: with SMTP id e1mr43385144wrn.198.1625838512692; Fri, 09 Jul 2021 06:48:32 -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 n20sm4734437wmk.12.2021.07.09.06.48.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jul 2021 06:48:32 -0700 (PDT) Date: Fri, 9 Jul 2021 15:48:29 +0200 From: Miklos Szeredi To: Christian =?utf-8?B?S8O2bmln?= Cc: linux-fsdevel@vger.kernel.org, linux-mm , Chengguang Xu , overlayfs Subject: Re: [PATCH] ovl: fix mmap denywrite Message-ID: References: <8d9ac67c-8e97-3f53-95b8-548a8bec6358@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <8d9ac67c-8e97-3f53-95b8-548a8bec6358@amd.com> X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 3F079400208F X-Stat-Signature: zz6fpmw6z38gbd31xdn34fgts3jbhkdq Authentication-Results: imf18.hostedemail.com; dkim=none ("invalid DKIM record") header.d=szeredi.hu header.s=google header.b=HA28AZwk; dmarc=none; spf=pass (imf18.hostedemail.com: domain of miklos@szeredi.hu designates 209.85.221.43 as permitted sender) smtp.mailfrom=miklos@szeredi.hu X-HE-Tag: 1625838514-940035 Content-Transfer-Encoding: quoted-printable 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: On Wed, Jun 23, 2021 at 01:41:02PM +0200, Christian K=C3=B6nig wrote: >=20 >=20 > Am 22.06.21 um 17:10 schrieb Miklos Szeredi: > > On Tue, 22 Jun 2021 at 14:43, Christian K=C3=B6nig wrote: > > > Am 22.06.21 um 14:30 schrieb Miklos Szeredi: > > > > 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. > > > >=20 > > > > Fix by making vma_set_file() switch the temporary counts obtained= and > > > > released by mmap_region(). > > > Mhm, I don't fully understand the background but that looks like > > > something specific to overlayfs to me. > > >=20 > > > So why are you changing the common helper? > > Need to hold the temporary counts until the final ones are obtained i= n > > vma_link(), which is out of overlayfs' scope. >=20 > Ah! So basically we need to move the denial counts which mmap_region() = added > to the original file to the new one as well. That's indeed a rather goo= d > point. >=20 > Can you rather change the vma_set_file() function to return the error a= nd > add a __must_check? >=20 > I can take care fixing the users in DMA-buf and DRM subsystem. Okay, but changing to __must_check has to be the last step to avoid compi= le errors. This v2 is with __must_check commented out. Thanks, Miklos --- From: Miklos Szeredi Subject: [PATCH v2] ovl: fix mmap denywrite Overlayfs did not honor positive i_writecount on realfile for VM_DENYWRIT= E 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 --- fs/overlayfs/file.c | 4 +++- include/linux/mm.h | 2 +- mm/mmap.c | 2 +- mm/util.c | 27 ++++++++++++++++++++++++++- 4 files changed, 31 insertions(+), 4 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 !=3D vma->vm_file)) return -EIO; =20 - vma_set_file(vma, realfile); + ret =3D vma_set_file(vma, realfile); + if (ret) + return ret; =20 old_cred =3D ovl_override_creds(file_inode(file)->i_sb); ret =3D call_mmap(vma->vm_file, vma); --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2776,7 +2776,7 @@ static inline void vma_set_page_prot(str } #endif =20 -void vma_set_file(struct vm_area_struct *vma, struct file *file); +int /* __must_check */ vma_set_file(struct vm_area_struct *vma, struct f= ile *file); =20 #ifdef CONFIG_NUMA_BALANCING unsigned long change_prot_numa(struct vm_area_struct *vma, --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1807,6 +1807,7 @@ unsigned long mmap_region(struct file *f */ vma->vm_file =3D get_file(file); error =3D call_mmap(file, vma); + file =3D vma->vm_file; if (error) goto unmap_and_free_vma; =20 @@ -1868,7 +1869,6 @@ unsigned long mmap_region(struct file *f if (vm_flags & VM_DENYWRITE) allow_write_access(file); } - file =3D vma->vm_file; out: perf_event_mmap(vma); =20 --- a/mm/util.c +++ b/mm/util.c @@ -314,12 +314,37 @@ 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(struct vm_area_struct *vma, struct file *file) { + vm_flags_t vm_flags =3D vma->vm_flags; + int err =3D 0; + /* Changing an anonymous vma with this is illegal */ get_file(file); + + /* Get temporary denial counts on replacement */ + if (vm_flags & VM_DENYWRITE) { + err =3D deny_write_access(file); + if (err) + goto out_put; + } + if (vm_flags & VM_SHARED) { + err =3D 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); =20