Skip to contentSkip to conversation

st.c: possible memory use after free after MTSETBLK ioctl

PATCH In review David Jeffery 2 participants 3 messages Sep 24, 2009 – Sep 27, 2009 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 912553534.308131253824005002.JavaMail.root@zmail02.collab.prod.int.phx2.redhat.com

#1 lore raw

PATCH 1/1 st.c: possible memory use after free after MTSETBLK ioctl

A memory use after free bug can manifest if the MTSETBLK or SET_DENS_AND_BLK ioctl features are used to set the tape's blocksize from 0 to non-zero. After the driver sets the new block size, in this one case it calls normalize_buffer() to free the device's internal data buffers. However, the ioctl code assumes there is always a buffer and does not check or allocate a buffer if there isn't one. So any following ioctl calls can corrupt a part of memory by writing data to memory that the st driver had freed.

This small patch forces the st driver to allocate a minimal one page buffer using enlarge_buffer() after the previous buffers were freed.

drivers/scsi/st.c +5 -1
@@ -2776,8 +2776,12 @@
 			int old_block_size = STp->block_size;
 			STp->block_size = arg & MT_ST_BLKSIZE_MASK;
 			if (STp->block_size != 0) {
-				if (old_block_size == 0)
+				if (old_block_size == 0){
 					normalize_buffer(STp->buffer);
+					if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
+						printk(KERN_ERR "No st buffer!\n");
+					}
+				}
 				(STp->buffer)->buffer_blocks =
 				    (STp->buffer)->buffer_size / STp->block_size;
 			}
#2 lore raw
Quoted 21 lines · David Jeffery

A memory use after free bug can manifest if the MTSETBLK or SET_DENS_AND_BLK ioctl features are used to set the tape's blocksize from 0 to non-zero. After the driver sets the new block size, in this one case it calls normalize_buffer() to free the device's internal data buffers. However, the ioctl code assumes there is always a buffer and does not check or allocate a buffer if there isn't one. So any following ioctl calls can corrupt a part of memory by writing data to memory that the st driver had freed.

This small patch forces the st driver to allocate a minimal one page buffer using enlarge_buffer() after the previous buffers were freed.

drivers/scsi/st.c +0 -0
@@ -2776,8 +2776,12 @@
 			int old_block_size = STp->block_size;
 			STp->block_size = arg & MT_ST_BLKSIZE_MASK;
 			if (STp->block_size != 0) {

drivers/scsi/st.c line 2787

Quoted 10 lines
drivers/scsi/st.c +5 -1
-				if (old_block_size == 0)
+				if (old_block_size == 0){
 					normalize_buffer(STp->buffer);
+					if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
+						printk(KERN_ERR "No st buffer!\n");
+					}
+				}
 				(STp->buffer)->buffer_blocks =
 				    (STp->buffer)->buffer_size / STp->block_size;
 			}

Good catch. This bug probably has not manifested itself because the next commands have either been ioctls not needing a buffer or a read/write that has enlarged the buffer.

The patch has one problem: it prints an error if the page can't be allocated but it does not do prevent using a non-allocated buffer in this case. Although this is a nearly theoretical possibility, it should be handled in some way. At least a comment should be added.

Is the patch against a current kernel? The line numbers don't match either 2.6.30 or 2.6.31?

Thanks,
Kai

#3 lore raw
Quoted 38 lines · Kai Makisara
Quoted 21 lines · David Jeffery

A memory use after free bug can manifest if the MTSETBLK or SET_DENS_AND_BLK ioctl features are used to set the tape's blocksize from 0 to non-zero. After the driver sets the new block size, in this one case it calls normalize_buffer() to free the device's internal data buffers. However, the ioctl code assumes there is always a buffer and does not check or allocate a buffer if there isn't one. So any following ioctl calls can corrupt a part of memory by writing data to memory that the st driver had freed.

This small patch forces the st driver to allocate a minimal one page buffer using enlarge_buffer() after the previous buffers were freed.

drivers/scsi/st.c +0 -0
@@ -2776,8 +2776,12 @@
 			int old_block_size = STp->block_size;
 			STp->block_size = arg & MT_ST_BLKSIZE_MASK;
 			if (STp->block_size != 0) {

drivers/scsi/st.c line 2787

Quoted 10 lines
drivers/scsi/st.c +5 -1
-				if (old_block_size == 0)
+				if (old_block_size == 0){
 					normalize_buffer(STp->buffer);
+					if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) {
+						printk(KERN_ERR "No st buffer!\n");
+					}
+				}
 				(STp->buffer)->buffer_blocks =
 				    (STp->buffer)->buffer_size / STp->block_size;
 			}

Good catch. This bug probably has not manifested itself because the next commands have either been ioctls not needing a buffer or a read/write that has enlarged the buffer.

Quoted 5 lines

The patch has one problem: it prints an error if the page can't be allocated but it does not do prevent using a non-allocated buffer in this case. Although this is a nearly theoretical possibility, it should be handled in some way. At least a comment should be added.

Having thought this a little more, it would probably be best to remove the call to normalize_buffer() instead of trying to cope with it failing. This just means that some memory may stay allocated longer. In the usage scenarios coming into my mind, the buffer is either small or it will be reallocated.

Would you like to make the patch?

Thanks,
Kai