libnvdimm: Handle negative return from nvdimm_clear_poison() in nsio_rw_bytes()
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'.
--- drivers/nvdimm/claim.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvdimm/claim.c +1 -1
@@ -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
Quoted 6 lines · Serhat Kumral
When
nvdimm_clear_poison()fails, it returns a negative error code. However, 'cleared' is a signed long while 'size' issize_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 LLMReported-by Dan Carpenter <[email protected]>Signed-off-by Serhat Kumral <[email protected]>
Makes sense
Quoted 17 lines
--- drivers/nvdimm/claim.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/nvdimm/claim.c +1
-1@@ -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 ;
Quoted 7 lines · Serhat Kumral
When
nvdimm_clear_poison()fails, it returns a negative error code. However, 'cleared' is a signed long while 'size' issize_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.
Quoted 7 lines · Serhat Kumral
When
nvdimm_clear_poison()fails, it returns a negative error code. However, 'cleared' is a signed long while 'size' issize_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