The OpenY4mFile function reads the file header without a size limit, which could lead to an infinite loop or OOM crash if the file does not contain a newline character. Additionally, file handles were not being closed on several error paths, leading to resource leaks. This CL: 1. Adds a check to abort reading if the header exceeds 64 bytes. 2. Fixes multiple file handle leaks where fclose() was missing. Bug: None Test: tools_unittests --gtest_filter="*OpenY4mFileAbortsOnLargeHeader*" Change-Id: I0d4ce793713fc420915bcda16c1fd754c04eb797 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/448082 Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org> Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Cr-Commit-Position: refs/heads/main@{#46939}
147 lines
4.0 KiB
C++
147 lines
4.0 KiB
C++
/*
|
|
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "rtc_tools/video_file_reader.h"
|
|
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
#include "api/scoped_refptr.h"
|
|
#include "api/video/video_frame_buffer.h"
|
|
#include "test/gtest.h"
|
|
#include "test/testsupport/file_utils.h"
|
|
|
|
namespace webrtc {
|
|
namespace test {
|
|
|
|
class Y4mFileReaderTest : public ::testing::Test {
|
|
public:
|
|
void SetUp() override {
|
|
const std::string filename =
|
|
TempFilename(test::OutputPath(), "test_video_file.y4m");
|
|
|
|
// Create simple test video of size 6x4.
|
|
FILE* file = fopen(filename.c_str(), "wb");
|
|
ASSERT_TRUE(file != nullptr);
|
|
fprintf(file, "YUV4MPEG2 W6 H4 F57:10 C420 dummyParam\n");
|
|
fprintf(file, "FRAME\n");
|
|
|
|
const int width = 6;
|
|
const int height = 4;
|
|
const int i40_size = width * height * 3 / 2;
|
|
// First frame.
|
|
for (int i = 0; i < i40_size; ++i)
|
|
fputc(static_cast<char>(i), file);
|
|
fprintf(file, "FRAME\n");
|
|
// Second frame.
|
|
for (int i = 0; i < i40_size; ++i)
|
|
fputc(static_cast<char>(i + i40_size), file);
|
|
fclose(file);
|
|
|
|
// Open the newly created file.
|
|
video = test::OpenY4mFile(filename);
|
|
ASSERT_TRUE(video);
|
|
}
|
|
|
|
scoped_refptr<test::Video> video;
|
|
};
|
|
|
|
TEST_F(Y4mFileReaderTest, TestParsingFileHeader) {
|
|
EXPECT_EQ(6, video->width());
|
|
EXPECT_EQ(4, video->height());
|
|
}
|
|
|
|
TEST_F(Y4mFileReaderTest, TestParsingNumberOfFrames) {
|
|
EXPECT_EQ(2u, video->number_of_frames());
|
|
}
|
|
|
|
TEST_F(Y4mFileReaderTest, TestPixelContent) {
|
|
int cnt = 0;
|
|
for (const scoped_refptr<I420BufferInterface> frame : *video) {
|
|
for (int i = 0; i < 6 * 4; ++i, ++cnt)
|
|
EXPECT_EQ(cnt, frame->DataY()[i]);
|
|
for (int i = 0; i < 3 * 2; ++i, ++cnt)
|
|
EXPECT_EQ(cnt, frame->DataU()[i]);
|
|
for (int i = 0; i < 3 * 2; ++i, ++cnt)
|
|
EXPECT_EQ(cnt, frame->DataV()[i]);
|
|
}
|
|
}
|
|
|
|
class YuvFileReaderTest : public ::testing::Test {
|
|
public:
|
|
void SetUp() override {
|
|
const std::string filename =
|
|
TempFilename(test::OutputPath(), "test_video_file.yuv");
|
|
|
|
// Create simple test video of size 6x4.
|
|
FILE* file = fopen(filename.c_str(), "wb");
|
|
ASSERT_TRUE(file != nullptr);
|
|
|
|
const int width = 6;
|
|
const int height = 4;
|
|
const int i40_size = width * height * 3 / 2;
|
|
// First frame.
|
|
for (int i = 0; i < i40_size; ++i)
|
|
fputc(static_cast<char>(i), file);
|
|
// Second frame.
|
|
for (int i = 0; i < i40_size; ++i)
|
|
fputc(static_cast<char>(i + i40_size), file);
|
|
fclose(file);
|
|
|
|
// Open the newly created file.
|
|
video = test::OpenYuvFile(filename, 6, 4);
|
|
ASSERT_TRUE(video);
|
|
}
|
|
|
|
scoped_refptr<test::Video> video;
|
|
};
|
|
|
|
TEST_F(YuvFileReaderTest, TestParsingFileHeader) {
|
|
EXPECT_EQ(6, video->width());
|
|
EXPECT_EQ(4, video->height());
|
|
}
|
|
|
|
TEST_F(YuvFileReaderTest, TestParsingNumberOfFrames) {
|
|
EXPECT_EQ(2u, video->number_of_frames());
|
|
}
|
|
|
|
TEST_F(YuvFileReaderTest, TestPixelContent) {
|
|
int cnt = 0;
|
|
for (const scoped_refptr<I420BufferInterface> frame : *video) {
|
|
for (int i = 0; i < 6 * 4; ++i, ++cnt)
|
|
EXPECT_EQ(cnt, frame->DataY()[i]);
|
|
for (int i = 0; i < 3 * 2; ++i, ++cnt)
|
|
EXPECT_EQ(cnt, frame->DataU()[i]);
|
|
for (int i = 0; i < 3 * 2; ++i, ++cnt)
|
|
EXPECT_EQ(cnt, frame->DataV()[i]);
|
|
}
|
|
}
|
|
|
|
TEST(VideoFileReaderTest, OpenY4mFileAbortsOnLargeHeader) {
|
|
const std::string file_name =
|
|
TempFilename(test::OutputPath(), "temp_large_header.y4m");
|
|
|
|
FILE* file = fopen(file_name.c_str(), "wb");
|
|
ASSERT_TRUE(file != nullptr);
|
|
|
|
fprintf(file, "YUV4MPEG2 ");
|
|
|
|
// Write 2000 'A's without a newline (Simulate the attack)
|
|
for (int i = 0; i < 2000; ++i) {
|
|
fputc('A', file);
|
|
}
|
|
fclose(file);
|
|
EXPECT_EQ(nullptr, OpenY4mFile(file_name));
|
|
|
|
remove(file_name.c_str());
|
|
}
|
|
} // namespace test
|
|
} // namespace webrtc
|