From 3639f26edacd0198b1d3e4834000a88b6a43612c Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Sun, 25 Jun 2017 12:15:00 +0200 Subject: [PATCH] New upstream version 1.2.7 --- ChangeLog | 6 ++++ Makefile.in | 8 ++--- configure | 20 +++++------ configure.ac | 2 +- dump/main.c | 2 +- fsck/main.c | 4 +-- libexfat/cluster.c | 21 ++++++----- libexfat/exfat.h | 6 ++-- libexfat/io.c | 31 +++++++++++++--- libexfat/lookup.c | 4 +-- libexfat/mount.c | 90 ++++++++++++++++++++++------------------------ libexfat/node.c | 45 +++++++++++++---------- 12 files changed, 135 insertions(+), 104 deletions(-) diff --git a/ChangeLog b/ChangeLog index 445cd34..1d3e3c4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +1.2.7 (2017-06-05) + +* Fixed handling of two last clusters: operations with files that occupy these +clusters could fail. +* Fixed crash when started with stdin, stdout or stderr closed. + 1.2.6 (2017-01-28) * Operations with directories (except initial listing) now make less diff --git a/Makefile.in b/Makefile.in index f16c91f..63a8388 100644 --- a/Makefile.in +++ b/Makefile.in @@ -516,7 +516,7 @@ distdir: $(DISTFILES) ! -type d ! -perm -444 -exec $(install_sh) -c -m a+r {} {} \; \ || chmod -R a+r "$(distdir)" dist-gzip: distdir - tardir=$(distdir) && $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).tar.gz + tardir=$(distdir) && $(am__tar) | eval GZIP= gzip $(GZIP_ENV) -c >$(distdir).tar.gz $(am__post_remove_distdir) dist-bzip2: distdir @@ -542,7 +542,7 @@ dist-shar: distdir @echo WARNING: "Support for shar distribution archives is" \ "deprecated." >&2 @echo WARNING: "It will be removed altogether in Automake 2.0" >&2 - shar $(distdir) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).shar.gz + shar $(distdir) | eval GZIP= gzip $(GZIP_ENV) -c >$(distdir).shar.gz $(am__post_remove_distdir) dist-zip: distdir @@ -560,7 +560,7 @@ dist dist-all: distcheck: dist case '$(DIST_ARCHIVES)' in \ *.tar.gz*) \ - GZIP=$(GZIP_ENV) gzip -dc $(distdir).tar.gz | $(am__untar) ;;\ + eval GZIP= gzip $(GZIP_ENV) -dc $(distdir).tar.gz | $(am__untar) ;;\ *.tar.bz2*) \ bzip2 -dc $(distdir).tar.bz2 | $(am__untar) ;;\ *.tar.lz*) \ @@ -570,7 +570,7 @@ distcheck: dist *.tar.Z*) \ uncompress -c $(distdir).tar.Z | $(am__untar) ;;\ *.shar.gz*) \ - GZIP=$(GZIP_ENV) gzip -dc $(distdir).shar.gz | unshar ;;\ + eval GZIP= gzip $(GZIP_ENV) -dc $(distdir).shar.gz | unshar ;;\ *.zip*) \ unzip $(distdir).zip ;;\ esac diff --git a/configure b/configure index c9a8d7e..480f077 100755 --- a/configure +++ b/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.69 for Free exFAT implementation 1.2.6. +# Generated by GNU Autoconf 2.69 for Free exFAT implementation 1.2.7. # # Report bugs to . # @@ -579,8 +579,8 @@ MAKEFLAGS= # Identity of this package. PACKAGE_NAME='Free exFAT implementation' PACKAGE_TARNAME='exfat-utils' -PACKAGE_VERSION='1.2.6' -PACKAGE_STRING='Free exFAT implementation 1.2.6' +PACKAGE_VERSION='1.2.7' +PACKAGE_STRING='Free exFAT implementation 1.2.7' PACKAGE_BUGREPORT='relan@users.noreply.github.com' PACKAGE_URL='https://github.com/relan/exfat' @@ -1228,7 +1228,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures Free exFAT implementation 1.2.6 to adapt to many kinds of systems. +\`configure' configures Free exFAT implementation 1.2.7 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1294,7 +1294,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of Free exFAT implementation 1.2.6:";; + short | recursive ) echo "Configuration of Free exFAT implementation 1.2.7:";; esac cat <<\_ACEOF @@ -1386,7 +1386,7 @@ fi test -n "$ac_init_help" && exit $ac_status if $ac_init_version; then cat <<\_ACEOF -Free exFAT implementation configure 1.2.6 +Free exFAT implementation configure 1.2.7 generated by GNU Autoconf 2.69 Copyright (C) 2012 Free Software Foundation, Inc. @@ -1441,7 +1441,7 @@ cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by Free exFAT implementation $as_me 1.2.6, which was +It was created by Free exFAT implementation $as_me 1.2.7, which was generated by GNU Autoconf 2.69. Invocation command line was $ $0 $@ @@ -2304,7 +2304,7 @@ fi # Define the identity of the package. PACKAGE='exfat-utils' - VERSION='1.2.6' + VERSION='1.2.7' cat >>confdefs.h <<_ACEOF @@ -4611,7 +4611,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 # report actual input values of CONFIG_FILES etc. instead of their # values after options handling. ac_log=" -This file was extended by Free exFAT implementation $as_me 1.2.6, which was +This file was extended by Free exFAT implementation $as_me 1.2.7, which was generated by GNU Autoconf 2.69. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -4678,7 +4678,7 @@ _ACEOF cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`" ac_cs_version="\\ -Free exFAT implementation config.status 1.2.6 +Free exFAT implementation config.status 1.2.7 configured by $0, generated by GNU Autoconf 2.69, with options \\"\$ac_cs_config\\" diff --git a/configure.ac b/configure.ac index be2e048..7412f59 100644 --- a/configure.ac +++ b/configure.ac @@ -21,7 +21,7 @@ # AC_INIT([Free exFAT implementation], - [1.2.6], + [1.2.7], [relan@users.noreply.github.com], [exfat-utils], [https://github.com/relan/exfat]) diff --git a/dump/main.c b/dump/main.c index 420a951..4a764a8 100644 --- a/dump/main.c +++ b/dump/main.c @@ -168,7 +168,7 @@ static int dump_file_fragments(const char* spec, const char* path) { off_t lsize; - if (CLUSTER_INVALID(cluster)) + if (CLUSTER_INVALID(*ef.sb, cluster)) { exfat_error("'%s' has invalid cluster %#x", path, cluster); rc = 1; diff --git a/fsck/main.c b/fsck/main.c index b413c58..b5e6f48 100644 --- a/fsck/main.c +++ b/fsck/main.c @@ -39,7 +39,7 @@ static int nodeck(struct exfat* ef, struct exfat_node* node) while (clusters--) { - if (CLUSTER_INVALID(c)) + if (CLUSTER_INVALID(*ef->sb, c)) { char name[EXFAT_UTF8_NAME_BUFFER_MAX]; @@ -98,7 +98,7 @@ static void dirck(struct exfat* ef, const char* path) exfat_put_node(ef, parent); return; } - while ((node = exfat_readdir(ef, &it))) + while ((node = exfat_readdir(&it))) { exfat_get_name(node, entry_path + path_length + 1); exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path, diff --git a/libexfat/cluster.c b/libexfat/cluster.c index 1d39233..5a26133 100644 --- a/libexfat/cluster.c +++ b/libexfat/cluster.c @@ -103,7 +103,7 @@ cluster_t exfat_advance_cluster(const struct exfat* ef, for (i = node->fptr_index; i < count; i++) { node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster); - if (CLUSTER_INVALID(node->fptr_cluster)) + if (CLUSTER_INVALID(*ef->sb, node->fptr_cluster)) break; /* the caller should handle this and print appropriate error message */ } @@ -214,10 +214,8 @@ static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint) static void free_cluster(struct exfat* ef, cluster_t cluster) { - if (CLUSTER_INVALID(cluster)) - exfat_bug("freeing invalid cluster 0x%x", cluster); if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size) - exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster, + exfat_bug("caller must check cluster validity (%#x, %#x)", cluster, ef->cmap.size); BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER); @@ -252,7 +250,7 @@ static int grow_file(struct exfat* ef, struct exfat_node* node, { /* get the last cluster of the file */ previous = exfat_advance_cluster(ef, node, current - 1); - if (CLUSTER_INVALID(previous)) + if (CLUSTER_INVALID(*ef->sb, previous)) { exfat_error("invalid cluster 0x%x while growing", previous); return -EIO; @@ -265,7 +263,7 @@ static int grow_file(struct exfat* ef, struct exfat_node* node, /* file does not have clusters (i.e. is empty), allocate the first one for it */ previous = allocate_cluster(ef, 0); - if (CLUSTER_INVALID(previous)) + if (CLUSTER_INVALID(*ef->sb, previous)) return -ENOSPC; node->fptr_cluster = node->start_cluster = previous; allocated = 1; @@ -276,7 +274,7 @@ static int grow_file(struct exfat* ef, struct exfat_node* node, while (allocated < difference) { next = allocate_cluster(ef, previous + 1); - if (CLUSTER_INVALID(next)) + if (CLUSTER_INVALID(*ef->sb, next)) { if (allocated != 0) shrink_file(ef, node, current + allocated, allocated); @@ -321,7 +319,7 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node, { cluster_t last = exfat_advance_cluster(ef, node, current - difference - 1); - if (CLUSTER_INVALID(last)) + if (CLUSTER_INVALID(*ef->sb, last)) { exfat_error("invalid cluster 0x%x while shrinking", last); return -EIO; @@ -343,12 +341,13 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node, /* free remaining clusters */ while (difference--) { - if (CLUSTER_INVALID(previous)) + if (CLUSTER_INVALID(*ef->sb, previous)) { exfat_error("invalid cluster 0x%x while freeing after shrink", previous); return -EIO; } + next = exfat_next_cluster(ef, node, previous); if (!set_next_cluster(ef, node->is_contiguous, previous, EXFAT_CLUSTER_FREE)) @@ -381,7 +380,7 @@ static int erase_range(struct exfat* ef, struct exfat_node* node, cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1; cluster = exfat_advance_cluster(ef, node, begin / CLUSTER_SIZE(*ef->sb)); - if (CLUSTER_INVALID(cluster)) + if (CLUSTER_INVALID(*ef->sb, cluster)) { exfat_error("invalid cluster 0x%x while erasing", cluster); return -EIO; @@ -395,7 +394,7 @@ static int erase_range(struct exfat* ef, struct exfat_node* node, { cluster = exfat_next_cluster(ef, node, cluster); /* the cluster cannot be invalid because we have just allocated it */ - if (CLUSTER_INVALID(cluster)) + if (CLUSTER_INVALID(*ef->sb, cluster)) exfat_bug("invalid cluster 0x%x after allocation", cluster); if (!erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster))) return -EIO; diff --git a/libexfat/exfat.h b/libexfat/exfat.h index 21b760e..b913ca2 100644 --- a/libexfat/exfat.h +++ b/libexfat/exfat.h @@ -43,8 +43,8 @@ #define SECTOR_SIZE(sb) (1 << (sb).sector_bits) #define CLUSTER_SIZE(sb) (SECTOR_SIZE(sb) << (sb).spc_bits) -#define CLUSTER_INVALID(c) \ - ((c) < EXFAT_FIRST_DATA_CLUSTER || (c) > EXFAT_LAST_DATA_CLUSTER) +#define CLUSTER_INVALID(sb, c) ((c) < EXFAT_FIRST_DATA_CLUSTER || \ + (c) - EXFAT_FIRST_DATA_CLUSTER >= le32_to_cpu((sb).cluster_count)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) @@ -161,7 +161,7 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node, int exfat_opendir(struct exfat* ef, struct exfat_node* dir, struct exfat_iterator* it); void exfat_closedir(struct exfat* ef, struct exfat_iterator* it); -struct exfat_node* exfat_readdir(struct exfat* ef, struct exfat_iterator* it); +struct exfat_node* exfat_readdir(struct exfat_iterator* it); int exfat_lookup(struct exfat* ef, struct exfat_node** node, const char* path); int exfat_split(struct exfat* ef, struct exfat_node** parent, diff --git a/libexfat/io.c b/libexfat/io.c index 514383c..63ccfdb 100644 --- a/libexfat/io.c +++ b/libexfat/io.c @@ -45,6 +45,11 @@ struct exfat_dev off_t size; /* in bytes */ }; +static bool is_open(int fd) +{ + return fcntl(fd, F_GETFD) != -1; +} + static int open_ro(const char* spec) { return open(spec, O_RDONLY); @@ -75,6 +80,24 @@ struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode) struct exfat_dev* dev; struct stat stbuf; + /* The system allocates file descriptors sequentially. If we have been + started with stdin (0), stdout (1) or stderr (2) closed, the system + will give us descriptor 0, 1 or 2 later when we open block device, + FUSE communication pipe, etc. As a result, functions using stdin, + stdout or stderr will actualy work with a different thing and can + corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */ + while (!is_open(STDIN_FILENO) + || !is_open(STDOUT_FILENO) + || !is_open(STDERR_FILENO)) + { + /* we don't need those descriptors, let them leak */ + if (open("/dev/null", O_RDWR) == -1) + { + exfat_error("failed to open /dev/null"); + return NULL; + } + } + dev = malloc(sizeof(struct exfat_dev)); if (dev == NULL) { @@ -289,7 +312,7 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node, return 0; cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb)); - if (CLUSTER_INVALID(cluster)) + if (CLUSTER_INVALID(*ef->sb, cluster)) { exfat_error("invalid cluster 0x%x while reading", cluster); return -EIO; @@ -299,7 +322,7 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node, remainder = MIN(size, node->size - offset); while (remainder > 0) { - if (CLUSTER_INVALID(cluster)) + if (CLUSTER_INVALID(*ef->sb, cluster)) { exfat_error("invalid cluster 0x%x while reading", cluster); return -EIO; @@ -345,7 +368,7 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node, return 0; cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb)); - if (CLUSTER_INVALID(cluster)) + if (CLUSTER_INVALID(*ef->sb, cluster)) { exfat_error("invalid cluster 0x%x while writing", cluster); return -EIO; @@ -355,7 +378,7 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node, remainder = size; while (remainder > 0) { - if (CLUSTER_INVALID(cluster)) + if (CLUSTER_INVALID(*ef->sb, cluster)) { exfat_error("invalid cluster 0x%x while writing", cluster); return -EIO; diff --git a/libexfat/lookup.c b/libexfat/lookup.c index 4b27cc5..137a84a 100644 --- a/libexfat/lookup.c +++ b/libexfat/lookup.c @@ -46,7 +46,7 @@ void exfat_closedir(struct exfat* ef, struct exfat_iterator* it) it->current = NULL; } -struct exfat_node* exfat_readdir(struct exfat* ef, struct exfat_iterator* it) +struct exfat_node* exfat_readdir(struct exfat_iterator* it) { if (it->current == NULL) it->current = it->parent->child; @@ -93,7 +93,7 @@ static int lookup_name(struct exfat* ef, struct exfat_node* parent, rc = exfat_opendir(ef, parent, &it); if (rc != 0) return rc; - while ((*node = exfat_readdir(ef, &it))) + while ((*node = exfat_readdir(&it))) { if (compare_name(ef, buffer, (*node)->name) == 0) { diff --git a/libexfat/mount.c b/libexfat/mount.c index f907f29..43c79eb 100644 --- a/libexfat/mount.c +++ b/libexfat/mount.c @@ -44,7 +44,7 @@ static uint64_t rootdir_size(const struct exfat* ef) clusters); return 0; } - if (CLUSTER_INVALID(rootdir_cluster)) + if (CLUSTER_INVALID(*ef->sb, rootdir_cluster)) { exfat_error("bad cluster %#x while reading root directory", rootdir_cluster); @@ -165,6 +165,22 @@ static int prepare_super_block(const struct exfat* ef) return commit_super_block(ef); } +static void exfat_free(struct exfat* ef) +{ + exfat_close(ef->dev); /* first of all, close the descriptor */ + ef->dev = NULL; /* struct exfat_dev is freed by exfat_close() */ + free(ef->root); + ef->root = NULL; + free(ef->zero_cluster); + ef->zero_cluster = NULL; + free(ef->cmap.chunk); + ef->cmap.chunk = NULL; + free(ef->upcase); + ef->upcase = NULL; + free(ef->sb); + ef->sb = NULL; +} + int exfat_mount(struct exfat* ef, const char* spec, const char* options) { int rc; @@ -195,75 +211,64 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options) ef->sb = malloc(sizeof(struct exfat_super_block)); if (ef->sb == NULL) { - exfat_close(ef->dev); exfat_error("failed to allocate memory for the super block"); + exfat_free(ef); return -ENOMEM; } memset(ef->sb, 0, sizeof(struct exfat_super_block)); if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0) { - exfat_close(ef->dev); - free(ef->sb); exfat_error("failed to read boot sector"); + exfat_free(ef); return -EIO; } if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0) { - exfat_close(ef->dev); - free(ef->sb); exfat_error("exFAT file system is not found"); + exfat_free(ef); return -EIO; } /* sector cannot be smaller than 512 bytes */ if (ef->sb->sector_bits < 9) { - exfat_close(ef->dev); exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits); - free(ef->sb); + exfat_free(ef); return -EIO; } /* officially exFAT supports cluster size up to 32 MB */ if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25) { - exfat_close(ef->dev); exfat_error("too big cluster size: 2^(%hhd+%hhd)", ef->sb->sector_bits, ef->sb->spc_bits); - free(ef->sb); + exfat_free(ef); return -EIO; } ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb)); if (ef->zero_cluster == NULL) { - exfat_close(ef->dev); - free(ef->sb); exfat_error("failed to allocate zero sector"); + exfat_free(ef); return -ENOMEM; } /* use zero_cluster as a temporary buffer for VBR checksum verification */ if (!verify_vbr_checksum(ef->dev, ef->zero_cluster, SECTOR_SIZE(*ef->sb))) { - free(ef->zero_cluster); - exfat_close(ef->dev); - free(ef->sb); + exfat_free(ef); return -EIO; } memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb)); if (ef->sb->version.major != 1 || ef->sb->version.minor != 0) { - free(ef->zero_cluster); - exfat_close(ef->dev); exfat_error("unsupported exFAT version: %hhu.%hhu", ef->sb->version.major, ef->sb->version.minor); - free(ef->sb); + exfat_free(ef); return -EIO; } if (ef->sb->fat_count != 1) { - free(ef->zero_cluster); - exfat_close(ef->dev); exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count); - free(ef->sb); + exfat_free(ef); return -EIO; } if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) > @@ -271,19 +276,27 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options) { /* this can cause I/O errors later but we don't fail mounting to let user rescue data */ - exfat_warn("file system is larger than underlying device: " - "%"PRIu64" > %"PRIu64, - le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb), + exfat_warn("file system in sectors is larger than device: " + "%"PRIu64" * %d > %"PRIu64, + le64_to_cpu(ef->sb->sector_count), SECTOR_SIZE(*ef->sb), exfat_get_size(ef->dev)); } + if ((off_t) le32_to_cpu(ef->sb->cluster_count) * CLUSTER_SIZE(*ef->sb) > + exfat_get_size(ef->dev)) + { + exfat_error("file system in clusters is larger than device: " + "%u * %d > %"PRIu64, + le32_to_cpu(ef->sb->cluster_count), CLUSTER_SIZE(*ef->sb), + exfat_get_size(ef->dev)); + exfat_free(ef); + return -EIO; + } ef->root = malloc(sizeof(struct exfat_node)); if (ef->root == NULL) { - free(ef->zero_cluster); - exfat_close(ef->dev); - free(ef->sb); exfat_error("failed to allocate root node"); + exfat_free(ef); return -ENOMEM; } memset(ef->root, 0, sizeof(struct exfat_node)); @@ -294,10 +307,7 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options) ef->root->size = rootdir_size(ef); if (ef->root->size == 0) { - free(ef->root); - free(ef->zero_cluster); - exfat_close(ef->dev); - free(ef->sb); + exfat_free(ef); return -EIO; } /* exFAT does not have time attributes for the root directory */ @@ -328,10 +338,7 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options) error: exfat_put_node(ef, ef->root); exfat_reset_cache(ef); - free(ef->root); - free(ef->zero_cluster); - exfat_close(ef->dev); - free(ef->sb); + exfat_free(ef); return -EIO; } @@ -363,17 +370,6 @@ void exfat_unmount(struct exfat* ef) exfat_flush(ef); /* ignore return code */ exfat_put_node(ef, ef->root); exfat_reset_cache(ef); - free(ef->root); - ef->root = NULL; finalize_super_block(ef); - exfat_close(ef->dev); /* close descriptor immediately after fsync */ - ef->dev = NULL; - free(ef->zero_cluster); - ef->zero_cluster = NULL; - free(ef->cmap.chunk); - ef->cmap.chunk = NULL; - free(ef->sb); - ef->sb = NULL; - free(ef->upcase); - ef->upcase = NULL; + exfat_free(ef); /* will close the descriptor */ } diff --git a/libexfat/node.c b/libexfat/node.c index a3f00af..b544bd9 100644 --- a/libexfat/node.c +++ b/libexfat/node.c @@ -204,10 +204,13 @@ static bool check_entries(const struct exfat_entry* entry, int n) return true; } -static bool check_node(const struct exfat_node* node, le16_t actual_checksum, - int cluster_size, const struct exfat_entry_meta1* meta1, +static bool check_node(const struct exfat* ef, struct exfat_node* node, + le16_t actual_checksum, const struct exfat_entry_meta1* meta1, const struct exfat_entry_meta2* meta2) { + int cluster_size = CLUSTER_SIZE(*ef->sb); + uint64_t clusters_heap_size = + (uint64_t) le32_to_cpu(ef->sb->cluster_count) * cluster_size; char buffer[EXFAT_UTF8_NAME_BUFFER_MAX]; bool ret = true; @@ -251,7 +254,7 @@ static bool check_node(const struct exfat_node* node, le16_t actual_checksum, node->start_cluster); ret = false; } - if (node->size > 0 && CLUSTER_INVALID(node->start_cluster)) + if (node->size > 0 && CLUSTER_INVALID(*ef->sb, node->start_cluster)) { exfat_get_name(node, buffer); exfat_error("'%s' points to invalid cluster %#x", buffer, @@ -259,6 +262,15 @@ static bool check_node(const struct exfat_node* node, le16_t actual_checksum, ret = false; } + /* File or directory cannot be larger than clusters heap. */ + if (node->size > clusters_heap_size) + { + exfat_get_name(node, buffer); + exfat_error("'%s' is larger than clusters heap: %"PRIu64" > %"PRIu64, + buffer, node->size, clusters_heap_size); + ret = false; + } + /* Empty file or directory must be marked as non-contiguous. */ if (node->size == 0 && node->is_contiguous) { @@ -280,8 +292,8 @@ static bool check_node(const struct exfat_node* node, le16_t actual_checksum, return ret; } -static int parse_file_entries(struct exfat* ef, struct exfat_node* parent, - struct exfat_node* node, const struct exfat_entry* entries, int n) +static int parse_file_entries(struct exfat* ef, struct exfat_node* node, + const struct exfat_entry* entries, int n) { const struct exfat_entry_meta1* meta1; const struct exfat_entry_meta2* meta2; @@ -314,8 +326,7 @@ static int parse_file_entries(struct exfat* ef, struct exfat_node* parent, init_node_meta2(node, meta2); init_node_name(node, entries + 2, mandatory_entries - 2); - if (!check_node(node, exfat_calc_checksum(entries, n), - CLUSTER_SIZE(*ef->sb), meta1, meta2)) + if (!check_node(ef, node, exfat_calc_checksum(entries, n), meta1, meta2)) return -EIO; return 0; @@ -337,7 +348,7 @@ static int parse_file_entry(struct exfat* ef, struct exfat_node* parent, return -ENOMEM; (*node)->entry_offset = *offset; - rc = parse_file_entries(ef, parent, *node, entries, n); + rc = parse_file_entries(ef, *node, entries, n); if (rc != 0) { free(*node); @@ -401,7 +412,7 @@ static int readdir(struct exfat* ef, struct exfat_node* parent, if (ef->upcase != NULL) break; upcase = (const struct exfat_entry_upcase*) &entry; - if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster))) + if (CLUSTER_INVALID(*ef->sb, le32_to_cpu(upcase->start_cluster))) { exfat_error("invalid cluster 0x%x in upcase table", le32_to_cpu(upcase->start_cluster)); @@ -452,14 +463,13 @@ static int readdir(struct exfat* ef, struct exfat_node* parent, case EXFAT_ENTRY_BITMAP: bitmap = (const struct exfat_entry_bitmap*) &entry; ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster); - if (CLUSTER_INVALID(ef->cmap.start_cluster)) + if (CLUSTER_INVALID(*ef->sb, ef->cmap.start_cluster)) { exfat_error("invalid cluster 0x%x in clusters bitmap", ef->cmap.start_cluster); return -EIO; } - ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) - - EXFAT_FIRST_DATA_CLUSTER; + ef->cmap.size = le32_to_cpu(ef->sb->cluster_count); if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8)) { exfat_error("invalid clusters bitmap size: %"PRIu64 @@ -865,7 +875,7 @@ static int find_slot(struct exfat* ef, struct exfat_node* dir, } static int commit_entry(struct exfat* ef, struct exfat_node* dir, - const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib) + const le16_t* name, off_t offset, uint16_t attrib) { struct exfat_node* node; const size_t name_length = utf16_length(name); @@ -925,7 +935,6 @@ static int create(struct exfat* ef, const char* path, uint16_t attrib) { struct exfat_node* dir; struct exfat_node* existing; - cluster_t cluster = EXFAT_CLUSTER_BAD; off_t offset = -1; le16_t name[EXFAT_NAME_MAX + 1]; int rc; @@ -947,7 +956,7 @@ static int create(struct exfat* ef, const char* path, uint16_t attrib) exfat_put_node(ef, dir); return rc; } - rc = commit_entry(ef, dir, name, cluster, offset, attrib); + rc = commit_entry(ef, dir, name, offset, attrib); if (rc != 0) { exfat_put_node(ef, dir); @@ -995,8 +1004,7 @@ int exfat_mkdir(struct exfat* ef, const char* path) } static int rename_entry(struct exfat* ef, struct exfat_node* dir, - struct exfat_node* node, const le16_t* name, cluster_t new_cluster, - off_t new_offset) + struct exfat_node* node, const le16_t* name, off_t new_offset) { const size_t name_length = utf16_length(name); const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX); @@ -1048,7 +1056,6 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path) struct exfat_node* node; struct exfat_node* existing; struct exfat_node* dir; - cluster_t cluster = EXFAT_CLUSTER_BAD; off_t offset = -1; le16_t name[EXFAT_NAME_MAX + 1]; int rc; @@ -1129,7 +1136,7 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path) exfat_put_node(ef, node); return rc; } - rc = rename_entry(ef, dir, node, name, cluster, offset); + rc = rename_entry(ef, dir, node, name, offset); if (rc != 0) { exfat_put_node(ef, dir); -- 2.39.5