Skip to contentSkip to conversation

iscsi: Handle failure for potentially large allocations]

PATCH Peter Lieven 3 participants 7 messages Aug 22, 2014 – Aug 25, 2014 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

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.

---
 block/iscsi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/iscsi.c +4 -1
index 84aa22a..06afa78 100644
@@ -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

#2 lore raw

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.

---
 block/iscsi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/iscsi.c +4 -1
index 84aa22a..06afa78 100644
@@ -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 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.

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

#3 lore raw

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.

---
 block/iscsi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/iscsi.c +4 -1
index 84aa22a..06afa78 100644
@@ -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 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.

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

#4 lore raw
In reply to Kevin Wolf

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

#5 lore raw

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

#6 lore raw

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

#7 lore raw
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