Skip to contentSkip to conversation

Fix ATAPI GET_CONFIGURATION function

PATCH In review Alexander Graf 2 participants 10 messages May 21, 2008 – May 30, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix ATAPI GET_CONFIGURATION function

The current implementation of GET_CONFIGURATION in the ATAPI stack of qemu replies a different length depending on the buffer, the data should be written into.

On the other hand the SCSI spec defines that length information has to be consistent and independent of return buffer lengths.

This patch makes the ATAPI emulation behave according to the spec and fixes the Darwin DVD driver.

qemu/hw/ide.c +5 -7
index a2a8299..6e585e1 100644
@@ -1717,6 +1717,7 @@ static void ide_atapi_cmd(IDEState *s)
     case GPCMD_GET_CONFIGURATION:
         {
             uint32_t len;
+            uint8_t index = 0;
 
             /* only feature 0 is supported */
             if (packet[2] != 0 || packet[3] != 0) {
@@ -1753,14 +1754,11 @@ static void ide_atapi_cmd(IDEState *s)
             }
 
             len = 8; /* header completed */
-            if (max_len > len) {
-                uint8_t index = 0;
 
-                buf[10] = 0x02 | 0x01; /* persistent and current */
-                len += 4; /* header */
-                len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
-                len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
-            }
+            buf[10] = 0x02 | 0x01; /* persistent and current */
+            len += 4; /* header */
+            len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
+            len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
             cpu_to_ube32(buf, len - 4); /* data length */
 
             ide_atapi_cmd_reply(s, len, max_len);
#1 lore raw

Is there anything particularly wrong with this patch? Does it break any guest OSs I am not aware of? If not, please include it as it's a pretty obvious bug.

Alex

Quoted 46 lines · Alexander Graf

The current implementation of GET_CONFIGURATION in the ATAPI stack of qemu replies a different length depending on the buffer, the data should be written into.

On the other hand the SCSI spec defines that length information has to be consistent and independent of return buffer lengths.

This patch makes the ATAPI emulation behave according to the spec and fixes the Darwin DVD driver.

qemu/hw/ide.c +5 -7
index a2a8299..6e585e1 100644
@@ -1717,6 +1717,7 @@ static void ide_atapi_cmd(IDEState *s)
    case GPCMD_GET_CONFIGURATION:
        {
            uint32_t len;
+            uint8_t index = 0;
 
            /* only feature 0 is supported */
            if (packet[2] != 0 || packet[3] != 0) {
@@ -1753,14 +1754,11 @@ static void ide_atapi_cmd(IDEState *s)
            }
 
            len = 8; /* header completed */
-            if (max_len > len) {
-                uint8_t index = 0;
 
-                buf[10] = 0x02 | 0x01; /* persistent and current */
-                len += 4; /* header */
-                len += ide_atapi_set_profile(buf, &index,  
 MMC_PROFILE_DVD_ROM);
-                len += ide_atapi_set_profile(buf, &index,  
 MMC_PROFILE_CD_ROM);
-            }
+            buf[10] = 0x02 | 0x01; /* persistent and current */
+            len += 4; /* header */
+            len += ide_atapi_set_profile(buf, &index,  
 MMC_PROFILE_DVD_ROM);
+            len += ide_atapi_set_profile(buf, &index,  
MMC_PROFILE_CD_ROM);
            cpu_to_ube32(buf, len - 4); /* data length */

            ide_atapi_cmd_reply(s, len, max_len);
#2 lore raw

Please ignore this patch. It is valid for KVM only.

Quoted 17 lines · Alexander Graf

Is there anything particularly wrong with this patch? Does it break any guest OSs I am not aware of? If not, please include it as it's a pretty obvious bug.

Alex

Quoted 10 lines · Alexander Graf

The current implementation of GET_CONFIGURATION in the ATAPI stack of qemu replies a different length depending on the buffer, the data should be written into.

On the other hand the SCSI spec defines that length information has to be consistent and independent of return buffer lengths.

This patch makes the ATAPI emulation behave according to the spec and fixes the Darwin DVD driver.

#3 lore raw
In reply to Alexander Graf
Quoted 1 line · Alexander Graf

Is there anything particularly wrong with this patch?

no, but the SPEC (I used MMC6 r01) says :

6.5.1.4 Allocation Length

The Allocation Length field specifies the maximum length in bytes of the Get Configuration response data. An Allocation Length field of zero indicates that no data shall be transferred. This condition shall not be considered an error.

this is only implicitly supported by truncating the response using max_len for the ide_atapi_cmd_reply with your patch and so the work of generating the response is still happening regardless.

Quoted 1 line

Does it break any guest OSs I am not aware of?

the original implementation (*) broke [Open]Solaris by overflowing the buffer that was used in the calls to detect the CDROM/DVDROM (using a 8 byte buffer)

Carlo

(*) http://svn.savannah.gnu.org/viewvc/trunk/hw/ide.c?root=qemu&r1=3147&r2=3161

#4 lore raw
Quoted 11 lines · Carlo Marcelo Arenas Belon
Quoted 1 line · Alexander Graf

Is there anything particularly wrong with this patch?

no, but the SPEC (I used MMC6 r01) says :

6.5.1.4 Allocation Length

The Allocation Length field specifies the maximum length in bytes of the Get Configuration response data. An Allocation Length field of zero indicates that no data shall be transferred. This condition shall not be considered an error.

This means that the Allocation Length field in the request specifies the maximum bytes of data to be returned. This does not affect the "Data Length" field in the response, which indicates the maximum amount of data available.

Quoted 3 lines

this is only implicitly supported by truncating the response using max_len for the ide_atapi_cmd_reply with your patch and so the work of generating the response is still happening regardless.

Isn't io_buffer memcpy()ed by at most max_len in the end to the DMA buffer of the target? Bytes after max_len should not even be written to target OS memory.

Quoted 4 lines
Quoted 1 line

Does it break any guest OSs I am not aware of?

the original implementation (*) broke [Open]Solaris by overflowing the buffer that was used in the calls to detect the CDROM/DVDROM (using a 8 byte buffer)

I don't see how that could break anything. The normal way of receiving a dynamic length packet with SCSI is as follows:

- Send a request with an Allocation Length of 4 (or slightly more) bytes
- Receive the response header, which includes the "Data Length" field. Now the OS knows the full length of the response packet
- Send a request with an Allocation Length of "Data Length" bytes
- Receive the full packet

I am pretty sure Solaris does the same. If anything breaks, it's a bug in the implementation of how buf gets written into the client memory.

Furthermore I tried to issue the same command on a real DVD Drive. There is a handy tool in Linux called "sg_raw". Using that you can just issue raw SCSI commands to your device. I received the following results:

 # sg_raw -vv -r 0x00 /dev/sg1 46 00 00 00 00 00 00 00 00 00
open /dev/sg1 with flags=0x802
SCSI Status: Good

Sense Information:
sense buffer empty

No data received

# sg_raw -vv -r 0x04 /dev/sg1 46 00 00 00 00 00 00 00 04 00
open /dev/sg1 with flags=0x802
SCSI Status: Good

Sense Information:
sense buffer empty

Received 4 bytes of data:
00 00 00 00 88 ....

# sg_raw -vv -r 0x08 /dev/sg1 46 00 00 00 00 00 00 00 08 00
open /dev/sg1 with flags=0x802
SCSI Status: Good

Sense Information:
sense buffer empty

Received 8 bytes of data:
00 00 00 00 88 00 00 00 10 ........

# sg_raw -vv -r 0x88 /dev/sg1 46 00 00 00 00 00 00 00 88 00
open /dev/sg1 with flags=0x802
SCSI Status: Good

Sense Information:
sense buffer empty

Received 136 bytes of data:
00 00 00 00 88 00 00 00 10 00 00 03 08 00 10 01
00 ................
10 00 08 00 00 00 01 03 04 00 00 00 02 00 02 03
04 ................
20 00 00 00 00 00 03 03 04 29 00 00 00 00 10 01
08 ........).......
30 00 00 08 00 00 10 01 00 00 1d 00 00 00 1e 04
04 ................
40 03 00 00 00 00 1f 01 00 00 2a 00 04 00 01 00
00 .........*......
50 00 2b 00 04 00 00 00 00 00 3b 01 04 00 00 00 00 .
+.......;......
60 01 00 03 00 01 03 00 04 03 00 01 00 01 04 03
00 ................
70 01 05 03 00 01 06 04 04 00 00 00 01 01 07 0d
04 ................
80 0c 00 00 00 01 0b 04 04 ........

As you can see the "Data Length" field in the response is constantly the maximum bytes available as response.

Alex

Quoted 7 lines

Carlo

(*) http://svn.savannah.gnu.org/viewvc/trunk/hw/ide.c?root=qemu&r1=3147&r2=3161

#5 lore raw

Alexander

sorry if I wasn't clear enough. I wasn't arguing about your patch (which is correct) but was trying to explain why the old implementation was done the way it was, in an (obviously excessive) effort to prevent a buffer overflow for the response (which used to be hardcoded to 32 bytes regardless of the size of the buffer) as shown by :

http://svn.savannah.gnu.org/viewvc/trunk/hw/ide.c?root=qemu&r1=3147&r2=3161

a slightly modified version of your patch (which I'd been using against kvm and validated correct with Linux and Solaris guests) attached.

the only difference, is that it handles explicitly the empty buffer case and cleans up the len calculation which shouldn't had been calculated conditionally as you pointed out.

Carlo

qemu/hw/ide.c 2008-05-25 01:44:27.000000000 -0700 renamed from qemu/hw/ide.c 2008-05-12 04:30:43.000000000 -0700 +34 -34
@@ -1716,6 +1716,7 @@
     case GPCMD_GET_CONFIGURATION:
         {
             uint32_t len;
+            uint8_t index = 0;
 
             /* only feature 0 is supported */
             if (packet[2] != 0 || packet[3] != 0) {
@@ -1726,41 +1727,40 @@
 
             /* XXX: could result in alignment problems in some architectures */
             max_len = ube16_to_cpu(packet + 7);
-            /*
-             * XXX: avoid overflow for io_buffer if max_len is bigger than the
-             *      size of that buffer (dimensioned to max number of sectors
-             *      to transfer at once)
-             *
-             *      Only a problem if the feature/profiles grow exponentially.
-             */
-            if (max_len > 512) /* XXX: assume 1 sector */
-                max_len = 512;
-
-            memset(buf, 0, max_len);
-            /* 
-             * the number of sectors from the media tells us which profile
-             * to use as current.  0 means there is no media
-             *
-             * XXX: fails to detect correctly DVDs with less data burned
-             *      than what a CD can hold
-             */
-            if ((s -> nb_sectors)) {
-                if ((s -> nb_sectors > CD_MAX_SECTORS))
-                    cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
-                else
-                    cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
-            }
 
-            len = 8; /* header completed */
-            if (max_len > len) {
-                uint8_t index = 0;
-
-                buf[10] = 0x02 | 0x01; /* persistent and current */
-                len += 4; /* header */
-                len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
-                len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
-            }
-            cpu_to_ube32(buf, len - 4); /* data length */
+            if (max_len > 0) {
+                /*
+                 * XXX: avoid overflow for io_buffer if max_len is bigger than
+                 *      the size of that buffer (dimensioned to max number of
+                 *      sectors to transfer at once)
+                 *
+                 *      Only a problem if the feature/profiles grow 
+                 */
+                if (max_len > 512) /* XXX: assume 1 sector */
+                    max_len = 512;
+
+                memset(buf, 0, max_len);
+                /* 
+                 * the number of sectors from the media tells us which profile
+                 * to use as current.  0 means there is no media
+                 *
+                 * XXX: fails to detect correctly DVDs with less data burned
+                 *      than what a CD can hold
+                 */
+                if (s -> nb_sectors) {
+                    if (s -> nb_sectors > CD_MAX_SECTORS)
+                        cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
+                    else
+                        cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
+                }
+
+                buf[10] = 0x02 | 0x01; /* persistent and current */
+                len = 12; /* headers: 8 + 4 */
+                len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
+                len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
+                cpu_to_ube32(buf, len - 4); /* data length */
+            } else
+                len = 0;
 
             ide_atapi_cmd_reply(s, len, max_len);
             break;
author Acked-by
#6 lore raw
Quoted 13 lines · Carlo Marcelo Arenas Belon

Alexander

sorry if I wasn't clear enough. I wasn't arguing about your patch (which is correct) but was trying to explain why the old implementation was done the way it was, in an (obviously excessive) effort to prevent a buffer overflow for the response (which used to be hardcoded to 32 bytes regardless of the size of the buffer) as shown by :

http://svn.savannah.gnu.org/viewvc/trunk/hw/ide.c?root=qemu&r1=3147&r2=3161

a slightly modified version of your patch (which I'd been using against kvm and validated correct with Linux and Solaris guests) attached.

Quoted 6 lines

the only difference, is that it handles explicitly the empty buffer case and cleans up the len calculation which shouldn't had been calculated conditionally as you pointed out.

Carlo
<kvm-69-qemu-ide-dvdrom.patch>

#7 lore raw
In reply to Carlo Marcelo Arenas Belon
Quoted 13 lines · Carlo Marcelo Arenas Belon

Alexander

sorry if I wasn't clear enough. I wasn't arguing about your patch (which is correct) but was trying to explain why the old implementation was done the way it was, in an (obviously excessive) effort to prevent a buffer overflow for the response (which used to be hardcoded to 32 bytes regardless of the size of the buffer) as shown by :

http://svn.savannah.gnu.org/viewvc/trunk/hw/ide.c?root=qemu&r1=3147&r2=3161

a slightly modified version of your patch (which I'd been using against kvm and validated correct with Linux and Solaris guests) attached.

Quoted 6 lines

the only difference, is that it handles explicitly the empty buffer case and cleans up the len calculation which shouldn't had been calculated conditionally as you pointed out.

Carlo
<kvm-69-qemu-ide-dvdrom.patch>

Hum ... it might be a good idea to inline patches so commenting on them is easier.
You don't need to check for max_len > 0, because if max_len == 0 the packet will be truncated back to 0 bytes by ide_atapi_cmd_reply(s, len, max_len).

Alex

#8 lore raw
Quoted 12 lines · Alexander Graf
Quoted 9 lines · Carlo Marcelo Arenas Belon

a slightly modified version of your patch (which I'd been using against kvm
and validated correct with Linux and Solaris guests) attached.

the only difference, is that it handles explicitly the empty buffer case and
cleans up the len calculation which shouldn't had been calculated conditionally as you pointed out.

Quoted 5 lines
Quoted 2 lines

Carlo
<kvm-69-qemu-ide-dvdrom.patch>

Hum ... it might be a good idea to inline patches so commenting on them is easier.

patch was attached inline just like you did with your original one (except it was using MIME type text/plain instead of text/x-patch). inlining it below so it is easier to comment on.

Quoted 3 lines

You don't need to check for max_len > 0, because if max_len == 0 the packet will be truncated back to 0 bytes by ide_atapi_cmd_reply(s, len, max_len).

yes, the check is not needed but as I said was added so that it can explicitly handle those cases for clarity and to save the work of building the response just to be discarded at the end. anyway, removed from the last version which should be also ready for commit.

Carlo

---

ide.c +12 -15
Index: hw/ide.c
===================================================================
@@ -1715,6 +1715,7 @@
     case GPCMD_GET_CONFIGURATION:
         {
             uint32_t len;
+            uint8_t index = 0;
 
             /* only feature 0 is supported */
             if (packet[2] != 0 || packet[3] != 0) {
@@ -1725,12 +1726,13 @@
 
             /* XXX: could result in alignment problems in some architectures */
             max_len = ube16_to_cpu(packet + 7);
+
             /*
-             * XXX: avoid overflow for io_buffer if max_len is bigger than the
-             *      size of that buffer (dimensioned to max number of sectors
-             *      to transfer at once)
+             * XXX: avoid overflow for io_buffer if max_len is bigger than
+             *      the size of that buffer (dimensioned to max number of
+             *      sectors to transfer at once)
              *
-             *      Only a problem if the feature/profiles grow exponentially.
+             *      Only a problem if the feature/profiles grow 
              */
             if (max_len > 512) /* XXX: assume 1 sector */
                 max_len = 512;
@@ -1743,22 +1745,17 @@
              * XXX: fails to detect correctly DVDs with less data burned
              *      than what a CD can hold
              */
-            if ((s -> nb_sectors)) {
-                if ((s -> nb_sectors > CD_MAX_SECTORS))
+            if (s -> nb_sectors) {
+                if (s -> nb_sectors > CD_MAX_SECTORS)
                     cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
                 else
                     cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
             }
 
-            len = 8; /* header completed */
-            if (max_len > len) {
-                uint8_t index = 0;
-
-                buf[10] = 0x02 | 0x01; /* persistent and current */
-                len += 4; /* header */
-                len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
-                len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
-            }
+            buf[10] = 0x02 | 0x01; /* persistent and current */
+            len = 12; /* headers: 8 + 4 */
+            len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
+            len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
             cpu_to_ube32(buf, len - 4); /* data length */
 
             ide_atapi_cmd_reply(s, len, max_len);
#9 lore raw
Quoted 101 lines · Carlo Marcelo Arenas Belon
Quoted 12 lines · Alexander Graf
Quoted 9 lines · Carlo Marcelo Arenas Belon

a slightly modified version of your patch (which I'd been using against kvm
and validated correct with Linux and Solaris guests) attached.

the only difference, is that it handles explicitly the empty buffer case and
cleans up the len calculation which shouldn't had been calculated conditionally as you pointed out.

Quoted 5 lines
Quoted 2 lines

Carlo
<kvm-69-qemu-ide-dvdrom.patch>

Hum ... it might be a good idea to inline patches so commenting on them is easier.

patch was attached inline just like you did with your original one (except it was using MIME type text/plain instead of text/x-patch). inlining it below so it is easier to comment on.

Quoted 3 lines

You don't need to check for max_len > 0, because if max_len == 0 the packet will be truncated back to 0 bytes by ide_atapi_cmd_reply(s, len, max_len).

yes, the check is not needed but as I said was added so that it can explicitly handle those cases for clarity and to save the work of building the response just to be discarded at the end. anyway, removed from the last version which should be also ready for commit.

Carlo

---
Index: hw/ide.c
===================================================================

ide.c +1 -0
@@ -1715,6 +1715,7 @@
    case GPCMD_GET_CONFIGURATION:
        {
            uint32_t len;
+            uint8_t index = 0;
 
            /* only feature 0 is supported */
            if (packet[2] != 0 || packet[3] != 0) {
@@ -1725,12 +1726,13 @@
 
            /* XXX: could result in alignment problems in some  

architectures */

            max_len = ube16_to_cpu(packet + 7);
ide.c +1 -1
+
            /*
-             * XXX: avoid overflow for io_buffer if max_len is  

bigger than the
- * size of that buffer (dimensioned to max number
of sectors

ide.c +1 -1
-             *      to transfer at once)
+             * XXX: avoid overflow for io_buffer if max_len is  

bigger than
+ * the size of that buffer (dimensioned to max
number of

ide.c +1 -1
+             *      sectors to transfer at once)
             *
-             *      Only a problem if the feature/profiles grow  

exponentially.

ide.c +1 -0
+             *      Only a problem if the feature/profiles grow
             */
            if (max_len > 512) /* XXX: assume 1 sector */
                max_len = 512;
@@ -1743,22 +1745,17 @@
             * XXX: fails to detect correctly DVDs with less data  

burned
* than what a CD can hold
*/

ide.c +2 -9
-            if ((s -> nb_sectors)) {
-                if ((s -> nb_sectors > CD_MAX_SECTORS))
+            if (s -> nb_sectors) {
+                if (s -> nb_sectors > CD_MAX_SECTORS)
                    cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
                else
                    cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
            }
 
-            len = 8; /* header completed */
-            if (max_len > len) {
-                uint8_t index = 0;
-
-                buf[10] = 0x02 | 0x01; /* persistent and current */
-                len += 4; /* header */
-                len += ide_atapi_set_profile(buf, &index,  

MMC_PROFILE_DVD_ROM);
- len += ide_atapi_set_profile(buf, &index,
MMC_PROFILE_CD_ROM);

ide.c +3 -1
-            }
+            buf[10] = 0x02 | 0x01; /* persistent and current */
+            len = 12; /* headers: 8 + 4 */
+            len += ide_atapi_set_profile(buf, &index,  

MMC_PROFILE_DVD_ROM);
+ len += ide_atapi_set_profile(buf, &index,

Quoted 6 lines
MMC_PROFILE_CD_ROM);
            cpu_to_ube32(buf, len - 4); /* data length */

            ide_atapi_cmd_reply(s, len, max_len);

Looks fine to me. Please commit this patch or comment on it if you think it should not be included. As Paul is the one in the Maintainers file listed as SCSI maintainer, I would also love to hear a comment on this from him.

Thank you,

Alex