From mboxrd@z Thu Jan 1 00:00:00 1970 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=-3.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no 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 7101AC433ED for ; Mon, 26 Apr 2021 17:30:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3795B613B2 for ; Mon, 26 Apr 2021 17:30:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236198AbhDZRba (ORCPT ); Mon, 26 Apr 2021 13:31:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235550AbhDZRb2 (ORCPT ); Mon, 26 Apr 2021 13:31:28 -0400 Received: from zeniv-ca.linux.org.uk (zeniv-ca.linux.org.uk [IPv6:2607:5300:60:148a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24328C061574; Mon, 26 Apr 2021 10:30:47 -0700 (PDT) Received: from viro by zeniv-ca.linux.org.uk with local (Exim 4.94 #2 (Red Hat Linux)) id 1lb54G-008SNi-Lj; Mon, 26 Apr 2021 17:30:44 +0000 Date: Mon, 26 Apr 2021 17:30:44 +0000 From: Al Viro To: haosdent Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, zhengyu.duan@shopee.com, Haosong Huang Subject: Re: NULL pointer dereference when access /proc/net Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 27, 2021 at 01:16:44AM +0800, haosdent wrote: > > really should not assume ->d_inode stable > > Hi, Alexander, sorry to disturb you again. Today I try to check what > `dentry->d_inode` and `nd->link_inode` looks like when `dentry` is > already been killed in `__dentry_kill`. > > ``` > nd->last.name: net/sockstat, dentry->d_lockref.count: -128, > dentry->d_inode: (nil), nd->link_inode: 0xffffffffab299966 > nd->last.name: net/sockstat, dentry->d_lockref.count: -128, > dentry->d_inode: (nil), nd->link_inode: 0xffffffffab299966 > nd->last.name: net/sockstat, dentry->d_lockref.count: -128, > dentry->d_inode: (nil), nd->link_inode: 0xffffffffab299966 > ``` > > It looks like `dentry->d_inode` could be NULL while `nd->link_inode` > is always has value. > But this make me confuse, by right `nd->link_inode` is get from > `dentry->d_inode`, right? It's sampled from there, yes. And in RCU mode there's nothing to prevent a previously positive dentry from getting negative and/or killed. ->link_inode (used to - it's gone these days) go with ->seq, which had been sampled from dentry->d_seq before fetching ->d_inode and then verified to have ->d_seq remain unchanged. That gives you "dentry used to have this inode at the time it had this d_seq", and that's what gets used to validate the sucker when we switch to non-RCU mode (look at legitimize_links()). IOW, we know that * at some point during the pathwalk that sucker had this inode * the inode won't get freed until we drop out of RCU mode * if we need to go to non-RCU (and thus grab dentry references) while we still need that inode, we will verify that nothing has happened to that link (same ->d_seq, so it still refers to the same inode) and grab dentry reference, making sure it won't go away or become negative under us. Or we'll fail (in case something _has_ happened to dentry) and repeat the entire thing in non-RCU mode.