Skip to contentSkip to conversation

ossaudio: fix out of bounds write

PATCH In review Volker Rümelin 4 participants 5 messages Jul 7, 2020 – Jul 9, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ossaudio: fix out of bounds write

In function oss_read() a read error currently does not exit the read loop. With no data to read the variable pos will quickly underflow and a subsequent successful read overwrites memory outside the buffer. This patch adds the missing break statement to the error path of the function.

To reproduce start qemu with -audiodev oss,id=audio0 and in the guest start audio recording. After some time this will trigger an exception.

Fixes 3ba4066d08 "ossaudio: port to the new audio backend api"
---
 audio/ossaudio.c | 1 +
 1 file changed, 1 insertion(+)
audio/ossaudio.c +1 -0
index f88d076ec2..a7dcaa31ad 100644
@@ -691,6 +691,7 @@ static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len)
                            len, dst);
                 break;
             }
+            break;
         }
 
         pos += nread;
Signature · 1 line
2.26.2
#1 lore raw
Quoted 5 lines · Volker Rümelin

In function oss_read() a read error currently does not exit the read loop. With no data to read the variable pos will quickly underflow and a subsequent successful read overwrites memory outside the buffer. This patch adds the missing break statement to the error path of the function.

Correct, but ...

Quoted 17 lines

To reproduce start qemu with -audiodev oss,id=audio0 and in the guest start audio recording. After some time this will trigger an exception.

Fixes 3ba4066d08 "ossaudio: port to the new audio backend api"
---
 audio/ossaudio.c | 1 +
 1 file changed, 1 insertion(+)
audio/ossaudio.c +0 -0
index f88d076ec2..a7dcaa31ad 100644

audio/ossaudio.c static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len) · line 697

Quoted 7 lines
audio/ossaudio.c +1 -0
@@ -691,6 +691,7 @@ static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len)
                            len, dst);
                 break;
             }
+            break;
         }
 
         pos += nread;

... now pos += -1, then the size returned misses the last byte.

#2 lore raw
Quoted 30 lines · Volker Rümelin
Quoted 5 lines

In function oss_read() a read error currently does not exit the read loop. With no data to read the variable pos will quickly underflow and a subsequent successful read overwrites memory outside the buffer. This patch adds the missing break statement to the error path of the function.

Correct, but ...

Quoted 22 lines

To reproduce start qemu with -audiodev oss,id=audio0 and in the guest start audio recording. After some time this will trigger an exception.

Fixes 3ba4066d08 "ossaudio: port to the new audio backend api"
---
 audio/ossaudio.c | 1 +
 1 file changed, 1 insertion(+)
audio/ossaudio.c +1 -0
index f88d076ec2..a7dcaa31ad 100644
@@ -691,6 +691,7 @@ static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len)
                            len, dst);
                 break;
             }
+            break;
         }
 
Quoted 3 lines

audio/ossaudio.c static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len) · line 697

Quoted 1 line
audio/ossaudio.c +0 -0
         pos += nread;

... now pos += -1, then the size returned misses the last byte.

Hi Philippe,

no, the added break breaks the while loop. The next executed instruction after this break is the return pos statement not pos += nread.

With best regards,
Volker

#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 21 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Volker Rümelin

In function oss_read() a read error currently does not exit the read loop. With no data to read the variable pos will quickly underflow and a subsequent successful read overwrites memory outside the buffer. This patch adds the missing break statement to the error path of the function.

Correct, but ...

Quoted 12 lines

To reproduce start qemu with -audiodev oss,id=audio0 and in the guest start audio recording. After some time this will trigger an exception.

Fixes 3ba4066d08 "ossaudio: port to the new audio backend api"
---
 audio/ossaudio.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

audio/ossaudio.c static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len) · line 694

Quoted 9 lines
audio/ossaudio.c +1 -0
index f88d076ec2..a7dcaa31ad 100644
@@ -691,6 +691,7 @@ static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len)
                            len, dst);
                 break;
             }
+            break;

Maybe it would be less confusing if you converted the switch(errno) to an if then you wouldn't have two senses of break; in close proximity. I was thinking something like

if (nread == -1) {
    if (errno != EINTR && errno != EAGAIN) {
        logerr();
    }
    break; /* from while, which is now clear */
}
Quoted 5 lines

audio/ossaudio.c static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len) · line 697

Quoted 3 lines
audio/ossaudio.c +0 -0
         }
 
         pos += nread;

... now pos += -1, then the size returned misses the last byte.

Don't you break from loop in if () above this so -1 is never added to pos after this patch? What happens for EINTR and EAGAIN? Now we break from the loop for those too, should it be continue; instead?

Regards,
BALATON Zoltan

#4 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 11 lines
Quoted 11 lines
audio/ossaudio.c +1 -0
index f88d076ec2..a7dcaa31ad 100644
@@ -691,6 +691,7 @@ static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len)
                            len, dst);
                 break;
             }
+            break;
         }
 
Quoted 3 lines

audio/ossaudio.c static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len) · line 697

Quoted 1 line
audio/ossaudio.c +0 -0
         pos += nread;

... now pos += -1, then the size returned misses the last byte.

No, it doesn't. break leaves the while loop, not the if condition. From patch context it isn't obvious though, you need to look at the source code ...

Patch queued.

thanks,
Gerd