From 575ba4bca69096d5c795f466f2fdd4600a36fd4f Mon Sep 17 00:00:00 2001 From: relan Date: Tue, 19 Jul 2016 18:29:25 +0300 Subject: [PATCH] Add node start cluster checks. --- libexfat/node.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/libexfat/node.c b/libexfat/node.c index 4a3ee40..c506fad 100644 --- a/libexfat/node.c +++ b/libexfat/node.c @@ -101,6 +101,13 @@ static int opendir(struct exfat* ef, const struct exfat_node* dir, exfat_get_name(dir, buffer, sizeof(buffer) - 1); exfat_bug("'%s' is not a directory", buffer); } + if (CLUSTER_INVALID(dir->start_cluster)) + { + exfat_get_name(dir, buffer, sizeof(buffer) - 1); + exfat_error("'%s' directory starts with invalid cluster %#x", buffer, + dir->start_cluster); + return -EIO; + } it->cluster = dir->start_cluster; it->offset = 0; it->chunk = malloc(CLUSTER_SIZE(*ef->sb)); @@ -230,6 +237,27 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum, ret = false; } + /* + Empty file must have zero start cluster. Non-empty file must start + with a valid cluster. Directories cannot be empty (i.e. must always + have a valid start cluster), but we will check this later in opendir() + to give user a chance to read current directory. + */ + if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE) + { + exfat_get_name(node, buffer, sizeof(buffer) - 1); + exfat_error("'%s' is empty but start cluster is %#x", buffer, + node->start_cluster); + ret = false; + } + if (node->size > 0 && CLUSTER_INVALID(node->start_cluster)) + { + exfat_get_name(node, buffer, sizeof(buffer) - 1); + exfat_error("'%s' points to invalid cluster %#x", buffer, + node->start_cluster); + ret = false; + } + return ret; } -- 2.11.0