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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32122C433EF for ; Wed, 10 Nov 2021 16:54:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1AC2961107 for ; Wed, 10 Nov 2021 16:54:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231209AbhKJQ5C (ORCPT ); Wed, 10 Nov 2021 11:57:02 -0500 Received: from outgoing-stata.csail.mit.edu ([128.30.2.210]:47904 "EHLO outgoing-stata.csail.mit.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229582AbhKJQ5C (ORCPT ); Wed, 10 Nov 2021 11:57:02 -0500 Received: from c-24-60-30-97.hsd1.ma.comcast.net ([24.60.30.97] helo=crash.local) by outgoing-stata.csail.mit.edu with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1mkqrU-000ML0-Bx; Wed, 10 Nov 2021 11:54:12 -0500 Received: from crash.local (localhost [127.0.0.1]) by crash.local (Postfix) with ESMTP id 7070012BD8AA8; Wed, 10 Nov 2021 11:54:11 -0500 (EST) To: Trond Myklebust , Anna Schumaker cc: linux-nfs@vger.kernel.org From: rtm@csail.mit.edu Reply-To: rtm@csail.mit.edu Subject: nfs_closedir can be called without preceding nfs_opendir -> crash MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Wed, 10 Nov 2021 11:54:11 -0500 Message-ID: <66112.1636563251@crash.local> Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org --=-=-= Content-Type: text/plain It's possible for nfs_closedir() to be called without filp->private_data ever having been set. This causes a crash when put_nfs_open_dir_context() calls list_del on filp->private_data->list. I have a test program that provokes this crash (with a misbehaving NFS server). Here's my partial understanding of what happens: * A program tries to create a new file "x". * The NFS v4 server replies OK to the open RPC, but includes attributes with an invalid (huge) fattr4.type. * This causes decode_attr_type() to not set NFS_ATTR_FATTR_TYPE, so the type test in nfs4_open_done() isn't executed, which would ordinarily prevent all this from happening. * But the open mostly succeeds, and allocates an inode; _nfs4_proc_open() sends another getattr RPC to get the type &c. * The server replies to this second getattr with type=NF4DIR, and nfs_fhget() sets inode->i_fop = nfs_dir_operations. But i_fop.open=nfs_diropen won't be called because this inode is stale... * The NF4DIR causes nfs_finish_open() to return EOPENSTALE, which causes do_filp_open() to retry the open. * The NFS server replies to this open with attribute type=NF4REG. * nfs_find_actor() allocates a new struct inode, since the type is different. * Meanwhile, path_openat() had called fput(file) for the original stale open, which causes a background task eventually to call ___fput, which makes the nfs_closedir() call that crashes. One way to view this is that inode->i_fop gets out of sync with file->private_data. I've attached my test program: # cc nfs_7.c # ./a.out ... [ 26.691442] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 [ 26.704718] Oops [#1] [ 26.709193] Modules linked in: [ 26.715724] CPU: 0 PID: 73 Comm: sh Not tainted 5.15.0-rc7-dirty #20 [ 26.725572] Hardware name: ucbbar,riscvemu-bare (DT) [ 26.733242] epc : __list_del_entry_valid+0x8/0xa0 [ 26.741500] ra : nfs_closedir+0x28/0x5c [ 26.748036] epc : ffffffff802ed742 ra : ffffffff80206bea sp : ffffffd00059bd60 ... [ 26.866512] status: 0000000200000121 badaddr: 0000000000000000 cause: 000000000000000d [ 26.877462] [] __list_del_entry_valid+0x8/0xa0 [ 26.887319] [] nfs_closedir+0x28/0x5c [ 26.895619] [] __fput+0x78/0x1c6 [ 26.903952] [] ____fput+0xc/0x14 [ 26.912274] [] task_work_run+0x68/0xa4 [ 26.920516] [] do_notify_resume+0x74/0x356 [ 26.930376] [] ret_from_exception+0x0/0xc --=-=-= Content-Type: application/octet-stream Content-Disposition: attachment; filename=nfs_7.c Content-Transfer-Encoding: base64 I2luY2x1ZG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GZoX2V4cGlyZV90eXBlCiAgICAg IH0gZWxzZSBpZihhID09IDMpewogICAgICAgIHB1dDY0KDApOyAvLyBjaGFuZ2UKICAgICAgfSBl bHNlIGlmKGEgPT0gNCl7CiAgICAgICAgcHV0NjQoNDA5NioxMCk7IC8vIHNpemUKICAgICAgfSBl bHNlIGlmKGEgPT0gNSl7CiAgICAgICAgcHV0MzIoMSk7IC8vIGxpbmsgc3VwcG9ydAogICAgICB9 IGVsc2UgaWYoYSA9PSA2KXsKICAgICAgICBwdXQzMigxKTsgLy8gc3ltbGluayBzdXBwb3J0CiAg ICAgIH0gZWxzZSBpZihhID09IDgpewogICAgICAgIHB1dDY0KDEpOyAvLyBmc2lkIG1ham9yCiAg ICAgICAgcHV0NjQoMSk7IC8vIGZzaWQgbWlub3IKICAgICAgfSBlbHNlIGlmKGEgPT0gMTApewog ICAgICAgIHB1dDMyKDEpOyAvLyBsZWFzZSB0aW1lCiAgICAgIH0gZWxzZSBpZihhID09IDExKXsK ICAgICAgICBwdXQzMigwKTsgLy8gcmRhdHRyX2Vycm9yCiAgICAgIH0gZWxzZSBpZihhID09IDEz KXsKICAgICAgICBwdXQzMigweGYpOyAvLyBhY2xzdXBwb3J0CiAgICAgIH0gZWxzZSBpZihhID09 IDE5KXsKICAgICAgICAvLyBmaWxlaGFuZGxlCiAgICAgICAgaW50IHhmaCA9IGZoOwogICAgICAg IGlmKHN5bV9maCkgeGZoIF49IGFhW2FhaSsrXTsKICAgICAgICBwdXRfb3BhcXVlKDQsIChjaGFy KikmeGZoKTsgLy8gZmgKICAgICAgfSBlbHNlIGlmKGEgPT0gMjApewogICAgICAgIHB1dDY0KGZo KTsgLy8gZmlsZWlkCiAgICAgIH0gZWxzZSBpZihhID09IDI3KXsKICAgICAgICBwdXQ2NCgweGZm ZmZmZmZmZmZmZik7IC8vIG1heCBmaWxlIHNpemUKICAgICAgfSBlbHNlIGlmKGEgPT0gMjgpewog ICAgICAgIHB1dDMyKDB4ZmZmZik7IC8vIG1heCBsaW5rCiAgICAgIH0gZWxzZSBpZihhID09IDI5 KXsKICAgICAgICBwdXQzMigyNTYpOyAvLyBtYXggbmFtZQogICAgICB9IGVsc2UgaWYoYSA9PSAz MCl7CiAgICAgICAgcHV0NjQoMTAqNDA5Nik7IC8vIG1heCByZWFkCiAgICAgIH0gZWxzZSBpZihh ID09IDMxKXsKICAgICAgICBwdXQ2NCgxMCo0MDk2KTsgLy8gbWF4IHdyaXRlCiAgICAgIH0gZWxz ZSBpZihhID09IDMzKXsKICAgICAgICBwdXQzMigwNzc3KTsgLy8gbW9kZQogICAgICB9IGVsc2Ug aWYoYSA9PSAzNSl7CiAgICAgICAgcHV0MzIoMyk7IC8vIG51bWxpbmtzCiAgICAgIH0gZWxzZSBp ZihhID09IDM2KXsKICAgICAgICBwdXRfb3BhcXVlKDYsICJvdGhlciIpOyAvLyBvd25lcgogICAg ICB9IGVsc2UgaWYoYSA9PSAzNyl7CiAgICAgICAgcHV0X29wYXF1ZSg2LCAib3RoZXIiKTsgLy8g b3duZXJfZ3JvdXAKICAgICAgfSBlbHNlIGlmKGEgPT0gNDEpewogICAgICAgIHB1dDMyKDEpOyAv LyByYXdkZXYgbWFqb3IKICAgICAgICBwdXQzMigxKTsgLy8gcmF3ZGV2IG1pbm9yCiAgICAgIH0g ZWxzZSBpZihhID09IDQ1KXsKICAgICAgICBwdXQ2NCg0MDk2KjEwKTsgLy8gc3BhY2UgdXNlZAog ICAgICB9IGVsc2UgaWYoYSA9PSA0Nyl7CiAgICAgICAgcHV0NjQoMCk7IC8vIHRpbWUgYWNjZXNz IHNlY29uZHMKICAgICAgICBwdXQzMigwKTsgLy8gbnNlY29uZHMKICAgICAgfSBlbHNlIGlmKGEg PT0gNTEpewogICAgICAgIHB1dDY0KDApOyAvLyB0aW1lIGRlbHRhIHNlY29uZHMKICAgICAgICBw dXQzMigwKTsgLy8gbnNlY29uZHMKICAgICAgfSBlbHNlIGlmKGEgPT0gNTIpewogICAgICAgIHB1 dDY0KDApOyAvLyB0aW1lIG1ldGFkYXRhIHNlY29uZHMKICAgICAgICBwdXQzMigwKTsgLy8gbnNl Y29uZHMKICAgICAgfSBlbHNlIGlmKGEgPT0gNTMpewogICAgICAgIHB1dDY0KDApOyAvLyB0aW1l IG1vZGlmeSBzZWNvbmRzCiAgICAgICAgcHV0MzIoMCk7IC8vIG5zZWNvbmRzCiAgICAgIH0gZWxz ZSBpZihhID09IDU1KXsKICAgICAgICBwdXQ2NCgwKTsgLy8gbW91bnRlZF9vbl9maWxlaWQgPz8/ CiAgICAgIH0gZWxzZSBpZihhID09IDYyKXsKICAgICAgICAvLyBmc19sYXlvdXRfdHlwZXMKICAg ICAgICBwdXQzMigxKTsKICAgICAgICBwdXQzMigxKTsgLy8gTEFZT1VUNF9ORlNWNF8xX0ZJTEVT CiAgICAgIH0gZWxzZSBpZihhID09IDc1KXsKICAgICAgICAvLyBGQVRUUjRfU1VQUEFUVFJfRVhD TENSRUFUCiAgICAgICAgcHV0MzIoMik7IC8vIGJpdG1hcCBsZW5ndGgKICAgICAgICBwdXQzMigw eGZmZmZmZmZmKTsKICAgICAgICBwdXQzMigweGZmZmZmZmZmKTsKICAgICAgfSBlbHNlIHsKICAg ICAgICBwcmludGYoInVua25vd24gcmVxdWVzdGVkIGF0dHIgJWRcbiIsIGEpOwogICAgICAgIHB1 dDY0KDApOyAvLyBYWFgKICAgICAgfQogICAgfQogIH0KICAqKGludCopKG9idWYrbGVuaSkgPSBo dG9ubChvaSAtIGxlbmkgLSA0KTsKfQoKdm9pZApwYXJzZV9vcF9nZXRhdHRyKCkKewogIGludCBi aXR3b3JkcyA9IHBhcnNlMzIoKTsKICBpbnQgd29yZHNbNjRdOwogIGZvcihpbnQgaSA9IDA7IGkg PCBiaXR3b3JkczsgaSsrKQogICAgd29yZHNbaV0gPSBwYXJzZTMyKCk7CiAgcHV0MzIoMCk7IC8v IE9LCiAgcHV0X2ZhdHRyNChiaXR3b3Jkcywgd29yZHMsIGN1cnJlbnRfZm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FkbihzMSwgJmlsZW4sIDQpIDwgMCkgYnJlYWs7CiAgICAgIGls ZW4gPSBudG9obChpbGVuKTsKICAgICAgaWxlbiAmPSAweDdmZmZmZmZmOwogICAgICBpZihyZWFk bihzMSwgaWJ1ZiwgaWxlbikgPCAwKSBicmVhazsKICAgICAgb2kgPSBpaSA9IDA7CiAgICAgIHB1 dDMyKDApOyAvLyBwbGFjZS1ob2xkZXIgZm9yIGxlbmd0aAogICAgICBwYXJzZV9ycGMoKTsKICAg ICAgKihpbnQqKShvYnVmKzApID0gaHRvbmwoKG9pIC0gNCkgfCAweDgwMDAwMDAwKTsKICAgICAg aWYoYWFpID4gTkFBKXsKICAgICAgICBwcmludGYoIm9vcHMgYWFpICVkIE5BQSAlZFxuIiwgYWFp LCBOQUEpOwogICAgICAgIGV4aXQoMSk7CiAgICAgIH0KICAgICAgaWYoc3ltc3RhcnQgIT0gLTEp ewogICAgICAgIGZvcihpbnQgaSA9IHN5bXN0YXJ0OyBpIDwgb2kgJiYgaSA8IHN5bWVuZCAmJiBh YWkgPCBOQUE7IGkgKz0gOCkKICAgICAgICAgICoodW5zaWduZWQgbG9uZyBsb25nICopKG9idWYg KyBpKSBePSBhYVthYWkrK107CiAgICAgICAgc3ltc3RhcnQgPSAtMTsKICAgICAgfQogICAgICBp Zih3cml0ZShzMSwgb2J1Ziwgb2kpPD0wKSBwZXJyb3IoIndyaXRlIik7CiAgICB9CiAgICBleGl0 KDEpOwogIH0KICBjbG9zZShzKTsKICBzbGVlcCgxNSk7Cn0K --=-=-=--