aboutsummaryrefslogtreecommitdiff
path: root/fs/squashfs/sqfs.c
AgeCommit message (Collapse)AuthorFilesLines
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ár1-2/+1
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-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-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-09-18fs/squashfs: Fix Coverity Scan defectsJoao Marcos Costa1-7/+13
Fix control flow issues and null pointer dereferences. Signed-off-by: Joao Marcos Costa <jmcosta944@gmail.com>
2020-08-24fs/squashfs: Fix Coverity Scan defectsJoao Marcos Costa1-11/+29
Fix defects such as uninitialized variables and untrusted pointer operations. Most part of the tainted variables and the related defects actually comes from Linux's macro get_unaligned_le**, extensively used in SquashFS code. Add sanity checks for those variables. Signed-off-by: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
2020-08-24fs/squashfs: replace sqfs_decompress() parameterJoao Marcos Costa1-16/+9
Replace 'u16 comp_type' by a reference to squashfs_ctxt structure. Signed-off-by: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
2020-08-24fs/squashfs: Add init and clean-up functions to decompressionJoao Marcos Costa1-6/+9
Add sqfs_decompressor_init() and sqfs_decompressor_cleanup(). These functions are called respectively in sqfs_probe() and sqfs_close(). For now, only ZSTD requires an initialization logic. ZSTD support will be added in a follow-up commit. Move squashfs_ctxt definition to sqfs_filesystem.h. This structure is passed to sqfs_decompressor_init() and sqfs_decompressor_cleanup(), so it can no longer be local to sqfs.c. Signed-off-by: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
2020-08-07fs/squashfs: new filesystemJoao Marcos Costa1-0/+1538
Add support for SquashFS filesystem. Right now, it does not support compression but support for zlib will be added in a follow-up commit. Signed-off-by: Joao Marcos Costa <joaomarcos.costa@bootlin.com>