"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/integritysetup.c" between
cryptsetup-2.3.6.tar.xz and cryptsetup-2.4.0.tar.xz

About: cryptsetup is a utility used to conveniently setup disk encryption based on the dm-crypt kernel module. These include plain dm-crypt volumes, LUKS volumes, loop-AES and TrueCrypt compatible format.

integritysetup.c  (cryptsetup-2.3.6.tar.xz):integritysetup.c  (cryptsetup-2.4.0.tar.xz)
skipping to change at line 22 skipping to change at line 22
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#include "cryptsetup.h"
#include <uuid/uuid.h> #include <uuid/uuid.h>
#define PACKAGE_INTEGRITY "integritysetup"
#define DEFAULT_ALG_NAME "crc32c" #define DEFAULT_ALG_NAME "crc32c"
static char *opt_data_device = NULL; #include "cryptsetup.h"
static char *opt_integrity = NULL; /* DEFAULT_ALG_NAME */ #include "integritysetup_args.h"
static char *opt_integrity_key_file = NULL;
static char *opt_journal_integrity = NULL; /* none */ #define PACKAGE_INTEGRITY "integritysetup"
static char *opt_journal_integrity_key_file = NULL;
static char *opt_journal_crypt = NULL; /* none */
static char *opt_journal_crypt_key_file = NULL;
/* helper strings converted to uint64_t later */
static char *opt_journal_size_str = NULL;
static uint64_t opt_journal_size = 0;
static int opt_interleave_sectors = 0;
static int opt_journal_watermark = 0;
static int opt_bitmap_sectors_per_bit = 0;
static int opt_journal_commit_time = 0;
static int opt_bitmap_flush_time = 0;
static int opt_tag_size = 0;
static int opt_sector_size = 0;
static int opt_buffer_sectors = 0;
static int opt_no_wipe = 0;
static int opt_integrity_key_size = 0;
static int opt_journal_integrity_key_size = 0;
static int opt_journal_crypt_key_size = 0;
static int opt_integrity_nojournal = 0;
static int opt_integrity_recovery = 0;
static int opt_integrity_bitmap = 0;
static int opt_integrity_legacy_padding = 0;
static int opt_integrity_legacy_hmac = 0;
static int opt_integrity_legacy_recalculate = 0;
static int opt_integrity_recalculate = 0;
static int opt_allow_discards = 0;
static const char *integrity_alg = DEFAULT_ALG_NAME;
static const char **action_argv; static const char **action_argv;
static int action_argc; static int action_argc;
static struct tools_log_params log_parms;
void tools_cleanup(void) void tools_cleanup(void)
{ {
FREE_AND_NULL(opt_data_device); tools_args_free(tool_core_args, ARRAY_SIZE(tool_core_args));
FREE_AND_NULL(opt_integrity);
FREE_AND_NULL(opt_integrity_key_file);
FREE_AND_NULL(opt_journal_integrity);
FREE_AND_NULL(opt_journal_integrity_key_file);
FREE_AND_NULL(opt_journal_crypt);
FREE_AND_NULL(opt_journal_crypt_key_file);
FREE_AND_NULL(opt_journal_size_str);
}
// FIXME: move this to tools and handle EINTR
static int _read_mk(const char *file, char **key, int keysize)
{
int fd;
if (keysize <= 0 || keysize > (DEFAULT_INTEGRITY_KEYFILE_SIZE_MAXKB * 102
4)) {
log_err(_("Invalid key size. Maximum is %u bytes."), DEFAULT_INTE
GRITY_KEYFILE_SIZE_MAXKB * 1024);
return -EINVAL;
}
*key = crypt_safe_alloc(keysize);
if (!*key)
return -ENOMEM;
fd = open(file, O_RDONLY);
if (fd == -1) {
log_err(_("Cannot read keyfile %s."), file);
goto fail;
}
if ((read(fd, *key, keysize) != keysize)) {
log_err(_("Cannot read %d bytes from keyfile %s."), keysize, file
);
close(fd);
goto fail;
}
close(fd);
return 0;
fail:
crypt_safe_free(*key);
*key = NULL;
return -EINVAL;
} }
static int _read_keys(char **integrity_key, struct crypt_params_integrity *param s) static int _read_keys(char **integrity_key, struct crypt_params_integrity *param s)
{ {
char *int_key = NULL, *journal_integrity_key = NULL, *journal_crypt_key = NULL; char *int_key = NULL, *journal_integrity_key = NULL, *journal_crypt_key = NULL;
int r; int r;
if (integrity_key && opt_integrity_key_file) { if (integrity_key && ARG_SET(OPT_INTEGRITY_KEY_FILE_ID)) {
r = _read_mk(opt_integrity_key_file, &int_key, opt_integrity_key_ r = tools_read_mk(ARG_STR(OPT_INTEGRITY_KEY_FILE_ID), &int_key, A
size); RG_UINT32(OPT_INTEGRITY_KEY_SIZE_ID));
if (r < 0) if (r < 0)
return r; return r;
params->integrity_key_size = opt_integrity_key_size; params->integrity_key_size = ARG_UINT32(OPT_INTEGRITY_KEY_SIZE_ID );
} }
if (opt_journal_integrity_key_file) { if (ARG_SET(OPT_JOURNAL_INTEGRITY_KEY_FILE_ID)) {
r = _read_mk(opt_journal_integrity_key_file, &journal_integrity_k r = tools_read_mk(ARG_STR(OPT_JOURNAL_INTEGRITY_KEY_FILE_ID), &jo
ey, opt_journal_integrity_key_size); urnal_integrity_key, ARG_UINT32(OPT_JOURNAL_INTEGRITY_KEY_SIZE_ID));
if (r < 0) { if (r < 0) {
crypt_safe_free(int_key); crypt_safe_free(int_key);
return r; return r;
} }
params->journal_integrity_key = journal_integrity_key; params->journal_integrity_key = journal_integrity_key;
params->journal_integrity_key_size = opt_journal_integrity_key_si ze; params->journal_integrity_key_size = ARG_UINT32(OPT_JOURNAL_INTEG RITY_KEY_SIZE_ID);
} }
if (opt_journal_crypt_key_file) { if (ARG_SET(OPT_JOURNAL_CRYPT_KEY_FILE_ID)) {
r = _read_mk(opt_journal_crypt_key_file, &journal_crypt_key, opt_ r = tools_read_mk(ARG_STR(OPT_JOURNAL_CRYPT_KEY_FILE_ID), &journa
journal_crypt_key_size); l_crypt_key, ARG_UINT32(OPT_JOURNAL_CRYPT_KEY_SIZE_ID));
if (r < 0) { if (r < 0) {
crypt_safe_free(int_key); crypt_safe_free(int_key);
crypt_safe_free(journal_integrity_key); crypt_safe_free(journal_integrity_key);
return r; return r;
} }
params->journal_crypt_key = journal_crypt_key; params->journal_crypt_key = journal_crypt_key;
params->journal_crypt_key_size = opt_journal_crypt_key_size; params->journal_crypt_key_size = ARG_UINT32(OPT_JOURNAL_CRYPT_KEY _SIZE_ID);
} }
if (integrity_key) if (integrity_key)
*integrity_key = int_key; *integrity_key = int_key;
return 0; return 0;
} }
static int _wipe_data_device(struct crypt_device *cd, const char *integrity_key) static int _wipe_data_device(struct crypt_device *cd, const char *integrity_key)
{ {
char tmp_name[64], tmp_path[128], tmp_uuid[40]; char tmp_name[64], tmp_path[128], tmp_uuid[40];
uuid_t tmp_uuid_bin; uuid_t tmp_uuid_bin;
int r; int r;
struct tools_progress_params prog_parms = {
.frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID),
.batch_mode = ARG_SET(OPT_BATCH_MODE_ID)
};
if (!opt_batch_mode) if (!ARG_SET(OPT_BATCH_MODE_ID))
log_std(_("Wiping device to initialize integrity checksum.\n" log_std(_("Wiping device to initialize integrity checksum.\n"
"You can interrupt this by pressing CTRL+c " "You can interrupt this by pressing CTRL+c "
"(rest of not wiped device will contain invalid checksum) .\n")); "(rest of not wiped device will contain invalid checksum) .\n"));
/* Activate the device a temporary one */ /* Activate the device a temporary one */
uuid_generate(tmp_uuid_bin); uuid_generate(tmp_uuid_bin);
uuid_unparse(tmp_uuid_bin, tmp_uuid); uuid_unparse(tmp_uuid_bin, tmp_uuid);
if (snprintf(tmp_name, sizeof(tmp_name), "temporary-cryptsetup-%s", tmp_u uid) < 0) if (snprintf(tmp_name, sizeof(tmp_name), "temporary-cryptsetup-%s", tmp_u uid) < 0)
return -EINVAL; return -EINVAL;
if (snprintf(tmp_path, sizeof(tmp_path), "%s/%s", crypt_get_dir(), tmp_na me) < 0) if (snprintf(tmp_path, sizeof(tmp_path), "%s/%s", crypt_get_dir(), tmp_na me) < 0)
return -EINVAL; return -EINVAL;
r = crypt_activate_by_volume_key(cd, tmp_name, integrity_key, r = crypt_activate_by_volume_key(cd, tmp_name, integrity_key,
opt_integrity_key_size, CRYPT_ACTIVATE_PRIVATE | CRYPT_ACTIVATE_N O_JOURNAL); ARG_UINT32(OPT_INTEGRITY_KEY_SIZE_ID), CRYPT_ACTIVATE_PRIVATE | C RYPT_ACTIVATE_NO_JOURNAL);
if (r < 0) if (r < 0)
return r; return r;
/* Wipe the device */ /* Wipe the device */
set_int_handler(0); set_int_handler(0);
r = crypt_wipe(cd, tmp_path, CRYPT_WIPE_ZERO, 0, 0, DEFAULT_WIPE_BLOCK, r = crypt_wipe(cd, tmp_path, CRYPT_WIPE_ZERO, 0, 0, DEFAULT_WIPE_BLOCK,
0, &tools_wipe_progress, NULL); 0, &tools_wipe_progress, &prog_parms);
if (crypt_deactivate(cd, tmp_name)) if (crypt_deactivate(cd, tmp_name))
log_err(_("Cannot deactivate temporary device %s."), tmp_path); log_err(_("Cannot deactivate temporary device %s."), tmp_path);
set_int_block(0); set_int_block(0);
return r; return r;
} }
static int action_format(int arg) static int action_format(void)
{ {
struct crypt_device *cd = NULL; struct crypt_device *cd = NULL;
struct crypt_params_integrity params = { struct crypt_params_integrity params = {
.journal_size = opt_journal_size, .journal_size = ARG_UINT64(OPT_JOURNAL_SIZE_ID),
.interleave_sectors = opt_interleave_sectors, .interleave_sectors = ARG_UINT32(OPT_INTERLEAVE_SECTORS_ID),
/* in bitmap mode we have to overload these values... */ /* in bitmap mode we have to overload these values... */
.journal_watermark = opt_integrity_bitmap ? opt_bitmap_sectors_pe .journal_watermark = ARG_SET(OPT_INTEGRITY_BITMAP_MODE_ID) ? ARG_
r_bit : opt_journal_watermark, UINT32(OPT_BITMAP_SECTORS_PER_BIT_ID) : ARG_UINT32(OPT_JOURNAL_WATERMARK_ID),
.journal_commit_time = opt_integrity_bitmap ? opt_bitmap_flush_ti .journal_commit_time = ARG_SET(OPT_INTEGRITY_BITMAP_MODE_ID) ? AR
me : opt_journal_commit_time, G_UINT32(OPT_BITMAP_FLUSH_TIME_ID) : ARG_UINT32(OPT_JOURNAL_COMMIT_TIME_ID),
.buffer_sectors = opt_buffer_sectors, .buffer_sectors = ARG_UINT32(OPT_BUFFER_SECTORS_ID),
.tag_size = opt_tag_size, .tag_size = ARG_UINT32(OPT_TAG_SIZE_ID),
.sector_size = opt_sector_size ?: SECTOR_SIZE, .sector_size = ARG_UINT32(OPT_SECTOR_SIZE_ID),
}, params2; }, params2;
char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journa l_crypt[MAX_CIPHER_LEN]; char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journa l_crypt[MAX_CIPHER_LEN];
char *integrity_key = NULL, *msg = NULL; char *integrity_key = NULL, *msg = NULL;
int r; int r;
size_t signatures; size_t signatures;
r = crypt_parse_hash_integrity_mode(integrity_alg, integrity); r = crypt_parse_hash_integrity_mode(ARG_STR(OPT_INTEGRITY_ID), integrity) ;
if (r < 0) { if (r < 0) {
log_err(_("No known integrity specification pattern detected.")); log_err(_("No known integrity specification pattern detected."));
return r; return r;
} }
params.integrity = integrity; params.integrity = integrity;
if (opt_journal_integrity) { if (ARG_SET(OPT_JOURNAL_INTEGRITY_ID)) {
r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journa r = crypt_parse_hash_integrity_mode(ARG_STR(OPT_JOURNAL_INTEGRITY
l_integrity); _ID), journal_integrity);
if (r < 0) { if (r < 0) {
log_err(_("No known integrity specification pattern detec ted.")); log_err(_("No known integrity specification pattern detec ted."));
return r; return r;
} }
params.journal_integrity = journal_integrity; params.journal_integrity = journal_integrity;
} }
if (opt_journal_crypt) { if (ARG_SET(OPT_JOURNAL_CRYPT_ID)) {
r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_cr r = crypt_parse_hash_integrity_mode(ARG_STR(OPT_JOURNAL_CRYPT_ID)
ypt); , journal_crypt);
if (r < 0) { if (r < 0) {
log_err(_("No known integrity specification pattern detec ted.")); log_err(_("No known integrity specification pattern detec ted."));
return r; return r;
} }
params.journal_crypt = journal_crypt; params.journal_crypt = journal_crypt;
} }
r = _read_keys(&integrity_key, &params); r = _read_keys(&integrity_key, &params);
if (r) if (r)
goto out; goto out;
r = crypt_init_data_device(&cd, action_argv[0], opt_data_device); r = crypt_init_data_device(&cd, action_argv[0], ARG_STR(OPT_DATA_DEVICE_I D));
if (r < 0) if (r < 0)
goto out; goto out;
r = asprintf(&msg, _("This will overwrite data on %s irrevocably."), acti if (!ARG_SET(OPT_BATCH_MODE_ID)) {
on_argv[0]); r = asprintf(&msg, _("This will overwrite data on %s irrevocably.
if (r == -1) { "), action_argv[0]);
r = -ENOMEM; if (r == -1) {
goto out; r = -ENOMEM;
} goto out;
}
r = yesDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL; r = yesDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL;
free(msg); free(msg);
if (r < 0) if (r < 0)
goto out; goto out;
}
r = tools_detect_signatures(action_argv[0], 0, &signatures); r = tools_detect_signatures(action_argv[0], 0, &signatures, ARG_SET(OPT_B ATCH_MODE_ID));
if (r < 0) if (r < 0)
goto out; goto out;
/* Signature candidates found */ /* Signature candidates found */
if (signatures && ((r = tools_wipe_all_signatures(action_argv[0])) < 0)) if (signatures && ((r = tools_wipe_all_signatures(action_argv[0])) < 0))
goto out; goto out;
if (opt_integrity_legacy_padding) if (ARG_SET(OPT_INTEGRITY_LEGACY_PADDING_ID))
crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_PADDING ); crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_PADDING );
if (opt_integrity_legacy_hmac) if (ARG_SET(OPT_INTEGRITY_LEGACY_HMAC_ID))
crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_HMAC); crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_HMAC);
r = crypt_format(cd, CRYPT_INTEGRITY, NULL, NULL, NULL, NULL, 0, &params) ; r = crypt_format(cd, CRYPT_INTEGRITY, NULL, NULL, NULL, NULL, 0, &params) ;
if (r < 0) /* FIXME: call wipe signatures again */ if (r < 0) /* FIXME: call wipe signatures again */
goto out; goto out;
if (!opt_batch_mode && !crypt_get_integrity_info(cd, &params2)) if (!ARG_SET(OPT_BATCH_MODE_ID) && !crypt_get_integrity_info(cd, &params2 ))
log_std(_("Formatted with tag size %u, internal integrity %s.\n") , log_std(_("Formatted with tag size %u, internal integrity %s.\n") ,
params2.tag_size, params2.integrity); params2.tag_size, params2.integrity);
if (!opt_no_wipe) if (!ARG_SET(OPT_NO_WIPE_ID))
r = _wipe_data_device(cd, integrity_key); r = _wipe_data_device(cd, integrity_key);
out: out:
crypt_safe_free(integrity_key); crypt_safe_free(integrity_key);
crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key); crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key);
crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key); crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key);
crypt_free(cd); crypt_free(cd);
return r; return r;
} }
static int action_open(int arg) static int action_open(void)
{ {
struct crypt_device *cd = NULL; struct crypt_device *cd = NULL;
struct crypt_params_integrity params = { struct crypt_params_integrity params = {
/* in bitmap mode we have to overload these values... */ /* in bitmap mode we have to overload these values... */
.journal_watermark = opt_integrity_bitmap ? opt_bitmap_sectors_pe .journal_watermark = ARG_SET(OPT_INTEGRITY_BITMAP_MODE_ID) ? ARG_
r_bit : opt_journal_watermark, UINT32(OPT_BITMAP_SECTORS_PER_BIT_ID) : ARG_UINT32(OPT_JOURNAL_WATERMARK_ID),
.journal_commit_time = opt_integrity_bitmap ? opt_bitmap_flush_ti .journal_commit_time = ARG_SET(OPT_INTEGRITY_BITMAP_MODE_ID) ? AR
me : opt_journal_commit_time, G_UINT32(OPT_BITMAP_FLUSH_TIME_ID) : ARG_UINT32(OPT_JOURNAL_COMMIT_TIME_ID),
.buffer_sectors = opt_buffer_sectors, .buffer_sectors = ARG_UINT32(OPT_BUFFER_SECTORS_ID),
}; };
uint32_t activate_flags = 0; uint32_t activate_flags = 0;
char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journa l_crypt[MAX_CIPHER_LEN]; char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journa l_crypt[MAX_CIPHER_LEN];
char *integrity_key = NULL; char *integrity_key = NULL;
int r; int r;
r = crypt_parse_hash_integrity_mode(integrity_alg, integrity); r = crypt_parse_hash_integrity_mode(ARG_STR(OPT_INTEGRITY_ID), integrity) ;
if (r < 0) { if (r < 0) {
log_err(_("No known integrity specification pattern detected.")); log_err(_("No known integrity specification pattern detected."));
return r; return r;
} }
params.integrity = integrity; params.integrity = integrity;
if (opt_journal_integrity) { if (ARG_SET(OPT_JOURNAL_INTEGRITY_ID)) {
r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journa r = crypt_parse_hash_integrity_mode(ARG_STR(OPT_JOURNAL_INTEGRITY
l_integrity); _ID), journal_integrity);
if (r < 0) { if (r < 0) {
log_err(_("No known integrity specification pattern detec ted.")); log_err(_("No known integrity specification pattern detec ted."));
return r; return r;
} }
params.journal_integrity = journal_integrity; params.journal_integrity = journal_integrity;
} }
if (opt_journal_crypt) { if (ARG_SET(OPT_JOURNAL_CRYPT_ID)) {
r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_cr r = crypt_parse_hash_integrity_mode(ARG_STR(OPT_JOURNAL_CRYPT_ID)
ypt); , journal_crypt);
if (r < 0) { if (r < 0) {
log_err(_("No known integrity specification pattern detec ted.")); log_err(_("No known integrity specification pattern detec ted."));
return r; return r;
} }
params.journal_crypt = journal_crypt; params.journal_crypt = journal_crypt;
} }
if (opt_integrity_nojournal || opt_integrity_bitmap) if (ARG_SET(OPT_INTEGRITY_NO_JOURNAL_ID) || ARG_SET(OPT_INTEGRITY_BITMAP_ MODE_ID))
activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL; activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
if (opt_integrity_recovery) if (ARG_SET(OPT_INTEGRITY_RECOVERY_MODE_ID))
activate_flags |= CRYPT_ACTIVATE_RECOVERY; activate_flags |= CRYPT_ACTIVATE_RECOVERY;
if (opt_integrity_bitmap) if (ARG_SET(OPT_INTEGRITY_BITMAP_MODE_ID))
activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP; activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
if (opt_integrity_recalculate || opt_integrity_legacy_recalculate) if (ARG_SET(OPT_INTEGRITY_RECALCULATE_ID) || ARG_SET(OPT_INTEGRITY_LEGACY _RECALC_ID))
activate_flags |= CRYPT_ACTIVATE_RECALCULATE; activate_flags |= CRYPT_ACTIVATE_RECALCULATE;
if (opt_allow_discards)
if (ARG_SET(OPT_INTEGRITY_RECALCULATE_RESET_ID))
activate_flags |= CRYPT_ACTIVATE_RECALCULATE_RESET;
if (ARG_SET(OPT_ALLOW_DISCARDS_ID))
activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS; activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
r = _read_keys(&integrity_key, &params); r = _read_keys(&integrity_key, &params);
if (r) if (r)
goto out; goto out;
if ((r = crypt_init_data_device(&cd, action_argv[0], opt_data_device))) if ((r = crypt_init_data_device(&cd, action_argv[0], ARG_STR(OPT_DATA_DEV ICE_ID))))
goto out; goto out;
r = crypt_load(cd, CRYPT_INTEGRITY, &params); r = crypt_load(cd, CRYPT_INTEGRITY, &params);
if (r) if (r)
goto out; goto out;
if (opt_integrity_legacy_recalculate) if (ARG_SET(OPT_INTEGRITY_LEGACY_RECALC_ID))
crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_RECALC) ; crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_RECALC) ;
r = crypt_activate_by_volume_key(cd, action_argv[1], integrity_key, r = crypt_activate_by_volume_key(cd, action_argv[1], integrity_key,
opt_integrity_key_size, activate_flags); ARG_UINT32(OPT_INTEGRITY_KEY_SIZE_ID), a ctivate_flags);
out: out:
crypt_safe_free(integrity_key); crypt_safe_free(integrity_key);
crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key); crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key);
crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key); crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key);
crypt_free(cd); crypt_free(cd);
return r; return r;
} }
static int action_close(int arg) static int action_close(void)
{ {
struct crypt_device *cd = NULL; struct crypt_device *cd = NULL;
crypt_status_info ci;
uint32_t flags = 0;
int r; int r;
if (ARG_SET(OPT_DEFERRED_ID))
flags |= CRYPT_DEACTIVATE_DEFERRED;
if (ARG_SET(OPT_CANCEL_DEFERRED_ID))
flags |= CRYPT_DEACTIVATE_DEFERRED_CANCEL;
r = crypt_init_by_name(&cd, action_argv[0]); r = crypt_init_by_name(&cd, action_argv[0]);
if (r == 0) if (r == 0)
r = crypt_deactivate(cd, action_argv[0]); r = crypt_deactivate_by_name(cd, action_argv[0], flags);
if (!r && ARG_SET(OPT_DEFERRED_ID)) {
ci = crypt_status(cd, action_argv[0]);
if (ci == CRYPT_ACTIVE || ci == CRYPT_BUSY)
log_std(_("Device %s is still active and scheduled for de
ferred removal.\n"),
action_argv[0]);
}
crypt_free(cd); crypt_free(cd);
return r; return r;
} }
static int action_status(int arg) static int action_status(void)
{ {
crypt_status_info ci; crypt_status_info ci;
struct crypt_active_device cad; struct crypt_active_device cad;
struct crypt_params_integrity ip = {}; struct crypt_params_integrity ip = {};
struct crypt_device *cd = NULL; struct crypt_device *cd = NULL;
char *backing_file; char *backing_file;
const char *device, *metadata_device; const char *device, *metadata_device;
int path = 0, r = 0; int path = 0, r = 0;
/* perhaps a path, not a dm device name */ /* perhaps a path, not a dm device name */
skipping to change at line 468 skipping to change at line 421
(cad.flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) ? "di scards " : ""); (cad.flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) ? "di scards " : "");
} }
out: out:
crypt_free(cd); crypt_free(cd);
if (r == -ENOTSUP) if (r == -ENOTSUP)
r = 0; r = 0;
return r; return r;
return -EINVAL; return -EINVAL;
} }
static int action_dump(int arg) static int action_dump(void)
{ {
struct crypt_device *cd = NULL; struct crypt_device *cd = NULL;
struct crypt_params_integrity params = {}; struct crypt_params_integrity params = {};
int r; int r;
if ((r = crypt_init(&cd, action_argv[0]))) if ((r = crypt_init(&cd, action_argv[0])))
return r; return r;
r = crypt_load(cd, CRYPT_INTEGRITY, &params); r = crypt_load(cd, CRYPT_INTEGRITY, &params);
if (!r) if (!r)
crypt_dump(cd); crypt_dump(cd);
crypt_free(cd); crypt_free(cd);
return r; return r;
} }
static struct action_type { static struct action_type {
const char *type; const char *type;
int (*handler)(int); int (*handler)(void);
int required_action_argc; int required_action_argc;
const char *arg_desc; const char *arg_desc;
const char *desc; const char *desc;
} action_types[] = { } action_types[] = {
{ "format", action_format, 1, N_("<integrity_device>"),N_("format dev { FORMAT_ACTION,action_format, 1, N_("<integrity_device>"),N_("format dev
ice") }, ice") },
{ "open", action_open, 2, N_("<integrity_device> <name>"),N_("ope { OPEN_ACTION, action_open, 2, N_("<integrity_device> <name>"),N_("ope
n device as <name>") }, n device as <name>") },
{ "close", action_close, 1, N_("<name>"),N_("close device (remove m { CLOSE_ACTION, action_close, 1, N_("<name>"),N_("close device (remove m
apping)") }, apping)") },
{ "status", action_status, 1, N_("<name>"),N_("show active device sta { STATUS_ACTION,action_status, 1, N_("<name>"),N_("show active device sta
tus") }, tus") },
{ "dump", action_dump, 1, N_("<integrity_device>"),N_("show on-di { DUMP_ACTION, action_dump, 1, N_("<integrity_device>"),N_("show on-di
sk information") }, sk information") },
{ NULL, NULL, 0, NULL, NULL } {}
}; };
static void help(poptContext popt_context, static void help(poptContext popt_context,
enum poptCallbackReason reason __attribute__((unused)), enum poptCallbackReason reason __attribute__((unused)),
struct poptOption *key, struct poptOption *key,
const char *arg __attribute__((unused)), const char *arg __attribute__((unused)),
void *data __attribute__((unused))) void *data __attribute__((unused)))
{ {
struct action_type *action; struct action_type *action;
skipping to change at line 542 skipping to change at line 495
} else } else
usage(popt_context, EXIT_SUCCESS, NULL, NULL); usage(popt_context, EXIT_SUCCESS, NULL, NULL);
} }
static int run_action(struct action_type *action) static int run_action(struct action_type *action)
{ {
int r; int r;
log_dbg("Running command %s.", action->type); log_dbg("Running command %s.", action->type);
r = action->handler(0); r = action->handler();
show_status(r); show_status(r);
return translate_errno(r); return translate_errno(r);
} }
static bool needs_size_conversion(unsigned int arg_id)
{
return arg_id == OPT_JOURNAL_SIZE_ID;
}
static void basic_options_cb(poptContext popt_context,
enum poptCallbackReason reason __attribute__((unused)),
struct poptOption *key,
const char *arg,
void *data __attribute__((unused)))
{
char msg[256];
tools_parse_arg_value(popt_context, tool_core_args[key->val].type, tool_c
ore_args + key->val, arg, key->val, needs_size_conversion);
/* special cases additional handling */
switch (key->val) {
case OPT_DEBUG_ID:
log_parms.debug = true;
/* fall through */
case OPT_VERBOSE_ID:
log_parms.verbose = true;
break;
case OPT_INTEGRITY_KEY_SIZE_ID:
/* fall through */
case OPT_JOURNAL_INTEGRITY_KEY_SIZE_ID:
/* fall through */
case OPT_JOURNAL_CRYPT_KEY_SIZE_ID:
if (ARG_UINT32(key->val) > (DEFAULT_INTEGRITY_KEYFILE_SIZE_MAXKB
* 1024)) {
snprintf(msg, sizeof(msg), _("Invalid --%s size. Maximum
is %u bytes."),
key->longName, DEFAULT_INTEGRITY_KEYFILE_SIZE_MA
XKB * 1024);
usage(popt_context, EXIT_FAILURE, msg,
poptGetInvocationName(popt_context));
}
}
}
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
static const char *null_action_argv[] = {NULL}; static const char *null_action_argv[] = {NULL};
static struct poptOption popt_help_options[] = { static struct poptOption popt_help_options[] = {
{ NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL }, { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
{ "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help m essage"), NULL }, { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help m essage"), NULL },
{ "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief us age"), NULL }, { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief us age"), NULL },
{ "version",'V', POPT_ARG_NONE, NULL, 0, N_("Print package ve rsion"), NULL }, { "version",'V', POPT_ARG_NONE, NULL, 0, N_("Print package ve rsion"), NULL },
POPT_TABLEEND POPT_TABLEEND
}; };
static struct poptOption popt_basic_options[] = {
{ NULL, '\0', POPT_ARG_CALLBACK, basic_options_cb, 0, NULL, NU
LL },
#define ARG(A, B, C, D, E, F, G, H) { A, B, C, NULL, A ## _ID, D, E },
#include "integritysetup_arg_list.h"
#undef arg
POPT_TABLEEND
};
static struct poptOption popt_options[] = { static struct poptOption popt_options[] = {
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_o { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("H
ptions, 0, N_("Help options:"), NULL }, elp options:"), NULL },
{ "verbose", 'v', POPT_ARG_NONE, &opt_verbose, { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_basic_options, 0, NULL
0, N_("Shows more detailed error messages"), NULL }, , NULL },
{ "debug", '\0', POPT_ARG_NONE, &opt_debug,
0, N_("Show debug messages"), NULL },
{ "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode,
0, N_("Do not ask for confirmation"), NULL },
{ "progress-frequency", '\0', POPT_ARG_INT, &opt_progress_freque
ncy, 0, N_("Progress line update (in seconds)"), N_("secs") },
{ "no-wipe", '\0', POPT_ARG_NONE, &opt_no_wipe,
0, N_("Do not wipe device after format"), NULL },
{ "data-device", '\0', POPT_ARG_STRING, &opt_data_device,
0, N_("Path to data device (if separated)"), N_("path") },
{ "journal-size", 'j', POPT_ARG_STRING,&opt_journal_size_s
tr, 0, N_("Journal size"), N_("bytes") },
{ "interleave-sectors", '\0', POPT_ARG_INT, &opt_interleave_sect
ors, 0, N_("Interleave sectors"), N_("SECTORS") },
{ "journal-watermark", '\0', POPT_ARG_INT, &opt_journal_waterma
rk, 0, N_("Journal watermark"),N_("percent") },
{ "journal-commit-time",'\0', POPT_ARG_INT, &opt_journal_commit_
time,0, N_("Journal commit time"), N_("ms") },
{ "bitmap-sectors-per-bit",'\0', POPT_ARG_INT,&opt_bitmap_sectors
_per_bit, 0, N_("Number of 512-byte sectors per bit (bitmap mode)."), NULL },
{ "bitmap-flush-time", '\0', POPT_ARG_INT, &opt_bitmap_flush_ti
me, 0, N_("Bitmap mode flush time"), N_("ms") },
{ "tag-size", 't', POPT_ARG_INT, &opt_tag_size,
0, N_("Tag size (per-sector)"), N_("bytes") },
{ "sector-size", 's', POPT_ARG_INT, &opt_sector_size,
0, N_("Sector size"), N_("bytes") },
{ "buffer-sectors", '\0', POPT_ARG_INT, &opt_buffer_sectors,
0, N_("Buffers size"), N_("SECTORS") },
{ "integrity", 'I', POPT_ARG_STRING, &opt_integr
ity, 0, N_("Data integrity algorithm"), NULL },
{ "integrity-key-size", '\0', POPT_ARG_INT, &opt_integr
ity_key_size, 0, N_("The size of the data integrity key"), N_("BITS") },
{ "integrity-key-file", '\0', POPT_ARG_STRING, &opt_integr
ity_key_file, 0, N_("Read the integrity key from a file"), NULL },
{ "journal-integrity", '\0', POPT_ARG_STRING, &opt_journa
l_integrity, 0, N_("Journal integrity algorithm"), NULL },
{ "journal-integrity-key-size",'\0', POPT_ARG_INT, &opt_journa
l_integrity_key_size,0, N_("The size of the journal integrity key"), N_("BITS")
},
{ "journal-integrity-key-file",'\0', POPT_ARG_STRING, &opt_journa
l_integrity_key_file,0, N_("Read the journal integrity key from a file"), NULL }
,
{ "journal-crypt", '\0', POPT_ARG_STRING, &opt_journa
l_crypt, 0, N_("Journal encryption algorithm"), NULL },
{ "journal-crypt-key-size", '\0', POPT_ARG_INT, &opt_journa
l_crypt_key_size, 0, N_("The size of the journal encryption key"), N_("BITS")
},
{ "journal-crypt-key-file", '\0', POPT_ARG_STRING, &opt_journa
l_crypt_key_file, 0, N_("Read the journal encryption key from a file"), NULL
},
{ "integrity-no-journal", 'D', POPT_ARG_NONE, &opt_integri
ty_nojournal, 0, N_("Disable journal for integrity device"), NULL },
{ "integrity-recovery-mode", 'R', POPT_ARG_NONE, &opt_integri
ty_recovery, 0, N_("Recovery mode (no journal, no tag checking)"), NULL },
{ "integrity-bitmap-mode", 'B', POPT_ARG_NONE, &opt_integri
ty_bitmap, 0, N_("Use bitmap to track changes and disable journal for integrity
device"), NULL },
{ "integrity-recalculate", '\0', POPT_ARG_NONE, &opt_integri
ty_recalculate, 0, N_("Recalculate initial tags automatically."), NULL },
{ "integrity-legacy-padding", '\0', POPT_ARG_NONE, &opt_integri
ty_legacy_padding, 0, N_("Use inefficient legacy padding (old kernels)"), NULL }
,
{ "integrity-legacy-hmac", '\0', POPT_ARG_NONE, &opt_integri
ty_legacy_hmac, 0, N_("Do not protect superblock with HMAC (old kernels)"), NULL
},
{ "integrity-legacy-recalculate",'\0',POPT_ARG_NONE, &opt_integri
ty_legacy_recalculate, 0, N_("Allow recalculating of volumes with HMAC keys (old
kernels)"), NULL },
{ "allow-discards", '\0', POPT_ARG_NONE, &opt_allow_d
iscards, 0, N_("Allow discards (aka TRIM) requests for device"), NULL },
POPT_TABLEEND POPT_TABLEEND
}; };
poptContext popt_context; poptContext popt_context;
struct action_type *action; struct action_type *action;
const char *aname; const char *aname;
int r; int r;
crypt_set_log_callback(NULL, tool_log, NULL); crypt_set_log_callback(NULL, tool_log, &log_parms);
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE); textdomain(PACKAGE);
popt_context = poptGetContext("integrity", argc, argv, popt_options, 0); popt_context = poptGetContext("integrity", argc, argv, popt_options, 0);
poptSetOtherOptionHelp(popt_context, poptSetOtherOptionHelp(popt_context,
_("[OPTION...] <action> <action-specific>")); _("[OPTION...] <action> <action-specific>"));
while ((r = poptGetNextOpt(popt_context)) >= 0) { while ((r = poptGetNextOpt(popt_context)) >= 0) {
skipping to change at line 651 skipping to change at line 609
if (action_argv[0] && action_argv[1]) { if (action_argv[0] && action_argv[1]) {
const char *tmp = action_argv[0]; const char *tmp = action_argv[0];
action_argv[0] = action_argv[1]; action_argv[0] = action_argv[1];
action_argv[1] = tmp; action_argv[1] = tmp;
} }
aname = "open"; aname = "open";
} else if (!strcmp(aname, "remove")) { } else if (!strcmp(aname, "remove")) {
aname = "close"; aname = "close";
} }
if (opt_integrity)
integrity_alg = opt_integrity;
for (action = action_types; action->type; action++) for (action = action_types; action->type; action++)
if (strcmp(action->type, aname) == 0) if (strcmp(action->type, aname) == 0)
break; break;
if (!action->type) if (!action->type)
usage(popt_context, EXIT_FAILURE, _("Unknown action."), usage(popt_context, EXIT_FAILURE, _("Unknown action."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if (action_argc < action->required_action_argc) { if (action_argc < action->required_action_argc) {
char buf[128]; char buf[128];
snprintf(buf, 128,_("%s: requires %s as arguments"), action->type , action->arg_desc); snprintf(buf, 128,_("%s: requires %s as arguments"), action->type , action->arg_desc);
usage(popt_context, EXIT_FAILURE, buf, usage(popt_context, EXIT_FAILURE, buf,
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
} }
if (opt_integrity_recalculate && strcmp(aname, "open")) tools_check_args(action->type, tool_core_args, ARRAY_SIZE(tool_core_args)
usage(popt_context, EXIT_FAILURE, , popt_context);
_("Option --integrity-recalculate can be used only for open
action."),
poptGetInvocationName(popt_context));
if (opt_allow_discards && strcmp(aname, "open"))
usage(popt_context, EXIT_FAILURE,
_("Option --allow-discards is allowed only for open operati
on."),
poptGetInvocationName(popt_context));
if (opt_interleave_sectors < 0 || opt_journal_watermark < 0 ||
opt_journal_commit_time < 0 || opt_tag_size < 0 ||
opt_sector_size < 0 || opt_buffer_sectors < 0 ||
opt_integrity_key_size < 0 || opt_journal_integrity_key_size < 0 ||
opt_journal_crypt_key_size < 0 || opt_bitmap_flush_time < 0 || opt_bi
tmap_sectors_per_bit < 0)
usage(popt_context, EXIT_FAILURE,
_("Negative number for option not permitted."),
poptGetInvocationName(popt_context));
if (strcmp(aname, "format") && (opt_journal_size_str || opt_interleave_se if (ARG_SET(OPT_INTEGRITY_KEY_FILE_ID) != ARG_SET(OPT_INTEGRITY_KEY_SIZE_
ctors || ID))
opt_sector_size || opt_tag_size || opt_no_wipe ))
usage(popt_context, EXIT_FAILURE,
_("Options --journal-size, --interleave-sectors, --sector-s
ize, --tag-size"
" and --no-wipe can be used only for format action."),
poptGetInvocationName(popt_context));
if (opt_journal_size_str &&
tools_string_to_size(NULL, opt_journal_size_str, &opt_journal_size))
usage(popt_context, EXIT_FAILURE, _("Invalid journal size specifi
cation."),
poptGetInvocationName(popt_context));
if ((opt_integrity_key_file && !opt_integrity_key_size) ||
(!opt_integrity_key_file && opt_integrity_key_size))
usage(popt_context, EXIT_FAILURE, _("Both key file and key size o ptions must be specified."), usage(popt_context, EXIT_FAILURE, _("Both key file and key size o ptions must be specified."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if ((opt_journal_integrity_key_file && !opt_journal_integrity_key_size) | if (ARG_SET(OPT_JOURNAL_INTEGRITY_KEY_FILE_ID) != ARG_SET(OPT_JOURNAL_INT
| EGRITY_KEY_SIZE_ID))
(!opt_journal_integrity_key_file && opt_journal_integrity_key_size))
usage(popt_context, EXIT_FAILURE, _("Both journal integrity key f ile and key size options must be specified."), usage(popt_context, EXIT_FAILURE, _("Both journal integrity key f ile and key size options must be specified."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if (!opt_journal_integrity && opt_journal_integrity_key_file) if (!ARG_SET(OPT_JOURNAL_INTEGRITY_ID) && ARG_SET(OPT_JOURNAL_INTEGRITY_K EY_FILE_ID))
usage(popt_context, EXIT_FAILURE, _("Journal integrity algorithm must be specified if journal integrity key is used."), usage(popt_context, EXIT_FAILURE, _("Journal integrity algorithm must be specified if journal integrity key is used."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if ((opt_journal_crypt_key_file && !opt_journal_crypt_key_size) || if (ARG_SET(OPT_JOURNAL_CRYPT_KEY_FILE_ID) != ARG_SET(OPT_JOURNAL_CRYPT_K
(!opt_journal_crypt_key_file && opt_journal_crypt_key_size)) EY_SIZE_ID))
usage(popt_context, EXIT_FAILURE, _("Both journal encryption key file and key size options must be specified."), usage(popt_context, EXIT_FAILURE, _("Both journal encryption key file and key size options must be specified."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if (!opt_journal_crypt && opt_journal_crypt_key_file) if (!ARG_SET(OPT_JOURNAL_CRYPT_ID) && ARG_SET(OPT_JOURNAL_CRYPT_KEY_FILE_ ID))
usage(popt_context, EXIT_FAILURE, _("Journal encryption algorithm must be specified if journal encryption key is used."), usage(popt_context, EXIT_FAILURE, _("Journal encryption algorithm must be specified if journal encryption key is used."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if (opt_integrity_recovery && opt_integrity_bitmap) if (ARG_SET(OPT_INTEGRITY_RECOVERY_MODE_ID) && ARG_SET(OPT_INTEGRITY_BITM AP_MODE_ID))
usage(popt_context, EXIT_FAILURE, _("Recovery and bitmap mode opt ions are mutually exclusive."), usage(popt_context, EXIT_FAILURE, _("Recovery and bitmap mode opt ions are mutually exclusive."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if (opt_integrity_bitmap && (opt_journal_integrity_key_file || opt_journa if (ARG_SET(OPT_INTEGRITY_BITMAP_MODE_ID) &&
l_crypt || opt_journal_watermark || opt_journal_commit_time)) (ARG_SET(OPT_JOURNAL_INTEGRITY_KEY_FILE_ID) ||
ARG_SET(OPT_JOURNAL_CRYPT_ID) || ARG_SET(OPT_JOURNAL_WATERMARK_ID) |
|
ARG_SET(OPT_JOURNAL_COMMIT_TIME_ID)))
usage(popt_context, EXIT_FAILURE, _("Journal options cannot be us ed in bitmap mode."), usage(popt_context, EXIT_FAILURE, _("Journal options cannot be us ed in bitmap mode."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if (!opt_integrity_bitmap && (opt_bitmap_flush_time || opt_bitmap_sectors if (!ARG_SET(OPT_INTEGRITY_BITMAP_MODE_ID) &&
_per_bit)) (ARG_SET(OPT_BITMAP_FLUSH_TIME_ID) || ARG_SET(OPT_BITMAP_SECTORS_PER_
BIT_ID)))
usage(popt_context, EXIT_FAILURE, _("Bitmap options can be used o nly in bitmap mode."), usage(popt_context, EXIT_FAILURE, _("Bitmap options can be used o nly in bitmap mode."),
poptGetInvocationName(popt_context)); poptGetInvocationName(popt_context));
if (opt_debug) { if (ARG_SET(OPT_CANCEL_DEFERRED_ID) && ARG_SET(OPT_DEFERRED_ID))
opt_verbose = 1; usage(popt_context, EXIT_FAILURE,
crypt_set_debug_level(-1); _("Options --cancel-deferred and --deferred cannot be used
at the same time."),
poptGetInvocationName(popt_context));
if (ARG_SET(OPT_DEBUG_ID)) {
crypt_set_debug_level(CRYPT_DEBUG_ALL);
dbg_version_and_cmd(argc, argv); dbg_version_and_cmd(argc, argv);
} }
r = run_action(action); r = run_action(action);
tools_cleanup(); tools_cleanup();
poptFreeContext(popt_context); poptFreeContext(popt_context);
return r; return r;
} }
 End of changes. 67 change blocks. 
305 lines changed or deleted 198 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)