From patchwork Fri Mar 4 16:14:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Luis Henriques X-Patchwork-Id: 12769418 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 48C84C433FE for ; Fri, 4 Mar 2022 16:13:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240656AbiCDQOk (ORCPT ); Fri, 4 Mar 2022 11:14:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233175AbiCDQOj (ORCPT ); Fri, 4 Mar 2022 11:14:39 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FBBA15721B; Fri, 4 Mar 2022 08:13:51 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 0E42C2113B; Fri, 4 Mar 2022 16:13:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1646410430; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2i3AYvAIrrLAz95RlVXW6R4/R4Sbs8yHQrijZ34iSgc=; b=AwGhhe4mSRmtgOqAPc7fMj+CmV/osuW6LDMZE7EO2GA8wlXq4o13/FqTWRK//v02rUEMh7 RNEOKkdciOz2YEbkNCe3Hx2HxVf4AieVhOd9COx7VxGB4Z0WxnJbyfjrmdM4izIJNkZDy6 KvRz4+V4Dy2mbRLYhYJWg+S4c0Ifii0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1646410430; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2i3AYvAIrrLAz95RlVXW6R4/R4Sbs8yHQrijZ34iSgc=; b=MWkcjt9wUo7c3YglXgAytNenhUmIQ/zXfq63zYf1tSG4uEEssYvjMtr1ljuSaU/MgKngwE sLUsFj2pWvBHZaAA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 9155A13B64; Fri, 4 Mar 2022 16:13:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id qOWhIL06ImKeRAAAMHmgww (envelope-from ); Fri, 04 Mar 2022 16:13:49 +0000 Received: from localhost (brahms.olymp [local]) by brahms.olymp (OpenSMTPD) with ESMTPA id 378d5cce; Fri, 4 Mar 2022 16:14:04 +0000 (UTC) From: =?utf-8?q?Lu=C3=ADs_Henriques?= To: Jeff Layton , Xiubo Li , Ilya Dryomov Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Lu?= =?utf-8?q?=C3=ADs_Henriques?= Subject: [PATCH 1/3] ceph: fix error path in ceph_readdir() Date: Fri, 4 Mar 2022 16:14:01 +0000 Message-Id: <20220304161403.19295-2-lhenriques@suse.de> In-Reply-To: <20220304161403.19295-1-lhenriques@suse.de> References: <20220304161403.19295-1-lhenriques@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org This was introduced by commit "ceph: add support to readdir for encrypted filenames" It will eventually leak the fscrypt_str names in this error path. Signed-off-by: Luís Henriques --- fs/ceph/dir.c | 1 - 1 file changed, 1 deletion(-) diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c index 44395aae7259..0bcb677d2199 100644 --- a/fs/ceph/dir.c +++ b/fs/ceph/dir.c @@ -500,7 +500,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) next_offset); if (err) { ceph_mdsc_put_request(dfi->last_readdir); - return err; goto out; } } else if (req->r_reply_info.dir_end) { From patchwork Fri Mar 4 16:14:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Luis Henriques X-Patchwork-Id: 12769420 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 9BD08C4332F for ; Fri, 4 Mar 2022 16:13:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240670AbiCDQOm (ORCPT ); Fri, 4 Mar 2022 11:14:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240183AbiCDQOj (ORCPT ); Fri, 4 Mar 2022 11:14:39 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4F66157207; Fri, 4 Mar 2022 08:13:51 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 876C01F38A; Fri, 4 Mar 2022 16:13:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1646410430; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+3iB4IVSxvoxrgPRlS4xZegvmXLYHfk7btvgwcWOpvg=; b=d2jF9WrhG5m83dax3NIdFlMlkxldqtyWg/OF7qKs/faW68IEoGLltHId6bZgIl6BzLW2sz aD9bu/qz44Kar+SaM6Ei3G2zouTnpOZ0GvwolTfCQgd815vfJ8LOe86dyGETNblhAC8fMc u6zm6N/ciO6n/+Ub+9QOB1Q/HAbjiBs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1646410430; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+3iB4IVSxvoxrgPRlS4xZegvmXLYHfk7btvgwcWOpvg=; b=rJYkoa8Z8Nz0U6cYo52Y0FqpTFa1/XxmMOGsYj4KUxEIV7a8kQf9NqrNmiw2yWSJdDIrzS 7hZmItE0lNSzSFDg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1592C13B64; Fri, 4 Mar 2022 16:13:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id cPRhAr46ImKeRAAAMHmgww (envelope-from ); Fri, 04 Mar 2022 16:13:50 +0000 Received: from localhost (brahms.olymp [local]) by brahms.olymp (OpenSMTPD) with ESMTPA id 8db641e2; Fri, 4 Mar 2022 16:14:04 +0000 (UTC) From: =?utf-8?q?Lu=C3=ADs_Henriques?= To: Jeff Layton , Xiubo Li , Ilya Dryomov Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Lu?= =?utf-8?q?=C3=ADs_Henriques?= Subject: [PATCH 2/3] ceph: fix use-after-free in ceph_readdir Date: Fri, 4 Mar 2022 16:14:02 +0000 Message-Id: <20220304161403.19295-3-lhenriques@suse.de> In-Reply-To: <20220304161403.19295-1-lhenriques@suse.de> References: <20220304161403.19295-1-lhenriques@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org After calling ceph_mdsc_put_request() on dfi->last_readdir, this field should be set to NULL, otherwise we may end-up freeing it twince and get the following splat: refcount_t: underflow; use-after-free. WARNING: CPU: 0 PID: 229 at lib/refcount.c:28 refcount_warn_saturate+0xa6/0xf0 ... Call Trace: ceph_readdir+0xd35/0x1460 [ceph] ? _raw_spin_unlock+0x12/0x30 ? preempt_count_add+0x73/0xa0 ? _raw_spin_unlock+0x12/0x30 ? __mark_inode_dirty+0x27c/0x3a0 iterate_dir+0x7d/0x190 __x64_sys_getdents64+0x80/0x120 ? compat_fillonedir+0x160/0x160 do_syscall_64+0x43/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae Signed-off-by: Luís Henriques --- fs/ceph/dir.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c index 0bcb677d2199..934402f5e9e6 100644 --- a/fs/ceph/dir.c +++ b/fs/ceph/dir.c @@ -555,6 +555,7 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) le32_to_cpu(rde->inode.in->mode) >> 12)) { dout("filldir stopping us...\n"); ceph_mdsc_put_request(dfi->last_readdir); + dfi->last_readdir = NULL; err = 0; goto out; } From patchwork Fri Mar 4 16:14:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Luis Henriques X-Patchwork-Id: 12769419 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 8DE53C433F5 for ; Fri, 4 Mar 2022 16:13:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240661AbiCDQOl (ORCPT ); Fri, 4 Mar 2022 11:14:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240343AbiCDQOj (ORCPT ); Fri, 4 Mar 2022 11:14:39 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42704157216; Fri, 4 Mar 2022 08:13:52 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 00F4F2114E; Fri, 4 Mar 2022 16:13:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1646410431; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=49z4mdxYb+Ve5OAnexCeNPyIAUWYX9Xt8aSEh8mjUHQ=; b=HxmS+dWjFP0qFYosQtFR6tN6gD/M1mM9ddQrm4IfYOiTixKVCK/ri5DQsz8ksr4S7zYZb/ cToSw46Y7C1ouZqIxlybJUtnHhbKEr7BK04j25v9ex4/pPI6PURAj8uVdfj2Bffz5J3GL+ QHja56Wts8FUtib7aDAylv3cGYAkffQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1646410431; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=49z4mdxYb+Ve5OAnexCeNPyIAUWYX9Xt8aSEh8mjUHQ=; b=e2Mbs4VJxn4k39CH9S7/322QWsqWrd5t8DPyCuWhypN2hdMf+p/puIp4941FhQt6Srzg1/ O2YowIT75PRu4tAA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 8FACA13B64; Fri, 4 Mar 2022 16:13:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id eKAcIL46ImKeRAAAMHmgww (envelope-from ); Fri, 04 Mar 2022 16:13:50 +0000 Received: from localhost (brahms.olymp [local]) by brahms.olymp (OpenSMTPD) with ESMTPA id 8201c2fe; Fri, 4 Mar 2022 16:14:04 +0000 (UTC) From: =?utf-8?q?Lu=C3=ADs_Henriques?= To: Jeff Layton , Xiubo Li , Ilya Dryomov Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Lu?= =?utf-8?q?=C3=ADs_Henriques?= Subject: [PATCH 3/3] ceph: add support for encrypted snapshot names Date: Fri, 4 Mar 2022 16:14:03 +0000 Message-Id: <20220304161403.19295-4-lhenriques@suse.de> In-Reply-To: <20220304161403.19295-1-lhenriques@suse.de> References: <20220304161403.19295-1-lhenriques@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org Since filenames in encrypted directories are already encrypted and shown as a base64-encoded string when the directory is locked, snapshot names should show a similar behaviour. Signed-off-by: Luís Henriques --- fs/ceph/dir.c | 9 +++++++++ fs/ceph/inode.c | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c index 934402f5e9e6..17d2f18a1fd1 100644 --- a/fs/ceph/dir.c +++ b/fs/ceph/dir.c @@ -1069,6 +1069,15 @@ static int ceph_mkdir(struct user_namespace *mnt_userns, struct inode *dir, op = CEPH_MDS_OP_MKSNAP; dout("mksnap dir %p snap '%pd' dn %p\n", dir, dentry, dentry); + /* + * Encrypted snapshots require d_revalidate to force a + * LOOKUPSNAP to cleanup dcache + */ + if (IS_ENCRYPTED(dir)) { + spin_lock(&dentry->d_lock); + dentry->d_flags |= DCACHE_NOKEY_NAME; + spin_unlock(&dentry->d_lock); + } } else if (ceph_snap(dir) == CEPH_NOSNAP) { dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode); op = CEPH_MDS_OP_MKDIR; diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c index 8b0832271fdf..357335a11384 100644 --- a/fs/ceph/inode.c +++ b/fs/ceph/inode.c @@ -182,6 +182,19 @@ struct inode *ceph_get_snapdir(struct inode *parent) ci->i_rbytes = 0; ci->i_btime = ceph_inode(parent)->i_btime; + /* if encrypted, just borrow fscrypt_auth from parent */ + if (IS_ENCRYPTED(parent)) { + struct ceph_inode_info *pci = ceph_inode(parent); + + ci->fscrypt_auth = kmemdup(pci->fscrypt_auth, + pci->fscrypt_auth_len, + GFP_KERNEL); + if (ci->fscrypt_auth) { + inode->i_flags |= S_ENCRYPTED; + ci->fscrypt_auth_len = pci->fscrypt_auth_len; + } else + dout("Failed to alloc memory for fscrypt_auth in snapdir\n"); + } if (inode->i_state & I_NEW) { inode->i_op = &ceph_snapdir_iops; inode->i_fop = &ceph_snapdir_fops;