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=-17.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 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 51321C07E99 for ; Mon, 12 Jul 2021 09:06:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C18FA610A6 for ; Mon, 12 Jul 2021 09:06:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C18FA610A6 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C7B306B008A; Mon, 12 Jul 2021 05:06:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C2B366B008C; Mon, 12 Jul 2021 05:06:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACC338D0001; Mon, 12 Jul 2021 05:06:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0006.hostedemail.com [216.40.44.6]) by kanga.kvack.org (Postfix) with ESMTP id 76D5F6B008A for ; Mon, 12 Jul 2021 05:06:29 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 857F5184B2551 for ; Mon, 12 Jul 2021 09:06:28 +0000 (UTC) X-FDA: 78353354856.25.62F52DE Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by imf17.hostedemail.com (Postfix) with ESMTP id DBEEDF000394 for ; Mon, 12 Jul 2021 09:06:27 +0000 (UTC) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 7FAAA22130; Mon, 12 Jul 2021 09:06:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1626080786; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=izU/w0ojrKIk9f0Qlt3E1Z7ABTszZ26JSf37sawJ7ik=; b=D+uNSSCiQrN5TfkBHQ8EpfB0GYISFcZMi4KUrzK4nnlsrkuosxZPZB/f7BrdNqGCqAfR+S TJRsLO8W8cSAOqgNKwRYqpRjkfczP1G+8p0gO5hGOPLMgC4KuZ7ZqKFZBdnlpLBdpUj2zQ NhsC4YlTn7afYrHbcj/iYniNsgXrF+A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1626080786; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=izU/w0ojrKIk9f0Qlt3E1Z7ABTszZ26JSf37sawJ7ik=; b=egBeRoFLR8TB2MA2tea/XLqqMk4u1Y4sae9oo6cA7NZ/ZfM9CnNgR/IddnHet4jnaCHsBh 29h1QEhzSy/S6EDw== Received: from quack2.suse.cz (unknown [10.100.224.230]) by relay2.suse.de (Postfix) with ESMTP id 498A7A3B88; Mon, 12 Jul 2021 09:06:26 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 1B13D1E62D6; Mon, 12 Jul 2021 11:06:26 +0200 (CEST) Date: Mon, 12 Jul 2021 11:06:26 +0200 From: Jan Kara To: Hugh Dickins Cc: Andrew Morton , Al Viro , Christoph Hellwig , Willy Tarreau , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH] fs, mm: fix race in unlinking swapfile Message-ID: <20210712090626.GB27936@quack2.suse.cz> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=D+uNSSCi; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=egBeRoFL; spf=pass (imf17.hostedemail.com: domain of jack@suse.cz designates 195.135.220.28 as permitted sender) smtp.mailfrom=jack@suse.cz; dmarc=none X-Rspamd-Server: rspam05 X-Stat-Signature: koquyntc4s6ehcm74yhzzqamsp3kbzxi X-Rspamd-Queue-Id: DBEEDF000394 X-HE-Tag: 1626080787-741177 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 07-07-21 13:48:31, Hugh Dickins wrote: > We had a recurring situation in which admin procedures setting up > swapfiles would race with test preparation clearing away swapfiles; > and just occasionally that got stuck on a swapfile "(deleted)" which > could never be swapped off. That is not supposed to be possible. > > 2.6.28 commit f9454548e17c ("don't unlink an active swapfile") admitted > that it was leaving a race window open: now close it. > > may_delete() makes the IS_SWAPFILE check (amongst many others) before > inode_lock has been taken on target: now repeat just that simple check > in vfs_unlink() and vfs_rename(), after taking inode_lock. > > Which goes most of the way to fixing the race, but swapon() must also > check after it acquires inode_lock, that the file just opened has not > already been unlinked. > > Fixes: f9454548e17c ("don't unlink an active swapfile") > Signed-off-by: Hugh Dickins Looks good to me. Feel free to add: Reviewed-by: Jan Kara Honza > --- > fs/namei.c | 8 +++++++- > mm/swapfile.c | 6 ++++++ > 2 files changed, 13 insertions(+), 1 deletion(-) > > diff --git a/fs/namei.c b/fs/namei.c > index bf6d8a738c59..ff866c07f4d2 100644 > --- a/fs/namei.c > +++ b/fs/namei.c > @@ -4024,7 +4024,9 @@ int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir, > return -EPERM; > > inode_lock(target); > - if (is_local_mountpoint(dentry)) > + if (IS_SWAPFILE(target)) > + error = -EPERM; > + else if (is_local_mountpoint(dentry)) > error = -EBUSY; > else { > error = security_inode_unlink(dir, dentry); > @@ -4526,6 +4528,10 @@ int vfs_rename(struct renamedata *rd) > else if (target) > inode_lock(target); > > + error = -EPERM; > + if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target))) > + goto out; > + > error = -EBUSY; > if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) > goto out; > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 1e07d1c776f2..7527afd95284 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -3130,6 +3130,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > struct filename *name; > struct file *swap_file = NULL; > struct address_space *mapping; > + struct dentry *dentry; > int prio; > int error; > union swap_header *swap_header; > @@ -3173,6 +3174,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > > p->swap_file = swap_file; > mapping = swap_file->f_mapping; > + dentry = swap_file->f_path.dentry; > inode = mapping->host; > > error = claim_swapfile(p, inode); > @@ -3180,6 +3182,10 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > goto bad_swap; > > inode_lock(inode); > + if (d_unlinked(dentry) || cant_mount(dentry)) { > + error = -ENOENT; > + goto bad_swap_unlock_inode; > + } > if (IS_SWAPFILE(inode)) { > error = -EBUSY; > goto bad_swap_unlock_inode; > -- > 2.26.2 > -- Jan Kara SUSE Labs, CR