From patchwork Mon Nov 23 02:40:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Campbell Suter X-Patchwork-Id: 1404755 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=snapit.group Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=snapit.group header.i=@snapit.group header.a=rsa-sha256 header.s=shd header.b=hATPbkFn; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CfmFM4zL8z9sRR for ; Mon, 23 Nov 2020 23:10:31 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 69269825C1; Mon, 23 Nov 2020 13:10:15 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=snapit.group Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=snapit.group header.i=@snapit.group header.b="hATPbkFn"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D70398242B; Mon, 23 Nov 2020 03:40:12 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 92DAA81281 for ; Mon, 23 Nov 2020 03:40:09 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=snapit.group Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=campbell@snapit.group Received: by mail-pf1-x443.google.com with SMTP id x24so1247334pfn.6 for ; Sun, 22 Nov 2020 18:40:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=snapit.group; s=shd; h=to:cc:from:subject:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=2D6J9TvEOOxl7He0HuvGrjJ+ydXUnmojNuSB2LQro+o=; b=hATPbkFny1v9B+7w7nXVTfIwRmp/VCz0RBMQGeOeZziD4dSpEispXQU0dfWzA0DkW+ p+vmtQNBZBUPBJIe6Fxa8nz2gjwQxn6DHUurTXpHcCPpjwSodnxlZyXncQ0iK4XkNM56 s2G0A19fyuExt0unNNDACt0VIFRLqhlI6AEUw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=2D6J9TvEOOxl7He0HuvGrjJ+ydXUnmojNuSB2LQro+o=; b=fVFTbbs1vSx3DOjVB+I7Vg9UdfBbRcFmcfbtdllIW9Bp4gGy+pIyRJQVYPn4FxmSkG YK0poT8VCEHWJineqOfCbV3GZl6MRLm9chvkXwSPNImCcqfli7Exnb6NnNEqUdq4emtP xcScO5B/Cvd2GpBOMVvVwMDDJ/XAZWZcXKWkqSernSLQ4wxit5bjK+mJ71JLPyu0PeJd j24xhWZEBykIbXnhGY3GJSIxmynhy5JQCyzX4wIRhw75ImEA1gaBdpsotMPRZAto8Kp/ C97opfY2KIEKg3dlQDJs8DSWCFRfcI59P0PluuzMhC6SprPmUEw2JO6WjxNftlG+tLxR au+Q== X-Gm-Message-State: AOAM5323wMtZiI/pi8y9sUsA5MlTH2S7b1yBM3LyOgnfDaZg61K3RW7K DYMjtfWIUGbNlx36iCEu+2hiPS/uic0zzukN X-Google-Smtp-Source: ABdhPJw9bXSDUjaBaZfRlZl5/WF26fbkHqbLdPkuhbuGVLlXhf6D5tYNoErLKf4MKRkaoXvEaZrGXA== X-Received: by 2002:a17:90a:ba17:: with SMTP id s23mr22125234pjr.154.1606099207567; Sun, 22 Nov 2020 18:40:07 -0800 (PST) Received: from ?IPv6:2406:e002:b017:4d01:9588:b6a6:3736:d9ff? ([2406:e002:b017:4d01:9588:b6a6:3736:d9ff]) by smtp.gmail.com with ESMTPSA id t20sm10908764pjg.25.2020.11.22.18.40.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 22 Nov 2020 18:40:06 -0800 (PST) To: u-boot@lists.denx.de Cc: Joao Marcos Costa From: Campbell Suter Subject: [PATCH] Fix squashfs failing to load sparse files Message-ID: Date: Mon, 23 Nov 2020 15:40:03 +1300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 Content-Language: en-GB X-Mailman-Approved-At: Mon, 23 Nov 2020 13:09:47 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean SquashFS supports sprase blocks in files - that is, if a given block is composed only of zeros, it's not written to the output file to save space and it's on-disk length field is set to zero to indicate that. Previously the squashfs driver did not recognise that, and would attempt to read and decompress a zero-sized block, which obviously failed. The following command may be used to create a file for testing: cat <(dd if=/dev/urandom of=/dev/stdout bs=1M count=1) \ <(dd if=/dev/zero of=/dev/stdout bs=1M count=1) \ <(dd if=/dev/urandom of=/dev/stdout bs=1k count=200) >test_file Signed-off-by: Campbell Suter --- fs/squashfs/sqfs.c | 54 ++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/fs/squashfs/sqfs.c b/fs/squashfs/sqfs.c index 608a2bb454..2516d920f8 100644 --- a/fs/squashfs/sqfs.c +++ b/fs/squashfs/sqfs.c @@ -1309,7 +1309,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, { char *dir = NULL, *fragment_block, *datablock = NULL, *data_buffer = NULL; char *fragment = NULL, *file = NULL, *resolved, *data; - u64 start, n_blks, table_size, data_offset, table_offset; + u64 start, n_blks, table_size, data_offset, table_offset, sparse_size; int ret, j, i_number, datablk_count = 0; struct squashfs_super_block *sblk = ctxt.sblk; struct squashfs_fragment_block_entry frag_entry; @@ -1443,28 +1443,43 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz); - data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); + /* Don't load any data for sparse blocks */ + if (finfo.blk_sizes[j] == 0) { + n_blks = 0; + table_offset = 0; + data_buffer = NULL; + data = NULL; + } else { + data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); - if (!data_buffer) { - ret = -ENOMEM; - goto out; - } + if (!data_buffer) { + ret = -ENOMEM; + goto out; + } - ret = sqfs_disk_read(start, n_blks, data_buffer); - if (ret < 0) { - /* - * Possible causes: too many data blocks or too large - * SquashFS block size. Tip: re-compile the SquashFS - * image with mksquashfs's -b option. - */ - printf("Error: too many data blocks to be read.\n"); - goto out; - } + ret = sqfs_disk_read(start, n_blks, data_buffer); + if (ret < 0) { + /* + * Possible causes: too many data blocks or too large + * SquashFS block size. Tip: re-compile the SquashFS + * image with mksquashfs's -b option. + */ + printf("Error: too many data blocks to be read.\n"); + goto out; + } - data = data_buffer + table_offset; + data = data_buffer + table_offset; + } /* Load the data */ - if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) { + if (finfo.blk_sizes[j] == 0) { + /* This is a sparse block */ + sparse_size = get_unaligned_le32(&sblk->block_size); + if ((*actread + sparse_size) > len) + sparse_size = len - *actread; + memset(buf + *actread, 0, sparse_size); + *actread += sparse_size; + } else if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) { dest_len = get_unaligned_le32(&sblk->block_size); ret = sqfs_decompress(&ctxt, datablock, &dest_len, data, table_size); @@ -1483,7 +1498,8 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, } data_offset += table_size; - free(data_buffer); + if (data_buffer) + free(data_buffer); data_buffer = NULL; if (*actread >= len) break;