OSDN Git Service

Check sector and cluster size before use.
[android-x86/external-exfat.git] / libexfat / mount.c
index 70c8af5..f25d71d 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2013  Andrew Nayenko
+       Copyright (C) 2010-2015  Andrew Nayenko
 
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
@@ -84,13 +84,11 @@ static bool match_option(const char* options, const char* option_name)
 
 static void parse_options(struct exfat* ef, const char* options)
 {
-       int sys_umask = umask(0);
        int opt_umask;
 
-       umask(sys_umask); /* restore umask */
-       opt_umask = get_int_option(options, "umask", 8, sys_umask);
-       ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777;
-       ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777;
+       opt_umask = get_int_option(options, "umask", 8, 0);
+       ef->dmask = get_int_option(options, "dmask", 8, opt_umask);
+       ef->fmask = get_int_option(options, "fmask", 8, opt_umask);
 
        ef->uid = get_int_option(options, "uid", 10, geteuid());
        ef->gid = get_int_option(options, "gid", 10, getegid());
@@ -98,7 +96,7 @@ static void parse_options(struct exfat* ef, const char* options)
        ef->noatime = match_option(options, "noatime");
 }
 
-static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
+static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector,
                off_t sector_size)
 {
        uint32_t vbr_checksum;
@@ -107,7 +105,7 @@ static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
        if (exfat_pread(dev, sector, sector_size, 0) < 0)
        {
                exfat_error("failed to read boot sector");
-               return 1;
+               return false;
        }
        vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
        for (i = 1; i < 11; i++)
@@ -115,7 +113,7 @@ static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
                if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
                {
                        exfat_error("failed to read VBR sector");
-                       return 1;
+                       return false;
                }
                vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
                                vbr_checksum);
@@ -123,16 +121,16 @@ static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
        if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
        {
                exfat_error("failed to read VBR checksum sector");
-               return 1;
+               return false;
        }
        for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
                if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
                {
                        exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
                                        le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
-                       return 1;
+                       return false;
                }
-       return 0;
+       return true;
 }
 
 static int commit_super_block(const struct exfat* ef)
@@ -208,6 +206,23 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
                exfat_error("exFAT file system is not found");
                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);
+               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);
+               return -EIO;
+       }
        ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
        if (ef->zero_cluster == NULL)
        {
@@ -217,8 +232,7 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
                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)) != 0)
+       if (!verify_vbr_checksum(ef->dev, ef->zero_cluster, SECTOR_SIZE(*ef->sb)))
        {
                free(ef->zero_cluster);
                exfat_close(ef->dev);
@@ -243,27 +257,15 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
                free(ef->sb);
                return -EIO;
        }
-       /* officially exFAT supports cluster size up to 32 MB */
-       if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
-       {
-               free(ef->zero_cluster);
-               exfat_close(ef->dev);
-               exfat_error("too big cluster size: 2^%d",
-                               (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
-               free(ef->sb);
-               return -EIO;
-       }
        if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) >
                        exfat_get_size(ef->dev))
        {
-               free(ef->zero_cluster);
-               exfat_error("file system is larger than underlying device: "
+               /* 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_get_size(ef->dev));
-               exfat_close(ef->dev);
-               free(ef->sb);
-               return -EIO;
        }
 
        ef->root = malloc(sizeof(struct exfat_node));
@@ -343,11 +345,12 @@ static void finalize_super_block(struct exfat* ef)
                ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
        }
 
-       commit_super_block(ef);
+       commit_super_block(ef); /* ignore return code */
 }
 
 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);