Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp808183lqt; Thu, 6 Jun 2024 21:34:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVism4qRw+g/1RZZCusooy1qbALd33/jp2/T/hB0xTysenR8UJAf2zg5f7jbpyXnc4JjS7PF6TlGFNh4UBdDk4o4V01V1fLt8fMruCZCg== X-Google-Smtp-Source: AGHT+IGnBc2KvRjjLrVFjeAPilgf/ucUtyLgSxMWKNNPrhc5jLNaZatLbZb6iKflyruymrC/S21w X-Received: by 2002:a81:de51:0:b0:61a:dc61:25e2 with SMTP id 00721157ae682-62cd56861bdmr14255877b3.49.1717734842679; Thu, 06 Jun 2024 21:34:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717734842; cv=pass; d=google.com; s=arc-20160816; b=Kb1vg6nlczj7Utwn1UqXJifq+swvnKwzNjcjyRF2CUhRUVyMqxXKuhbF97612i+Egu wD0L1JcMA1Jayt2bFNkRCXI1kJBmx1V3KFH11/KHLxXU0QXei0+DtzTPfW1m2ff1kO+a 3m1Shr98vqf3NwXgrrhQk6Ky9c1AIldBl8bSXqRFy2ex8UT/mRTANuOp32xm/mR/XEOf 0zGl0nxfiRi92ST/Um89EZfqiktaRQL5a/IeHyazAdwGvSGzpJ9x2sE2psUwVBHqaMVo +6X5PeNzf3xecwy4KhxMKFgCpBaCbpOywBLnE3ewyGV+r3ynbRcIKdTIJDTccNfz9ALN xfQA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=Xpe7qAMs0hd+iCKrWi0COGull1vm5Gm88/5OtP8rC6g=; fh=yXYChwFvQA/NaaLzCC0IJdkW11OhjXgO4DHXzVmnGFw=; b=jC+Ind0w65A+tus7lIxek8igOUEa4E6cjrZb92GSrNep6jLnKJB9vbRgj6Jn0F/1bW ec9M9GUB0xFMp6+fxdSLw+Zu8QEpMJY5W1+EWF79FICpsQh+lxJJSnxEMT70hYilysN5 ycQ+f+ymZ+7w9+0KcajV0KCSi9riTdgwiW0HbCiOpqAS5l7BZPt0wmbF0fRXweUBGD4E n47eM/eQHl9sqTRvoikNuIU7r0COU8zfMtmPyIbhBHHUf9Gs/FTmgEFKiZjQn5TZ/VO/ lknwWH6jzI3oXt9W91iWci8+BfUwCtcwq+yKiTGozv7Bzkj/Kn13971nIwhoM/fHwM07 fjtg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-205310-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205310-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6b04fa2217esi23725316d6.490.2024.06.06.21.34.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 21:34:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205310-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-205310-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205310-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 52B941C23D5D for ; Fri, 7 Jun 2024 04:34:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB9391553A2; Fri, 7 Jun 2024 04:27:07 +0000 (UTC) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D142249EB for ; Fri, 7 Jun 2024 04:27:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734427; cv=none; b=cTpYLTHicC5oMwH0pLI+ZUh+a+35WY8CSy0N3fBbFywrc+DKinvKhrKuSRY3/Lnq2IdNE95tCJ8at3hvV2s5DGtAd/+Vqd50tYfSzUjl3G79JhIdNH29upRlsaeoZP6avQZ8Tt1/BFc/UrmxNKj30AA45uffS8f8Rg1E638qEQ8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734427; c=relaxed/simple; bh=vxv9ZTCKBH0Kde9S4B2p/FaYMTHVm1da3JAmUyZwRKE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IZXQC3fxvzmcGQX7aibU3OyAQUUg1QhPc7CNtlUSWbk8yyrxV8OveSneiyVtuPo7q0+PJjuOegomGFNTCYubIwQ8INC4mjg3v5XLk9Du+zN2qJnzOiTrmWxtDnjacQUmL83I9YMNUGavwSUwA+EFArnmpfTlyVo/A+tyJ5Uf+pM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VwShz6dzKzmXv6; Fri, 7 Jun 2024 12:22:27 +0800 (CST) Received: from kwepemm600013.china.huawei.com (unknown [7.193.23.68]) by mail.maildlp.com (Postfix) with ESMTPS id BF71A140203; Fri, 7 Jun 2024 12:26:48 +0800 (CST) Received: from huawei.com (10.175.104.67) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Fri, 7 Jun 2024 12:26:32 +0800 From: Zhihao Cheng To: , , , , , CC: , Subject: [RFC PATCH mtd-utils 009/110] mkfs.ubifs: Fix incorrect dir size calculation in encryption scenario Date: Fri, 7 Jun 2024 12:24:34 +0800 Message-ID: <20240607042615.2069840-10-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240607042615.2069840-1-chengzhihao1@huawei.com> References: <20240607042615.2069840-1-chengzhihao1@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600013.china.huawei.com (7.193.23.68) The size of directory should be the total length of encrypted entry name, otherwise it could trigger errors while checking filesystem: dbg_check_filesystem [ubifs]: directory inode 89 size is 352, but calculated size is 400 Fixes: 4c55918dd747d ("Implement filename encryption") Signed-off-by: Zhihao Cheng --- ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c index 8bf073ce..25c49967 100644 --- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c @@ -1723,15 +1723,17 @@ static void set_dent_cookie(struct ubifs_dent_node *dent) * @name: directory entry name * @inum: target inode number of the directory entry * @type: type of the target inode + * @kname_len: the length of name stored in the directory entry node is + * returned here */ static int add_dent_node(ino_t dir_inum, const char *name, ino_t inum, - unsigned char type, struct fscrypt_context *fctx) + unsigned char type, struct fscrypt_context *fctx, + int *kname_len) { struct ubifs_dent_node *dent = node_buf; union ubifs_key key; struct qstr dname; char *kname; - int kname_len; int len; dbg_msg(3, "%s ino %lu type %u dir ino %lu", name, (unsigned long)inum, @@ -1749,7 +1751,7 @@ static int add_dent_node(ino_t dir_inum, const char *name, ino_t inum, set_dent_cookie(dent); if (!fctx) { - kname_len = dname.len; + *kname_len = dname.len; kname = strdup(name); if (!kname) return errmsg("cannot allocate memory"); @@ -1765,18 +1767,18 @@ static int add_dent_node(ino_t dir_inum, const char *name, ino_t inum, if (ret < 0) return ret; - kname_len = ret; + *kname_len = ret; } - dent_key_init(c, &key, dir_inum, kname, kname_len); - dent->nlen = cpu_to_le16(kname_len); - memcpy(dent->name, kname, kname_len); - dent->name[kname_len] = '\0'; - len = UBIFS_DENT_NODE_SZ + kname_len + 1; + dent_key_init(c, &key, dir_inum, kname, *kname_len); + dent->nlen = cpu_to_le16(*kname_len); + memcpy(dent->name, kname, *kname_len); + dent->name[*kname_len] = '\0'; + len = UBIFS_DENT_NODE_SZ + *kname_len + 1; key_write(&key, dent->key); - return add_node(&key, kname, kname_len, dent, len); + return add_node(&key, kname, *kname_len, dent, len); } /** @@ -2026,7 +2028,7 @@ static int add_directory(const char *dir_name, ino_t dir_inum, struct stat *st, { struct dirent *entry; DIR *dir = NULL; - int err = 0; + int kname_len, err = 0; loff_t size = UBIFS_INO_NODE_SZ; char *name = NULL; unsigned int nlink = 2; @@ -2139,13 +2141,13 @@ static int add_directory(const char *dir_name, ino_t dir_inum, struct stat *st, goto out_free; } - err = add_dent_node(dir_inum, entry->d_name, inum, type, fctx); + err = add_dent_node(dir_inum, entry->d_name, inum, type, fctx, + &kname_len); if (err) { free_fscrypt_context(new_fctx); goto out_free; } - size += ALIGN(UBIFS_DENT_NODE_SZ + strlen(entry->d_name) + 1, - 8); + size += ALIGN(UBIFS_DENT_NODE_SZ + kname_len + 1, 8); if (new_fctx) free_fscrypt_context(new_fctx); @@ -2210,13 +2212,14 @@ static int add_directory(const char *dir_name, ino_t dir_inum, struct stat *st, goto out_free; } - err = add_dent_node(dir_inum, nh_elt->name, inum, type, fctx); + err = add_dent_node(dir_inum, nh_elt->name, inum, type, fctx, + &kname_len); if (err) { free_fscrypt_context(new_fctx); goto out_free; } - size += ALIGN(UBIFS_DENT_NODE_SZ + strlen(nh_elt->name) + 1, 8); + size += ALIGN(UBIFS_DENT_NODE_SZ + kname_len + 1, 8); nh_elt = next_name_htbl_element(ph_elt, &itr); if (new_fctx) -- 2.13.6