Skip to content

Commit c043b22

Browse files
authored
fix: check strides and plane sizes of custom I420 buffers before copying them (#454)
A Java I420Buffer whose stride is below its width or whose planes are too small made the native copy read past them. Reject such buffers, and sizes WebRTC cannot allocate, with a RuntimeException.
1 parent 2572481 commit c043b22

2 files changed

Lines changed: 197 additions & 0 deletions

File tree

‎webrtc-jni/src/main/cpp/src/api/VideoFrame.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,33 @@
2424
#include "api/video/i420_buffer.h"
2525
#include "rtc_base/time_utils.h"
2626

27+
#include <climits>
28+
#include <cstdint>
29+
2730
namespace jni
2831
{
32+
namespace
33+
{
34+
// Throws unless the plane holds every byte a copy reads: the last row ends at the width.
35+
void CheckPlane(JNIEnv * env, const char * name, const JavaRef<jobject> & data, jint stride, jint width, jint height)
36+
{
37+
if (stride < width) {
38+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
39+
JavaRuntimeException(env, "Stride %d of the %s plane is less than its width %d", stride, name, width)));
40+
}
41+
42+
// Neither factor exceeds an int, so the product cannot overflow.
43+
const int64_t required = static_cast<int64_t>(stride) * (height - 1) + width;
44+
const jlong capacity = env->GetDirectBufferCapacity(data);
45+
46+
if (capacity < required) {
47+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
48+
JavaRuntimeException(env, "The %s plane holds %lld bytes, but a %dx%d plane with stride %d needs %lld",
49+
name, static_cast<long long>(capacity), width, height, stride, static_cast<long long>(required))));
50+
}
51+
}
52+
}
53+
2954
namespace VideoFrame
3055
{
3156
webrtc::VideoFrame toNative(JNIEnv * env, const JavaRef<jobject> & javaFrame)
@@ -158,6 +183,19 @@ namespace jni
158183
return nullptr;
159184
}
160185

186+
const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
187+
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
188+
189+
// WebRTC aborts the process on a copy larger than it can address with an int.
190+
if (static_cast<int64_t>(width) * height + 2 * static_cast<int64_t>(chromaWidth) * chromaHeight > INT_MAX) {
191+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
192+
JavaRuntimeException(env, "Buffer size %dx%d is too large", width, height)));
193+
}
194+
195+
CheckPlane(env, "Y", dataY, strideY, width, height);
196+
CheckPlane(env, "U", dataU, strideU, chromaWidth, chromaHeight);
197+
CheckPlane(env, "V", dataV, strideV, chromaWidth, chromaHeight);
198+
161199
return webrtc::I420Buffer::Copy(width, height, y, strideY, u, strideU, v, strideV);
162200
}
163201
}

‎webrtc/src/test/java/dev/onvoid/webrtc/media/video/VideoProcessorTests.java‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,66 @@ void staleSinkDropsFrames() {
9898
}
9999
}
100100

101+
@Test
102+
void acceptsCustomBufferLayouts() {
103+
VideoDesktopSource source = new VideoDesktopSource();
104+
TestProcessor processor = new TestProcessor();
105+
106+
source.setVideoProcessor(processor);
107+
108+
try {
109+
// Tightly packed, odd-sized, and padded rows whose last row ends at the width.
110+
processor.sink.onVideoFrame(new VideoFrame(new LaidOutI420Buffer(64, 48), 0));
111+
processor.sink.onVideoFrame(new VideoFrame(new LaidOutI420Buffer(5, 3), 0));
112+
processor.sink.onVideoFrame(new VideoFrame(new LaidOutI420Buffer(64, 48,
113+
80, 80 * 47 + 64, 40, 40 * 23 + 32, 48, 48 * 23 + 32), 0));
114+
}
115+
finally {
116+
source.dispose();
117+
}
118+
}
119+
120+
@Test
121+
void rejectsCustomBufferOverreads() {
122+
VideoDesktopSource source = new VideoDesktopSource();
123+
TestProcessor processor = new TestProcessor();
124+
125+
source.setVideoProcessor(processor);
126+
127+
int w = 64;
128+
int h = 48;
129+
int cw = 32;
130+
int ch = 24;
131+
132+
// Each would make the copy read past the end of a plane.
133+
LaidOutI420Buffer[] buffers = {
134+
new LaidOutI420Buffer(w, h, w - 1, w * h, cw, cw * ch, cw, cw * ch),
135+
new LaidOutI420Buffer(w, h, -w, w * h, cw, cw * ch, cw, cw * ch),
136+
new LaidOutI420Buffer(w, h, w, w * h, cw - 1, cw * ch, cw, cw * ch),
137+
new LaidOutI420Buffer(w, h, w, w * h, cw, cw * ch, 0, cw * ch),
138+
new LaidOutI420Buffer(w, h, w, w * h - 1, cw, cw * ch, cw, cw * ch),
139+
new LaidOutI420Buffer(w, h, w * 2, w * h, cw, cw * ch, cw, cw * ch),
140+
new LaidOutI420Buffer(w, h, w, w * h, cw, cw * ch - 1, cw, cw * ch),
141+
new LaidOutI420Buffer(w, h, w, w * h, cw, cw * ch, cw, cw * (ch - 1)),
142+
new LaidOutI420Buffer(w, h, w, w * h, Integer.MAX_VALUE, cw * ch, cw, cw * ch),
143+
// Larger than WebRTC can allocate, with planes that claim to fit.
144+
new LaidOutI420Buffer(46341, 46341, 46341, 16, 23171, 16, 23171, 16),
145+
new LaidOutI420Buffer(Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 16, Integer.MAX_VALUE, 16, Integer.MAX_VALUE, 16),
146+
};
147+
148+
try {
149+
for (LaidOutI420Buffer buffer : buffers) {
150+
VideoFrame frame = new VideoFrame(buffer, 0);
151+
152+
assertThrows(RuntimeException.class, () -> processor.sink.onVideoFrame(frame),
153+
() -> buffer.toString());
154+
}
155+
}
156+
finally {
157+
source.dispose();
158+
}
159+
}
160+
101161
@Test
102162
void processedDesktopFramesReachTrack() throws Exception {
103163
DesktopSource screen = firstScreen();
@@ -250,6 +310,105 @@ private static void fill(ByteBuffer buffer, byte value) {
250310

251311

252312

313+
/**
314+
* An I420 buffer in Java with the strides and plane sizes it is given.
315+
*/
316+
private static class LaidOutI420Buffer implements I420Buffer {
317+
318+
private final int width;
319+
private final int height;
320+
private final int strideY;
321+
private final int strideU;
322+
private final int strideV;
323+
private final ByteBuffer dataY;
324+
private final ByteBuffer dataU;
325+
private final ByteBuffer dataV;
326+
327+
328+
LaidOutI420Buffer(int width, int height) {
329+
this(width, height, width, width * height, (width + 1) / 2,
330+
((width + 1) / 2) * ((height + 1) / 2), (width + 1) / 2,
331+
((width + 1) / 2) * ((height + 1) / 2));
332+
}
333+
334+
LaidOutI420Buffer(int width, int height, int strideY, int sizeY, int strideU, int sizeU,
335+
int strideV, int sizeV) {
336+
this.width = width;
337+
this.height = height;
338+
this.strideY = strideY;
339+
this.strideU = strideU;
340+
this.strideV = strideV;
341+
this.dataY = ByteBuffer.allocateDirect(sizeY);
342+
this.dataU = ByteBuffer.allocateDirect(sizeU);
343+
this.dataV = ByteBuffer.allocateDirect(sizeV);
344+
}
345+
346+
@Override
347+
public ByteBuffer getDataY() {
348+
return dataY.slice();
349+
}
350+
351+
@Override
352+
public ByteBuffer getDataU() {
353+
return dataU.slice();
354+
}
355+
356+
@Override
357+
public ByteBuffer getDataV() {
358+
return dataV.slice();
359+
}
360+
361+
@Override
362+
public int getStrideY() {
363+
return strideY;
364+
}
365+
366+
@Override
367+
public int getStrideU() {
368+
return strideU;
369+
}
370+
371+
@Override
372+
public int getStrideV() {
373+
return strideV;
374+
}
375+
376+
@Override
377+
public int getWidth() {
378+
return width;
379+
}
380+
381+
@Override
382+
public int getHeight() {
383+
return height;
384+
}
385+
386+
@Override
387+
public I420Buffer toI420() {
388+
return this;
389+
}
390+
391+
@Override
392+
public VideoFrameBuffer cropAndScale(int cropX, int cropY, int cropWidth,
393+
int cropHeight, int scaleWidth, int scaleHeight) {
394+
throw new UnsupportedOperationException();
395+
}
396+
397+
@Override
398+
public void retain() {
399+
}
400+
401+
@Override
402+
public void release() {
403+
}
404+
405+
@Override
406+
public String toString() {
407+
return String.format("%dx%d, Y %d/%d, U %d/%d, V %d/%d", width, height,
408+
strideY, dataY.capacity(), strideU, dataU.capacity(), strideV, dataV.capacity());
409+
}
410+
}
411+
253412
private static class TestProcessor implements VideoProcessor {
254413

255414
VideoTrackSink sink;

0 commit comments

Comments
 (0)