Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2802369pxv; Mon, 12 Jul 2021 02:10:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxT8Mf/+KSSY2K1CMjskOoTB/wfx/nMkoNTT8wGBhneumCy+5TSkjGw+QzOyoJJh3dFZ+l1 X-Received: by 2002:a02:3845:: with SMTP id v5mr43605235jae.119.1626081053492; Mon, 12 Jul 2021 02:10:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626081053; cv=none; d=google.com; s=arc-20160816; b=ue4j3eld6tFkxJ9L99cEVYJgVxQ+nnsR9IulqWm77ywNNiAkgjGaVjjzWOXAxqShBJ /PCJKTn+AMRh3+3rB3skiUCiKy+8Av/kEvHvS8/IMM6ZhzBSeX2HGbKKY1MuDW1DiQFO AjtFKBNFqfIsDe4CgpVqqevSje+m4B4TxrTJO1pkLT4CI9dWa9iMEW5+imNsoYPp+wjT 5hqAx6K/iPFJ0SpwexVh5pnsOpIA933B/65RaPQh3EItGn1Mo58debwor1wdww8/G1yJ GBzgfNi9IiGb77h9jTHtmerhdZWl47CYOoZwdLjk/egGTBdZjriFKvqOSXM3BmwZcDsE aaZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:dkim-signature; bh=izU/w0ojrKIk9f0Qlt3E1Z7ABTszZ26JSf37sawJ7ik=; b=XA7k2XDCXJrALhoCirqLj9B3BYc/fokrUZAI0vmGPqI6hmIxEUjClxgnxccGuUm82T KYt6eaEGJMOxhsonklbd6tzn2tnSHq5J5ujRzSVuZAkzK4V9LVK87RM3uC05uMPN9GpE +8PJMioMRveojPCjZoYeObkYj5TZuSGN2aaCWK9peR70qV0xeLlZ6GV/SaZV2f56myOj 0OY/tbArbdfcjLCRjvmp1l89VR5Wys0cRtKA9DH32YOcmshX720Vl17C3zltz60D8YN0 u4SAyemKPvzJMp2VCzqqDgTAtd5AUVV6xyeA7WRtWsjWaKjU6hnRPTl824wHRuEu1e2L DmfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=D+uNSSCi; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h17si18042295jav.42.2021.07.12.02.10.42; Mon, 12 Jul 2021 02:10:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=D+uNSSCi; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348253AbhGLJL5 (ORCPT + 99 others); Mon, 12 Jul 2021 05:11:57 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:39144 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356380AbhGLJJQ (ORCPT ); Mon, 12 Jul 2021 05:09:16 -0400 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) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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