From cc6b5770cbed74a85ca77958fa95b2460a14e791 Mon Sep 17 00:00:00 2001 From: liujinchang Date: Thu, 19 Nov 2026 15:47:23 +0800 Subject: [PATCH] fix(tabbar): guard out-of-range index and division by zero in fuzz cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add bounds check in Tabbar::updateTab() before writing m_tabPaths/m_tabTruePaths to fix CWE-787 out-of-bounds write - Guard the division by DTabBar::count() in Tabbar::tabSizeHint() with count() > 0 to fix CWE-369 divide-by-zero - Add bounds check in Tabbar::handleTabIsRemoved() before reading m_tabPaths.at() to fix CWE-125 out-of-bounds read - Validate index in ThemeListModel::data() before accessing m_themes to fix CWE-125 out-of-bounds read - Add fuzz regression tests for all four PMS BUG#383519 findings with valid-index positive controls 修复(tabbar): 修复模糊测试发现的越界索引与除零问题 - Tabbar::updateTab() 写入 m_tabPaths/m_tabTruePaths 前增加越界校验,修复 CWE-787 越界写 - Tabbar::tabSizeHint() 以 count() > 0 保护对 DTabBar::count() 的除法,修复 CWE-369 除零 - Tabbar::handleTabIsRemoved() 读取 m_tabPaths.at() 前增加越界校验,修复 CWE-125 越界读 - ThemeListModel::data() 访问 m_themes 前校验索引有效性,修复 CWE-125 越界读 - 新增覆盖 PMS BUG#383519 全部四个问题的模糊测试回归用例,并含合法索引正向对照 Log: 修复安全专项模糊测试发现的标签栏越界写、越界读与除零问题,并补充回归测试 Bug: https://pms.uniontech.com/bug-view-383519.html PMS: BUG-383519 --- src/controls/tabbar.cpp | 14 ++- src/thememodule/themelistmodel.cpp | 6 +- tests/src/controls/ut_tabbar_fuzz383519.cpp | 104 ++++++++++++++++++++ 3 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 tests/src/controls/ut_tabbar_fuzz383519.cpp diff --git a/src/controls/tabbar.cpp b/src/controls/tabbar.cpp index 51693ac22..6c6b6add0 100644 --- a/src/controls/tabbar.cpp +++ b/src/controls/tabbar.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2017 - 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -245,6 +245,10 @@ void Tabbar::closeRightTabs(const QString &filePath) void Tabbar::updateTab(int index, const QString &filePath, const QString &tabName) { + if (index < 0 || index >= m_tabPaths.count()) { + return; + } + // 适配助记符 '&' 后设置文本 setTabText(index, tabName); m_tabPaths[index] = filePath; @@ -705,7 +709,9 @@ QSize Tabbar::tabSizeHint(int index) const //计算每个tab平均宽度 返回 100到160 int aveargeWidth = 160; - aveargeWidth = total / DTabBar::count(); + if (DTabBar::count() > 0) { + aveargeWidth = total / DTabBar::count(); + } if (aveargeWidth >= 160) { aveargeWidth = 160; @@ -791,6 +797,10 @@ void Tabbar::handleTabReleased(int index) void Tabbar::handleTabIsRemoved(int index) { //qDebug() << "handleTabIsRemoved" << index; + if (index < 0 || index >= m_tabPaths.count()) { + return; + } + const QString filePath = m_tabPaths.at(index); Window *window = static_cast(this->window()); m_tabPaths.removeAt(index); diff --git a/src/thememodule/themelistmodel.cpp b/src/thememodule/themelistmodel.cpp index 5a03e95fd..7ff2c83c3 100644 --- a/src/thememodule/themelistmodel.cpp +++ b/src/thememodule/themelistmodel.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -47,6 +47,10 @@ QVariant ThemeListModel::data(const QModelIndex &index, int role) const { const int r = index.row(); + if (!index.isValid() || r < 0 || r >= m_themes.count()) { + return QVariant(); + } + const QString &name = m_themes.at(r).first; const QString &path = m_themes.at(r).second; diff --git a/tests/src/controls/ut_tabbar_fuzz383519.cpp b/tests/src/controls/ut_tabbar_fuzz383519.cpp new file mode 100644 index 000000000..d370ebd8d --- /dev/null +++ b/tests/src/controls/ut_tabbar_fuzz383519.cpp @@ -0,0 +1,104 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +// 回归用例:PMS BUG#383519【安全专项】【模糊测试】deepin-editor 存在 4 个安全问题。 +// 与安全团队 fuzz 用例一一对应: +// fuzz_tabbar.cpp/tabbar_update_tab_fuzz() -> Tabbar::updateTab() 越界写 (CWE-787) +// fuzz_themelistmodel.cpp/themelistmodel_data_fuzz() -> ThemeListModel::data() 越界读 (CWE-125) +// fuzz_tabbar.cpp/tabbar_tab_size_hint_fuzz() -> Tabbar::tabSizeHint() 除零 (CWE-369) +// fuzz_tabbar.cpp/tabbar_handle_tab_is_removed_fuzz() -> Tabbar::handleTabIsRemoved() 越界读 (CWE-125) + +#include +#include "../src/controls/tabbar.h" +#include "../src/thememodule/themelistmodel.h" +#include "../src/widgets/window.h" +#include "src/stub.h" + +namespace fuzz383519 { + +void removeWrapper_stub() +{ +} + +} + +using namespace fuzz383519; + +// 用例1:updateTab() 对越界 index 直接写 m_tabPaths[index]/m_tabTruePaths[index](CWE-787 越界写) +TEST(UT_Tabbar_Fuzz383519, updateTab_outOfRange) +{ + Tabbar *tab = new Tabbar; + + // 空 tabbar 上传越界 index:负值、超出尾部、超大值 + tab->updateTab(-1, "/tmp/fuzz383519.txt", "fuzz"); + tab->updateTab(3, "/tmp/fuzz383519.txt", "fuzz"); + tab->updateTab(100000, "/tmp/fuzz383519.txt", "fuzz"); + EXPECT_EQ(0, tab->count()); + EXPECT_TRUE(tab->m_tabPaths.isEmpty()); + EXPECT_TRUE(tab->m_tabTruePaths.isEmpty()); + + // 正向对照:合法 index 行为不变 + tab->addTab("/tmp/fuzz383519.txt", "fuzz"); + tab->updateTab(0, "/tmp/fuzz383519_other.txt", "fuzz2"); + EXPECT_EQ(1, tab->count()); + EXPECT_EQ(QString("/tmp/fuzz383519_other.txt"), tab->m_tabPaths.value(0)); + + tab->deleteLater(); +} + +// 用例2:data() 对越界 row 直接 m_themes.at(r)(CWE-125 越界读) +TEST(UT_Tabbar_Fuzz383519, themelistmodelData_outOfRange) +{ + ThemeListModel model; + const int rows = model.rowCount(QModelIndex()); + + // 无效 QModelIndex(row == -1) + EXPECT_FALSE(model.data(QModelIndex(), ThemeListModel::ThemeName).isValid()); + // row 越过尾部:QAbstractListModel::index() 对越界行返回无效索引 + EXPECT_FALSE(model.data(model.index(rows, 0), ThemeListModel::ThemeName).isValid()); + EXPECT_FALSE(model.data(model.index(rows + 3, 0), ThemeListModel::ThemePath).isValid()); + EXPECT_FALSE(model.data(model.index(rows + 100000, 0), ThemeListModel::ThemeName).isValid()); + + // 正向对照:合法行返回主题名(环境未安装主题文件时跳过) + if (rows > 0) { + EXPECT_TRUE(model.data(model.index(0, 0), ThemeListModel::ThemeName).isValid()); + } +} + +// 用例3:tabSizeHint() 在 count()==0 时 total / count() 整数除零(CWE-369) +TEST(UT_Tabbar_Fuzz383519, tabSizeHint_divideByZero) +{ + Tabbar *tab = new Tabbar; + + const QSize size = tab->tabSizeHint(0); + EXPECT_GE(size.width(), 110); + EXPECT_LE(size.width(), 160); + + tab->deleteLater(); +} + +// 用例4:handleTabIsRemoved() 对越界 index 直接 m_tabPaths.at(index)(CWE-125 越界读) +TEST(UT_Tabbar_Fuzz383519, handleTabIsRemoved_outOfRange) +{ + // 打桩隔离 Window::removeWrapper 依赖,聚焦越界防护 + Stub stub; + stub.set(ADDR(Window, removeWrapper), removeWrapper_stub); + + Tabbar *tab = new Tabbar; + + // 空 tabbar 上移除:index 0 与越界值 + tab->handleTabIsRemoved(0); + tab->handleTabIsRemoved(3); + EXPECT_EQ(0, tab->count()); + EXPECT_TRUE(tab->m_tabPaths.isEmpty()); + + // 正向对照:走真实移除信号路径(removeTab -> tabIsRemoved -> handleTabIsRemoved) + tab->addTab("/tmp/fuzz383519.txt", "fuzz"); + EXPECT_EQ(1, tab->count()); + tab->removeTab(0); + EXPECT_EQ(0, tab->count()); + EXPECT_TRUE(tab->m_tabPaths.isEmpty()); + + tab->deleteLater(); +}