Do not allow resize if device size is not aligned to sector size.

This commit is contained in:
Milan Broz
2017-10-03 13:49:16 +02:00
parent 8dc35a7906
commit cc5c91158d
2 changed files with 17 additions and 0 deletions

View File

@@ -2049,6 +2049,14 @@ int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
if (r)
goto out;
if (new_size & ((dmd.u.crypt.sector_size >> SECTOR_SHIFT) - 1)) {
log_err(cd, _("Device %s size is not aligned to requested sector size (%u bytes).\n"),
crypt_get_device_name(cd), (unsigned)dmd.u.crypt.sector_size);
r = -EINVAL;
goto out;
}
if (new_size == dmd.size) {
log_dbg("Device has already requested size %" PRIu64
" sectors.", dmd.size);

View File

@@ -472,6 +472,15 @@ $CRYPTSETUP -q remove $DEV_NAME || fail
echo $PWD1 | $CRYPTSETUP -q create $DEV_NAME --hash sha1 --size 100 $LOOPDEV || fail
$CRYPTSETUP -q status $DEV_NAME | grep "size:" | grep -q "100 sectors" || fail
$CRYPTSETUP -q remove $DEV_NAME || fail
# 4k sector resize (if kernel supports it)
echo $PWD1 | $CRYPTSETUP -q open --type plain $LOOPDEV $DEV_NAME --sector-size 4096 --size 8 >/dev/null 2>&1
if [ $? -eq 0 ] ; then
$CRYPTSETUP -q status $DEV_NAME | grep "size:" | grep -q "8 sectors"
$CRYPTSETUP -q resize $DEV_NAME --size 16 || fail
$CRYPTSETUP -q status $DEV_NAME | grep "size:" | grep -q "16 sectors"
$CRYPTSETUP -q resize $DEV_NAME --size 9 2>/dev/null && fail
$CRYPTSETUP -q remove $DEV_NAME || fail
fi
# verify is ignored on non-tty input
echo $PWD1 | $CRYPTSETUP create $DEV_NAME $LOOPDEV --hash sha1 --verify-passphrase 2>/dev/null || fail
$CRYPTSETUP -q remove $DEV_NAME || fail