Skip to content

Commit 5c95bf2

Browse files
oberparVasily Gorbik
authored andcommitted
s390/cert_store: fix string length handling
Building cert_store.o with W=1 reveals this bug: CC arch/s390/kernel/cert_store.o arch/s390/kernel/cert_store.c:443:45: warning: ‘sprintf’ may write a terminating nul past the end of the destination [-Wformat-overflow=] 443 | sprintf(desc + name_len, ":%04u:%08u", vce->vce_hdr.vc_index, cs_token); | ^ arch/s390/kernel/cert_store.c:443:9: note: ‘sprintf’ output between 15 and 18 bytes into a destination of size 15 443 | sprintf(desc + name_len, ":%04u:%08u", vce->vce_hdr.vc_index, cs_token); Fix this by using the correct maximum width for each integer component in both buffer length calculation and format string. Also switch to using snprintf() to guard against potential future changes to the integer range of each component. Fixes: 8cf57d7 ("s390: add support for user-defined certificates") Reported-by: Heiko Carstens <[email protected]> Reviewed-by: Alexander Gordeev <[email protected]> Signed-off-by: Peter Oberparleiter <[email protected]> Signed-off-by: Vasily Gorbik <[email protected]>
1 parent 8d533ca commit 5c95bf2

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

arch/s390/kernel/cert_store.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,16 @@ static char *get_key_description(struct vcssb *vcssb, const struct vce *vce)
432432
char *desc;
433433

434434
cs_token = vcssb->cs_token;
435-
/* Description string contains "%64s:%04u:%08u\0". */
435+
/* Description string contains "%64s:%05u:%010u\0". */
436436
name_len = sizeof(vce->vce_hdr.vc_name);
437-
len = name_len + 1 + 4 + 1 + 8 + 1;
437+
len = name_len + 1 + 5 + 1 + 10 + 1;
438438
desc = kmalloc(len, GFP_KERNEL);
439439
if (!desc)
440440
return NULL;
441441

442442
memcpy(desc, vce->vce_hdr.vc_name, name_len);
443-
sprintf(desc + name_len, ":%04u:%08u", vce->vce_hdr.vc_index, cs_token);
443+
snprintf(desc + name_len, len - name_len, ":%05u:%010u",
444+
vce->vce_hdr.vc_index, cs_token);
444445

445446
return desc;
446447
}

0 commit comments

Comments
 (0)