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=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 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 56054C11F67 for ; Tue, 29 Jun 2021 17:05:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40FE461CA2 for ; Tue, 29 Jun 2021 17:05:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232231AbhF2RHk (ORCPT ); Tue, 29 Jun 2021 13:07:40 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:45003 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230097AbhF2RHj (ORCPT ); Tue, 29 Jun 2021 13:07:39 -0400 Received: from 1.general.cking.uk.vpn ([10.172.193.212]) by youngberry.canonical.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1lyHAc-00022F-8e; Tue, 29 Jun 2021 17:05:10 +0000 To: Trond Myklebust Cc: Anna Schumaker , "linux-nfs@vger.kernel.org" , "linux-kernel@vger.kernel.org" From: Colin Ian King Subject: re: NFS: nfs_find_open_context() may only select open files Message-ID: <81cc22c8-051d-6826-e7e2-bd9b7e03bede@canonical.com> Date: Tue, 29 Jun 2021 18:05:09 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Static analysis on linux-next with Coverity has found a potential null pointer dereference in the following commit: commit 92735943dc6cf52aeaf2ce9aee397dee55e3ef05 Author: Trond Myklebust Date: Tue May 11 23:41:10 2021 -0400 NFS: nfs_find_open_context() may only select open files The analysis is as follows: 1113 struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode) 1114 { 1115 struct nfs_inode *nfsi = NFS_I(inode); 1. assign_zero: Assigning: ctx = NULL. 1116 struct nfs_open_context *pos, *ctx = NULL; 1117 1118 rcu_read_lock(); 2. Condition 1 /* !0 */, taking true branch. 3. Condition !rcu_read_lock_any_held(), taking true branch. 4. Condition debug_lockdep_rcu_enabled(), taking true branch. 5. Condition !__warned, taking true branch. 6. Condition 0 /* !((((sizeof (nfsi->open_files.next) == sizeof (char) || sizeof (nfsi->open_files.next) == sizeof (short)) || sizeof (nfsi->open_files.next) == sizeof (int)) || sizeof (nfsi->open_files.next) == sizeof (long)) || sizeof (nfsi->open_files.next) == sizeof (long long)) */, taking false branch. 7. Condition 0 /* !!(!__builtin_types_compatible_p() && !__builtin_types_compatible_p()) */, taking false branch. 8. Condition &pos->list != &nfsi->open_files, taking true branch. 13. Condition 0 /* !((((sizeof (pos->list.next) == sizeof (char) || sizeof (pos->list.next) == sizeof (short)) || sizeof (pos->list.next) == sizeof (int)) || sizeof (pos->list.next) == sizeof (long)) || sizeof (pos->list.next) == sizeof (long long)) */, taking false branch. 14. Condition 0 /* !!(!__builtin_types_compatible_p() && !__builtin_types_compatible_p()) */, taking false branch. 15. Condition &pos->list != &nfsi->open_files, taking true branch. 20. Condition 0 /* !((((sizeof (pos->list.next) == sizeof (char) || sizeof (pos->list.next) == sizeof (short)) || sizeof (pos->list.next) == sizeof (int)) || sizeof (pos->list.next) == sizeof (long)) || sizeof (pos->list.next) == sizeof (long long)) */, taking false branch. 21. Condition 0 /* !!(!__builtin_types_compatible_p() && !__builtin_types_compatible_p()) */, taking false branch. 22. Condition &pos->list != &nfsi->open_files, taking true branch. 1119 list_for_each_entry_rcu(pos, &nfsi->open_files, list) { 9. Condition cred != NULL, taking true branch. 10. Condition cred_fscmp(pos->cred, cred) != 0, taking false branch. 16. Condition cred != NULL, taking true branch. 17. Condition cred_fscmp(pos->cred, cred) != 0, taking false branch. 23. Condition cred != NULL, taking true branch. 24. Condition cred_fscmp(pos->cred, cred) != 0, taking false branch. 1120 if (cred != NULL && cred_fscmp(pos->cred, cred) != 0) 1121 continue; 11. Condition (pos->mode & (3U /* (fmode_t)1 | (fmode_t)2 */)) != mode, taking true branch. 18. Condition (pos->mode & (3U /* (fmode_t)1 | (fmode_t)2 */)) != mode, taking true branch. 25. Condition (pos->mode & (3U /* (fmode_t)1 | (fmode_t)2 */)) != mode, taking false branch. 1122 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode) 12. Continuing loop. 19. Continuing loop. 1123 continue; Explicit null dereferenced (FORWARD_NULL) 26. var_deref_model: Passing null pointer &ctx->flags to test_bit, which dereferences it. 1124 if (!test_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags)) 1125 continue; 1126 ctx = get_nfs_open_context(pos); 1127 if (ctx) 1128 break; 1129 } 1130 rcu_read_unlock(); 1131 return ctx; 1132 } Coverity is indicating that the test_bit call on &ctx->flags can cause a null pointer dereference when ctx is NULL. I'm not entirely convinced if this is a false positive, so I though I had better report this issue. Colin