Skip to contentSkip to conversation

sd: close hole in > 2T device rejection when !CONFIG_LBDAF

PATCH In review Steven J. Magnani 3 participants 8 messages Feb 27, 2017 – Apr 4, 2017 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sd: close hole in > 2T device rejection when !CONFIG_LBDAF

When the kernel is compiled _without_ support for large (>= 2TiB) block devices, code in the sd driver's read_capacity() routines rejects devices whose count of native-sized blocks does not fit in the 32 bit sector_t type. A device reporting 4294967296 512-byte blocks will be rejected, but a device of equal capacity reporting 2147483648 1024-byte blocks will not.

The latter case causes problems, for example misreporting of device capacity by BLKGETSIZE64. This is because the kernel converts a device's capacity in native-sized blocks to an equivalent number of 512-byte units and stores the result in a sector_t - which is too small for devices such as those noted above.

To prevent this, when the kernel is compiled without support for large block devices, the read_capacity() routines must reject any device whose capacity is 2 TiB or greater regardless of its count of native-sized blocks.

---

drivers/scsi/sd.c +10 -3
@@ -2066,7 +2066,7 @@ static int read_capacity_16(struct scsi_
 	int the_result;
 	int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
 	unsigned int alignment;
-	unsigned long long lba;
+	unsigned long long lba, lba_in_sectors;
 	unsigned sector_size;
 
 	if (sdp->no_read_capacity_16)
@@ -2122,7 +2122,10 @@ static int read_capacity_16(struct scsi_
 		return -ENODEV;
 	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
+	/* Make sure logical_to_sectors() won't overflow */
+	lba_in_sectors = lba << (ilog2(sector_size) - 9);
+	if ((sizeof(sdkp->capacity) == 4) &&
+	    ((lba >= 0xffffffffULL) || (lba_in_sectors >= 0xffffffffULL))) {
 		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
 			"kernel compiled with support for large block "
 			"devices.\n");
@@ -2162,6 +2165,7 @@ static int read_capacity_10(struct scsi_
 	int the_result;
 	int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
 	sector_t lba;
+	unsigned long long lba_in_sectors;
 	unsigned sector_size;
 
 	do {
@@ -2208,7 +2212,10 @@ static int read_capacity_10(struct scsi_
 		return sector_size;
 	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
+	/* Make sure logical_to_sectors() won't overflow */
+	lba_in_sectors = ((unsigned long long) lba) << (ilog2(sector_size) - 9);
+	if ((sizeof(sdkp->capacity) == 4) &&
+	    (lba_in_sectors >= 0xffffffffULL)) {
 		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
 			"kernel compiled with support for large block "
 			"devices.\n");
#1 lore raw
Quoted 22 lines · Steven J. Magnani
drivers/scsi/sd.c +5 -1
@@ -2122,7 +2122,10 @@ static int read_capacity_16(struct scsi_
 		return -ENODEV;
 	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
+	/* Make sure logical_to_sectors() won't overflow */
+	lba_in_sectors = lba << (ilog2(sector_size) - 9);
+	if ((sizeof(sdkp->capacity) == 4) &&
+	    ((lba >= 0xffffffffULL) || (lba_in_sectors >= 0xffffffffULL))) {
 		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
 			"kernel compiled with support for large block "
 			"devices.\n");
@@ -2162,6 +2165,7 @@ static int read_capacity_10(struct scsi_
 	int the_result;
 	int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
 	sector_t lba;
+	unsigned long long lba_in_sectors;
 	unsigned sector_size;
 
 	do {
@@ -2208,7 +2212,10 @@ static int read_capacity_10(struct scsi_
 		return sector_size;

drivers/scsi/sd.c static int read_capacity_10(struct scsi_ · line 2221

Quoted 10 lines
drivers/scsi/sd.c +4 -1
 	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
+	/* Make sure logical_to_sectors() won't overflow */
+	lba_in_sectors = ((unsigned long long) lba) << (ilog2(sector_size) - 9);
+	if ((sizeof(sdkp->capacity) == 4) &&
+	    (lba_in_sectors >= 0xffffffffULL)) {
 		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
 			"kernel compiled with support for large block "
 			"devices.\n");

Why are the two checks slightly different? Could the same code be used for both checks? BTW, using the macro below would make the above checks less verbose and easier to read:

/*
 * Test whether the result of a shift-left operation would be larger than
 * what fits in a variable with the type of @a.
 */

#define shift_left_overflows(a, b)					\
	({								\
		typeof(a) _minus_one = -1LL;				\
		typeof(a) _plus_one = 1;				\
		bool _a_is_signed = _minus_one < 0;			\
		int _shift = sizeof(a) * 8 - ((b) + _a_is_signed);	\
		_shift < 0 || ((a) & ~((_plus_one << _shift) - 1)) != 0;\
	})

Bart.

#2 lore raw

Hi Bart -

Thanks for taking the time to look this over.

Quoted 25 lines · Bart Van Assche
Quoted 24 lines · Steven J. Magnani
drivers/scsi/sd.c +5 -1
@@ -2122,7 +2122,10 @@ static int read_capacity_16(struct scsi_
  		return -ENODEV;
  	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
+	/* Make sure logical_to_sectors() won't overflow */
+	lba_in_sectors = lba << (ilog2(sector_size) - 9);
+	if ((sizeof(sdkp->capacity) == 4) &&
+	    ((lba >= 0xffffffffULL) || (lba_in_sectors >= 0xffffffffULL))) {
  		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  			"kernel compiled with support for large block "
  			"devices.\n");
@@ -2162,6 +2165,7 @@ static int read_capacity_10(struct scsi_
  	int the_result;
  	int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  	sector_t lba;
+	unsigned long long lba_in_sectors;
  	unsigned sector_size;
 
  	do {
@@ -2208,7 +2212,10 @@ static int read_capacity_10(struct scsi_
  		return sector_size;
  	}
 
Quoted 10 lines

drivers/scsi/sd.c static int read_capacity_10(struct scsi_ · line 2221

Quoted 8 lines
drivers/scsi/sd.c +4 -1
-	if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
+	/* Make sure logical_to_sectors() won't overflow */
+	lba_in_sectors = ((unsigned long long) lba) << (ilog2(sector_size) - 9);
+	if ((sizeof(sdkp->capacity) == 4) &&
+	    (lba_in_sectors >= 0xffffffffULL)) {
  		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  			"kernel compiled with support for large block "
  			"devices.\n");

Why are the two checks slightly different? Could the same code be used for both checks?

The checks are different because with READ CAPACITY(16) a _really_ huge device could report a max LBA so large that left-shifting it causes bits to drop off the end. That's not an issue with READ CAPACITY(10) because at most the 32-bit LBA reported by the device will become a 35-bit value (since the max supported block size is 4096 == (512 << 3)).

Quoted 16 lines

BTW, using the macro below would make the above checks less verbose and easier to read:

/*
* Test whether the result of a shift-left operation would be larger than
* what fits in a variable with the type of @a.
*/

#define shift_left_overflows(a, b)					\
	({								\
		typeof(a) _minus_one = -1LL;				\
		typeof(a) _plus_one = 1;				\
		bool _a_is_signed = _minus_one < 0;			\
		int _shift = sizeof(a) * 8 - ((b) + _a_is_signed);	\
		_shift < 0 || ((a) & ~((_plus_one << _shift) - 1)) != 0;\
	})
Quoted 1 line

Bart.

Perhaps but I am not a fan of putting braces in non-obvious places such as within array subscripts (which I've encountered recently) or conditional expressions, which is what this amounts to.

Regards,
------------------------------------------------------------------------
Steven J. Magnani "I claim this network for MARS!
www.digidescorp.com Earthling, return my space modulator!"

#include <standard.disclaimer>

#3 lore raw
Quoted 9 lines · Steve Magnani
Quoted 2 lines · Bart Van Assche

Why are the two checks slightly different? Could the same code be used for both checks?

The checks are different because with READ CAPACITY(16) a _really_ huge device could report a max LBA so large that left-shifting it causes bits to drop off the end. That's not an issue with READ CAPACITY(10) because at most the 32-bit LBA reported by the device will become a 35-bit value (since the max supported block size is 4096 == (512 << 3)).

Sorry but I still don't understand why the two checks are different. How about the (untested) patch below? The approach below avoids that the check is duplicated and - at least in my opinion - results in code that is easier to read.

Thanks,

Bart.

drivers/scsi/sd.c +12 -2
index cb6e68dd6df0..3533d1e46bde 100644
@@ -2082,6 +2082,16 @@ static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
 	sdkp->capacity = 0; /* unknown mapped to zero - as usual */
 }
 
+/*
+ * Check whether or not logical_to_sectors(sdp, lba) will overflow.
+ */
+static bool lba_too_large(u64 lba, u32 logical_block_size)
+{
+	int shift = sizeof(sector_t) * 8 + 9 - ilog2(logical_block_size);
+
+	return shift >= 0 && shift < 64 && lba >= (1ULL << shift);
+}
+
 #define RC16_LEN 32
 #if RC16_LEN > SD_BUF_SIZE
 #error RC16_LEN must not be more than SD_BUF_SIZE
@@ -2154,7 +2164,7 @@ static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
 		return -ENODEV;
 	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
+	if (lba_too_large(lba + 1, sector_size)) {
 		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
 			"kernel compiled with support for large block "
 			"devices.\n");
@@ -2243,7 +2253,7 @@ static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
 		return sector_size;
 	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
+	if (lba_too_large(lba + 1ULL, sector_size)) {
 		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
 			"kernel compiled with support for large block "
 			"devices.\n");
Signature · 1 line
2.11.0
#4 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Bart" == Bart Van Assche <[email protected]> writes:

Bart,

Bart> Sorry but I still don't understand why the two checks are Bart> different. How about the (untested) patch below? The approach Bart> below avoids that the check is duplicated and - at least in my Bart> opinion - results in code that is easier to read.

I'll take a closer look at your patch tomorrow. I am sympathetic to having a sanity check helper function. That would also give us a single place to filter out crackpot values reported by USB doodads.

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#5 lore raw
In reply to Bart Van Assche
Quoted 3 lines · Bart Van Assche

...
How about the (untested) patch below? The approach below avoids that the check is duplicated and - at least in my opinion - results in code that is easier to read.

I find lba_too_large() a little dense, but functionally OK. The "shift >= 0" clause could be dropped. I tested this on my "problem" system (READ CAPACITY(10)) without incident.

Quoted 40 lines
drivers/scsi/sd.c +12 -2
index cb6e68dd6df0..3533d1e46bde 100644
@@ -2082,6 +2082,16 @@ static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
  	sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  }
 
+/*
+ * Check whether or not logical_to_sectors(sdp, lba) will overflow.
+ */
+static bool lba_too_large(u64 lba, u32 logical_block_size)
+{
+	int shift = sizeof(sector_t) * 8 + 9 - ilog2(logical_block_size);
+
+	return shift >= 0 && shift < 64 && lba >= (1ULL << shift);
+}
+
  #define RC16_LEN 32
  #if RC16_LEN > SD_BUF_SIZE
  #error RC16_LEN must not be more than SD_BUF_SIZE
@@ -2154,7 +2164,7 @@ static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
  		return -ENODEV;
  	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
+	if (lba_too_large(lba + 1, sector_size)) {
  		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  			"kernel compiled with support for large block "
  			"devices.\n");
@@ -2243,7 +2253,7 @@ static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
  		return sector_size;
  	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
+	if (lba_too_large(lba + 1ULL, sector_size)) {
  		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  			"kernel compiled with support for large block "
  			"devices.\n");
#6 lore raw
In reply to Bart Van Assche

Bart Van Assche <[email protected]> writes:

Hi Bart,

Quoted 4 lines

Sorry but I still don't understand why the two checks are different. How about the (untested) patch below? The approach below avoids that the check is duplicated and - at least in my opinion - results in code that is easier to read.

Just tripped over this issue in connection with something else. However, I had to make a few passes to convince myself that your proposed fix was correct. How about something like the following?

Martin

drivers/scsi/sd.c +12 -2
index fb9b4d29af0b..6084c415c070 100644
@@ -2102,6 +2102,16 @@ static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
 
 #define READ_CAPACITY_RETRIES_ON_RESET	10
 
+static bool sd_addressable_capacity(u64 lba, unsigned int sector_size)
+{
+	u64 last_sector = lba + 1ULL << ilog2(sector_size) - 9;
+
+	if (sizeof(sector_t) == 4 && last_sector > 0xffffffffULL)
+		return false;
+
+	return true;
+}
+
 static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
 						unsigned char *buffer)
 {
@@ -2167,7 +2177,7 @@ static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
 		return -ENODEV;
 	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
+	if (!sd_addressable_capacity(lba, sector_size)) {
 		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
 			"kernel compiled with support for large block "
 			"devices.\n");
@@ -2256,7 +2266,7 @@ static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
 		return sector_size;
 	}
 
-	if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
+	if (!sd_addressable_capacity(lba, sector_size)) {
 		sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
 			"kernel compiled with support for large block "
 			"devices.\n");
#7 lore raw
Quoted 7 lines · Martin K. Petersen
drivers/scsi/sd.c +0 -0
index fb9b4d29af0b..6084c415c070 100644
@@ -2102,6 +2102,16 @@ static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
 
 #define READ_CAPACITY_RETRIES_ON_RESET	10

drivers/scsi/sd.c static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp, · line 2113

Quoted 10 lines
drivers/scsi/sd.c +9 -0
 
+static bool sd_addressable_capacity(u64 lba, unsigned int sector_size)
+{
+	u64 last_sector = lba + 1ULL << ilog2(sector_size) - 9;
+
+	if (sizeof(sector_t) == 4 && last_sector > 0xffffffffULL)
+		return false;
+
+	return true;
+}

Hello Martin,

How about replacing 0xffffffffULL with U32_MAX, adding parentheses in the last_sector computation to make clear that + and - have precedence over << and adding a comment above sd_addressable_capacity() that explains its purpose and also that the shift operation must not be replaced with a call to logical_to_sectors()? Otherwise this patch looks fine to me.

Thanks,

Bart.