Skip to content

Commit

Permalink
vold: Add a versioned superblock to secure containers.
Browse files Browse the repository at this point in the history
Add an un-encrypted superblock to the end of a secure container to
help identify the crypto cipher used. This is required in order to
provide some semblence of versioning, in case we have cause to
change the crypto type on an update - say for example switching
from software to hardware encryption, or from 128 -> a zillion
bit keys.

NOTE: This format is incompatible with previous secure containers.
After this change they will no longer be mountable.

Signed-off-by: San Mehat <[email protected]>
  • Loading branch information
San Mehat committed Mar 3, 2010
1 parent 2350c44 commit fcf24fe
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 40 deletions.
42 changes: 42 additions & 0 deletions Asec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _ASEC_H
#define _ASEC_H

struct asec_superblock {
#define ASEC_SB_MAGIC 0xc0def00d
unsigned int magic;

#define ASEC_SB_VER 1
unsigned char ver;

#define ASEC_SB_C_CIPHER_NONE 0
#define ASEC_SB_C_CIPHER_TWOFISH 1
#define ASEC_SB_C_CIPHER_AES 2
unsigned char c_cipher;

#define ASEC_SB_C_CHAIN_NONE 0
unsigned char c_chain;

#define ASEC_SB_C_OPTS_NONE 0
unsigned char c_opts;

#define ASEC_SB_C_MODE_NONE 0
unsigned char c_mode;
} __attribute__((packed));

#endif
2 changes: 2 additions & 0 deletions Devmapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ int Devmapper::create(const char *name, const char *loopFile, const char *key,
ioctlInit(io, 4096, name, DM_STATUS_TABLE_FLAG);
io->target_count = 1;
tgt->status = 0;

tgt->sector_start = 0;
tgt->length = numSectors;

strcpy(tgt->target_type, "crypt");

char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
Expand Down
37 changes: 18 additions & 19 deletions Fat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,34 +148,33 @@ int Fat::doMount(const char *fsPath, const char *mountPoint,
return rc;
}

int Fat::format(const char *fsPath) {
unsigned int nr_sec;
int Fat::format(const char *fsPath, unsigned int numSectors) {
int fd;

if ((fd = open(fsPath, O_RDWR)) < 0) {
LOGE("Error opening disk file (%s)", strerror(errno));
return -1;
}

if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
LOGE("Unable to get device size (%s)", strerror(errno));
close(fd);
return -1;
}
close(fd);

const char *args[9];
const char *args[11];
int rc;

args[0] = MKDOSFS_PATH;
args[1] = "-F";
args[2] = "32";
args[3] = "-O";
args[4] = "android";
args[5] = "-c";
args[6] = "8";
args[7] = fsPath;
args[8] = NULL;
rc = logwrap(9, args, 1);

if (numSectors) {
char tmp[32];
snprintf(tmp, sizeof(tmp), "%u", numSectors);
const char *size = tmp;
args[7] = "-s";
args[8] = size;
args[9] = fsPath;
args[10] = NULL;
rc = logwrap(11, args, 1);
} else {
args[7] = fsPath;
args[8] = NULL;
rc = logwrap(9, args, 1);
}

if (rc == 0) {
LOGI("Filesystem formatted OK");
Expand Down
2 changes: 1 addition & 1 deletion Fat.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Fat {
static int doMount(const char *fsPath, const char *mountPoint, bool ro,
bool remount, int ownerUid, int ownerGid, int permMask,
bool createLost);
static int format(const char *fsPath);
static int format(const char *fsPath, unsigned int numSectors);
};

#endif
2 changes: 1 addition & 1 deletion Volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ int Volume::formatVol() {
sprintf(devicePath, "/dev/block/vold/%d:%d",
MAJOR(partNode), MINOR(partNode));

if (Fat::format(devicePath)) {
if (Fat::format(devicePath, 0)) {
LOGE("Failed to format (%s)", strerror(errno));
goto err;
}
Expand Down
125 changes: 106 additions & 19 deletions VolumeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "Fat.h"
#include "Devmapper.h"
#include "Process.h"
#include "Asec.h"

VolumeManager *VolumeManager::sInstance = NULL;

Expand Down Expand Up @@ -168,6 +169,11 @@ int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {

int VolumeManager::createAsec(const char *id, unsigned int numSectors,
const char *fstype, const char *key, int ownerUid) {
struct asec_superblock sb;
memset(&sb, 0, sizeof(sb));

sb.magic = ASEC_SB_MAGIC;
sb.ver = ASEC_SB_VER;

if (numSectors < ((1024*1024)/512)) {
LOGE("Invalid container size specified (%d sectors)", numSectors);
Expand All @@ -191,7 +197,18 @@ int VolumeManager::createAsec(const char *id, unsigned int numSectors,
return -1;
}

if (Loop::createImageFile(asecFileName, numSectors)) {
/*
* Add some headroom
*/
unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
unsigned numImgSectors = numSectors + fatSize + 2;

if (numImgSectors % 63) {
numImgSectors += (63 - (numImgSectors % 63));
}

// Add +1 for our superblock which is at the end
if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
LOGE("ASEC image file creation failed (%s)", strerror(errno));
return -1;
}
Expand All @@ -207,7 +224,9 @@ int VolumeManager::createAsec(const char *id, unsigned int numSectors,
bool cleanupDm = false;

if (strcmp(key, "none")) {
if (Devmapper::create(id, loopDevice, key, numSectors, dmDevice,
// XXX: This is all we support for now
sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
if (Devmapper::create(id, loopDevice, key, numImgSectors, dmDevice,
sizeof(dmDevice))) {
LOGE("ASEC device mapping failed (%s)", strerror(errno));
Loop::destroyByDevice(loopDevice);
Expand All @@ -216,15 +235,54 @@ int VolumeManager::createAsec(const char *id, unsigned int numSectors,
}
cleanupDm = true;
} else {
sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
strcpy(dmDevice, loopDevice);
}

/*
* Drop down the superblock at the end of the file
*/

int sbfd = open(loopDevice, O_RDWR);
if (sbfd < 0) {
LOGE("Failed to open new DM device for superblock write (%s)", strerror(errno));
if (cleanupDm) {
Devmapper::destroy(id);
}
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}

if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
close(sbfd);
LOGE("Failed to lseek for superblock (%s)", strerror(errno));
if (cleanupDm) {
Devmapper::destroy(id);
}
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}

if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) {
close(sbfd);
LOGE("Failed to write superblock (%s)", strerror(errno));
if (cleanupDm) {
Devmapper::destroy(id);
}
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}
close(sbfd);

if (strcmp(fstype, "none")) {
if (strcmp(fstype, "fat")) {
LOGW("Unknown fstype '%s' specified for container", fstype);
}

if (Fat::format(dmDevice)) {
if (Fat::format(dmDevice, numImgSectors)) {
LOGE("ASEC FAT format failed (%s)", strerror(errno));
if (cleanupDm) {
Devmapper::destroy(id);
Expand Down Expand Up @@ -467,24 +525,53 @@ int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {

char dmDevice[255];
bool cleanupDm = false;
if (strcmp(key, "none")) {
if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) {
unsigned int nr_sec = 0;
int fd;
int fd;
unsigned int nr_sec = 0;

if ((fd = open(loopDevice, O_RDWR)) < 0) {
LOGE("Failed to open loopdevice (%s)", strerror(errno));
Loop::destroyByDevice(loopDevice);
return -1;
}
if ((fd = open(loopDevice, O_RDWR)) < 0) {
LOGE("Failed to open loopdevice (%s)", strerror(errno));
Loop::destroyByDevice(loopDevice);
return -1;
}

if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
LOGE("Failed to get loop size (%s)", strerror(errno));
Loop::destroyByDevice(loopDevice);
close(fd);
return -1;
}
close(fd);
if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
LOGE("Failed to get loop size (%s)", strerror(errno));
Loop::destroyByDevice(loopDevice);
close(fd);
return -1;
}

/*
* Validate superblock
*/
struct asec_superblock sb;
memset(&sb, 0, sizeof(sb));
if (lseek(fd, ((nr_sec-1) * 512), SEEK_SET) < 0) {
LOGE("lseek failed (%s)", strerror(errno));
close(fd);
Loop::destroyByDevice(loopDevice);
return -1;
}
if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
LOGE("superblock read failed (%s)", strerror(errno));
close(fd);
Loop::destroyByDevice(loopDevice);
return -1;
}

close(fd);

LOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
LOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
Loop::destroyByDevice(loopDevice);
errno = EMEDIUMTYPE;
return -1;
}
nr_sec--; // We don't want the devmapping to extend onto our superblock

if (strcmp(key, "none")) {
if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) {
if (Devmapper::create(id, loopDevice, key, nr_sec,
dmDevice, sizeof(dmDevice))) {
LOGE("ASEC device mapping failed (%s)", strerror(errno));
Expand Down

0 comments on commit fcf24fe

Please sign in to comment.