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=-8.2 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_MUTT autolearn=unavailable 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 9CFC0C48BE9 for ; Mon, 24 Jun 2019 11:14:01 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 75DCC208E3 for ; Mon, 24 Jun 2019 11:14:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="W6oQtfbm" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 75DCC208E3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Wasy52NGs59smC8kqYXX02lbmym2uJoNXoUPZItGyw8=; b=W6oQtfbmobGBCS EK9hoIoaRR3jAptvliS6Ebi+i5007QKKjmoOxrTs/TEqpZ28VWbLscROCoTg6IfuJt5esGruYvSx1 UTbJfA67/s6Nhj0skZUhBRGVVNFiUpKXyq63z5kNmr08NFg1fcpgegHQI2VU2TVWaTclHw55xxe1E dXJHL0rPyuhNMXijc/eAQxRw2V7AKqK4aj+McUnWWhmzYPO+2qF8fJQ96KvmqaIQ9H7NJ4hxvjh1s NfjUfk+dz2XEEBMdwp4pNy5IJgfdCMoj4tRoysSAIUZeJZNcHjFjPYNo2SBG6InpjbuBNo5svsdnx GqNkPCb4hf1DQQFGMmKQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hfMv9-0007Ci-Mk; Mon, 24 Jun 2019 11:13:59 +0000 Received: from mx2.suse.de ([195.135.220.15] helo=mx1.suse.de) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hfMv5-0007Bk-Rr for linux-mtd@lists.infradead.org; Mon, 24 Jun 2019 11:13:57 +0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id EE700AC20; Mon, 24 Jun 2019 11:13:51 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 6211D1E2F23; Mon, 24 Jun 2019 13:13:49 +0200 (CEST) Date: Mon, 24 Jun 2019 13:13:49 +0200 From: Jan Kara To: "Darrick J. Wong" Subject: Re: [PATCH 1/7] mm/fs: don't allow writes to immutable files Message-ID: <20190624111349.GF32376@quack2.suse.cz> References: <156116141046.1664939.11424021489724835645.stgit@magnolia> <156116141836.1664939.12249697737780481978.stgit@magnolia> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <156116141836.1664939.12249697737780481978.stgit@magnolia> User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190624_041356_191284_F401DC33 X-CRM114-Status: GOOD ( 23.08 ) X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-efi@vger.kernel.org, linux-btrfs@vger.kernel.org, yuchao0@huawei.com, linux-mm@kvack.org, clm@fb.com, adilger.kernel@dilger.ca, matthew.garrett@nebula.com, linux-nilfs@vger.kernel.org, linux-ext4@vger.kernel.org, devel@lists.orangefs.org, josef@toxicpanda.com, reiserfs-devel@vger.kernel.org, viro@zeniv.linux.org.uk, dsterba@suse.com, jaegeuk@kernel.org, tytso@mit.edu, ard.biesheuvel@linaro.org, linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-xfs@vger.kernel.org, jk@ozlabs.org, jack@suse.com, linux-fsdevel@vger.kernel.org, linux-mtd@lists.infradead.org, ocfs2-devel@oss.oracle.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" Errors-To: linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org On Fri 21-06-19 16:56:58, Darrick J. Wong wrote: > From: Darrick J. Wong > > The chattr manpage has this to say about immutable files: > > "A file with the 'i' attribute cannot be modified: it cannot be deleted > or renamed, no link can be created to this file, most of the file's > metadata can not be modified, and the file can not be opened in write > mode." > > Once the flag is set, it is enforced for quite a few file operations, > such as fallocate, fpunch, fzero, rm, touch, open, etc. However, we > don't check for immutability when doing a write(), a PROT_WRITE mmap(), > a truncate(), or a write to a previously established mmap. > > If a program has an open write fd to a file that the administrator > subsequently marks immutable, the program still can change the file > contents. Weird! > > The ability to write to an immutable file does not follow the manpage > promise that immutable files cannot be modified. Worse yet it's > inconsistent with the behavior of other syscalls which don't allow > modifications of immutable files. > > Therefore, add the necessary checks to make the write, mmap, and > truncate behavior consistent with what the manpage says and consistent > with other syscalls on filesystems which support IMMUTABLE. > > Signed-off-by: Darrick J. Wong Looks good to me. You can add: Reviewed-by: Jan Kara Honza > --- > fs/attr.c | 13 ++++++------- > mm/filemap.c | 3 +++ > mm/memory.c | 3 +++ > mm/mmap.c | 8 ++++++-- > 4 files changed, 18 insertions(+), 9 deletions(-) > > > diff --git a/fs/attr.c b/fs/attr.c > index d22e8187477f..1fcfdcc5b367 100644 > --- a/fs/attr.c > +++ b/fs/attr.c > @@ -233,19 +233,18 @@ int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **de > > WARN_ON_ONCE(!inode_is_locked(inode)); > > - if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { > - if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) > - return -EPERM; > - } > + if (IS_IMMUTABLE(inode)) > + return -EPERM; > + > + if ((ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) && > + IS_APPEND(inode)) > + return -EPERM; > > /* > * If utimes(2) and friends are called with times == NULL (or both > * times are UTIME_NOW), then we need to check for write permission > */ > if (ia_valid & ATTR_TOUCH) { > - if (IS_IMMUTABLE(inode)) > - return -EPERM; > - > if (!inode_owner_or_capable(inode)) { > error = inode_permission(inode, MAY_WRITE); > if (error) > diff --git a/mm/filemap.c b/mm/filemap.c > index aac71aef4c61..dad85e10f5f8 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -2935,6 +2935,9 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from) > loff_t count; > int ret; > > + if (IS_IMMUTABLE(inode)) > + return -EPERM; > + > if (!iov_iter_count(from)) > return 0; > > diff --git a/mm/memory.c b/mm/memory.c > index ddf20bd0c317..4311cfdade90 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -2235,6 +2235,9 @@ static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) > > vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; > > + if (vmf->vma->vm_file && IS_IMMUTABLE(file_inode(vmf->vma->vm_file))) > + return VM_FAULT_SIGBUS; > + > ret = vmf->vma->vm_ops->page_mkwrite(vmf); > /* Restore original flags so that caller is not surprised */ > vmf->flags = old_flags; > diff --git a/mm/mmap.c b/mm/mmap.c > index 7e8c3e8ae75f..ac1e32205237 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -1483,8 +1483,12 @@ unsigned long do_mmap(struct file *file, unsigned long addr, > case MAP_SHARED_VALIDATE: > if (flags & ~flags_mask) > return -EOPNOTSUPP; > - if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE)) > - return -EACCES; > + if (prot & PROT_WRITE) { > + if (!(file->f_mode & FMODE_WRITE)) > + return -EACCES; > + if (IS_IMMUTABLE(file_inode(file))) > + return -EPERM; > + } > > /* > * Make sure we don't allow writing to an append-only > > -- Jan Kara SUSE Labs, CR ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/