Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp8257664rwp; Wed, 19 Jul 2023 07:26:49 -0700 (PDT) X-Google-Smtp-Source: APBJJlGWjXSC+HTYhgoGvr2lS69xfa3eEn1CFhoiP13shaUi6HptDCNWRUPhM0WhrcEMn0b7JbrE X-Received: by 2002:a17:907:75f9:b0:98c:e72c:6b83 with SMTP id jz25-20020a17090775f900b0098ce72c6b83mr2332817ejc.45.1689776809060; Wed, 19 Jul 2023 07:26:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689776809; cv=none; d=google.com; s=arc-20160816; b=nXUvzm6TwF0aT9yt5ysCTcxCGphpb/lr1XnJHCm9OHoCQ+X6KGtZ3VC9giDPE2Nvmv rniwM8yfxUvM8XIMW+NVgp9fR4Zq7JN/VTYi5g+6rP7JKS+1AmhU08og6ldsXsyi7xZ3 1KgfjeRUsJg9E6AHqgDGiT59MKCi7FC0rRJRTS0PTBW9/PT2hm+xSnvUOsSDTTNgTa0t OOYMg0E0NnJMEk2cIIne7LA5wSHBQMmqPaDiBcJ0npbw2HwpsOLNeLpCdmrVhzdRa/PM v6AwDHXdmVRscgL/yfFs5dvmTCFUfHXc8dZ0cRO4nBDPkl1OvefHbtQYY3qV0TE6opWU Px1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=lwwJZCVySKme+pSwV95NsetQCeZG0aZIXWDSxaqrCbE=; fh=CejRK1k9GFB91vpbnH1OZYzBqQOIQ+wVJjyusoe9lMg=; b=rLOFmmZI6QcYqjAAp1skZHv5uvppHytbzBHhee4UO8zWBmaVHrXjY8ZTzTZzy6C+vN +fUNFeih+5N3NFBC2jIKvrtx72yF76fB8iwyklvTMOQyiSUTnyz0q+Xews6uY2dDZn8v M8fu+Mu71anRcrORYrmNcj/ckY7T+5zDZU2hZhr4Vy/DbInPJoKCB/eaNplxnLWpYxIA BchPczBIi80Z2JAF1VCfTdwLcuwmNCAwaoG2Lv59g34mCwemMZOwCyOlBCvLVatEABRX 1Z/Afo4NEbMKZejMSXySWlLzc8BPfX96BR49BCokSXd0W2+a7S3q9EzuVXGwpDJy1zSI l1oQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ju20-20020a17090798b400b00988357b5f3fsi2829316ejc.758.2023.07.19.07.26.17; Wed, 19 Jul 2023 07:26:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231566AbjGSOYd (ORCPT + 99 others); Wed, 19 Jul 2023 10:24:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231250AbjGSOY2 (ORCPT ); Wed, 19 Jul 2023 10:24:28 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4C2F2690; Wed, 19 Jul 2023 07:24:08 -0700 (PDT) Received: from kwepemm600013.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4R5dJQ0gzxzNmKd; Wed, 19 Jul 2023 22:20:22 +0800 (CST) Received: from [10.174.178.46] (10.174.178.46) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Wed, 19 Jul 2023 22:23:40 +0800 Subject: Re: [RFC PATCH 05/21] ubifs: Pass worst-case buffer size to compression routines To: Ard Biesheuvel , Eric Biggers CC: , Herbert Xu , Kees Cook , Haren Myneni , Nick Terrell , Minchan Kim , Sergey Senozhatsky , Jens Axboe , Giovanni Cabiddu , Richard Weinberger , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Steffen Klassert , , , , , , References: <20230718125847.3869700-1-ardb@kernel.org> <20230718125847.3869700-6-ardb@kernel.org> <20230718223813.GC1005@sol.localdomain> From: Zhihao Cheng Message-ID: <3330004f-acac-81b4-e382-a17221a0a128@huawei.com> Date: Wed, 19 Jul 2023 22:23:39 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.178.46] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600013.china.huawei.com (7.193.23.68) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 在 2023/7/19 16:33, Ard Biesheuvel 写道: > On Wed, 19 Jul 2023 at 00:38, Eric Biggers wrote: >> >> On Tue, Jul 18, 2023 at 02:58:31PM +0200, Ard Biesheuvel wrote: >>> Currently, the ubifs code allocates a worst case buffer size to >>> recompress a data node, but does not pass the size of that buffer to the >>> compression code. This means that the compression code will never use I think you mean the 'out_len' which describes the lengh of 'buf' is passed into ubifs_decompress, which effects the result of decompressor(eg. lz4 uses length to calculate the buffer end pos). So, we should pass the real lenghth of 'buf'. Reviewed-by: Zhihao Cheng >>> the additional space, and might fail spuriously due to lack of space. >>> >>> So let's multiply out_len by WORST_COMPR_FACTOR after allocating the >>> buffer. Doing so is guaranteed not to overflow, given that the preceding >>> kmalloc_array() call would have failed otherwise. >>> >>> Signed-off-by: Ard Biesheuvel >>> --- >>> fs/ubifs/journal.c | 2 ++ >>> 1 file changed, 2 insertions(+) >>> >>> diff --git a/fs/ubifs/journal.c b/fs/ubifs/journal.c >>> index dc52ac0f4a345f30..4e5961878f336033 100644 >>> --- a/fs/ubifs/journal.c >>> +++ b/fs/ubifs/journal.c >>> @@ -1493,6 +1493,8 @@ static int truncate_data_node(const struct ubifs_info *c, const struct inode *in >>> if (!buf) >>> return -ENOMEM; >>> >>> + out_len *= WORST_COMPR_FACTOR; >>> + >>> dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ; >>> data_size = dn_size - UBIFS_DATA_NODE_SZ; >>> compr_type = le16_to_cpu(dn->compr_type); >> >> This looks like another case where data that would be expanded by compression >> should just be stored uncompressed instead. >> >> In fact, it seems that UBIFS does that already. ubifs_compress() has this: >> >> /* >> * If the data compressed only slightly, it is better to leave it >> * uncompressed to improve read speed. >> */ >> if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF) >> goto no_compr; >> >> So it's unclear why the WORST_COMPR_FACTOR thing is needed at all. >> > > It is not. The buffer is used for decompression in the truncation > path, so none of this logic even matters. Even if the subsequent > recompression of the truncated data node could result in expansion > beyond the uncompressed size of the original data (which seems > impossible to me), increasing the size of this buffer would not help > as it is the input buffer for the compression not the output buffer. > . >