Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp74783pxu; Wed, 14 Oct 2020 20:48:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQiVRXHe6LX8tk2xAGxjn81bBJRCYAQeSuAuS7IGVTSswVa/0NEQK7N30sLFjSD6zakOZJ X-Received: by 2002:a17:906:5618:: with SMTP id f24mr2339896ejq.86.1602733715806; Wed, 14 Oct 2020 20:48:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602733715; cv=none; d=google.com; s=arc-20160816; b=zKFcclMzEDJ18/lUWEiz92Ei8BGbJqkS0gWN2vi6xl3xqr6A6+tnM80geUD9diEUdD rJxMXQXYb+Db4lu0YHnZAD2GsjFysAeQvPYm3pzRCN7IVt8yYGmn1SuO+tAi3JYldnHl /rBCmdi8XPlHc5YgZQlCxuirR3uaKoQpzmZvVomx6SOH7uJp3rPz/vDCF41ShvudicQv sBfeMaYOFre9yb0PFo0l7znPxzxtBc9sfpXzKSQmnHqsoXrbSdEp7zmSEUmFnB4XgAKs LAjD+NB3/HIJJkuqoQsHWtQloZdSm1vuy7hEoHZ54AKdj8GgPlyCfOb98oAQa6A8jpZr 1OfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=ydk0W5+dVTHFaWMuiNwSh1JQDYQlJ1mWE/U+tW4GWl4=; b=pX1SgNTbWFohDrG2jGNdYfqamYSn3izVBg1MhBjWbcLVlyDm/uTse0T6a0hil320eI UojudOZOwTlmcYegYydOP4i6NNhj1ph81+bcQO9F7zx/gGZ9TOjN/yppzzc6BHxEzJk+ qmOoS4hfoiiX8iUTCnNDuw5nkfLCVCk0KU2UiDu7CGc7rDcgUf38nRIe24xt/wM43nQ3 wiobLS3FPCjbP+rSnz2v9aSwrBxYh49PFM5eeT4PKuW4Me4XO1k8csFhpAtSL+zWEg0C hBh4lP3WHlfMYm23d5z9nGpYqurF3JjkC/3mRxOFatQEu/KqetB1k7/+YqKMmxJI09pg FVtA== ARC-Authentication-Results: i=1; mx.google.com; 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 bh20si1208251ejb.294.2020.10.14.20.48.13; Wed, 14 Oct 2020 20:48:35 -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; 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 S1729633AbgJOBfK (ORCPT + 99 others); Wed, 14 Oct 2020 21:35:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728839AbgJOBeh (ORCPT ); Wed, 14 Oct 2020 21:34:37 -0400 Received: from ZenIV.linux.org.uk (zeniv.linux.org.uk [IPv6:2002:c35c:fd02::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBDB5C05112D; Wed, 14 Oct 2020 16:08:16 -0700 (PDT) Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kSpsS-000OAs-QS; Wed, 14 Oct 2020 23:08:12 +0000 Date: Thu, 15 Oct 2020 00:08:12 +0100 From: Al Viro To: Andrii Nakryiko Cc: linux-fsdevel@vger.kernel.org, torvalds@linux-foundation.org, kernel-team@fb.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH] fs: fix NULL dereference due to data race in prepend_path() Message-ID: <20201014230812.GK3576660@ZenIV.linux.org.uk> References: <20201014204529.934574-1-andrii@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201014204529.934574-1-andrii@kernel.org> Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 14, 2020 at 01:45:28PM -0700, Andrii Nakryiko wrote: > Fix data race in prepend_path() with re-reading mnt->mnt_ns twice without > holding the lock. is_mounted() does check for NULL, but is_anon_ns(mnt->mnt_ns) > might re-read the pointer again which could be NULL already, if in between > reads one of kern_unmount()/kern_unmount_array()/umount_tree() sets mnt->mnt_ns > to NULL. Cute... What config/compiler has resulted in that? I agree with the analysis, but I really hate the open-coded (and completely unexplained) use of IS_ERR_OR_NULL() there. > - if (is_mounted(vfsmnt) && !is_anon_ns(mnt->mnt_ns)) > + mnt_ns = READ_ONCE(mnt->mnt_ns); > + /* open-coded is_mounted() to use local mnt_ns */ > + if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns)) > error = 1; // absolute root > else > error = 2; // detached or not attached yet Better turn that into an inlined helper in fs/mount.h, next to is_mounted(), IMO, and kill that IS_ERR_OR_NULL garbage there. What that thing does is if (ns == NULL || ns == MNT_NS_INTERNAL) and it's *not* on any kind of hot path to warrant that kind of microoptimizations. So let's make that static inline bool is_real_ns(struct mnt_namespace *mnt_ns) { return mnt_ns && mnt_ns != MNT_NS_INTERNAL; } turn is_mounted(m) into is_real_ns(m->mnt_ns) and replace that line in your fix with if (is_real_ns(mnt_ns) && !is_anon_ns(mnt_ns)) Objections?