From patchwork Wed Mar 2 12:13:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiubo Li X-Patchwork-Id: 547608 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 581A7C433EF for ; Wed, 2 Mar 2022 12:13:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241685AbiCBMOU (ORCPT ); Wed, 2 Mar 2022 07:14:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241667AbiCBMOS (ORCPT ); Wed, 2 Mar 2022 07:14:18 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 122842A257 for ; Wed, 2 Mar 2022 04:13:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646223215; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/zzDQuZ8eYQqFFfnBFeCOwyCjci2o1PmC4eykL7+NEE=; b=IEomYVlPjvyNUtPww99tRriyrruRQwfcp/60qByfucPU4S9vzrPHWnrtczf+fIXfSInouU YXezGsxxIWD2IlHIA5dqsm8GokrGWcKCR8+QP9gbynJRRhu7VYq8tl/yFT8EyFAo1b35/j HU8wZx9/8KxFDPpwuR0hPsUMqd+ddgs= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-519-7wBGgq2LPmOPt29eugNZKQ-1; Wed, 02 Mar 2022 07:13:33 -0500 X-MC-Unique: 7wBGgq2LPmOPt29eugNZKQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C5CDE1800D50; Wed, 2 Mar 2022 12:13:32 +0000 (UTC) Received: from lxbceph1.gsslab.pek2.redhat.com (unknown [10.72.47.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0D358781EC; Wed, 2 Mar 2022 12:13:30 +0000 (UTC) From: xiubli@redhat.com To: jlayton@kernel.org Cc: idryomov@gmail.com, vshankar@redhat.com, ceph-devel@vger.kernel.org, Xiubo Li Subject: [PATCH v3 1/6] ceph: fail the request when failing to decode dentry names Date: Wed, 2 Mar 2022 20:13:18 +0800 Message-Id: <20220302121323.240432-2-xiubli@redhat.com> In-Reply-To: <20220302121323.240432-1-xiubli@redhat.com> References: <20220302121323.240432-1-xiubli@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org From: Xiubo Li If we just skip the corrupt dentry names without setting the rde's offset it will crash in ceph_readdir(): ------------[ cut here ]------------ kernel BUG at fs/ceph/dir.c:537! invalid opcode: 0000 [#1] PREEMPT SMP KASAN NOPTI CPU: 16 PID: 21641 Comm: ls Tainted: G E 5.17.0-rc2+ #92 Hardware name: Red Hat RHEV Hypervisor, BIOS 1.11.0-2.el7 04/01/2014 The corresponding code in ceph_readdir() is: BUG_ON(rde->offset < ctx->pos); For now let's just fail the readdir request since it's nasty to handle it and will do better error handling later in future. Signed-off-by: Xiubo Li --- fs/ceph/dir.c | 13 +++++++------ fs/ceph/inode.c | 5 +++-- fs/ceph/mds_client.c | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c index 44395aae7259..fa3924959537 100644 --- a/fs/ceph/dir.c +++ b/fs/ceph/dir.c @@ -537,6 +537,13 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) .ctext_len = rde->altname_len }; u32 olen = oname.len; + err = ceph_fname_to_usr(&fname, &tname, &oname, NULL); + if (err) { + pr_err("%s unable to decode %.*s, got %d\n", __func__, + rde->name_len, rde->name, err); + goto out; + } + BUG_ON(rde->offset < ctx->pos); BUG_ON(!rde->inode.in); @@ -545,12 +552,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) i, rinfo->dir_nr, ctx->pos, rde->name_len, rde->name, &rde->inode.in); - err = ceph_fname_to_usr(&fname, &tname, &oname, NULL); - if (err) { - dout("Unable to decode %.*s. Skipping it.\n", rde->name_len, rde->name); - continue; - } - if (!dir_emit(ctx, oname.name, oname.len, ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)), le32_to_cpu(rde->inode.in->mode) >> 12)) { diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c index cbeba8a93a07..b573a0f33450 100644 --- a/fs/ceph/inode.c +++ b/fs/ceph/inode.c @@ -1904,8 +1904,9 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req, err = ceph_fname_to_usr(&fname, &tname, &oname, &is_nokey); if (err) { - dout("Unable to decode %.*s. Skipping it.", rde->name_len, rde->name); - continue; + pr_err("%s unable to decode %.*s, got %d\n", __func__, + rde->name_len, rde->name, err); + goto out; } dname.name = oname.name; diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index 31c1b441c0a1..8d704ddd7291 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -3477,7 +3477,7 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) if (err == 0) { if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR || req->r_op == CEPH_MDS_OP_LSSNAP)) - ceph_readdir_prepopulate(req, req->r_session); + err = ceph_readdir_prepopulate(req, req->r_session); } current->journal_info = NULL; mutex_unlock(&req->r_fill_mutex); From patchwork Wed Mar 2 12:13:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiubo Li X-Patchwork-Id: 547606 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 375DDC433EF for ; Wed, 2 Mar 2022 12:13:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241686AbiCBMOc (ORCPT ); Wed, 2 Mar 2022 07:14:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241704AbiCBMOa (ORCPT ); Wed, 2 Mar 2022 07:14:30 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7838F457A4 for ; Wed, 2 Mar 2022 04:13:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646223226; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IplMnW0psISCiMEAUbj/4bUoWcpi2a170XO5Mcx9pv0=; b=hw+1y8pMbModdl29N1Z2Ne91yflKXApJyxaxega6CxCsKUjz6838Y2MOubaV68oGINq/rD +VVHRSLrGaOjCn9eRTqhtu0/Z7JjhLgy0XgPs/lCEWbKVeGsd6MF62NW1eOu+T54GdNvdj QJDsbQSXRAw9iPv6fSTQETGKzYxuuRE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-5-EID3b_BOOEu_dP7O7xR0rw-1; Wed, 02 Mar 2022 07:13:40 -0500 X-MC-Unique: EID3b_BOOEu_dP7O7xR0rw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8746E5200; Wed, 2 Mar 2022 12:13:39 +0000 (UTC) Received: from lxbceph1.gsslab.pek2.redhat.com (unknown [10.72.47.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id C0DC778203; Wed, 2 Mar 2022 12:13:37 +0000 (UTC) From: xiubli@redhat.com To: jlayton@kernel.org Cc: idryomov@gmail.com, vshankar@redhat.com, ceph-devel@vger.kernel.org, Xiubo Li Subject: [PATCH v3 4/6] ceph: use the parent inode of '.snap' to dencrypt the names for readdir Date: Wed, 2 Mar 2022 20:13:21 +0800 Message-Id: <20220302121323.240432-5-xiubli@redhat.com> In-Reply-To: <20220302121323.240432-1-xiubli@redhat.com> References: <20220302121323.240432-1-xiubli@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org From: Xiubo Li The inode for '.snap' directory will always with no key setup, so we can use the parent inode to do this. Signed-off-by: Xiubo Li --- fs/ceph/inode.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c index 3ef8d9ae01dc..2d4e5ee9a373 100644 --- a/fs/ceph/inode.c +++ b/fs/ceph/inode.c @@ -1823,7 +1823,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req, struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; struct qstr dname; struct dentry *dn; - struct inode *in; + struct inode *in, *pinode; int err = 0, skipped = 0, ret, i; u32 frag = le32_to_cpu(req->r_args.readdir.frag); u32 last_hash = 0; @@ -1882,11 +1882,13 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req, cache_ctl.index = req->r_readdir_cache_idx; fpos_offset = req->r_readdir_offset; - err = ceph_fname_alloc_buffer(inode, &tname); + pinode = ceph_get_snap_parent_inode(inode); + + err = ceph_fname_alloc_buffer(pinode, &tname); if (err < 0) goto out; - err = ceph_fname_alloc_buffer(inode, &oname); + err = ceph_fname_alloc_buffer(pinode, &oname); if (err < 0) goto out; @@ -1896,7 +1898,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req, struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; struct ceph_vino tvino; u32 olen = oname.len; - struct ceph_fname fname = { .dir = inode, + struct ceph_fname fname = { .dir = pinode, .name = rde->name, .name_len = rde->name_len, .ctext = rde->altname, @@ -2051,8 +2053,9 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req, req->r_readdir_cache_idx = cache_ctl.index; } ceph_readdir_cache_release(&cache_ctl); - ceph_fname_free_buffer(inode, &tname); - ceph_fname_free_buffer(inode, &oname); + ceph_fname_free_buffer(pinode, &tname); + ceph_fname_free_buffer(pinode, &oname); + iput(pinode); dout("readdir_prepopulate done\n"); return err; } From patchwork Wed Mar 2 12:13:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiubo Li X-Patchwork-Id: 547607 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 7754AC433F5 for ; Wed, 2 Mar 2022 12:13:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241695AbiCBMO3 (ORCPT ); Wed, 2 Mar 2022 07:14:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241687AbiCBMO2 (ORCPT ); Wed, 2 Mar 2022 07:14:28 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CAF6736B48 for ; Wed, 2 Mar 2022 04:13:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646223224; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aEjMzFzz4snOv5WhbNqLQcqKyY8H58ML4fOsuWjty84=; b=JWs5uwth16EuY9FhDLE4tiooNfz3ZiyhzI2cLiPTe9aQSsM8nUOweKvMRR0rp5/0dWFcJl 0L2PjZSITFthcM+QLF0JrKPX5P6YuR4s7RzHiqiOfP91lBFDP6u/ukewsvuWhwRlowRRxt tbWRy6S6eYmLuSs5ZC8TjTPZj+4C/0k= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-594-JWvvUMtEOQicf16oozGyYA-1; Wed, 02 Mar 2022 07:13:42 -0500 X-MC-Unique: JWvvUMtEOQicf16oozGyYA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C50461800D50; Wed, 2 Mar 2022 12:13:41 +0000 (UTC) Received: from lxbceph1.gsslab.pek2.redhat.com (unknown [10.72.47.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 08A5578203; Wed, 2 Mar 2022 12:13:39 +0000 (UTC) From: xiubli@redhat.com To: jlayton@kernel.org Cc: idryomov@gmail.com, vshankar@redhat.com, ceph-devel@vger.kernel.org, Xiubo Li Subject: [PATCH v3 5/6] ceph: use the parent inode of '.snap' to encrypt name to build path Date: Wed, 2 Mar 2022 20:13:22 +0800 Message-Id: <20220302121323.240432-6-xiubli@redhat.com> In-Reply-To: <20220302121323.240432-1-xiubli@redhat.com> References: <20220302121323.240432-1-xiubli@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org From: Xiubo Li The inode for '.snap' directory will always with no key setup, so we can use the parent inode to do this. Signed-off-by: Xiubo Li --- fs/ceph/mds_client.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index 8d704ddd7291..f8fd474f80cf 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -2466,8 +2466,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen) */ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for_wire) { - struct dentry *cur; - struct inode *inode; + struct dentry *cur, *parent; + struct inode *inode, *pinode; char *path; int pos; unsigned seq; @@ -2480,13 +2480,16 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for if (!path) return ERR_PTR(-ENOMEM); retry: + pinode = NULL; + parent = NULL; pos = PATH_MAX - 1; path[pos] = '\0'; seq = read_seqbegin(&rename_lock); cur = dget(dentry); for (;;) { - struct dentry *parent; + parent = dget_parent(cur); + pinode = ceph_get_snap_parent_inode(d_inode(parent)); spin_lock(&cur->d_lock); inode = d_inode(cur); @@ -2494,12 +2497,11 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for dout("build_path path+%d: %p SNAPDIR\n", pos, cur); spin_unlock(&cur->d_lock); - parent = dget_parent(cur); } else if (for_wire && inode && dentry != cur && ceph_snap(inode) == CEPH_NOSNAP) { spin_unlock(&cur->d_lock); pos++; /* get rid of any prepended '/' */ break; - } else if (!for_wire || !IS_ENCRYPTED(d_inode(cur->d_parent))) { + } else if (!for_wire || !IS_ENCRYPTED(pinode)) { pos -= cur->d_name.len; if (pos < 0) { spin_unlock(&cur->d_lock); @@ -2507,7 +2509,6 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for } memcpy(path + pos, cur->d_name.name, cur->d_name.len); spin_unlock(&cur->d_lock); - parent = dget_parent(cur); } else { int len, ret; char buf[FSCRYPT_BASE64URL_CHARS(NAME_MAX)]; @@ -2519,32 +2520,32 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for memcpy(buf, cur->d_name.name, cur->d_name.len); len = cur->d_name.len; spin_unlock(&cur->d_lock); - parent = dget_parent(cur); - ret = __fscrypt_prepare_readdir(d_inode(parent)); + ret = __fscrypt_prepare_readdir(pinode); if (ret < 0) { dput(parent); dput(cur); + iput(pinode); return ERR_PTR(ret); } - if (fscrypt_has_encryption_key(d_inode(parent))) { - len = ceph_encode_encrypted_fname(d_inode(parent), cur, buf); + if (fscrypt_has_encryption_key(pinode)) { + len = ceph_encode_encrypted_fname(pinode, cur, buf); if (len < 0) { dput(parent); dput(cur); + iput(pinode); return ERR_PTR(len); } } pos -= len; - if (pos < 0) { - dput(parent); + if (pos < 0) break; - } memcpy(path + pos, buf, len); } dput(cur); cur = parent; + parent = NULL; /* Are we at the root? */ if (IS_ROOT(cur)) @@ -2555,7 +2556,13 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for break; path[pos] = '/'; + iput(pinode); + pinode = NULL; } + if (pinode) + iput(pinode); + if (parent) + dput(parent); inode = d_inode(cur); base = inode ? ceph_ino(inode) : 0; dput(cur);