From: Simon Que Date: Mon, 7 Jan 2013 21:01:28 +0000 (-0800) Subject: CHROMIUM: md: fix dm-bootcache string compare length X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=456307565f96b27a4c5b79ca7e6a9b64f960f7ab;p=people%2Faperard%2Flinux-chromebook.git CHROMIUM: md: fix dm-bootcache string compare length The bootcache header has 12-character fields for date and time. However, the __DATE__ string is only 9 characters long incl. null terminator. When comparing __DATE__ and __TIME__ to the header's fields, we should use the lengths of __DATE__ and __TIME__, because they are <= 12 (size of the header fields). e.g. strncmp(hdr->date, __DATE__, sizeof(hdr->date)) --> sizeof(hdr->date) is 12 but __DATE__ is only 9 characters long, so will throw a smatch error. strncmp(hdr->date, __DATE__, strlen(__DATE__) + 1) --> strlen(__DATE__) + 1 == 9, so this will not result in comparing a string that is shorter than the required length. BUG=chromium-os:37656 TEST=Build kernel with USE=test, after this CL has been merged: https://gerrit.chromium.org/gerrit/#/c/40054/ dm-bootcache.c should not throw any smatch errors about strncmp(). Change-Id: I88c7cbb96dc4da611b96609ef21b3787483c25db Signed-off-by: Simon Que Reviewed-on: https://gerrit.chromium.org/gerrit/40549 Reviewed-by: Paul Taysom --- diff --git a/drivers/md/dm-bootcache.c b/drivers/md/dm-bootcache.c index 9a4dc104b1d47..d83337d47e5a8 100644 --- a/drivers/md/dm-bootcache.c +++ b/drivers/md/dm-bootcache.c @@ -727,9 +727,9 @@ static int is_valid_hdr(struct bootcache *cache, struct bootcache_hdr *hdr) return 0; if (hdr->max_hw_sectors != cache->hdr.max_hw_sectors) return 0; - if (strncmp(hdr->date, __DATE__, sizeof(hdr->date)) != 0) + if (strncmp(hdr->date, __DATE__, strlen(__DATE__) + 1) != 0) return 0; - if (strncmp(hdr->time, __TIME__, sizeof(hdr->time)) != 0) + if (strncmp(hdr->time, __TIME__, strlen(__TIME__) + 1) != 0) return 0; if (strncmp(hdr->signature, cache->hdr.signature, sizeof(hdr->signature)) != 0)