util-lib: add cleanup function for crypt_free

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-11-02 09:16:47 +01:00
parent 40fd52f28d
commit 294bd45470
5 changed files with 45 additions and 32 deletions

27
src/basic/crypt-util.h Normal file
View File

@ -0,0 +1,27 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
/***
This file is part of systemd.
Copyright 2017 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#if HAVE_LIBCRYPTSETUP
#include <libcryptsetup.h>
#include "macro.h"
DEFINE_TRIVIAL_CLEANUP_FUNC(struct crypt_device *, crypt_free);
#endif

View File

@ -61,6 +61,7 @@ basic_sources_plain = files('''
copy.h
cpu-set-util.c
cpu-set-util.h
crypt-util.h
def.h
device-nodes.c
device-nodes.h

View File

@ -28,6 +28,7 @@
#include "alloc-util.h"
#include "ask-password-api.h"
#include "crypt-util.h"
#include "device-util.h"
#include "escape.h"
#include "fileio.h"
@ -604,7 +605,7 @@ static int help(void) {
}
int main(int argc, char *argv[]) {
struct crypt_device *cd = NULL;
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
int r = -EINVAL;
if (argc <= 1) {
@ -766,9 +767,6 @@ int main(int argc, char *argv[]) {
r = 0;
finish:
if (cd)
crypt_free(cd);
free(arg_cipher);
free(arg_hash);
free(arg_header);

View File

@ -24,6 +24,7 @@
#define CRYPT_LUKS NULL
#endif
#endif
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/wait.h>
@ -32,6 +33,7 @@
#include "ask-password-api.h"
#include "blkid-util.h"
#include "copy.h"
#include "crypt-util.h"
#include "def.h"
#include "device-nodes.h"
#include "dissect-image.h"
@ -850,7 +852,7 @@ static int decrypt_partition(
DecryptedImage *d) {
_cleanup_free_ char *node = NULL, *name = NULL;
struct crypt_device *cd;
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
int r;
assert(m);
@ -877,37 +879,28 @@ static int decrypt_partition(
return log_debug_errno(r, "Failed to initialize dm-crypt: %m");
r = crypt_load(cd, CRYPT_LUKS, NULL);
if (r < 0) {
log_debug_errno(r, "Failed to load LUKS metadata: %m");
goto fail;
}
if (r < 0)
return log_debug_errno(r, "Failed to load LUKS metadata: %m");
r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase),
((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) |
((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0));
if (r < 0)
if (r < 0) {
log_debug_errno(r, "Failed to activate LUKS device: %m");
if (r == -EPERM) {
r = -EKEYREJECTED;
goto fail;
return r == -EPERM ? -EKEYREJECTED : r;
}
if (r < 0)
goto fail;
d->decrypted[d->n_decrypted].name = name;
name = NULL;
d->decrypted[d->n_decrypted].device = cd;
cd = NULL;
d->n_decrypted++;
m->decrypted_node = node;
node = NULL;
return 0;
fail:
crypt_free(cd);
return r;
}
static int verity_partition(
@ -919,7 +912,7 @@ static int verity_partition(
DecryptedImage *d) {
_cleanup_free_ char *node = NULL, *name = NULL;
struct crypt_device *cd;
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
int r;
assert(m);
@ -949,30 +942,27 @@ static int verity_partition(
r = crypt_load(cd, CRYPT_VERITY, NULL);
if (r < 0)
goto fail;
return r;
r = crypt_set_data_device(cd, m->node);
if (r < 0)
goto fail;
return r;
r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY);
if (r < 0)
goto fail;
return r;
d->decrypted[d->n_decrypted].name = name;
name = NULL;
d->decrypted[d->n_decrypted].device = cd;
cd = NULL;
d->n_decrypted++;
m->decrypted_node = node;
node = NULL;
return 0;
fail:
crypt_free(cd);
return r;
}
#endif

View File

@ -18,10 +18,10 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <libcryptsetup.h>
#include <stdio.h>
#include <sys/stat.h>
#include "crypt-util.h"
#include "log.h"
#include "hexdecoct.h"
#include "string-util.h"
@ -46,7 +46,7 @@ static void log_glue(int level, const char *msg, void *usrptr) {
}
int main(int argc, char *argv[]) {
struct crypt_device *cd = NULL;
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
int r;
if (argc <= 1) {
@ -144,9 +144,6 @@ int main(int argc, char *argv[]) {
r = 0;
finish:
if (cd)
crypt_free(cd);
free(arg_root_hash);
free(arg_data_what);
free(arg_hash_what);