From patchwork Mon Mar 13 22:12:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 13173382 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80A10C7618D for ; Mon, 13 Mar 2023 22:15:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229875AbjCMWPA (ORCPT ); Mon, 13 Mar 2023 18:15:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229743AbjCMWO7 (ORCPT ); Mon, 13 Mar 2023 18:14:59 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67603637E5; Mon, 13 Mar 2023 15:14:58 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 17A5AB81200; Mon, 13 Mar 2023 22:14:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A3C12C4339B; Mon, 13 Mar 2023 22:14:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678745695; bh=zvmdnOguTfDqdiHnoYELVOIodEHEhZGnF56sAJiM7Q4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CzVPS94eSnz40BVL80XJ1ikdaJ5Q9eWNwhszlVJVAMSTvhEVj+jtUZvZfykglblFl Z77i8g2BTkuZiMfLtiu+7kOSf7SYLWhQ3xYZiKFe1UrKzGHt9Je5GOq2alB+j2R9iy 4r6AREpNy87oiQfk+vysEfaaMCOtJ98YY70aCZGuEemnQ1oy07jVu+5AMo5KBu42pn J5VerWN7idgs1bfmM8AaHJgcLqP+i1ousYohJ4L2EDPT+gv7HxgpZl8H5nJE2ICm3r J3nfN16DQ0BjxNkZXQuAPtiFpX6Y8h2HIX0Nfkwv6w+oa7HXbN2tbY5cj2qD31hBCo uDDMGkQnX8DGg== From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org, stable@vger.kernel.org, syzbot+93e495f6a4f748827c88@syzkaller.appspotmail.com Subject: [PATCH 1/3] fscrypt: destroy keyring after security_sb_delete() Date: Mon, 13 Mar 2023 15:12:29 -0700 Message-Id: <20230313221231.272498-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230313221231.272498-1-ebiggers@kernel.org> References: <20230313221231.272498-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Eric Biggers fscrypt_destroy_keyring() must be called after all potentially-encrypted inodes were evicted; otherwise it cannot safely destroy the keyring. Since inodes that are in-use by the Landlock LSM don't get evicted until security_sb_delete(), this means that fscrypt_destroy_keyring() must be called *after* security_sb_delete(). This fixes a WARN_ON followed by a NULL dereference, only possible if Landlock was being used on encrypted files. Fixes: d7e7b9af104c ("fscrypt: stop using keyrings subsystem for fscrypt_master_key") Cc: stable@vger.kernel.org Reported-by: syzbot+93e495f6a4f748827c88@syzkaller.appspotmail.com Link: https://lore.kernel.org/r/00000000000044651705f6ca1e30@google.com Signed-off-by: Eric Biggers Reviewed-by: Christian Brauner --- fs/super.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/fs/super.c b/fs/super.c index 84332d5cb817a..04bc62ab7dfea 100644 --- a/fs/super.c +++ b/fs/super.c @@ -475,13 +475,22 @@ void generic_shutdown_super(struct super_block *sb) cgroup_writeback_umount(); - /* evict all inodes with zero refcount */ + /* Evict all inodes with zero refcount. */ evict_inodes(sb); - /* only nonzero refcount inodes can have marks */ + + /* + * Clean up and evict any inodes that still have references due + * to fsnotify or the security policy. + */ fsnotify_sb_delete(sb); - fscrypt_destroy_keyring(sb); security_sb_delete(sb); + /* + * Now that all potentially-encrypted inodes have been evicted, + * the fscrypt keyring can be destroyed. + */ + fscrypt_destroy_keyring(sb); + if (sb->s_dio_done_wq) { destroy_workqueue(sb->s_dio_done_wq); sb->s_dio_done_wq = NULL; From patchwork Mon Mar 13 22:12:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 13173383 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F04BEC76195 for ; Mon, 13 Mar 2023 22:15:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229996AbjCMWPB (ORCPT ); Mon, 13 Mar 2023 18:15:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229743AbjCMWPB (ORCPT ); Mon, 13 Mar 2023 18:15:01 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0FA566D30; Mon, 13 Mar 2023 15:14:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id DF4A2CE1155; Mon, 13 Mar 2023 22:14:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F1CB8C433EF; Mon, 13 Mar 2023 22:14:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678745696; bh=xsrnPqp48cSYbDpR2xQdPUoe1QfIQR1l2DoWMI93/3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h5so0pJORZZ/pyCGqikZCM/k9xAq/zJJlrDv0NetzjaqtDW3DGQTi38Nmek69gk3B SPIcrEnK2oRkHpO6Qd0b6LkLnRtTEj9DeJvcvX/7bmk6K9oulFdO+G3a5jmH/JRAEr xTB8HRmedl7Q5S/oURdm0YmZxXFKS0Fidow+86BnHZT0Kvux8LK3wIBiY1wgkp0vM7 V2zwPwY/vVh2y3iGL1qodiZj+yOXKhhaE8wuIHKAcPb/MCSepWC9zNC5H0wonxWqJQ vLGic1rsLhxBGol4s7L+2ye7fjjVxoAi78yiFGdmXf0suk4+OmXFgMIILeUnzyQDJO SGtoPLUCHKh/A== From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 2/3] fscrypt: improve fscrypt_destroy_keyring() documentation Date: Mon, 13 Mar 2023 15:12:30 -0700 Message-Id: <20230313221231.272498-3-ebiggers@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230313221231.272498-1-ebiggers@kernel.org> References: <20230313221231.272498-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Eric Biggers Document that it must be called after all potentially-encrypted inodes have been evicted. Signed-off-by: Eric Biggers --- fs/crypto/keyring.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/fs/crypto/keyring.c b/fs/crypto/keyring.c index 78086f8dbda52..bb15709ac9a40 100644 --- a/fs/crypto/keyring.c +++ b/fs/crypto/keyring.c @@ -207,10 +207,11 @@ static int allocate_filesystem_keyring(struct super_block *sb) * Release all encryption keys that have been added to the filesystem, along * with the keyring that contains them. * - * This is called at unmount time. The filesystem's underlying block device(s) - * are still available at this time; this is important because after user file - * accesses have been allowed, this function may need to evict keys from the - * keyslots of an inline crypto engine, which requires the block device(s). + * This is called at unmount time, after all potentially-encrypted inodes have + * been evicted. The filesystem's underlying block device(s) are still + * available at this time; this is important because after user file accesses + * have been allowed, this function may need to evict keys from the keyslots of + * an inline crypto engine, which requires the block device(s). */ void fscrypt_destroy_keyring(struct super_block *sb) { @@ -227,12 +228,12 @@ void fscrypt_destroy_keyring(struct super_block *sb) hlist_for_each_entry_safe(mk, tmp, bucket, mk_node) { /* - * Since all inodes were already evicted, every key - * remaining in the keyring should have an empty inode - * list, and should only still be in the keyring due to - * the single active ref associated with ->mk_secret. - * There should be no structural refs beyond the one - * associated with the active ref. + * Since all potentially-encrypted inodes were already + * evicted, every key remaining in the keyring should + * have an empty inode list, and should only still be in + * the keyring due to the single active ref associated + * with ->mk_secret. There should be no structural refs + * beyond the one associated with the active ref. */ WARN_ON(refcount_read(&mk->mk_active_refs) != 1); WARN_ON(refcount_read(&mk->mk_struct_refs) != 1); From patchwork Mon Mar 13 22:12:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 13173384 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 867BDC6FD19 for ; Mon, 13 Mar 2023 22:15:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229899AbjCMWPD (ORCPT ); Mon, 13 Mar 2023 18:15:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229880AbjCMWPB (ORCPT ); Mon, 13 Mar 2023 18:15:01 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A32DD67034; Mon, 13 Mar 2023 15:14:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 23BCFCE125A; Mon, 13 Mar 2023 22:14:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E58BC4339C; Mon, 13 Mar 2023 22:14:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678745696; bh=vw79fexAzk+yE0svCHr1v/BgnaY2Tly6xl5VXbAiZhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LK6JKHs6m9sUCerh7e5n//h20BLNUyW/1NH6LLDt4oCBFJ18lLsJAtst3HYsL6uXH fNmdLHDgNVj0nU9wLa9f5lq+1yWgl9nr3s6e5KCpGMSPJw+gvw9fKtaUIkEEhbAFfD ABqnLHHxLNzjRHeOD9721NfZVDsvuyZno0deDky7oPxSiSgIEldJxEnhTlv1+HQX1u XGqVm4Hn1t9DSYxR8gGod3Sk66w1ZkGAhGkPsZzG6lmBDXef8T2WzKQfty5L3HrQyg 54gJyMdwcu3Vd3+MxjXkP3mhABw/jpoOxrm0YhpS09XBWO4HerjCUgard6W4OaTDOs fCNnL60d9i01g== From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 3/3] fscrypt: check for NULL keyring in fscrypt_put_master_key_activeref() Date: Mon, 13 Mar 2023 15:12:31 -0700 Message-Id: <20230313221231.272498-4-ebiggers@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230313221231.272498-1-ebiggers@kernel.org> References: <20230313221231.272498-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Eric Biggers It is a bug for fscrypt_put_master_key_activeref() to see a NULL keyring. But it used to be possible due to the bug, now fixed, where fscrypt_destroy_keyring() was called before security_sb_delete(). To be consistent with how fscrypt_destroy_keyring() uses WARN_ON for the same issue, WARN and leak the fscrypt_master_key if the keyring is NULL instead of dereferencing the NULL pointer. This is a robustness improvement, not a fix. Signed-off-by: Eric Biggers --- fs/crypto/keyring.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/crypto/keyring.c b/fs/crypto/keyring.c index bb15709ac9a40..13d336a6cc5da 100644 --- a/fs/crypto/keyring.c +++ b/fs/crypto/keyring.c @@ -92,6 +92,8 @@ void fscrypt_put_master_key_activeref(struct super_block *sb, * destroying any subkeys embedded in it. */ + if (WARN_ON(!sb->s_master_keys)) + return; spin_lock(&sb->s_master_keys->lock); hlist_del_rcu(&mk->mk_node); spin_unlock(&sb->s_master_keys->lock);