summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-11-02 09:16:47 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-11-30 20:43:25 +0100
commit294bd454701bcb41012466c8a54d8df12357e611 (patch)
tree89d8c3b12cfc39ec72474ed7162516684ba3949d /src
parent40fd52f28d464d357376d8c641f5a80fdbda59a3 (diff)
downloadsystemd-294bd454701bcb41012466c8a54d8df12357e611.tar.gz
util-lib: add cleanup function for crypt_free
Diffstat (limited to 'src')
-rw-r--r--src/basic/crypt-util.h27
-rw-r--r--src/basic/meson.build1
-rw-r--r--src/cryptsetup/cryptsetup.c6
-rw-r--r--src/shared/dissect-image.c36
-rw-r--r--src/veritysetup/veritysetup.c7
5 files changed, 45 insertions, 32 deletions
diff --git a/src/basic/crypt-util.h b/src/basic/crypt-util.h
new file mode 100644
index 0000000000..b95eb9a4e7
--- /dev/null
+++ b/src/basic/crypt-util.h
@@ -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
diff --git a/src/basic/meson.build b/src/basic/meson.build
index bf11757b74..68064ab693 100644
--- a/src/basic/meson.build
+++ b/src/basic/meson.build
@@ -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
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
index b19d03e9f0..38468023cb 100644
--- a/src/cryptsetup/cryptsetup.c
+++ b/src/cryptsetup/cryptsetup.c
@@ -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);
diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c
index 1a7ccbe2b2..2714b3921c 100644
--- a/src/shared/dissect-image.c
+++ b/src/shared/dissect-image.c
@@ -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
diff --git a/src/veritysetup/veritysetup.c b/src/veritysetup/veritysetup.c
index 18554aa231..2376f1dc2c 100644
--- a/src/veritysetup/veritysetup.c
+++ b/src/veritysetup/veritysetup.c
@@ -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);