diff mbox series

[3/4] badblocks: fix slab-out-of-bounds in _badblocks_check()

Message ID 20231223063728.3229446-4-linan666@huaweicloud.com (mailing list archive)
State New, archived
Headers show
Series badblocks: bugfix and cleanup of _badblocks_check() | expand

Commit Message

Li Nan Dec. 23, 2023, 6:37 a.m. UTC
From: Li Nan <linan122@huawei.com>

prev_badblocks() will return -1 if checked range start < p[0]. Accessing
p[-1] will cause bug as below:

  BUG: KASAN: slab-out-of-bounds in badblocks_check+0x2c4

Fix it by checking 'prev' before accessing badblocks->page.

Fixes: 3ea3354cb9f0 ("badblocks: improve badblocks_check() for multiple ranges handling")
Signed-off-by: Li Nan <linan122@huawei.com>
---
 block/badblocks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/block/badblocks.c b/block/badblocks.c
index 054d05b93641..71a3e43351da 100644
--- a/block/badblocks.c
+++ b/block/badblocks.c
@@ -1316,7 +1316,7 @@  static int _badblocks_check(struct badblocks *bb, sector_t s, int sectors,
 		goto out;
 	}
 
-	if (overlap_front(bb, prev, &bad)) {
+	if (prev >= 0 && overlap_front(bb, prev, &bad)) {
 		if (set == 0) {
 			*first_bad = BB_OFFSET(p[prev]);
 			*bad_sectors = BB_LEN(p[prev]);