]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util-lib: add cleanup function for crypt_free
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 2 Nov 2017 08:16:47 +0000 (09:16 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 Nov 2017 19:43:25 +0000 (20:43 +0100)
src/basic/crypt-util.h [new file with mode: 0644]
src/basic/meson.build
src/cryptsetup/cryptsetup.c
src/shared/dissect-image.c
src/veritysetup/veritysetup.c

diff --git a/src/basic/crypt-util.h b/src/basic/crypt-util.h
new file mode 100644 (file)
index 0000000..b95eb9a
--- /dev/null
@@ -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
index bf11757b744014a25a2ed7be5be772f6d44f573a..68064ab693699688e326221d7b6b38d835457f3e 100644 (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
index b19d03e9f07a6265d37ed0a91e1a0b3d889695da..38468023cb39c5df61f72755d03e9ab478350996 100644 (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);
index 1a7ccbe2b246e8014cb1e3b9a85688ebc88fc1c9..2714b3921cb7dd3ede37372473afebcefd781717 100644 (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
 
index 18554aa2316e011a75903885669e5bad8f8343a8..2376f1dc2c8bc24761d641ade7e8763ce21a72c3 100644 (file)
   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);