Skip to content

Commit 28513fc

Browse files
Milan Brozkergon
authored andcommitted
dm crypt: simplify crypt_config destruction logic
Use just one label and reuse common destructor for crypt target. Parse remaining argv arguments in logic order. Also do not ignore error values from IV init and set key functions. No functional change in this patch except changed return codes based on above. Signed-off-by: Milan Broz <mbroz@redhat.com> Signed-off-by: Alasdair G Kergon <agk@redhat.com>
1 parent 7e507eb commit 28513fc

1 file changed

Lines changed: 93 additions & 87 deletions

File tree

‎drivers/md/dm-crypt.c‎

Lines changed: 93 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -999,21 +999,60 @@ static int crypt_wipe_key(struct crypt_config *cc)
999999
return crypto_ablkcipher_setkey(cc->tfm, cc->key, cc->key_size);
10001000
}
10011001

1002+
static void crypt_dtr(struct dm_target *ti)
1003+
{
1004+
struct crypt_config *cc = ti->private;
1005+
1006+
ti->private = NULL;
1007+
1008+
if (!cc)
1009+
return;
1010+
1011+
if (cc->io_queue)
1012+
destroy_workqueue(cc->io_queue);
1013+
if (cc->crypt_queue)
1014+
destroy_workqueue(cc->crypt_queue);
1015+
1016+
if (cc->bs)
1017+
bioset_free(cc->bs);
1018+
1019+
if (cc->page_pool)
1020+
mempool_destroy(cc->page_pool);
1021+
if (cc->req_pool)
1022+
mempool_destroy(cc->req_pool);
1023+
if (cc->io_pool)
1024+
mempool_destroy(cc->io_pool);
1025+
1026+
if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1027+
cc->iv_gen_ops->dtr(cc);
1028+
1029+
if (cc->tfm && !IS_ERR(cc->tfm))
1030+
crypto_free_ablkcipher(cc->tfm);
1031+
1032+
if (cc->dev)
1033+
dm_put_device(ti, cc->dev);
1034+
1035+
kfree(cc->iv_mode);
1036+
1037+
/* Must zero key material before freeing */
1038+
kzfree(cc);
1039+
}
1040+
10021041
/*
10031042
* Construct an encryption mapping:
10041043
* <cipher> <key> <iv_offset> <dev_path> <start>
10051044
*/
10061045
static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
10071046
{
10081047
struct crypt_config *cc;
1009-
struct crypto_ablkcipher *tfm;
10101048
char *tmp;
10111049
char *cipher;
10121050
char *chainmode;
10131051
char *ivmode;
10141052
char *ivopts;
10151053
unsigned int key_size;
10161054
unsigned long long tmpll;
1055+
int ret = -EINVAL;
10171056

10181057
if (argc != 5) {
10191058
ti->error = "Not enough arguments";
@@ -1032,12 +1071,13 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
10321071
key_size = strlen(argv[1]) >> 1;
10331072

10341073
cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1035-
if (cc == NULL) {
1036-
ti->error =
1037-
"Cannot allocate transparent encryption context";
1074+
if (!cc) {
1075+
ti->error = "Cannot allocate transparent encryption context";
10381076
return -ENOMEM;
10391077
}
10401078

1079+
ti->private = cc;
1080+
10411081
/* Compatibility mode for old dm-crypt cipher strings */
10421082
if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
10431083
chainmode = "cbc";
@@ -1046,35 +1086,36 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
10461086

10471087
if (strcmp(chainmode, "ecb") && !ivmode) {
10481088
ti->error = "This chaining mode requires an IV mechanism";
1049-
goto bad_cipher;
1089+
goto bad;
10501090
}
10511091

1092+
ret = -ENOMEM;
10521093
if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)",
10531094
chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) {
10541095
ti->error = "Chain mode + cipher name is too long";
1055-
goto bad_cipher;
1096+
goto bad;
10561097
}
10571098

1058-
tfm = crypto_alloc_ablkcipher(cc->cipher, 0, 0);
1059-
if (IS_ERR(tfm)) {
1099+
cc->tfm = crypto_alloc_ablkcipher(cc->cipher, 0, 0);
1100+
if (IS_ERR(cc->tfm)) {
10601101
ti->error = "Error allocating crypto tfm";
1061-
goto bad_cipher;
1102+
goto bad;
10621103
}
10631104

10641105
strcpy(cc->cipher, cipher);
10651106
strcpy(cc->chainmode, chainmode);
1066-
cc->tfm = tfm;
10671107

1068-
if (crypt_set_key(cc, argv[1]) < 0) {
1108+
ret = crypt_set_key(cc, argv[1]);
1109+
if (ret < 0) {
10691110
ti->error = "Error decoding and setting key";
1070-
goto bad_ivmode;
1111+
goto bad;
10711112
}
10721113

10731114
/*
10741115
* Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
10751116
* See comments at iv code
10761117
*/
1077-
1118+
ret = -EINVAL;
10781119
if (ivmode == NULL)
10791120
cc->iv_gen_ops = NULL;
10801121
else if (strcmp(ivmode, "plain") == 0)
@@ -1089,20 +1130,28 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
10891130
cc->iv_gen_ops = &crypt_iv_null_ops;
10901131
else {
10911132
ti->error = "Invalid IV mode";
1092-
goto bad_ivmode;
1133+
goto bad;
10931134
}
10941135

1095-
if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
1096-
cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
1097-
goto bad_ivmode;
1136+
/* Allocate IV */
1137+
if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1138+
ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1139+
if (ret < 0) {
1140+
ti->error = "Error creating IV";
1141+
goto bad;
1142+
}
1143+
}
10981144

1099-
if (cc->iv_gen_ops && cc->iv_gen_ops->init &&
1100-
cc->iv_gen_ops->init(cc) < 0) {
1101-
ti->error = "Error initialising IV";
1102-
goto bad_slab_pool;
1145+
/* Initialize IV (set keys for ESSIV etc) */
1146+
if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1147+
ret = cc->iv_gen_ops->init(cc);
1148+
if (ret < 0) {
1149+
ti->error = "Error initialising IV";
1150+
goto bad;
1151+
}
11031152
}
11041153

1105-
cc->iv_size = crypto_ablkcipher_ivsize(tfm);
1154+
cc->iv_size = crypto_ablkcipher_ivsize(cc->tfm);
11061155
if (cc->iv_size)
11071156
/* at least a 64 bit sector number should fit in our buffer */
11081157
cc->iv_size = max(cc->iv_size,
@@ -1116,130 +1165,87 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
11161165
}
11171166
}
11181167

1168+
ret = -ENOMEM;
11191169
cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
11201170
if (!cc->io_pool) {
11211171
ti->error = "Cannot allocate crypt io mempool";
1122-
goto bad_slab_pool;
1172+
goto bad;
11231173
}
11241174

11251175
cc->dmreq_start = sizeof(struct ablkcipher_request);
1126-
cc->dmreq_start += crypto_ablkcipher_reqsize(tfm);
1176+
cc->dmreq_start += crypto_ablkcipher_reqsize(cc->tfm);
11271177
cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
1128-
cc->dmreq_start += crypto_ablkcipher_alignmask(tfm) &
1178+
cc->dmreq_start += crypto_ablkcipher_alignmask(cc->tfm) &
11291179
~(crypto_tfm_ctx_alignment() - 1);
11301180

11311181
cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
11321182
sizeof(struct dm_crypt_request) + cc->iv_size);
11331183
if (!cc->req_pool) {
11341184
ti->error = "Cannot allocate crypt request mempool";
1135-
goto bad_req_pool;
1185+
goto bad;
11361186
}
11371187
cc->req = NULL;
11381188

11391189
cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
11401190
if (!cc->page_pool) {
11411191
ti->error = "Cannot allocate page mempool";
1142-
goto bad_page_pool;
1192+
goto bad;
11431193
}
11441194

11451195
cc->bs = bioset_create(MIN_IOS, 0);
11461196
if (!cc->bs) {
11471197
ti->error = "Cannot allocate crypt bioset";
1148-
goto bad_bs;
1198+
goto bad;
11491199
}
11501200

1201+
ret = -EINVAL;
11511202
if (sscanf(argv[2], "%llu", &tmpll) != 1) {
11521203
ti->error = "Invalid iv_offset sector";
1153-
goto bad_device;
1204+
goto bad;
11541205
}
11551206
cc->iv_offset = tmpll;
11561207

1208+
if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1209+
ti->error = "Device lookup failed";
1210+
goto bad;
1211+
}
1212+
11571213
if (sscanf(argv[4], "%llu", &tmpll) != 1) {
11581214
ti->error = "Invalid device sector";
1159-
goto bad_device;
1215+
goto bad;
11601216
}
11611217
cc->start = tmpll;
11621218

1163-
if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1164-
ti->error = "Device lookup failed";
1165-
goto bad_device;
1166-
}
1167-
1219+
ret = -ENOMEM;
11681220
if (ivmode && cc->iv_gen_ops) {
11691221
if (ivopts)
11701222
*(ivopts - 1) = ':';
11711223
cc->iv_mode = kstrdup(ivmode, GFP_KERNEL);
11721224
if (!cc->iv_mode) {
11731225
ti->error = "Error kmallocing iv_mode string";
1174-
goto bad_ivmode_string;
1226+
goto bad;
11751227
}
11761228
} else
11771229
cc->iv_mode = NULL;
11781230

11791231
cc->io_queue = create_singlethread_workqueue("kcryptd_io");
11801232
if (!cc->io_queue) {
11811233
ti->error = "Couldn't create kcryptd io queue";
1182-
goto bad_io_queue;
1234+
goto bad;
11831235
}
11841236

11851237
cc->crypt_queue = create_singlethread_workqueue("kcryptd");
11861238
if (!cc->crypt_queue) {
11871239
ti->error = "Couldn't create kcryptd queue";
1188-
goto bad_crypt_queue;
1240+
goto bad;
11891241
}
11901242

11911243
ti->num_flush_requests = 1;
1192-
ti->private = cc;
11931244
return 0;
11941245

1195-
bad_crypt_queue:
1196-
destroy_workqueue(cc->io_queue);
1197-
bad_io_queue:
1198-
kfree(cc->iv_mode);
1199-
bad_ivmode_string:
1200-
dm_put_device(ti, cc->dev);
1201-
bad_device:
1202-
bioset_free(cc->bs);
1203-
bad_bs:
1204-
mempool_destroy(cc->page_pool);
1205-
bad_page_pool:
1206-
mempool_destroy(cc->req_pool);
1207-
bad_req_pool:
1208-
mempool_destroy(cc->io_pool);
1209-
bad_slab_pool:
1210-
if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1211-
cc->iv_gen_ops->dtr(cc);
1212-
bad_ivmode:
1213-
crypto_free_ablkcipher(tfm);
1214-
bad_cipher:
1215-
/* Must zero key material before freeing */
1216-
kzfree(cc);
1217-
return -EINVAL;
1218-
}
1219-
1220-
static void crypt_dtr(struct dm_target *ti)
1221-
{
1222-
struct crypt_config *cc = (struct crypt_config *) ti->private;
1223-
1224-
destroy_workqueue(cc->io_queue);
1225-
destroy_workqueue(cc->crypt_queue);
1226-
1227-
if (cc->req)
1228-
mempool_free(cc->req, cc->req_pool);
1229-
1230-
bioset_free(cc->bs);
1231-
mempool_destroy(cc->page_pool);
1232-
mempool_destroy(cc->req_pool);
1233-
mempool_destroy(cc->io_pool);
1234-
1235-
kfree(cc->iv_mode);
1236-
if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1237-
cc->iv_gen_ops->dtr(cc);
1238-
crypto_free_ablkcipher(cc->tfm);
1239-
dm_put_device(ti, cc->dev);
1240-
1241-
/* Must zero key material before freeing */
1242-
kzfree(cc);
1246+
bad:
1247+
crypt_dtr(ti);
1248+
return ret;
12431249
}
12441250

12451251
static int crypt_map(struct dm_target *ti, struct bio *bio,

0 commit comments

Comments
 (0)