Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2337127ybz; Thu, 23 Apr 2020 16:16:38 -0700 (PDT) X-Google-Smtp-Source: APiQypKadsIOMLkZUysDyyorVXZohrP6V2W/4IilGY5O/h3gyHSorBK0XPv2+zzqU4qSaLCJeuJn X-Received: by 2002:a17:907:2098:: with SMTP id pv24mr4879762ejb.22.1587683798755; Thu, 23 Apr 2020 16:16:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587683798; cv=none; d=google.com; s=arc-20160816; b=m9pH5voN8+mLqRdXmHgRbryGtywBfjWTxnldWgkFrcEy6a7+RLEc2r4UR+fVfJ7Yl9 XvTjhsVYe9cv2ntoNNSrWX9NBtY4+HjDgMaQ/NuQWD19DZhX7Dtb8yxOWP3g4gl1WZ33 FgDOqA3KqcEFpgT531jpdVzehyAdYfD7+5BlhJFvbtQcreYwfSoBNYD58VhJKMF9gEHG zv1UR0cBs894PP1cs5ECOXLBYWs4oAQ4Jtg1GHvY9TNmcdNDWBXr1YG1cXGAkz2jCP/6 jWneSjQKFYGpRvfXdRwk8+42luLqKU6RJgVNKuEzEkuws81eEB72sMoiXfoVNA3Gpk1C VQHw== 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:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=cwhMMo+3pFAF9I4wY6Zp19GLGbVQ4w10FwsH49resuY=; b=hxZzQc8cCfl5u8yj71gqH4b9C3cbyW7iX6qw/EbmBejWhc1S+p+u8IaQL3w9bXZVHw Mp8hOamm9mXqSasM1lp8/41f/bGKfyipqlhuo7OcGF/Y5hXFMGZNKy0FYRE8E1xy9dYd NlYe4bUFyUfVobinS9JCcG0u2mt4mreN2zdxK1q1RIDd1oUB3iO+DomNXZf9hNNv+Iq8 k9nrRRZCvgbLiP+vpz+vweIwMSsfTxp7fTY5zQhPff1HTNRQTMnKb8pOgLJm93OIZd6e QUQX7ybtizC7ze3Yk/ime//Idh7Ufncv2+RbvfS6JQxmYkW3pmwbK2sm8JWrxsBPfzMX K4Jg== 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 rv10si2033090ejb.519.2020.04.23.16.16.15; Thu, 23 Apr 2020 16:16:38 -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 S1729247AbgDWXLb (ORCPT + 99 others); Thu, 23 Apr 2020 19:11:31 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:50246 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728561AbgDWXGw (ORCPT ); Thu, 23 Apr 2020 19:06:52 -0400 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1jRkvd-0004rZ-G2; Fri, 24 Apr 2020 00:06:45 +0100 Received: from ben by deadeye with local (Exim 4.93) (envelope-from ) id 1jRkvZ-00E700-RN; Fri, 24 Apr 2020 00:06:41 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, Denis Kirjanov , "Al Viro" , syzbot+190005201ced78a74ad6@syzkaller.appspotmail.com, "Rantala, Tommi T. (Nokia - FI/Espoo)" , "Linus Torvalds" Date: Fri, 24 Apr 2020 00:07:17 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 210/245] vfs: fix do_last() regression In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.83-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Al Viro commit 6404674acd596de41fd3ad5f267b4525494a891a upstream. Brown paperbag time: fetching ->i_uid/->i_mode really should've been done from nd->inode. I even suggested that, but the reason for that has slipped through the cracks and I went for dir->d_inode instead - made for more "obvious" patch. Analysis: - at the entry into do_last() and all the way to step_into(): dir (aka nd->path.dentry) is known not to have been freed; so's nd->inode and it's equal to dir->d_inode unless we are already doomed to -ECHILD. inode of the file to get opened is not known. - after step_into(): inode of the file to get opened is known; dir might be pointing to freed memory/be negative/etc. - at the call of may_create_in_sticky(): guaranteed to be out of RCU mode; inode of the file to get opened is known and pinned; dir might be garbage. The last was the reason for the original patch. Except that at the do_last() entry we can be in RCU mode and it is possible that nd->path.dentry->d_inode has already changed under us. In that case we are going to fail with -ECHILD, but we need to be careful; nd->inode is pointing to valid struct inode and it's the same as nd->path.dentry->d_inode in "won't fail with -ECHILD" case, so we should use that. Reported-by: "Rantala, Tommi T. (Nokia - FI/Espoo)" Reported-by: syzbot+190005201ced78a74ad6@syzkaller.appspotmail.com Wearing-brown-paperbag: Al Viro Fixes: d0cb50185ae9 ("do_last(): fetch directory ->i_mode and ->i_uid before it's too late") Signed-off-by: Al Viro Signed-off-by: Linus Torvalds Signed-off-by: Ben Hutchings --- fs/namei.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/fs/namei.c +++ b/fs/namei.c @@ -2945,8 +2945,8 @@ static int do_last(struct nameidata *nd, int *opened, struct filename *name) { struct dentry *dir = nd->path.dentry; - kuid_t dir_uid = dir->d_inode->i_uid; - umode_t dir_mode = dir->d_inode->i_mode; + kuid_t dir_uid = nd->inode->i_uid; + umode_t dir_mode = nd->inode->i_mode; int open_flag = op->open_flag; bool will_truncate = (open_flag & O_TRUNC) != 0; bool got_write = false;