lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230504124828.679770-5-linan666@huaweicloud.com>
Date:   Thu,  4 May 2023 20:48:21 +0800
From:   linan666@...weicloud.com
To:     axboe@...nel.dk, linan122@...wei.com, vishal.l.verma@...el.com,
        dan.j.williams@...el.com
Cc:     linux-block@...r.kernel.org, linux-kernel@...r.kernel.org,
        yukuai3@...wei.com, yi.zhang@...wei.com, houtao1@...wei.com,
        yangerkun@...wei.com
Subject: [PATCH v2 04/11] block/badblocks: fix badblocks overlap

From: Li Nan <linan122@...wei.com>

badblocks will overlap if we set it as below:

  # echo 0 1 > bad_blocks
  # echo 3 1 > bad_blocks
  # echo 5 1 > bad_blocks
  # echo 0 512 > bad_blocks
  # cat bad_blocks
    0 512
    5 1

The reason for this is we only combine two badblocks once even if it
should be combined many times.

Fix it by classifying combine:
  1)lo contain hi
      just remove hi, and check next hi.
  2)lo intersects with hi
      newlen > BB_MAX_LEN, set lo to MAX, and the remaining is hi.
      newlen <= BB_MAX_LEN, remain unchanged, 2-in-1.

Fixes: 9e0e252a048b ("badblocks: Add core badblock management code")
Signed-off-by: Li Nan <linan122@...wei.com>
---
 block/badblocks.c | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/block/badblocks.c b/block/badblocks.c
index 2c2ef8284a3f..f34351b59414 100644
--- a/block/badblocks.c
+++ b/block/badblocks.c
@@ -245,6 +245,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
 			/* merging is possible */
 			if (e <= s + sectors) {
 				/* full overlap */
+
 				e = s + sectors;
 				ack = acknowledged;
 			} else
@@ -267,17 +268,35 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
 	if (sectors == 0 && hi < bb->count) {
 		/* we might be able to combine lo and hi */
 		/* Note: 's' is at the end of 'lo' */
-		sector_t a = BB_OFFSET(p[lo]);
-		int newlen = max(s, BB_OFFSET(p[hi]) + BB_LEN(p[hi])) - a;
-
-		if (s >= BB_OFFSET(p[hi]) && newlen < BB_MAX_LEN) {
-			/* yes, we can combine them */
-			int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
-
-			p[lo] = BB_MAKE(a, newlen, ack);
-			memmove(p + hi, p + hi + 1,
-				(bb->count - hi - 1) * 8);
-			bb->count--;
+		sector_t loa = BB_OFFSET(p[lo]), hia = BB_OFFSET(p[hi]);
+		sector_t hie = hia + BB_LEN(p[hi]);
+		int newlen = max(s, hie) - loa;
+		int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
+
+		if (s >= hia) {
+			while (s >= hie) {
+				/* lo contains hi, just remove hi */
+				memmove(p + hi, p + hi + 1,
+					(bb->count - hi - 1) * 8);
+				bb->count--;
+				if (hi >= bb->count)
+					break;
+				hia = BB_OFFSET(p[hi]);
+				hie = hia + BB_LEN(p[hi]);
+			}
+			if (s >= hia && hi < bb->count) {
+				if (newlen > BB_MAX_LEN) {
+					p[lo] = BB_MAKE(loa, BB_MAX_LEN, ack);
+					p[hi] = BB_MAKE(loa + BB_MAX_LEN,
+							newlen - BB_MAX_LEN,
+							BB_ACK(p[hi]));
+				} else {
+					p[lo] = BB_MAKE(loa, newlen, ack);
+					memmove(p + hi, p + hi + 1,
+						(bb->count - hi - 1) * 8);
+					bb->count--;
+				}
+			}
 			changed = true;
 		}
 	}
-- 
2.31.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ