Commit 3a27c84d authored by Milan Broz's avatar Milan Broz

Rewrite and export crypt_wipe function.

The crypt_wipe can be used to wipe any part of the device,
and also to initialize integrity based device (to reset checksum).
parent c6408f4b
Version 1.7:
- Export wipe device functions
- Support K/M suffixes for align payload (new switch?).
- TRIM for keyslots
- Do we need crypt_data_path() - path to data device (if differs)?
......
......@@ -26,6 +26,7 @@
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <unistd.h>
#include <inttypes.h>
......@@ -77,7 +78,7 @@ int device_size(struct device *device, uint64_t *size);
int device_open(struct device *device, int flags);
void device_disable_direct_io(struct device *device);
int device_is_identical(struct device *device1, struct device *device2);
int device_is_rotational(struct device *device);
enum devcheck { DEV_OK = 0, DEV_EXCL = 1, DEV_SHARED = 2 };
int device_block_adjust(struct crypt_device *cd,
......@@ -139,24 +140,13 @@ int PLAIN_activate(struct crypt_device *cd,
void *crypt_get_hdr(struct crypt_device *cd, const char *type);
/**
* Different methods used to erase sensitive data concerning
* either encrypted payload area or master key inside keyslot
* area
*/
typedef enum {
CRYPT_WIPE_ZERO, /**< overwrite area using zero blocks */
CRYPT_WIPE_DISK, /**< erase disk (using Gutmann method if it is rotational disk)*/
CRYPT_WIPE_SSD, /**< erase solid state disk (random write) */
CRYPT_WIPE_RANDOM /**< overwrite area using some up to now unspecified
* random algorithm */
} crypt_wipe_type;
int crypt_wipe(struct device *device,
uint64_t offset,
uint64_t size,
crypt_wipe_type type,
int exclusive);
int crypt_wipe_device(struct crypt_device *cd,
struct device *device,
crypt_wipe_pattern pattern,
uint64_t offset,
uint64_t length,
size_t wipe_block_size,
int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
void *usrptr);
#endif /* INTERNAL_H */
......@@ -1220,6 +1220,58 @@ int crypt_keyfile_read(struct crypt_device *cd,
/** No on-disk header (only hashes) */
#define CRYPT_KEYFILE_STOP_EOL (1 << 0)
/**
* @defgroup wipe Device wipe helper
*
* Wipe or fill a device with a pattern.
*
* @addtogroup wipe
* @{
*/
/**
* Wipe pattern
*/
typedef enum {
CRYPT_WIPE_ZERO, /**< Fill with zeroes */
CRYPT_WIPE_RANDOM, /**< Use RNG to fill data */
CRYPT_WIPE_ENCRYPTED_ZERO, /**< Add encryption and fill with zeroes as plaintext */
CRYPT_WIPE_SPECIAL, /**< Compatibility only, do not use (Gutmann method) */
} crypt_wipe_pattern;
/**
* Wipe/Fill (part of) a device with the selected pattern.
*
* @param cd crypt device handle
* @param dev_path path to device to wipe or @e NULL if data device should be used
* @param pattern selected wipe pattern
* @param offset offset on device (in bytes)
* @param length length of area to be wiped (in bytes)
* @param wipe_block_size used block for wiping (one step) (in bytes)
* @param flags wipe flags
* @param progress callback function called after each @e wipe_block_size or @e NULL
* @param usrptr provided identification in callback
*
* @return @e 0 on success or negative errno value otherwise.
*
* @note A @e progress callback can interrupt wipe process by returning non-zero code.
*/
int crypt_wipe(struct crypt_device *cd,
const char *dev_path, /* if null, use data device */
crypt_wipe_pattern pattern,
uint64_t offset,
uint64_t length,
size_t wipe_block_size,
uint32_t flags,
int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
void *usrptr
);
/** Use direct-io */
#define CRYPT_WIPE_NO_DIRECT_IO (1 << 0)
/** @} */
#ifdef __cplusplus
}
#endif
......
......@@ -68,6 +68,8 @@ CRYPTSETUP_2.0 {
crypt_header_restore;
crypt_keyfile_read;
crypt_wipe;
local:
*;
};
......@@ -1027,9 +1027,9 @@ int LUKS_del_key(unsigned int keyIndex,
startOffset = hdr->keyblock[keyIndex].keyMaterialOffset;
endOffset = startOffset + AF_split_sectors(hdr->keyBytes, hdr->keyblock[keyIndex].stripes);
r = crypt_wipe(device, startOffset * SECTOR_SIZE,
(endOffset - startOffset) * SECTOR_SIZE,
CRYPT_WIPE_DISK, 0);
r = crypt_wipe_device(ctx, device, CRYPT_WIPE_SPECIAL, startOffset * SECTOR_SIZE,
(endOffset - startOffset) * SECTOR_SIZE,
(endOffset - startOffset) * SECTOR_SIZE, NULL, NULL);
if (r) {
if (r == -EACCES) {
log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
......
......@@ -1039,23 +1039,19 @@ static int _crypt_format_luks1(struct crypt_device *cd,
alignment_offset / SECTOR_SIZE,
cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec,
cd->metadata_device ? 1 : 0, cd);
if(r < 0)
if (r < 0)
return r;
/* Wipe first 8 sectors - fs magic numbers etc. */
r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
if(r < 0) {
if (r == -EBUSY)
log_err(cd, _("Cannot format device %s which is still in use.\n"),
mdata_device_path(cd));
else if (r == -EACCES) {
log_err(cd, _("Cannot format device %s, permission denied.\n"),
mdata_device_path(cd));
r = -EINVAL;
} else
log_err(cd, _("Cannot wipe header on device %s.\n"),
mdata_device_path(cd));
r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_EXCL, 0, NULL, NULL);
if (r < 0)
return r;
/* Wipe first 8 sectors - fs magic numbers etc. */
r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO, 0,
8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
if (r < 0) {
log_err(cd, _("Cannot wipe header on device %s.\n"),
mdata_device_path(cd));
return r;
}
......@@ -1239,20 +1235,16 @@ static int _crypt_format_integrity(struct crypt_device *cd,
return -EINVAL;
}
r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_EXCL, 0, NULL, NULL);
if (r < 0)
return r;
/* Wipe first 8 sectors - fs magic numbers etc. */
r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO, 0,
8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
if (r < 0) {
if (r == -EBUSY)
log_err(cd, _("Cannot format device %s which is still in use.\n"),
mdata_device_path(cd));
else if (r == -EACCES) {
log_err(cd, _("Cannot format device %s, permission denied.\n"),
mdata_device_path(cd));
r = -EINVAL;
} else
log_err(cd, _("Cannot wipe header on device %s.\n"),
mdata_device_path(cd));
log_err(cd, _("Cannot wipe header on device %s.\n"),
mdata_device_path(cd));
return r;
}
......@@ -1285,9 +1277,12 @@ static int _crypt_format_integrity(struct crypt_device *cd,
cd->u.integrity.params.buffer_sectors = params->buffer_sectors;
cd->u.integrity.params.sector_size = params->sector_size;
cd->u.integrity.params.tag_size = params->tag_size;
cd->u.integrity.params.integrity = params->integrity;
cd->u.integrity.params.journal_integrity = params->journal_integrity;
cd->u.integrity.params.journal_crypt = params->journal_crypt;
if (params->integrity)
cd->u.integrity.params.integrity = strdup(params->integrity);
if (params->journal_integrity)
cd->u.integrity.params.journal_integrity = strdup(params->journal_integrity);
if (params->journal_crypt)
cd->u.integrity.params.journal_crypt = strdup(params->journal_crypt);
r = INTEGRITY_format(cd, params, cd->u.integrity.journal_crypt_key, cd->u.integrity.journal_mac_key);
if (r)
......@@ -1582,6 +1577,8 @@ void crypt_free(struct crypt_device *cd)
device_free(cd->u.verity.fec_device);
} else if (isINTEGRITY(cd->type)) {
free(CONST_CAST(void*)cd->u.integrity.params.integrity);
free(CONST_CAST(void*)cd->u.integrity.params.journal_integrity);
free(CONST_CAST(void*)cd->u.integrity.params.journal_crypt);
crypt_free_volume_key(cd->u.integrity.journal_crypt_key);
crypt_free_volume_key(cd->u.integrity.journal_mac_key);
} else if (!cd->type) {
......
......@@ -30,6 +30,9 @@
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <unistd.h>
#ifdef HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h> /* for major, minor */
#endif
#include "internal.h"
struct device {
......@@ -482,7 +485,10 @@ int device_block_adjust(struct crypt_device *cd,
if (r == -EBUSY)
log_err(cd, _("Cannot use device %s which is in use "
"(already mapped or mounted).\n"),
device->path);
device->path);
else if (r == -EACCES)
log_err(cd, _("Cannot use device %s, permission denied.\n"),
device->path);
else
log_err(cd, _("Cannot get info about device %s.\n"),
device->path);
......@@ -547,3 +553,16 @@ int device_is_identical(struct device *device1, struct device *device2)
return 0;
}
int device_is_rotational(struct device *device)
{
struct stat st;
if (stat(device_path(device), &st) < 0)
return -EINVAL;
if (!S_ISBLK(st.st_mode))
return 0;
return crypt_dev_is_rotational(major(st.st_rdev), minor(st.st_rdev));
}
......@@ -20,44 +20,15 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h> /* for major, minor */
#endif
#include <fcntl.h>
#include "libcryptsetup.h"
#include "internal.h"
#define MAXIMUM_WIPE_BYTES 1024 * 1024 * 32 /* 32 MiB */
static ssize_t _crypt_wipe_zero(int fd, int bsize, char *buffer,
uint64_t offset, uint64_t size)
{
memset(buffer, 0, size);
return write_lseek_blockwise(fd, bsize, buffer, size, offset);
}
static ssize_t _crypt_wipe_random(int fd, int bsize, char *buffer,
uint64_t offset, uint64_t size)
{
if (crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL) < 0)
return -EINVAL;
return write_lseek_blockwise(fd, bsize, buffer, size, offset);
}
/*
* Wipe using Peter Gutmann method described in
* http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
* Note: used only for rotational device (and even there it is not needed today...)
*/
static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
{
......@@ -75,28 +46,28 @@ static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
{"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
};
for(i = 0; i < buffer_size / 3; ++i) {
for (i = 0; i < buffer_size / 3; ++i) {
memcpy(buffer, write_modes[turn], 3);
buffer += 3;
}
}
static ssize_t _crypt_wipe_disk(int fd, int bsize, char *buffer,
uint64_t offset, uint64_t size)
static int crypt_wipe_special(int fd, int bsize, char *buffer,
uint64_t offset, size_t size)
{
int r;
unsigned int i;
ssize_t written;
for(i = 0; i < 39; ++i) {
for (i = 0; i < 39; ++i) {
if (i < 5) {
r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
} else if(i >= 5 && i < 32) {
} else if (i >= 5 && i < 32) {
wipeSpecial(buffer, size, i - 5);
r = 0;
} else if(i >= 32 && i < 38) {
} else if (i >= 32 && i < 38) {
r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
} else if(i >= 38 && i < 39) {
} else if (i >= 38 && i < 39) {
memset(buffer, 0xFF, size);
r = 0;
}
......@@ -105,96 +76,178 @@ static ssize_t _crypt_wipe_disk(int fd, int bsize, char *buffer,
written = write_lseek_blockwise(fd, bsize, buffer, size, offset);
if (written < 0 || written != (ssize_t)size)
return written;
return -EIO;
}
/* Rewrite it finally with random */
return _crypt_wipe_random(fd, bsize, buffer, offset, size);
if (crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL) < 0)
return -EINVAL;
written = write_lseek_blockwise(fd, bsize, buffer, size, offset);
if (written < 0 || written != (ssize_t)size)
return -EIO;
return 0;
}
static ssize_t _crypt_wipe_ssd(int fd, int bsize, char *buffer,
uint64_t offset, uint64_t size)
static int wipe_block(int devfd, crypt_wipe_pattern pattern, char *sf,
size_t device_block_size, size_t wipe_block_size,
uint64_t offset, bool *need_block_init)
{
// FIXME: for now just rewrite it by random
return _crypt_wipe_random(fd, bsize, buffer, offset, size);
int r;
if (pattern == CRYPT_WIPE_SPECIAL)
return crypt_wipe_special(devfd, device_block_size, sf, offset, wipe_block_size);
if (*need_block_init) {
if (pattern == CRYPT_WIPE_ZERO) {
memset(sf, 0, wipe_block_size);
*need_block_init = false;
r = 0;
} else if (pattern == CRYPT_WIPE_RANDOM) {
r = crypt_random_get(NULL, sf, wipe_block_size, CRYPT_RND_NORMAL);
*need_block_init = true;
} else if (pattern == CRYPT_WIPE_ENCRYPTED_ZERO) {
// FIXME
r = crypt_random_get(NULL, sf, wipe_block_size, CRYPT_RND_NORMAL);
*need_block_init = true;
} else
r = -EINVAL;
if (r)
return r;
}
if (write_blockwise(devfd, device_block_size, sf, wipe_block_size) == (ssize_t)wipe_block_size)
return 0;
return -EIO;
}
int crypt_wipe(struct device *device,
uint64_t offset,
uint64_t size,
crypt_wipe_type type,
int exclusive)
int crypt_wipe_device(struct crypt_device *cd,
struct device *device,
crypt_wipe_pattern pattern,
uint64_t offset,
uint64_t length,
size_t wipe_block_size,
int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
void *usrptr)
{
struct stat st;
char *buffer;
int devfd, flags, bsize;
ssize_t written;
int r, devfd = -1, bsize;
char *sf = NULL;
uint64_t dev_size;
bool need_block_init = true;
if (!size || size % SECTOR_SIZE || (size > MAXIMUM_WIPE_BYTES)) {
log_dbg("Unsupported wipe size for device %s: %ld.",
device_path(device), (unsigned long)size);
/* Note: LUKS1 calls it with wipe_block not aligned to multiple of bsize */
bsize = device_block_size(device);
if ((bsize <= 0) || (wipe_block_size < (size_t)bsize))
return -EINVAL;
}
if (stat(device_path(device), &st) < 0) {
log_dbg("Device %s not found.", device_path(device));
/* Everything must be aligned to SECTOR_SIZE */
if ((offset % SECTOR_SIZE) || (length % SECTOR_SIZE) || (wipe_block_size % SECTOR_SIZE))
return -EINVAL;
}
if (type == CRYPT_WIPE_DISK && S_ISBLK(st.st_mode)) {
if (!crypt_dev_is_rotational(major(st.st_rdev),
minor(st.st_rdev))) {
type = CRYPT_WIPE_SSD;
log_dbg("Non-rotational device, using SSD wipe mode.");
} else
log_dbg("Rotational device, using normal wipe mode.");
}
devfd = device_open(device, O_RDWR);
if (devfd < 0)
return errno ? -errno : -EINVAL;
bsize = device_block_size(device);
if (bsize <= 0)
return -EINVAL;
r = device_size(device, &dev_size);
if (r)
goto out;
buffer = malloc(size);
if (!buffer)
return -ENOMEM;
if (length) {
if ((dev_size <= offset) || (dev_size - offset) < length) {
r = -EINVAL;
goto out;
}
dev_size = offset + length;
}
flags = O_RDWR;
r = posix_memalign((void **)&sf, crypt_getpagesize(), wipe_block_size);
if (r)
goto out;
/* use O_EXCL only for block devices */
if (exclusive && S_ISBLK(st.st_mode))
flags |= O_EXCL;
if (lseek64(devfd, offset, SEEK_SET) < 0) {
log_err(cd, "Cannot seek to device offset.\n");
r = -EIO;
goto out;
}
/* coverity[toctou] */
devfd = device_open(device, flags);
if (devfd < 0) {
free(buffer);
return errno ? -errno : -EINVAL;
if (progress && progress(dev_size, offset, usrptr)) {
r = -EINTR;
goto out;
}
// FIXME: use fixed block size and loop here
switch (type) {
case CRYPT_WIPE_ZERO:
written = _crypt_wipe_zero(devfd, bsize, buffer, offset, size);
break;
case CRYPT_WIPE_DISK:
written = _crypt_wipe_disk(devfd, bsize, buffer, offset, size);
break;
case CRYPT_WIPE_SSD:
written = _crypt_wipe_ssd(devfd, bsize, buffer, offset, size);
if (pattern == CRYPT_WIPE_SPECIAL && !device_is_rotational(device)) {
log_dbg("Non-rotational device, using random data wipe mode.");
pattern = CRYPT_WIPE_RANDOM;
}
while (offset < dev_size) {
if ((offset + wipe_block_size) > dev_size)
wipe_block_size = dev_size - offset;
//log_dbg("Wipe %012" PRIu64 "-%012" PRIu64 " bytes", offset, offset + wipe_block_size);
r = wipe_block(devfd, pattern, sf, bsize, wipe_block_size, offset, &need_block_init);
if (r) {
log_err(cd, "Device wipe error, offset %" PRIu64 ".\n", offset);
break;
case CRYPT_WIPE_RANDOM:
written = _crypt_wipe_random(devfd, bsize, buffer, offset, size);
}
offset += wipe_block_size;
if (progress && progress(dev_size, offset, usrptr)) {
r = -EINTR;
break;
default:
log_dbg("Unsupported wipe type requested: (%d)", type);
written = -1;
}
}
fsync(devfd);
out:
close(devfd);
free(buffer);
free(sf);
return r;
}
if (written != (ssize_t)size || written < 0)
return -EIO;
int crypt_wipe(struct crypt_device *cd,
const char *dev_path,
crypt_wipe_pattern pattern,
uint64_t offset,
uint64_t length,
size_t wipe_block_size,
uint32_t flags,
int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
void *usrptr)
{
struct device *device;
int r;
return 0;
if (!cd)
return -EINVAL;
if (!dev_path)
device = crypt_data_device(cd);
else {
r = device_alloc(&device, dev_path);
if (r < 0)
return r;
if (flags & CRYPT_WIPE_NO_DIRECT_IO)
device_disable_direct_io(device);
}
if (!wipe_block_size)
wipe_block_size = 1024*1024;
log_dbg("Wipe [%u] device %s, offset %" PRIu64 ", length %" PRIu64 ", block %zu.",
(unsigned)pattern, device_path(device), offset, length, wipe_block_size);
r = crypt_wipe_device(cd, device, pattern, offset, length,
wipe_block_size, progress, usrptr);
if (dev_path)
device_free(device);
return r;
}
......@@ -37,6 +37,7 @@
#include <fcntl.h>
#include <popt.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "lib/nls.h"
#include "lib/utils_crypt.h"
......@@ -85,6 +86,11 @@ int tools_is_stdin(const char *key_file);
int tools_string_to_size(struct crypt_device *cd, const char *s, uint64_t *size);
int tools_is_cipher_null(const char *cipher);
void tools_clear_line(void);
void tools_time_progress(uint64_t device_size, uint64_t bytes,
struct timeval *start_time, struct timeval *end_time);
/* Log */
#define log_dbg(x...) clogger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
#define log_std(x...) clogger(NULL, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)
......
......@@ -22,6 +22,7 @@
*/
#include "cryptsetup.h"
#include <math.h>
#include <signal.h>
int opt_verbose = 0;
......@@ -325,3 +326,67 @@ int tools_string_to_size(struct crypt_device *cd, const char *s, uint64_t *size)
*size = tmp;
return 0;
}
/* Time progress helper */
/* The difference in seconds between two times in "timeval" format. */
static double time_diff(struct timeval *start, struct timeval *end)
{
return (end->tv_sec - start->tv_sec)
+ (end->tv_usec - start->tv_usec) / 1E6;
}
// FIXME: detect terminal type
void tools_clear_line(void)
{
/* vt100 code clear line */
log_std("\33[2K\r");
}
void tools_time_progress(uint64_t device_size, uint64_t bytes,
struct timeval *start_time, struct timeval *end_time)
{
struct timeval now_time;
unsigned long long mbytes, eta;
double tdiff, mib;
int final = (bytes == device_size);
gettimeofday(&now_time, NULL);
if (start_time->tv_sec == 0 && start_time->tv_usec == 0) {
*start_time = now_time;
*end_time = now_time;
return;
}
if (!final && time_diff(end_time, &now_time) < 0.5)
return;
*end_time = now_time;
tdiff = time_diff(start_time, end_time);
if (!tdiff)
return;
mbytes = bytes / 1024 / 1024;
mib = (double)(mbytes) / tdiff;
if (!mib)
return;
/* FIXME: calculate this from last minute only and remaining space */
eta = (unsigned long long)(device_size / 1024 / 1024 / mib - tdiff);
tools_clear_line();
if (final)
log_std("Finished, time %02llu:%02llu.%03llu, "
"%4llu MiB written, speed %5.1f MiB/s\n",
(unsigned long long)tdiff / 60,
(unsigned long long)tdiff % 60,
(unsigned long long)((tdiff - floor(tdiff)) * 1000.0),
mbytes, mib);
else
log_std("Progress: %5.1f%%, ETA %02llu:%02llu, "
"%4llu MiB written, speed %5.1f MiB/s",
(double)bytes / device_size * 100,
eta / 60, eta % 60, mbytes, mib);
fflush(stdout);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment