iscsi: Handle failure for potentially large allocations]
Am 22.08.2014 um 09:35 schrieb Peter Lieven:
Quoted 30 lines
Some code in the block layer makes potentially huge allocations. Failure is not completely unexpected there, so avoid aborting qemu and handle out-of-memory situations gracefully.
This patch addresses the allocations in the iscsi block driver.
Signed-off-by Kevin Wolf <[email protected]>Acked-by Paolo Bonzini <[email protected]>Reviewed-by Benoit Canet <[email protected]>Reviewed-by Eric Blake <[email protected]>--- block/iscsi.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)block/iscsi.c +4
-1@@ -893,7 +893,10 @@ coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, nb_blocks =sector_qemu2lun (nb_sectors, iscsilun);if (iscsilun->zeroblock ==NULL ) { - iscsilun->zeroblock =g_malloc0 (iscsilun->block_size); + iscsilun->zeroblock =g_try_malloc0 (iscsilun->block_size); +if (iscsilun->zeroblock ==NULL ) { +return -ENOMEM ; + } }
Quoted 1 line
iscsi_co_init_iscsitask(iscsilun, &iTask);
Unfortunately, I missed that one. The zeroblock is typicalls 512 Byte or 4K depending on the blocksize. What is significantly larger is the allocationmap. It is typically created on iscsi_open, but is also recreated on iscsi_truncate. I don't have the context why this patch was introduced, but I would vote for introducing a bitmap_try_new and issue a warning if the allocation fails. The allocationmap is optional we can work without it. If the pointer is NULL its not used.
Peter
Am 22.08.2014 um 09:40 hat Peter Lieven geschrieben:
Quoted 31 lines
Am 22.08.2014 um 09:35 schrieb Peter Lieven:
Quoted 30 lines
Some code in the block layer makes potentially huge allocations. Failure is not completely unexpected there, so avoid aborting qemu and handle out-of-memory situations gracefully.
This patch addresses the allocations in the iscsi block driver.
Signed-off-by Kevin Wolf <[email protected]>Acked-by Paolo Bonzini <[email protected]>Reviewed-by Benoit Canet <[email protected]>Reviewed-by Eric Blake <[email protected]>--- block/iscsi.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)block/iscsi.c +4
-1@@ -893,7 +893,10 @@ coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, nb_blocks =sector_qemu2lun (nb_sectors, iscsilun);if (iscsilun->zeroblock ==NULL ) { - iscsilun->zeroblock =g_malloc0 (iscsilun->block_size); + iscsilun->zeroblock =g_try_malloc0 (iscsilun->block_size); +if (iscsilun->zeroblock ==NULL ) { +return -ENOMEM ; + } }
Quoted 4 lines
Quoted 1 line
iscsi_co_init_iscsitask(iscsilun, &iTask);Unfortunately, I missed that one. The zeroblock is typicalls 512 Byte or 4K depending on the blocksize.
I don't remember the details, but I think when I went through all drivers, I couldn't convince myself that a reasonable block size is enforced somewhere. So I just went ahead and converted the call to be on the safe side. It can never hurt anyway.
Quoted 5 lines
What is significantly larger is the allocationmap. It is typically created on
iscsi_open, but is also recreated oniscsi_truncate. I don't have the context why this patch was introduced, but I would vote for introducing abitmap_try_newand issue a warning if the allocation fails. The allocationmap is optional we can work without it. If the pointer is NULL its not used.
Right, that one I missed because it doesn't directly use g_malloc().
Your proposal sounds good to me. Are you going to prepare a patch?
Kevin
Am 22.08.2014 um 10:42 schrieb Kevin Wolf:
Quoted 40 lines
Am 22.08.2014 um 09:40 hat Peter Lieven geschrieben:
Quoted 31 lines
Am 22.08.2014 um 09:35 schrieb Peter Lieven:
Quoted 30 lines
Some code in the block layer makes potentially huge allocations. Failure is not completely unexpected there, so avoid aborting qemu and handle out-of-memory situations gracefully.
This patch addresses the allocations in the iscsi block driver.
Signed-off-by Kevin Wolf <[email protected]>Acked-by Paolo Bonzini <[email protected]>Reviewed-by Benoit Canet <[email protected]>Reviewed-by Eric Blake <[email protected]>--- block/iscsi.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)block/iscsi.c +4
-1@@ -893,7 +893,10 @@ coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, nb_blocks =sector_qemu2lun (nb_sectors, iscsilun);if (iscsilun->zeroblock ==NULL ) { - iscsilun->zeroblock =g_malloc0 (iscsilun->block_size); + iscsilun->zeroblock =g_try_malloc0 (iscsilun->block_size); +if (iscsilun->zeroblock ==NULL ) { +return -ENOMEM ; + } }Quoted 3 lines
Quoted 1 line
iscsi_co_init_iscsitask(iscsilun, &iTask);Unfortunately, I missed that one. The zeroblock is typicalls 512 Byte or 4K depending on the blocksize.
I don't remember the details, but I think when I went through all drivers, I couldn't convince myself that a reasonable block size is enforced somewhere. So I just went ahead and converted the call to be on the safe side. It can never hurt anyway.
Quoted 8 lines
Quoted 5 lines
What is significantly larger is the allocationmap. It is typically created on
iscsi_open, but is also recreated oniscsi_truncate. I don't have the context why this patch was introduced, but I would vote for introducing abitmap_try_newand issue a warning if the allocation fails. The allocationmap is optional we can work without it. If the pointer is NULL its not used.Right, that one I missed because it doesn't directly use
g_malloc().Your proposal sounds good to me. Are you going to prepare a patch?
will do.
Peter
Il 22/08/2014 10:42, Kevin Wolf ha scritto:
Quoted 7 lines
Quoted 2 lines
Unfortunately, I missed that one. The zeroblock is typicalls 512 Byte or 4K depending on the blocksize.
I don't remember the details, but I think when I went through all drivers, I couldn't convince myself that a reasonable block size is enforced somewhere. So I just went ahead and converted the call to be on the safe side. It can never hurt anyway.
Yeah, a malicious iSCSI target could have unreasonable block sizes.
This means the minimum transfer size for SCSI devices could be on the order of half a GiB, and that could cause other unbounded allocations in the read-modify-write code. Are those protected too?
Paolo
Am 24.08.2014 um 18:30 schrieb Paolo Bonzini:
Quoted 8 lines
Il 22/08/2014 10:42, Kevin Wolf ha scritto:
Quoted 6 lines
Quoted 2 lines
Unfortunately, I missed that one. The zeroblock is typicalls 512 Byte or 4K depending on the blocksize.
I don't remember the details, but I think when I went through all drivers, I couldn't convince myself that a reasonable block size is enforced somewhere. So I just went ahead and converted the call to be on the safe side. It can never hurt anyway.
Yeah, a malicious iSCSI target could have unreasonable block sizes.
Maybe we should just allow 512b or 4kb blocksize and refuse all other?
Peter
Quoted 6 lines
This means the minimum transfer size for SCSI devices could be on the order of half a GiB, and that could cause other unbounded allocations in the read-modify-write code. Are those protected too?
Paolo
Il 24/08/2014 21:31, Peter Lieven ha scritto:
Quoted 11 lines
Am 24.08.2014 um 18:30 schrieb Paolo Bonzini:
Quoted 8 lines
Il 22/08/2014 10:42, Kevin Wolf ha scritto:
Quoted 6 lines
Quoted 2 lines
Unfortunately, I missed that one. The zeroblock is typicalls 512 Byte or 4K depending on the blocksize.
I don't remember the details, but I think when I went through all drivers, I couldn't convince myself that a reasonable block size is enforced somewhere. So I just went ahead and converted the call to be on the safe side. It can never hurt anyway.
Yeah, a malicious iSCSI target could have unreasonable block sizes.
Maybe we should just allow 512b or 4kb blocksize and refuse all other?
At least CDs and DVDs use 2kb blocksize. :)
Paolo
Quoted 5 lines · Paolo Bonzini
Il 24/08/2014 21:31, Peter Lieven ha scritto:
Quoted 4 lines
Am 24.08.2014 um 18:30 schrieb Paolo Bonzini:
Quoted 3 lines
Il 22/08/2014 10:42, Kevin Wolf ha scritto:
Quoted 2 lines
Quoted 2 lines
Unfortunately, I missed that one. The zeroblock is typicalls 512 Byte or 4K depending on the blocksize.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
I don't remember the details, but I think when I went through all drivers, I couldn't convince myself that a reasonable block size is enforced somewhere. So I just went ahead and converted the call to be on the safe side. It can never hurt anyway.
Yeah, a malicious iSCSI target could have unreasonable block sizes.
Maybe we should just allow 512b or 4kb blocksize and refuse all other?
At least CDs and DVDs use 2kb blocksize. :)
power of 2 && <= 4096 ?
Quoted 3 lines
Paolo