Skip to content

Commit

Permalink
vold: fix 64 bit ioctl error
Browse files Browse the repository at this point in the history
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 <mateusz.nowak@intel.com>
Signed-off-by: Zhiquan Liu <zhiquan.liu@intel.com>
  • Loading branch information
m-a-nowak authored and zhiquan-liu committed Oct 21, 2015
1 parent 0331d4a commit a4f48d0
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions CommandListener.cpp
Expand Up @@ -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")) {
Expand All @@ -417,7 +417,7 @@ int CommandListener::AsecCmd::runCommand(SocketClient *cli,
cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", 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);
Expand Down
2 changes: 1 addition & 1 deletion Devmapper.cpp
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion Devmapper.h
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Loop.cpp
Expand Up @@ -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) {
Expand All @@ -270,15 +270,15 @@ 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) {
SLOGE("Error opening imagefile (%s)", strerror(errno));
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) {
Expand Down
4 changes: 2 additions & 2 deletions Loop.h
Expand Up @@ -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);
};
Expand Down
26 changes: 13 additions & 13 deletions VolumeManager.cpp
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -1007,15 +1007,15 @@ 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
numImgSectors = adjustSectorNumFAT(numSectors);
/*
* 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;
Expand Down
4 changes: 2 additions & 2 deletions VolumeManager.h
Expand Up @@ -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);

/**
Expand Down
8 changes: 4 additions & 4 deletions fs/Ext4.cpp
Expand Up @@ -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<std::string> 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<std::string> cmd;
cmd.push_back(kMkfsPath);
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions fs/Ext4.h
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions fs/Vfat.cpp
Expand Up @@ -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<std::string> cmd;
cmd.push_back(kMkfsPath);
cmd.push_back("-F");
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion fs/Vfat.h
Expand Up @@ -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
Expand Down

0 comments on commit a4f48d0

Please sign in to comment.