Skip to content

Commit 3ebed37

Browse files
shifteekeithbusch
authored andcommitted
nvme-auth: alloc nvme_dhchap_key as single buffer
Co-developed-by: Akash Appaiah <Akash.Appaiah@dell.com> Signed-off-by: Akash Appaiah <Akash.Appaiah@dell.com> Signed-off-by: Mark O'Donovan <shiftee@posteo.net> Reviewed-by: Hannes Reinecke <hare@suse.de> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent bbacf79 commit 3ebed37

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

drivers/nvme/common/auth.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ size_t nvme_auth_hmac_hash_len(u8 hmac_id)
150150
}
151151
EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
152152

153+
u32 nvme_auth_key_struct_size(u32 key_len)
154+
{
155+
struct nvme_dhchap_key key;
156+
157+
return struct_size(&key, key, key_len);
158+
}
159+
EXPORT_SYMBOL_GPL(nvme_auth_key_struct_size);
160+
153161
struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
154162
u8 key_hash)
155163
{
@@ -163,14 +171,9 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
163171
p = strrchr(secret, ':');
164172
if (p)
165173
allocated_len = p - secret;
166-
key = kzalloc(sizeof(*key), GFP_KERNEL);
174+
key = nvme_auth_alloc_key(allocated_len, 0);
167175
if (!key)
168176
return ERR_PTR(-ENOMEM);
169-
key->key = kzalloc(allocated_len, GFP_KERNEL);
170-
if (!key->key) {
171-
ret = -ENOMEM;
172-
goto out_free_key;
173-
}
174177

175178
key_len = base64_decode(secret, allocated_len, key->key);
176179
if (key_len < 0) {
@@ -213,19 +216,29 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
213216
key->hash = key_hash;
214217
return key;
215218
out_free_secret:
216-
kfree_sensitive(key->key);
217-
out_free_key:
218-
kfree(key);
219+
nvme_auth_free_key(key);
219220
return ERR_PTR(ret);
220221
}
221222
EXPORT_SYMBOL_GPL(nvme_auth_extract_key);
222223

224+
struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash)
225+
{
226+
u32 num_bytes = nvme_auth_key_struct_size(len);
227+
struct nvme_dhchap_key *key = kzalloc(num_bytes, GFP_KERNEL);
228+
229+
if (key) {
230+
key->len = len;
231+
key->hash = hash;
232+
}
233+
return key;
234+
}
235+
EXPORT_SYMBOL_GPL(nvme_auth_alloc_key);
236+
223237
void nvme_auth_free_key(struct nvme_dhchap_key *key)
224238
{
225239
if (!key)
226240
return;
227-
kfree_sensitive(key->key);
228-
kfree(key);
241+
kfree_sensitive(key);
229242
}
230243
EXPORT_SYMBOL_GPL(nvme_auth_free_key);
231244

@@ -237,7 +250,7 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn)
237250
u8 *transformed_key;
238251
int ret;
239252

240-
if (!key || !key->key) {
253+
if (!key) {
241254
pr_warn("No key specified\n");
242255
return ERR_PTR(-ENOKEY);
243256
}

include/linux/nvme-auth.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include <crypto/kpp.h>
1010

1111
struct nvme_dhchap_key {
12-
u8 *key;
1312
size_t len;
1413
u8 hash;
14+
u8 key[];
1515
};
1616

1717
u32 nvme_auth_get_seqnum(void);
@@ -24,9 +24,11 @@ const char *nvme_auth_digest_name(u8 hmac_id);
2424
size_t nvme_auth_hmac_hash_len(u8 hmac_id);
2525
u8 nvme_auth_hmac_id(const char *hmac_name);
2626

27+
u32 nvme_auth_key_struct_size(u32 key_len);
2728
struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
2829
u8 key_hash);
2930
void nvme_auth_free_key(struct nvme_dhchap_key *key);
31+
struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash);
3032
u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn);
3133
int nvme_auth_generate_key(u8 *secret, struct nvme_dhchap_key **ret_key);
3234
int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len,

0 commit comments

Comments
 (0)