Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7765AC433EF for ; Mon, 21 Feb 2022 17:05:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235852AbiBURFZ (ORCPT ); Mon, 21 Feb 2022 12:05:25 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:53050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231158AbiBURFY (ORCPT ); Mon, 21 Feb 2022 12:05:24 -0500 Received: from out01.mta.xmission.com (out01.mta.xmission.com [166.70.13.231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10CB7CD2 for ; Mon, 21 Feb 2022 09:05:01 -0800 (PST) Received: from in01.mta.xmission.com ([166.70.13.51]:52886) by out01.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1nMC7Q-009jh6-3I; Mon, 21 Feb 2022 10:05:00 -0700 Received: from ip68-227-174-4.om.om.cox.net ([68.227.174.4]:52582 helo=email.froward.int.ebiederm.org.xmission.com) by in01.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1nMC7O-002fv0-Ny; Mon, 21 Feb 2022 10:04:59 -0700 From: "Eric W. Biederman" To: Al Viro Cc: linux-fsdevel@vger.kernel.org, Linus Torvalds References: Date: Mon, 21 Feb 2022 11:04:32 -0600 In-Reply-To: (Al Viro's message of "Mon, 21 Feb 2022 03:02:35 +0000") Message-ID: <87v8x8b17j.fsf@email.froward.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1nMC7O-002fv0-Ny;;;mid=<87v8x8b17j.fsf@email.froward.int.ebiederm.org>;;;hst=in01.mta.xmission.com;;;ip=68.227.174.4;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1+r0TF891M5Ij36ary+2FiCC6M741uvs54= X-SA-Exim-Connect-IP: 68.227.174.4 X-SA-Exim-Mail-From: ebiederm@xmission.com Subject: Re: [RFC] umount/__detach_mounts() race X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Al Viro writes: > umount_tree() is very definitely not supposed to be called > on MNT_UMOUNT subtrees (== stuck-together fragments that got > unmounted, but not split into individual mount nodes). Refcounting > rules are different there and umount_tree() assumes that we start with > the normal ones. > > do_umount() appears to be checking for that: > > if (flags & MNT_DETACH) { > if (!list_empty(&mnt->mnt_list)) > umount_tree(mnt, UMOUNT_PROPAGATE); > retval = 0; > } else { > shrink_submounts(mnt); > retval = -EBUSY; > if (!propagate_mount_busy(mnt, 2)) { > if (!list_empty(&mnt->mnt_list)) > umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); > retval = 0; > } > } > > which would prevent umount_tree() on those - mnt_list eviction happens > for the same nodes that get MNT_UMOUNT. However, shrink_submounts() > will call umount_tree() for e.g. nfs automounts it finds on victim > mount, and if ours happens to be already unmounted, with automounts > stuck to it, we have trouble. > > It looks like something that should be impossible to hit, but... > > A: umount(2) looks the sucker up > B: rmdir(2) in another namespace (where nothing is mounted on that mountpoint) > does __detach_mounts(), which grabs namespace_sem, sees the mount A is about > to try and kill and calls umount_tree(mnt, UMOUNT_CONNECTED). Which detaches > our mount (and its children, automounts included) from the namespace it's in, > modifies their refcounts accordingly and keeps the entire thing in one > piece. > A: in do_umount() blocks on namespace_sem > B: drops namespace_sem > A: gets to the quoted code. mnt is already MNT_UMOUNT (and has empty > ->mnt_list), but it does have (equally MNT_UMOUNT) automounts under it, > etc. So shrink_submounts() finds something to umount and calls umount_tree(). > Buggered refcounts happen. > > Does anybody see a problem with the following patch? I think it looks like 2 patches. One patch to remove some list_empty checks. > - if (!list_empty(&mnt->mnt_list)) > - umount_tree(mnt, UMOUNT_PROPAGATE); > + umount_tree(mnt, UMOUNT_PROPAGATE); Another patch to add a MNT_UMOUNT condition. The MNT_UMOUNT condition should probably be checked optimistically along with MNT_FORCE in can_umount as well to be explicit. I think it is redundant with check_mnt but it would add clarity to what we are looking for, and keep people thinking about the MNT_UMOUNT races. Testing MNT_UMOUNT after the locks have been grabbed seem very reasonable. Alternately the code could call check_mnt again and lean on that test. But testing MNT_UMOUNT seems sufficient. > diff --git a/fs/namespace.c b/fs/namespace.c > index 42d4fc21263b2..1604b9d7a69d9 100644 > --- a/fs/namespace.c > +++ b/fs/namespace.c > @@ -1654,21 +1654,20 @@ static int do_umount(struct mount *mnt, int flags) > lock_mount_hash(); > > /* Recheck MNT_LOCKED with the locks held */ > + /* ... and don't go there if we'd raced and it's already unmounted */ > retval = -EINVAL; > - if (mnt->mnt.mnt_flags & MNT_LOCKED) > + if (mnt->mnt.mnt_flags & (MNT_LOCKED|MNT_UMOUNT)) > goto out; > > event++; > if (flags & MNT_DETACH) { > - if (!list_empty(&mnt->mnt_list)) > - umount_tree(mnt, UMOUNT_PROPAGATE); > + umount_tree(mnt, UMOUNT_PROPAGATE); > retval = 0; > } else { > shrink_submounts(mnt); > retval = -EBUSY; > if (!propagate_mount_busy(mnt, 2)) { > - if (!list_empty(&mnt->mnt_list)) > - umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); > + umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); > retval = 0; > } > } Eric