Skip to content

Commit 2d20ca8

Browse files
shirishpargaonkarSteve French
authored andcommitted
Eliminate sparse warning - bad constant expression
Eliminiate sparse warning during usage of crypto_shash_* APIs error: bad constant expression Allocate memory for shash descriptors once, so that we do not kmalloc/kfree it for every signature generation (shash descriptor for md5 hash). From ed7538619817777decc44b5660b52268077b74f3 Mon Sep 17 00:00:00 2001 From: Shirish Pargaonkar <shirishpargaonkar@gmail.com> Date: Tue, 24 Aug 2010 11:47:43 -0500 Subject: [PATCH] eliminate sparse warnings during crypto_shash_* APis usage Signed-off-by: Shirish Pargaonkar <shirishpargaonkar@gmail.com> Signed-off-by: Steve French <sfrench@us.ibm.com>
1 parent 24e6cf9 commit 2d20ca8

File tree

2 files changed

+128
-72
lines changed

2 files changed

+128
-72
lines changed

fs/cifs/cifsencrypt.c

Lines changed: 121 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,38 @@ extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8,
4545
static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
4646
struct TCP_Server_Info *server, char *signature)
4747
{
48-
int rc = 0;
49-
struct {
50-
struct shash_desc shash;
51-
char ctx[crypto_shash_descsize(server->ntlmssp.md5)];
52-
} sdesc;
48+
int rc;
5349

5450
if (cifs_pdu == NULL || server == NULL || signature == NULL)
5551
return -EINVAL;
5652

57-
sdesc.shash.tfm = server->ntlmssp.md5;
58-
sdesc.shash.flags = 0x0;
53+
if (!server->ntlmssp.sdescmd5) {
54+
cERROR(1,
55+
"cifs_calculate_signature: can't generate signature\n");
56+
return -1;
57+
}
5958

60-
rc = crypto_shash_init(&sdesc.shash);
59+
rc = crypto_shash_init(&server->ntlmssp.sdescmd5->shash);
6160
if (rc) {
62-
cERROR(1, "could not initialize master crypto API hmacmd5\n");
61+
cERROR(1, "cifs_calculate_signature: oould not init md5\n");
6362
return rc;
6463
}
6564

6665
if (server->secType == RawNTLMSSP)
67-
crypto_shash_update(&sdesc.shash,
66+
crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
6867
server->session_key.data.ntlmv2.key,
6968
CIFS_NTLMV2_SESSKEY_SIZE);
7069
else
71-
crypto_shash_update(&sdesc.shash,
70+
crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
7271
(char *)&server->session_key.data,
7372
server->session_key.len);
7473

75-
crypto_shash_update(&sdesc.shash,
74+
crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
7675
cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
7776

78-
rc = crypto_shash_final(&sdesc.shash, signature);
77+
rc = crypto_shash_final(&server->ntlmssp.sdescmd5->shash, signature);
7978

80-
return 0;
79+
return rc;
8180
}
8281

8382

@@ -115,55 +114,53 @@ static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
115114
struct TCP_Server_Info *server, char *signature)
116115
{
117116
int i;
118-
int rc = 0;
119-
struct {
120-
struct shash_desc shash;
121-
char ctx[crypto_shash_descsize(server->ntlmssp.md5)];
122-
} sdesc;
117+
int rc;
123118

124119
if (iov == NULL || server == NULL || signature == NULL)
125120
return -EINVAL;
126121

127-
sdesc.shash.tfm = server->ntlmssp.md5;
128-
sdesc.shash.flags = 0x0;
122+
if (!server->ntlmssp.sdescmd5) {
123+
cERROR(1, "cifs_calc_signature2: can't generate signature\n");
124+
return -1;
125+
}
129126

130-
rc = crypto_shash_init(&sdesc.shash);
127+
rc = crypto_shash_init(&server->ntlmssp.sdescmd5->shash);
131128
if (rc) {
132-
cERROR(1, "could not initialize master crypto API hmacmd5\n");
129+
cERROR(1, "cifs_calc_signature2: oould not init md5\n");
133130
return rc;
134131
}
135132

136133
if (server->secType == RawNTLMSSP)
137-
crypto_shash_update(&sdesc.shash,
134+
crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
138135
server->session_key.data.ntlmv2.key,
139136
CIFS_NTLMV2_SESSKEY_SIZE);
140137
else
141-
crypto_shash_update(&sdesc.shash,
138+
crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
142139
(char *)&server->session_key.data,
143140
server->session_key.len);
144141

145142
for (i = 0; i < n_vec; i++) {
146143
if (iov[i].iov_len == 0)
147144
continue;
148145
if (iov[i].iov_base == NULL) {
149-
cERROR(1, "null iovec entry");
146+
cERROR(1, "cifs_calc_signature2: null iovec entry");
150147
return -EIO;
151148
}
152149
/* The first entry includes a length field (which does not get
153150
signed that occupies the first 4 bytes before the header */
154151
if (i == 0) {
155152
if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
156153
break; /* nothing to sign or corrupt header */
157-
crypto_shash_update(&sdesc.shash,
154+
crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
158155
iov[i].iov_base + 4, iov[i].iov_len - 4);
159156
} else
160-
crypto_shash_update(&sdesc.shash,
157+
crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
161158
iov[i].iov_base, iov[i].iov_len);
162159
}
163160

164-
rc = crypto_shash_final(&sdesc.shash, signature);
161+
rc = crypto_shash_final(&server->ntlmssp.sdescmd5->shash, signature);
165162

166-
return 0;
163+
return rc;
167164
}
168165

169166
int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
@@ -313,76 +310,89 @@ static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
313310
wchar_t *user;
314311
wchar_t *domain;
315312
wchar_t *server;
316-
struct {
317-
struct shash_desc shash;
318-
char ctx[crypto_shash_descsize(ses->server->ntlmssp.hmacmd5)];
319-
} sdesc;
313+
314+
if (!ses->server->ntlmssp.sdeschmacmd5) {
315+
cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
316+
return -1;
317+
}
320318

321319
/* calculate md4 hash of password */
322320
E_md4hash(ses->password, nt_hash);
323321

324-
sdesc.shash.tfm = ses->server->ntlmssp.hmacmd5;
325-
sdesc.shash.flags = 0x0;
326-
327322
crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, nt_hash,
328323
CIFS_NTHASH_SIZE);
329324

330-
rc = crypto_shash_init(&sdesc.shash);
325+
rc = crypto_shash_init(&ses->server->ntlmssp.sdeschmacmd5->shash);
331326
if (rc) {
332-
cERROR(1, "could not initialize master crypto API hmacmd5\n");
327+
cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n");
333328
return rc;
334329
}
335330

336331
/* convert ses->userName to unicode and uppercase */
337332
len = strlen(ses->userName);
338333
user = kmalloc(2 + (len * 2), GFP_KERNEL);
339-
if (user == NULL)
334+
if (user == NULL) {
335+
cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n");
336+
rc = -ENOMEM;
340337
goto calc_exit_2;
338+
}
341339
len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
342340
UniStrupr(user);
343341

344-
crypto_shash_update(&sdesc.shash, (char *)user, 2 * len);
342+
crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
343+
(char *)user, 2 * len);
345344

346345
/* convert ses->domainName to unicode and uppercase */
347346
if (ses->domainName) {
348347
len = strlen(ses->domainName);
349348

350349
domain = kmalloc(2 + (len * 2), GFP_KERNEL);
351-
if (domain == NULL)
350+
if (domain == NULL) {
351+
cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
352+
rc = -ENOMEM;
352353
goto calc_exit_1;
354+
}
353355
len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
354356
nls_cp);
355357
/* the following line was removed since it didn't work well
356358
with lower cased domain name that passed as an option.
357359
Maybe converting the domain name earlier makes sense */
358360
/* UniStrupr(domain); */
359361

360-
crypto_shash_update(&sdesc.shash, (char *)domain, 2 * len);
362+
crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
363+
(char *)domain, 2 * len);
361364

362365
kfree(domain);
363366
} else if (ses->serverName) {
364367
len = strlen(ses->serverName);
365368

366369
server = kmalloc(2 + (len * 2), GFP_KERNEL);
367-
if (server == NULL)
370+
if (server == NULL) {
371+
cERROR(1, "calc_ntlmv2_hash: server mem alloc failure");
372+
rc = -ENOMEM;
368373
goto calc_exit_1;
374+
}
369375
len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
370376
nls_cp);
371377
/* the following line was removed since it didn't work well
372378
with lower cased domain name that passed as an option.
373379
Maybe converting the domain name earlier makes sense */
374380
/* UniStrupr(domain); */
375381

376-
crypto_shash_update(&sdesc.shash, (char *)server, 2 * len);
382+
crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
383+
(char *)server, 2 * len);
377384

378385
kfree(server);
379386
}
387+
388+
rc = crypto_shash_final(&ses->server->ntlmssp.sdeschmacmd5->shash,
389+
ses->server->ntlmv2_hash);
390+
380391
calc_exit_1:
381392
kfree(user);
382393
calc_exit_2:
383394
/* BB FIXME what about bytes 24 through 40 of the signing key?
384395
compare with the NTLM example */
385-
rc = crypto_shash_final(&sdesc.shash, ses->server->ntlmv2_hash);
386396

387397
return rc;
388398
}
@@ -442,34 +452,33 @@ CalcNTLMv2_response(const struct TCP_Server_Info *server,
442452
char *v2_session_response)
443453
{
444454
int rc;
445-
struct {
446-
struct shash_desc shash;
447-
char ctx[crypto_shash_descsize(server->ntlmssp.hmacmd5)];
448-
} sdesc;
449455

450-
sdesc.shash.tfm = server->ntlmssp.hmacmd5;
451-
sdesc.shash.flags = 0x0;
456+
if (!server->ntlmssp.sdeschmacmd5) {
457+
cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
458+
return -1;
459+
}
452460

453461
crypto_shash_setkey(server->ntlmssp.hmacmd5, server->ntlmv2_hash,
454462
CIFS_HMAC_MD5_HASH_SIZE);
455463

456-
rc = crypto_shash_init(&sdesc.shash);
464+
rc = crypto_shash_init(&server->ntlmssp.sdeschmacmd5->shash);
457465
if (rc) {
458-
cERROR(1, "could not initialize master crypto API hmacmd5\n");
466+
cERROR(1, "CalcNTLMv2_response: could not init hmacmd5");
459467
return rc;
460468
}
461469

462470
memcpy(v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
463471
server->cryptKey, CIFS_SERVER_CHALLENGE_SIZE);
464-
crypto_shash_update(&sdesc.shash,
472+
crypto_shash_update(&server->ntlmssp.sdeschmacmd5->shash,
465473
v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
466474
sizeof(struct ntlmv2_resp) - CIFS_SERVER_CHALLENGE_SIZE);
467475

468476
if (server->tilen)
469-
crypto_shash_update(&sdesc.shash,
477+
crypto_shash_update(&server->ntlmssp.sdeschmacmd5->shash,
470478
server->tiblob, server->tilen);
471479

472-
rc = crypto_shash_final(&sdesc.shash, v2_session_response);
480+
rc = crypto_shash_final(&server->ntlmssp.sdeschmacmd5->shash,
481+
v2_session_response);
473482

474483
return rc;
475484
}
@@ -480,10 +489,6 @@ setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
480489
{
481490
int rc = 0;
482491
struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf;
483-
struct {
484-
struct shash_desc shash;
485-
char ctx[crypto_shash_descsize(ses->server->ntlmssp.hmacmd5)];
486-
} sdesc;
487492

488493
buf->blob_signature = cpu_to_le32(0x00000101);
489494
buf->reserved = 0;
@@ -511,21 +516,24 @@ setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
511516
return rc;
512517
}
513518

519+
if (!ses->server->ntlmssp.sdeschmacmd5) {
520+
cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
521+
return -1;
522+
}
523+
514524
crypto_shash_setkey(ses->server->ntlmssp.hmacmd5,
515525
ses->server->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
516526

517-
sdesc.shash.tfm = ses->server->ntlmssp.hmacmd5;
518-
sdesc.shash.flags = 0x0;
519-
520-
rc = crypto_shash_init(&sdesc.shash);
527+
rc = crypto_shash_init(&ses->server->ntlmssp.sdeschmacmd5->shash);
521528
if (rc) {
522-
cERROR(1, "could not initialize master crypto API hmacmd5\n");
529+
cERROR(1, "setup_ntlmv2_rsp: could not init hmacmd5\n");
523530
return rc;
524531
}
525532

526-
crypto_shash_update(&sdesc.shash, resp_buf, CIFS_HMAC_MD5_HASH_SIZE);
533+
crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
534+
resp_buf, CIFS_HMAC_MD5_HASH_SIZE);
527535

528-
rc = crypto_shash_final(&sdesc.shash,
536+
rc = crypto_shash_final(&ses->server->ntlmssp.sdeschmacmd5->shash,
529537
ses->server->session_key.data.ntlmv2.key);
530538

531539
memcpy(&ses->server->session_key.data.ntlmv2.resp, resp_buf,
@@ -578,24 +586,65 @@ cifs_crypto_shash_release(struct TCP_Server_Info *server)
578586

579587
if (server->ntlmssp.hmacmd5)
580588
crypto_free_shash(server->ntlmssp.hmacmd5);
589+
590+
kfree(server->ntlmssp.sdeschmacmd5);
591+
592+
kfree(server->ntlmssp.sdescmd5);
581593
}
582594

583595
int
584596
cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
585597
{
598+
int rc;
599+
unsigned int size;
600+
586601
server->ntlmssp.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
587602
if (!server->ntlmssp.hmacmd5 ||
588603
IS_ERR(server->ntlmssp.hmacmd5)) {
589-
cERROR(1, "could not allocate master crypto API hmacmd5\n");
604+
cERROR(1, "could not allocate crypto hmacmd5\n");
590605
return 1;
591606
}
592607

593608
server->ntlmssp.md5 = crypto_alloc_shash("md5", 0, 0);
594609
if (!server->ntlmssp.md5 || IS_ERR(server->ntlmssp.md5)) {
595-
crypto_free_shash(server->ntlmssp.hmacmd5);
596-
cERROR(1, "could not allocate master crypto API md5\n");
597-
return 1;
610+
cERROR(1, "could not allocate crypto md5\n");
611+
rc = 1;
612+
goto cifs_crypto_shash_allocate_ret1;
598613
}
599614

615+
size = sizeof(struct shash_desc) +
616+
crypto_shash_descsize(server->ntlmssp.hmacmd5);
617+
server->ntlmssp.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
618+
if (!server->ntlmssp.sdeschmacmd5) {
619+
cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5\n");
620+
rc = -ENOMEM;
621+
goto cifs_crypto_shash_allocate_ret2;
622+
}
623+
server->ntlmssp.sdeschmacmd5->shash.tfm = server->ntlmssp.hmacmd5;
624+
server->ntlmssp.sdeschmacmd5->shash.flags = 0x0;
625+
626+
627+
size = sizeof(struct shash_desc) +
628+
crypto_shash_descsize(server->ntlmssp.md5);
629+
server->ntlmssp.sdescmd5 = kmalloc(size, GFP_KERNEL);
630+
if (!server->ntlmssp.sdescmd5) {
631+
cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5\n");
632+
rc = -ENOMEM;
633+
goto cifs_crypto_shash_allocate_ret3;
634+
}
635+
server->ntlmssp.sdescmd5->shash.tfm = server->ntlmssp.md5;
636+
server->ntlmssp.sdescmd5->shash.flags = 0x0;
637+
600638
return 0;
639+
640+
cifs_crypto_shash_allocate_ret3:
641+
kfree(server->ntlmssp.sdeschmacmd5);
642+
643+
cifs_crypto_shash_allocate_ret2:
644+
crypto_free_shash(server->ntlmssp.md5);
645+
646+
cifs_crypto_shash_allocate_ret1:
647+
crypto_free_shash(server->ntlmssp.hmacmd5);
648+
649+
return rc;
601650
}

0 commit comments

Comments
 (0)