From a4f48d0f44d09b6a9a3b16f6c0121ffd5123eef3 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Mon, 3 Aug 2015 18:06:39 +0200 Subject: [PATCH] vold: fix 64 bit ioctl error Changing the num_sectors used in ioctl with BLKGETSIZE because the kernel expects an unsigned long type and then changes 64 bits with a 64 bits userspace. This overwrites what's located close to the parameter location if any. Change-Id: I78fd61a1084de2741f39b926aa436462518709a0 Signed-off-by: Mateusz Nowak Signed-off-by: Zhiquan Liu --- CommandListener.cpp | 4 ++-- Devmapper.cpp | 2 +- Devmapper.h | 2 +- Loop.cpp | 6 +++--- Loop.h | 4 ++-- VolumeManager.cpp | 26 +++++++++++++------------- VolumeManager.h | 4 ++-- fs/Ext4.cpp | 8 ++++---- fs/Ext4.h | 4 ++-- fs/Vfat.cpp | 4 ++-- fs/Vfat.h | 2 +- 11 files changed, 33 insertions(+), 33 deletions(-) diff --git a/CommandListener.cpp b/CommandListener.cpp index a9a8031d..03f8c731 100644 --- a/CommandListener.cpp +++ b/CommandListener.cpp @@ -408,7 +408,7 @@ int CommandListener::AsecCmd::runCommand(SocketClient *cli, return 0; } - unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; + unsigned long numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; const bool isExternal = (atoi(argv[7]) == 1); rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal); } else if (!strcmp(argv[1], "resize")) { @@ -417,7 +417,7 @@ int CommandListener::AsecCmd::runCommand(SocketClient *cli, cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize ", false); return 0; } - unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; + unsigned long numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; rc = vm->resizeAsec(argv[2], numSectors, argv[4]); } else if (!strcmp(argv[1], "finalize")) { dumpArgs(argc, argv, -1); diff --git a/Devmapper.cpp b/Devmapper.cpp index 703eade6..e56a4daf 100644 --- a/Devmapper.cpp +++ b/Devmapper.cpp @@ -164,7 +164,7 @@ int Devmapper::lookupActive(const char *name, char *ubuffer, size_t len) { } int Devmapper::create(const char *name, const char *loopFile, const char *key, - unsigned int numSectors, char *ubuffer, size_t len) { + unsigned long numSectors, char *ubuffer, size_t len) { char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE); if (!buffer) { SLOGE("Error allocating memory (%s)", strerror(errno)); diff --git a/Devmapper.h b/Devmapper.h index 54f808f6..5b65b53a 100644 --- a/Devmapper.h +++ b/Devmapper.h @@ -25,7 +25,7 @@ class SocketClient; class Devmapper { public: static int create(const char *name, const char *loopFile, const char *key, - unsigned int numSectors, char *buffer, size_t len); + unsigned long numSectors, char *buffer, size_t len); static int destroy(const char *name); static int lookupActive(const char *name, char *buffer, size_t len); static int dumpState(SocketClient *c); diff --git a/Loop.cpp b/Loop.cpp index 059b9635..a5863b31 100644 --- a/Loop.cpp +++ b/Loop.cpp @@ -253,7 +253,7 @@ int Loop::destroyByFile(const char * /*loopFile*/) { return -1; } -int Loop::createImageFile(const char *file, unsigned int numSectors) { +int Loop::createImageFile(const char *file, unsigned long numSectors) { int fd; if ((fd = creat(file, 0600)) < 0) { @@ -270,7 +270,7 @@ int Loop::createImageFile(const char *file, unsigned int numSectors) { return 0; } -int Loop::resizeImageFile(const char *file, unsigned int numSectors) { +int Loop::resizeImageFile(const char *file, unsigned long numSectors) { int fd; if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) { @@ -278,7 +278,7 @@ int Loop::resizeImageFile(const char *file, unsigned int numSectors) { return -1; } - SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors); + SLOGD("Attempting to increase size of %s to %lu sectors.", file, numSectors); if (fallocate(fd, 0, 0, numSectors * 512)) { if (errno == ENOSYS || errno == ENOTSUP) { diff --git a/Loop.h b/Loop.h index f7c0392e..72130b03 100644 --- a/Loop.h +++ b/Loop.h @@ -31,8 +31,8 @@ class Loop { static int create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len); static int destroyByDevice(const char *loopDevice); static int destroyByFile(const char *loopFile); - static int createImageFile(const char *file, unsigned int numSectors); - static int resizeImageFile(const char *file, unsigned int numSectors); + static int createImageFile(const char *file, unsigned long numSectors); + static int resizeImageFile(const char *file, unsigned long numSectors); static int dumpState(SocketClient *c); }; diff --git a/VolumeManager.cpp b/VolumeManager.cpp index ce355df7..46682dc0 100755 --- a/VolumeManager.cpp +++ b/VolumeManager.cpp @@ -115,21 +115,21 @@ static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigne return 0; } -static int adjustSectorNumExt4(unsigned numSectors) { +static unsigned long adjustSectorNumExt4(unsigned long numSectors) { // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for // preventing costly operations or unexpected ENOSPC error. // Ext4::format() uses default block size without clustering. - unsigned clusterSectors = 4096 / 512; - unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0); + unsigned long clusterSectors = 4096 / 512; + unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0); numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors; return ROUND_UP_POWER_OF_2(numSectors, 3); } -static int adjustSectorNumFAT(unsigned numSectors) { +static unsigned long adjustSectorNumFAT(unsigned long numSectors) { /* * Add some headroom */ - unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; + unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2; numSectors += fatSize + 2; /* * FAT is aligned to 32 kb with 512b sectors. @@ -154,7 +154,7 @@ static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, c return 0; } -static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) { +static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , unsigned long numImgSectors, bool* createdDMDevice, bool debug) { if (strcmp(key, "none")) { if (Devmapper::lookupActive(idHash, buffer, len)) { if (Devmapper::create(idHash, loopDevice, key, numImgSectors, @@ -767,7 +767,7 @@ int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxle return 0; } -int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype, +int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype, const char *key, const int ownerUid, bool isExternal) { struct asec_superblock sb; memset(&sb, 0, sizeof(sb)); @@ -795,7 +795,7 @@ int VolumeManager::createAsec(const char *id, unsigned int numSectors, const cha sb.ver = ASEC_SB_VER; if (numSectors < ((1024*1024)/512)) { - SLOGE("Invalid container size specified (%d sectors)", numSectors); + SLOGE("Invalid container size specified (%lu sectors)", numSectors); errno = EINVAL; return -1; } @@ -824,7 +824,7 @@ int VolumeManager::createAsec(const char *id, unsigned int numSectors, const cha return -1; } - unsigned numImgSectors; + unsigned long numImgSectors; if (usingExt4) numImgSectors = adjustSectorNumExt4(numSectors); else @@ -961,7 +961,7 @@ int VolumeManager::createAsec(const char *id, unsigned int numSectors, const cha return 0; } -int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) { +int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) { char asecFileName[255]; char mountPoint[255]; bool cleanupDm = false; @@ -991,7 +991,7 @@ int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *k struct asec_superblock sb; int fd; - unsigned int oldNumSec = 0; + unsigned long oldNumSec = 0; if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) { SLOGE("Failed to open ASEC file (%s)", strerror(errno)); @@ -1007,7 +1007,7 @@ int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *k oldNumSec = info.st_size / 512; - unsigned numImgSectors; + unsigned long numImgSectors; if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) numImgSectors = adjustSectorNumExt4(numSectors); else @@ -1015,7 +1015,7 @@ int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *k /* * add one block for the superblock */ - SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1); + SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1); if (oldNumSec == numImgSectors + 1) { SLOGW("Size unchanged; ignoring resize request"); return 0; diff --git a/VolumeManager.h b/VolumeManager.h index fa2237f5..39fc8f95 100644 --- a/VolumeManager.h +++ b/VolumeManager.h @@ -143,9 +143,9 @@ class VolumeManager { /* ASEC */ int findAsec(const char *id, char *asecPath = NULL, size_t asecPathLen = 0, const char **directory = NULL) const; - int createAsec(const char *id, unsigned numSectors, const char *fstype, + int createAsec(const char *id, unsigned long numSectors, const char *fstype, const char *key, const int ownerUid, bool isExternal); - int resizeAsec(const char *id, unsigned numSectors, const char *key); + int resizeAsec(const char *id, unsigned long numSectors, const char *key); int finalizeAsec(const char *id); /** diff --git a/fs/Ext4.cpp b/fs/Ext4.cpp index 3ae4159b..4cb9d31d 100644 --- a/fs/Ext4.cpp +++ b/fs/Ext4.cpp @@ -151,17 +151,17 @@ status_t Mount(const std::string& source, const std::string& target, bool ro, return rc; } -status_t Resize(const std::string& source, unsigned int numSectors) { +status_t Resize(const std::string& source, unsigned long numSectors) { std::vector cmd; cmd.push_back(kResizefsPath); cmd.push_back("-f"); cmd.push_back(source); - cmd.push_back(StringPrintf("%u", numSectors)); + cmd.push_back(StringPrintf("%lu", numSectors)); return ForkExecvp(cmd); } -status_t Format(const std::string& source, unsigned int numSectors, +status_t Format(const std::string& source, unsigned long numSectors, const std::string& target) { std::vector cmd; cmd.push_back(kMkfsPath); @@ -172,7 +172,7 @@ status_t Format(const std::string& source, unsigned int numSectors, if (numSectors) { cmd.push_back("-l"); - cmd.push_back(StringPrintf("%u", numSectors * 512)); + cmd.push_back(StringPrintf("%lu", numSectors * 512)); } // Always generate a real UUID diff --git a/fs/Ext4.h b/fs/Ext4.h index a5efa74a..f78dc957 100644 --- a/fs/Ext4.h +++ b/fs/Ext4.h @@ -30,9 +30,9 @@ bool IsSupported(); status_t Check(const std::string& source, const std::string& target); status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount, bool executable); -status_t Format(const std::string& source, unsigned int numSectors, +status_t Format(const std::string& source, unsigned long numSectors, const std::string& target); -status_t Resize(const std::string& source, unsigned int numSectors); +status_t Resize(const std::string& source, unsigned long numSectors); } // namespace ext4 } // namespace vold diff --git a/fs/Vfat.cpp b/fs/Vfat.cpp index 7bd05ec6..7338c1e4 100644 --- a/fs/Vfat.cpp +++ b/fs/Vfat.cpp @@ -164,7 +164,7 @@ status_t Mount(const std::string& source, const std::string& target, bool ro, return rc; } -status_t Format(const std::string& source, unsigned int numSectors) { +status_t Format(const std::string& source, unsigned long numSectors) { std::vector cmd; cmd.push_back(kMkfsPath); cmd.push_back("-F"); @@ -177,7 +177,7 @@ status_t Format(const std::string& source, unsigned int numSectors) { if (numSectors) { cmd.push_back("-s"); - cmd.push_back(StringPrintf("%u", numSectors)); + cmd.push_back(StringPrintf("%lu", numSectors)); } cmd.push_back(source); diff --git a/fs/Vfat.h b/fs/Vfat.h index 306c7db1..40be5f66 100644 --- a/fs/Vfat.h +++ b/fs/Vfat.h @@ -31,7 +31,7 @@ status_t Check(const std::string& source); status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount, bool executable, int ownerUid, int ownerGid, int permMask, bool createLost); -status_t Format(const std::string& source, unsigned int numSectors); +status_t Format(const std::string& source, unsigned long numSectors); } // namespace vfat } // namespace vold