Skip to contentSkip to conversation

libnvdimm: Handle negative return from nvdimm_clear_poison() in nsio_rw_bytes()

PATCH Applied Serhat Kumral 3 participants 4 messages Oct 2 – Oct 15 libnvdimm: non-volatile memory device

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libnvdimm: Handle negative return from nvdimm_clear_poison() in nsio_rw_bytes()

When nvdimm_clear_poison() fails, it returns a negative error code. However, 'cleared' is a signed long while 'size' is size_t. The comparison 'cleared < size' promotes 'cleared' to an unsigned long. So the error condition can be bypassed and the function can incorrectly return success if the subsequent flush succeeds.

Explicitly check for negative error codes before comparing with 'size'.

Fixes 868f036fee4b ("libnvdimm: fix mishandled nvdimm_clear_poison() return value")
Assisted-by LLM
---
 drivers/nvdimm/claim.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvdimm/claim.c +1 -1
index 309cd2cddb0e..2725dceaf938 100644
@@ -263,7 +263,7 @@ static int nsio_rw_bytes(struct nd_namespace_common *ndns,
 			might_sleep();
 			cleared = nvdimm_clear_poison(&ndns->dev,
 					nsio->res.start + offset, size);
-			if (cleared < size)
+			if (cleared < 0 || cleared < size)
 				rc = -EIO;
 			if (cleared > 0 && cleared / 512) {
 				cleared /= 512;
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Serhat Kumral

When nvdimm_clear_poison() fails, it returns a negative error code. However, 'cleared' is a signed long while 'size' is size_t. The comparison 'cleared < size' promotes 'cleared' to an unsigned long. So the error condition can be bypassed and the function can incorrectly return success if the subsequent flush succeeds.

Quoted 7 lines

Explicitly check for negative error codes before comparing with 'size'.

Fixes 868f036fee4b ("libnvdimm: fix mishandled nvdimm_clear_poison() return value")
Assisted-by LLM

Makes sense

Quoted 17 lines
---
 drivers/nvdimm/claim.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvdimm/claim.c +1 -1
index 309cd2cddb0e..2725dceaf938 100644
@@ -263,7 +263,7 @@ static int nsio_rw_bytes(struct nd_namespace_common *ndns,
 			might_sleep();
 			cleared = nvdimm_clear_poison(&ndns->dev,
 					nsio->res.start + offset, size);
-			if (cleared < size)
+			if (cleared < 0 || cleared < size)
 				rc = -EIO;
 			if (cleared > 0 && cleared / 512) {
 				cleared /= 512;
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Serhat Kumral
Quoted 7 lines · Serhat Kumral

When nvdimm_clear_poison() fails, it returns a negative error code. However, 'cleared' is a signed long while 'size' is size_t. The comparison 'cleared < size' promotes 'cleared' to an unsigned long. So the error condition can be bypassed and the function can incorrectly return success if the subsequent flush succeeds.

Explicitly check for negative error codes before comparing with 'size'.

The fix looks good to me. Thanks for addressing this.

maintainer Applied
#3 lore raw
In reply to Serhat Kumral
Quoted 7 lines · Serhat Kumral

When nvdimm_clear_poison() fails, it returns a negative error code. However, 'cleared' is a signed long while 'size' is size_t. The comparison 'cleared < size' promotes 'cleared' to an unsigned long. So the error condition can be bypassed and the function can incorrectly return success if the subsequent flush succeeds.

Explicitly check for negative error codes before comparing with 'size'.

Applied to libnvdimm-for-next:
https://git.kernel.org/nvdimm/nvdimm/c/c0962f508e80