aboutsummaryrefslogtreecommitdiff
path: root/fs/squashfs
AgeCommit message (Collapse)AuthorFilesLines
2023-11-16fs/squashfs: enable LZ4 compression supportDavid Oberhollenzer1-0/+23
The structure is identical to the existing compressor implementations, trivially adding lz4 decompression to sqfs_decompress. The changes were tested using a sandbox build. An LZ4 compressed squashfs image was bound as a host block device. Signed-off-by: David Oberhollenzer <goliath@infraroot.at> Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com>
2023-11-16fs/squashfs: remove unused declarationsDavid Oberhollenzer3-58/+0
This patch removes a number of struct and macro declaration that were found through `git-grep` to be unused. Most of those are related to compressor options and super block flags. For reading a SquashFS image, we do not need the compressor options or the flags. Those only encode settings used for packing the image, mksquashfs uses them when appending data to an existing image. The kernel implementation does not touch those, and we don't need them either. Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2023-01-18lib: zstd: update to latest Linux zstd 1.5.2Brandon Maier1-6/+9
Update the zstd implementation to match Linux zstd 1.5.2 from commit 2aa14b1ab2. This was motivated by running into decompression corruption issues when trying to uncompress files compressed with newer versions of zstd. zstd users also claim significantly improved decompression times with newer zstd versions which is a side benefit. Original zstd code was copied from Linux commit 2aa14b1ab2 which is a custom-built implementation based on zstd 1.3.1. Linux switched to an implementation that is a copy of the upstream zstd code in Linux commit e0c1b49f5b, this results in a large code diff. However this should make future updates easier along with other benefits[1]. This commit is a straight mirror of the Linux zstd code, except to: - update a few #include that do not translate cleanly - linux/swab.h -> asm/byteorder.h - linux/limits.h -> linux/kernel.h - linux/module.h -> linux/compat.h - remove assert() from debug.h so it doesn't conflict with u-boot's assert() - strip out the compressor code as was done in the previous u-boot zstd - update existing zstd users to the new Linux zstd API - change the #define for MEM_STATIC to use INLINE_KEYWORD for codesize - add a new KConfig option that sets zstd build options to minify code based on zstd's ZSTD_LIB_MINIFY[2]. These changes were tested by booting a zstd 1.5.2 compressed kernel inside a FIT. And the squashfs changes by loading a file from zstd compressed squashfs with sqfsload. buildman was used to compile test other boards and check for binary bloat, as follows: > $ buildman -b zstd2 --boards dh_imx6,m53menlo,mvebu_espressobin-88f3720,sandbox,sandbox64,stm32mp15_dhcom_basic,stm32mp15_dhcor_basic,turris_mox,turris_omnia -sS > Summary of 6 commits for 9 boards (8 threads, 1 job per thread) > 01: Merge branch '2023-01-10-platform-updates' > arm: w+ m53menlo dh_imx6 > 02: lib: zstd: update to latest Linux zstd 1.5.2 > aarch64: (for 2/2 boards) all -3186.0 rodata +920.0 text -4106.0 > arm: (for 5/5 boards) all +1254.4 rodata +940.0 text +314.4 > sandbox: (for 2/2 boards) all -4452.0 data -16.0 rodata +640.0 text -5076.0 [1] https://github.com/torvalds/linux/commit/e0c1b49f5b674cca7b10549c53b3791d0bbc90a8 [2] https://github.com/facebook/zstd/blob/f302ad8811643c428c4e3498e28f53a0578020d3/lib/libzstd.mk#L31 Signed-off-by: Brandon Maier <brandon.maier@collins.com> [trini: Set ret to -EINVAL for the error of "failed to detect compressed" to fix warning, drop ZSTD_SRCSIZEHINT_MAX for non-Linux host tool builds] Signed-off-by: Tom Rini <trini@konsulko.com>
2023-01-11fs/squashfs: Only use export table if availableDavid Oberhollenzer1-5/+11
For a squashfs filesystem, the fragment table is followed by the following tables: NFS export table, ID table, xattr table. The export and xattr tables are both completely optional, but the ID table is mandatory. The Linux implementation refuses to mount the image if the ID table is missing. Tables that are no present have their location in the super block set to 0xFFFFFFFFFFFFFFFF. The u-boot implementation previously assumed that it can always rely on the export table location as an upper bound for the fragment table, trying (and failing) to read past filesystem bounds if it is not present. This patch changes the driver to use the ID table instead and only use the export table location if it lies between the two. Signed-off-by: David Oberhollenzer <goliath@infraroot.at> Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
2022-12-08fs/squashfs: use lldiv function for mathKasper Revsbech1-1/+1
When compling for x86: u-boot/fs/squashfs/sqfs.c:90: undefined reference to `__udivmoddi4' Signed-off-by: Kasper Revsbech <kasper.revsbech.ext@siemensgamesa.com> Tested-by: Sean Nyekjaer <sean@geanix.com>
2022-07-11Merge branch 'next'Tom Rini1-1/+1
2022-06-28fs/squashfs: Use kcalloc when relevantMiquel Raynal1-1/+3
A crafted squashfs image could embed a huge number of empty metadata blocks in order to make the amount of malloc()'d memory overflow and be much smaller than expected. Because of this flaw, any random code positioned at the right location in the squashfs image could be memcpy'd from the squashfs structures into U-Boot code location while trying to access the rearmost blocks, before being executed. In order to prevent this vulnerability from being exploited in eg. a secure boot environment, let's add a check over the amount of data that is going to be allocated. Such a check could look like: if (!elem_size || n > SIZE_MAX / elem_size) return NULL; The right way to do it would be to enhance the calloc() implementation but this is quite an impacting change for such a small fix. Another solution would be to add the check before the malloc call in the squashfs implementation, but this does not look right. So for now, let's use the kcalloc() compatibility function from Linux, which has this check. Fixes: c5100613037 ("fs/squashfs: new filesystem") Reported-by: Tatsuhiko Yasumatsu <Tatsuhiko.Yasumatsu@sony.com> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Tested-by: Tatsuhiko Yasumatsu <Tatsuhiko.Yasumatsu@sony.com>
2022-06-20Merge branch 'master' into nextTom Rini1-3/+5
Merge in v2022.07-rc5.
2022-06-16fs/squashfs: sqfs_read: Prevent arbitrary code executionMiquel Raynal1-3/+5
Following Jincheng's report, an out-of-band write leading to arbitrary code execution is possible because on one side the squashfs logic accepts directory names up to 65535 bytes (u16), while U-Boot fs logic accepts directory names up to 255 bytes long. Prevent such an exploit from happening by capping directory name sizes to 255. Use a define for this purpose so that developers can link the limitation to its source and eventually kill it some day by dynamically allocating this array (if ever desired). Link: https://lore.kernel.org/all/CALO=DHFB+yBoXxVr5KcsK0iFdg+e7ywko4-e+72kjbcS8JBfPw@mail.gmail.com Reported-by: Jincheng Wang <jc.w4ng@gmail.com> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Tested-by: Jincheng Wang <jc.w4ng@gmail.com>
2022-06-06fs/squashfs: fix sqfs_read_sblk()Heinrich Schuchardt1-1/+1
Setting sblk = NULL has no effect on the caller. We want to set *sblk = NULL if an error occurrs to avoid usage after free. Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
2022-06-03squashfs: Fix compilation on big endian systemsPali Rohár2-4/+2
Signed-off-by: Pali Rohár <pali@kernel.org> Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
2022-05-23fs/squashfs: use lldiv function for mathSean Nyekjaer1-2/+3
When compling for x86: ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read': u-boot/fs/squashfs/sqfs.c:1443: undefined reference to `__udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to `__udivmoddi4' Signed-off-by: Sean Nyekjaer <sean.nyekjaer.ext@siemensgamesa.com> Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com> Reviewed-by: Pali Rohár <pali@kernel.org>
2022-04-19fs/squashfs: simplify sqfs_read()Heinrich Schuchardt1-8/+5
* Don't check argument of free(). Free does this itself. * Reduce scope of data_buffer. Remove duplicate free(). * Avoid superfluous NULL assignment. Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com> Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
2022-01-29squashfs: show an error message if the inode_table can't be, allocatedLars Weber1-0/+2
Signed-off-by: Lars Weber <weber@weber-software.com>
2021-09-16sqfs: Suppress the message about missing filesystemSimon Glass1-1/+1
This message comes up a lot when scanning filesystems. It suggests to the user that there is some sort of error, but in fact there is no reason to expect that a particular partition has a sqfs filesystem. Other filesystems don't print this error. Turn it into a debug message. Signed-off-by: Simon Glass <sjg@chromium.org> Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
2021-08-04fs/squashfs: Fix some hardlinks reading the wrong inodeCampbell Suter1-1/+1
In SquashFS, the contents of a directory is stored by squashfs_directory_entry structures which contain the file's name, inode and position within the filesystem. The inode number is not stored directly; instead each directory has one or more headers which set a base inode number, and files store the offset from that to the file's inode number. In mksquashfs, each inode is allocated a number in the same order as they are written to the directory table; thus the offset from the header's base inode number to the file's inode number is usually positive. Hardlinks are simply stored with two directory entries referencing the same file. This means the second entry will thus have an inode number much lower than the surrounding files. Since the header's base inode number comes from the first entry that uses the header, this delta will usually be negative. Previously, U-Boot's squashfs_directory_entry.inode_offset field was declared as an unsigned value. Thus when a negative value was found, it would either resolve to an invalid inode number or to that of an unrelated file. A squashfs image to test this can be created like so: echo hi > sqfs_test_files/001-root-file mkdir sqfs_test_files/002-subdir touch sqfs_test_files/002-subdir/003-file ln sqfs_test_files/{001-root-file,002-subdir/004-link} mksquashfs sqfs_test_files/ test.sqfs -noappend Note that squashfs sorts the files ASCIIbetacally, so we can use the names to control the order they appear in. The ordering is important - the first reference to the file must have a lower inode number than the directory in which the second reference resides, and the second reference cannot be the first file in the directory. Listing this sample image in U-Boot results in: => sqfsls virtio 2 002-subdir 0 003-file Inode not found. 0 004-link Signed-off-by: Campbell Suter <campbell@snapit.group> Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
2021-06-09fs/squashfs: fix reading of fragmented filesJoao Marcos Costa1-10/+6
The fragmented files were not correctly read because of two issues: - The squashfs_file_info struct has a field named 'comp', which tells if the file's fragment is compressed or not. This field was always set to 'true' in sqfs_get_regfile_info and sqfs_get_lregfile_info. It should actually take sqfs_frag_lookup's return value. This patch addresses these two assignments. - In sqfs_read, the fragments (compressed or not) were copied to the output buffer through a for loop which was reading data at the wrong offset. Replace these loops by equivalent calls to memcpy, with the right parameters. I tested this patch by comparing the MD5 checksum of a few fragmented files with the respective md5sum output in sandbox, considering both compressed and uncompressed fragments. Signed-off-by: Joao Marcos Costa <jmcosta944@gmail.com> Tested-by: Richard Genoud <richard.genoud@posteo.net> Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
2021-05-26fs/squashfs: zero out unused fields in fs_direntHeinrich Schuchardt1-1/+1
When reading directories the UEFI sub-system must supply file attributes and timestamps. These fields will have to be added to struct fs_dirent. SquashFS should not fill these fields with random data. Ensure that they are zeroed out. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
2021-03-04fs/squashfs: Fix compilation errorPali Rohár1-0/+1
Commit 401d1c4f5d2d29c4bc4beaec95402ca23eb63295 ("common: Drop asm/global_data.h from common header") broke compilation of squashfs filesystem when CONFIG_CMD_SQUASHFS=y is enabled. Compilation is failing on error: aarch64-linux-gnu-ld.bfd: u-boot/fs/squashfs/sqfs_inode.c:121: undefined reference to `le32_to_cpu' Fixes: 401d1c4f5d2d29c4bc4beaec95402ca23eb63295 ("common: Drop asm/global_data.h from common header") Suggested-by: Tom Rini <trini@konsulko.com> Signed-off-by: Pali Rohár <pali@kernel.org> Reviewed-by: Tom Rini <trini@konsulko.com>
2021-02-24fs/squashfs: NULL dereference in sqfs_closedir()Heinrich Schuchardt1-0/+3
sqfs_opendir() called in sqfs_size(), sqfs_read(), sqfs_exists() may fail leading to sqfs_closedir(NULL) being called. Do not dereference NULL. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2021-01-20Fix squashfs failing to load sparse filesCampbell Suter1-19/+35
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 <campbell@snapit.group>
2020-12-02fs/squashfs: sqfs_close/sqfs_read_sblk: set ctxt.sblk to NULL after freeRichard Genoud1-1/+3
This will prevent a double free error if sqfs_close() is called twice. Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: implement exists() functionRichard Genoud1-0/+38
This permits to find a file and use the distro_bootcmd Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read: remove buggy offset functionalityRichard Genoud1-4/+12
offset is the offset in the file read, not the offset in the destination buffer. If the offset is not null, this will lead to a memory corruption. So, for now, we are returning an error if the offset is used. Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read: don't write beyond buffer sizeRichard Genoud1-0/+8
The length of the buffer wasn't taken into account when writing to the given buffer. Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_probe: use sqfs_decompressor_init() return valueRichard Genoud1-2/+0
sqfs_decompressor_init() returns a value, so it's better to use it than to force the return value to EINVAL (it could be ENOMEM) Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_probe: reset cur_dev/cur_part_info to NULL on errorRichard Genoud1-1/+1
Resetting the context on error will prevent some checks like: if (!ctx.cur_dev) To pass when the probe method has failed Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_probe: fix possible memory leak on errorRichard Genoud1-5/+9
If SquashFS magic number is invalid, there's a memory leak. Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read: fix memory leak on finfo.blk_sizesRichard Genoud1-26/+22
finfo.blk_sizes may not be freed in case of error in the for loop Setting it to null and freeing it at the end makes prevents that from happening. Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_get_abs_path: fix possible memory leak on errorRichard Genoud1-14/+18
if sqfs_tokenize(rel_tokens, rc, rel); fails, the function exits without freeing the array base_tokens. Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_get_abs_path: fix error checkRichard Genoud1-1/+1
the return value of sqfs_tokenize(rel_tokens, rc, rel); wasn't checked. (but "ret" value was !) This is obviouly a typo. Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_frag_lookup: simplify error handlingRichard Genoud1-12/+16
For consistency with other functions. Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read: fix another memory leakRichard Genoud1-0/+2
data_buffer was allocated in a loop and freed only once. Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read: fix memory leakRichard Genoud1-1/+1
sqfs_closedir() should be called to free memory allocated by sqfs_opendir() Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read: remove useless sqfs_closedir()Richard Genoud1-1/+0
as sqfs_opendir failed, there's no need to call sqfs_closedir Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read: fix dangling pointer dirs->entryRichard Genoud1-0/+1
dirs->entry shouldn't be left dangling as it could be freed twice. Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_size: remove useless sqfs_closedir()Richard Genoud1-1/+0
as sqfs_opendir failed, there's no need to call sqfs_closedir Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_size: fix dangling pointer dirs->entryRichard Genoud1-0/+2
dirs->entry shouldn't be left dangling as it could be freed twice. Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_concat_tokens: check if malloc succeedsRichard Genoud1-0/+3
memory allocation should always be checked Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read_inode_table: fix dangling pointerRichard Genoud1-0/+1
inode_table should not be left dangling as it may be freed in sqfs_opendir Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_search_dir: fix memory leaksRichard Genoud1-13/+51
path, target, res, rem and sym_tokens were not free on error nor success. Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_search_dir: fix dangling pointerRichard Genoud1-0/+5
dirs->entry shouldn't be left dangling as it could be freed twice. Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_read_directory_table: fix memory leakRichard Genoud1-14/+17
pos_list wasn't freed on every error Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_split_path: fix memory leak and dangling pointersRichard Genoud1-12/+28
*file and *dir were not freed on error Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_closedir: fix memory leakRichard Genoud1-0/+1
sqfs_dirs wasn't freed anywhere. Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_opendir: simplify error handlingRichard Genoud1-15/+12
Using only one label permits to prevents bugs when moving code around. Reviewed-by: Joao Marcos Costa <jmcosta944@gmail.com> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: sqfs_opendir: fix some memory leaks and dangling pointersRichard Genoud1-8/+29
When trying to load an non-existing file, the cpu hangs! Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
2020-11-19fs/squashfs: Fix index off by 1 for inode SQFS_LDIR_TYPEGerard Koskamp1-2/+2
I've created a squashfs file system with Yocto (it use squashfs-tools) and u-boot command sqfsls give the error:'Error while searching inode: unknown type.' After some digging in the code I found that the index is off by 1. This patch fix this issue and I can successful use the sqfsls command. After search for the squashfs format I found a link talk about a similar issue but this time in the documentation. The link is: https://github.com/AgentD/squashfs-tools-ng/commit/e6588526838caece9529 Signed-off-by: Gerard Koskamp <gerard.koskamp@nedap.com> Tested-by: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
2020-09-29fs/squashfs: parameter check sqfs_read_metablock()Heinrich Schuchardt1-2/+2
We should check if the incoming parameter file_mapping is not NULL instead of checking after adding an offset. Reported-by: Coverity CID 307210 Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Acked-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2020-09-18fs/squashfs: Fix Coverity Scan defectsJoao Marcos Costa3-10/+18
Fix control flow issues and null pointer dereferences. Signed-off-by: Joao Marcos Costa <jmcosta944@gmail.com>