Skip to content

Commit 294bd45

Browse files
committed
util-lib: add cleanup function for crypt_free
1 parent 40fd52f commit 294bd45

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

src/basic/crypt-util.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
/***
3+
This file is part of systemd.
4+
5+
Copyright 2017 Zbigniew Jędrzejewski-Szmek
6+
7+
systemd is free software; you can redistribute it and/or modify it
8+
under the terms of the GNU Lesser General Public License as published by
9+
the Free Software Foundation; either version 2.1 of the License, or
10+
(at your option) any later version.
11+
12+
systemd is distributed in the hope that it will be useful, but
13+
WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public License
18+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
19+
***/
20+
21+
#if HAVE_LIBCRYPTSETUP
22+
#include <libcryptsetup.h>
23+
24+
#include "macro.h"
25+
26+
DEFINE_TRIVIAL_CLEANUP_FUNC(struct crypt_device *, crypt_free);
27+
#endif

src/basic/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ basic_sources_plain = files('''
6161
copy.h
6262
cpu-set-util.c
6363
cpu-set-util.h
64+
crypt-util.h
6465
def.h
6566
device-nodes.c
6667
device-nodes.h

src/cryptsetup/cryptsetup.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "alloc-util.h"
3030
#include "ask-password-api.h"
31+
#include "crypt-util.h"
3132
#include "device-util.h"
3233
#include "escape.h"
3334
#include "fileio.h"
@@ -604,7 +605,7 @@ static int help(void) {
604605
}
605606

606607
int main(int argc, char *argv[]) {
607-
struct crypt_device *cd = NULL;
608+
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
608609
int r = -EINVAL;
609610

610611
if (argc <= 1) {
@@ -766,9 +767,6 @@ int main(int argc, char *argv[]) {
766767
r = 0;
767768

768769
finish:
769-
if (cd)
770-
crypt_free(cd);
771-
772770
free(arg_cipher);
773771
free(arg_hash);
774772
free(arg_header);

src/shared/dissect-image.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define CRYPT_LUKS NULL
2525
#endif
2626
#endif
27+
2728
#include <sys/mount.h>
2829
#include <sys/prctl.h>
2930
#include <sys/wait.h>
@@ -32,6 +33,7 @@
3233
#include "ask-password-api.h"
3334
#include "blkid-util.h"
3435
#include "copy.h"
36+
#include "crypt-util.h"
3537
#include "def.h"
3638
#include "device-nodes.h"
3739
#include "dissect-image.h"
@@ -850,7 +852,7 @@ static int decrypt_partition(
850852
DecryptedImage *d) {
851853

852854
_cleanup_free_ char *node = NULL, *name = NULL;
853-
struct crypt_device *cd;
855+
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
854856
int r;
855857

856858
assert(m);
@@ -877,37 +879,28 @@ static int decrypt_partition(
877879
return log_debug_errno(r, "Failed to initialize dm-crypt: %m");
878880

879881
r = crypt_load(cd, CRYPT_LUKS, NULL);
880-
if (r < 0) {
881-
log_debug_errno(r, "Failed to load LUKS metadata: %m");
882-
goto fail;
883-
}
882+
if (r < 0)
883+
return log_debug_errno(r, "Failed to load LUKS metadata: %m");
884884

885885
r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase),
886886
((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) |
887887
((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0));
888-
if (r < 0)
888+
if (r < 0) {
889889
log_debug_errno(r, "Failed to activate LUKS device: %m");
890-
if (r == -EPERM) {
891-
r = -EKEYREJECTED;
892-
goto fail;
890+
return r == -EPERM ? -EKEYREJECTED : r;
893891
}
894-
if (r < 0)
895-
goto fail;
896892

897893
d->decrypted[d->n_decrypted].name = name;
898894
name = NULL;
899895

900896
d->decrypted[d->n_decrypted].device = cd;
897+
cd = NULL;
901898
d->n_decrypted++;
902899

903900
m->decrypted_node = node;
904901
node = NULL;
905902

906903
return 0;
907-
908-
fail:
909-
crypt_free(cd);
910-
return r;
911904
}
912905

913906
static int verity_partition(
@@ -919,7 +912,7 @@ static int verity_partition(
919912
DecryptedImage *d) {
920913

921914
_cleanup_free_ char *node = NULL, *name = NULL;
922-
struct crypt_device *cd;
915+
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
923916
int r;
924917

925918
assert(m);
@@ -949,30 +942,27 @@ static int verity_partition(
949942

950943
r = crypt_load(cd, CRYPT_VERITY, NULL);
951944
if (r < 0)
952-
goto fail;
945+
return r;
953946

954947
r = crypt_set_data_device(cd, m->node);
955948
if (r < 0)
956-
goto fail;
949+
return r;
957950

958951
r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY);
959952
if (r < 0)
960-
goto fail;
953+
return r;
961954

962955
d->decrypted[d->n_decrypted].name = name;
963956
name = NULL;
964957

965958
d->decrypted[d->n_decrypted].device = cd;
959+
cd = NULL;
966960
d->n_decrypted++;
967961

968962
m->decrypted_node = node;
969963
node = NULL;
970964

971965
return 0;
972-
973-
fail:
974-
crypt_free(cd);
975-
return r;
976966
}
977967
#endif
978968

src/veritysetup/veritysetup.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
along with systemd; If not, see <http://www.gnu.org/licenses/>.
1919
***/
2020

21-
#include <libcryptsetup.h>
2221
#include <stdio.h>
2322
#include <sys/stat.h>
2423

24+
#include "crypt-util.h"
2525
#include "log.h"
2626
#include "hexdecoct.h"
2727
#include "string-util.h"
@@ -46,7 +46,7 @@ static void log_glue(int level, const char *msg, void *usrptr) {
4646
}
4747

4848
int main(int argc, char *argv[]) {
49-
struct crypt_device *cd = NULL;
49+
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
5050
int r;
5151

5252
if (argc <= 1) {
@@ -144,9 +144,6 @@ int main(int argc, char *argv[]) {
144144
r = 0;
145145

146146
finish:
147-
if (cd)
148-
crypt_free(cd);
149-
150147
free(arg_root_hash);
151148
free(arg_data_what);
152149
free(arg_hash_what);

0 commit comments

Comments
 (0)