Skip to contentSkip to conversation

PATCH libata-2.6 4/5] Prevent the device from overrunning the buffer in __atapi_pio_bytes()

PATCH Albert Lee 3 participants 5 messages Mar 18, 2005 – Mar 29, 2005

Subsystems (2)
lore
Share & tools
lore

Albert Lee

author
#0 lore raw

Hi Jeff,

Problem:
Some bad behaved CD-ROM drives will return more data than ask to.
(I have such CD-RW drive and it crashed the kernel.)

Changes:
- Add additional check in __atapi_pio_bytes() to prevent the device from overrunning the buffer.

Attached please find the patch against the libata-2.6 tree for your review. Thanks.

Albert

---------------------------------------

drivers/scsi/libata-core.c +8 -0
@@ -2338,6 +2338,14 @@
 		ap->pio_task_state = PIO_ST_LAST;
 
 next_sg:
+	/* check whether qc->sg is full */
+	if (unlikely(qc->cursg >= qc->n_elem)) {
+		printk(KERN_WARNING "ata%u: qc->sg full, %u bytes trailing\n", ap->id, bytes);
+
+		ap->pio_task_state = PIO_ST_ERR;
+		return;
+	}
+
 	sg = &qc->sg[qc->cursg];
 
 	page = sg->page;
drivers/scsi/libata-core.c +8 -0
@@ -2338,6 +2338,14 @@
 		ap->pio_task_state = PIO_ST_LAST;
 
 next_sg:
+	/* check whether qc->sg is full */
+	if (unlikely(qc->cursg >= qc->n_elem)) {
+		printk(KERN_WARNING "ata%u: qc->sg full, %u bytes trailing\n", ap->id, bytes);
+
+		ap->pio_task_state = PIO_ST_ERR;
+		return;
+	}
+
 	sg = &qc->sg[qc->cursg];
 
 	page = sg->page;
#1 lore raw
Quoted 5 lines · Albert Lee

Hi Jeff,

Problem:
Some bad behaved CD-ROM drives will return more data than ask to.
(I have such CD-RW drive and it crashed the kernel.)

These devices are compliant with original ATAPI spec.

Such condition shouldn't be treated as an error
- extra data should be read and dumped.

Quoted 48 lines

Changes:
- Add additional check in __atapi_pio_bytes() to prevent the device from overrunning the buffer.

Attached please find the patch against the libata-2.6 tree for your review. Thanks.

Albert

---------------------------------------

drivers/scsi/libata-core.c 2005-03-18 14:00:34.000000000 +0800 renamed from drivers/scsi/libata-core.c 2005-03-18 12:56:44.000000000 +0800 +8 -0
@@ -2338,6 +2338,14 @@
                ap->pio_task_state = PIO_ST_LAST;
 
  next_sg:
+       /* check whether qc->sg is full */
+       if (unlikely(qc->cursg >= qc->n_elem)) {
+               printk(KERN_WARNING "ata%u: qc->sg full, %u bytes trailing\n", ap->id, bytes);
+
+               ap->pio_task_state = PIO_ST_ERR;
+               return;
+       }
+
        sg = &qc->sg[qc->cursg];
 
        page = sg->page;
drivers/scsi/libata-core.c 2005-03-18 14:00:34.000000000 +0800 renamed from drivers/scsi/libata-core.c 2005-03-18 12:56:44.000000000 +0800 +8 -0
@@ -2338,6 +2338,14 @@
                ap->pio_task_state = PIO_ST_LAST;
 
 next_sg:
+       /* check whether qc->sg is full */
+       if (unlikely(qc->cursg >= qc->n_elem)) {
+               printk(KERN_WARNING "ata%u: qc->sg full, %u bytes trailing\n", ap->id, bytes);
+
+               ap->pio_task_state = PIO_ST_ERR;
+               return;
+       }
+
        sg = &qc->sg[qc->cursg];
 
        page = sg->page;
author
#2 lore raw
Quoted 8 lines
Quoted 4 lines · Bartlomiej Zolnierkiewicz

Problem:
Some bad behaved CD-ROM drives will return more data than ask to.
(I have such CD-RW drive and it crashed the kernel.)

Quoted 6 lines

These devices are compliant with original ATAPI spec.

Such condition shouldn't be treated as an error
- extra data should be read and dumped.

Hi Bart,

For read, we can read and discard the extra data from the device.
For write, the device is asking for more data than we have.
Should we supply some dummy data to the device? Or we just stop and return it as error?

Albert

#3 lore raw
Quoted 16 lines · Albert Lee
Quoted 10 lines
Quoted 4 lines · Bartlomiej Zolnierkiewicz

Problem:
Some bad behaved CD-ROM drives will return more data than ask to.
(I have such CD-RW drive and it crashed the kernel.)

Quoted 6 lines

These devices are compliant with original ATAPI spec.

Such condition shouldn't be treated as an error
- extra data should be read and dumped.

Quoted 5 lines

Hi Bart,

For read, we can read and discard the extra data from the device.
For write, the device is asking for more data than we have.
Should we supply some dummy data to the device? Or we just stop and return it as error?

For write, we need to pad. For DMA, we need to add additional zeroes, until the DMA transfer ends on a dword boundary. For PIO, we must at least pad to a word boundary.

Jeff

author
#4 lore raw
Quoted 19 lines · Jeff Garzik
Quoted 17 lines · Albert Lee
Quoted 9 lines
Quoted 4 lines · Bartlomiej Zolnierkiewicz

Problem:
Some bad behaved CD-ROM drives will return more data than ask to.
(I have such CD-RW drive and it crashed the kernel.)

Quoted 6 lines

These devices are compliant with original ATAPI spec.

Such condition shouldn't be treated as an error
- extra data should be read and dumped.

Hi Bart,

Quoted 9 lines
Quoted 3 lines

For read, we can read and discard the extra data from the device.
For write, the device is asking for more data than we have.
Should we supply some dummy data to the device? Or we just stop and return it as error?

For write, we need to pad. For DMA, we need to add additional zeroes, until the DMA transfer ends on a dword boundary. For PIO, we must at least pad to a word boundary.

Jeff

Attached please find the revised patch for your review.

Albert

---------------------------------------

drivers/scsi/libata-core.c +17 -0
@@ -2338,6 +2338,23 @@
 		ap->pio_task_state = PIO_ST_LAST;
 
 next_sg:
+	/* check whether qc->sg is full */
+	if (unlikely(qc->cursg >= qc->n_elem)) {
+		unsigned char pad_buf[2];
+		unsigned int words = (bytes+1) >> 1; /* pad to word boundary */
+		unsigned int i;
+
+		DPRINTK("ata%u: padding %u bytes\n", ap->id, bytes);
+
+		memset(&pad_buf, 0, sizeof(pad_buf));
+		for (i = 0; i < words; i++) {
+			ata_data_xfer(ap, pad_buf, sizeof(pad_buf), do_write);
+		}
+
+		ap->pio_task_state = PIO_ST_LAST;
+		return;
+	}
+
 	sg = &qc->sg[qc->cursg];
 
 	page = sg->page;
drivers/scsi/libata-core.c +17 -0
@@ -2338,6 +2338,23 @@
 		ap->pio_task_state = PIO_ST_LAST;
 
 next_sg:
+	/* check whether qc->sg is full */
+	if (unlikely(qc->cursg >= qc->n_elem)) {
+		unsigned char pad_buf[2];
+		unsigned int words = (bytes+1) >> 1; /* pad to word boundary */
+		unsigned int i;
+
+		DPRINTK("ata%u: padding %u bytes\n", ap->id, bytes);
+
+		memset(&pad_buf, 0, sizeof(pad_buf));
+		for (i = 0; i < words; i++) {
+			ata_data_xfer(ap, pad_buf, sizeof(pad_buf), do_write);
+		}
+
+		ap->pio_task_state = PIO_ST_LAST;
+		return;
+	}
+
 	sg = &qc->sg[qc->cursg];
 
 	page = sg->page;