Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions deepin-devicemanager/src/DeviceManager/DeviceStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,41 @@ using namespace DDLog;
#define DISK_SCALE_1024 1024
#define DISK_SCALE_1000 1000

// MANFID: 十六进制字符串 -> 厂商名
static const QMap<QString, QString> MANFID_TABLE = {
{"000001", "Panasonic"},
{"000002", "Toshiba"},
{"000003", "SanDisk"},
{"00001b", "Samsung"},
{"00001d", "ADATA"},
{"000027", "Phison"},
{"000028", "Lexar"},
{"000031", "Silicon Power"},
{"000041", "Kingston"},
{"000074", "Transcend Information"},
{"000076", "Patriot Memory"},
{"000082", "Sony"},
{"00009c", "Angelbird / Hoodman"}
};

// OEMID: 十六进制字符串 -> 厂商名
static const QMap<QString, QString> OEMID_TABLE = {
{"3432", "Kingston"},
{"4144", "ADATA"},
{"4245", "Lexar / Angelbird / Hoodman"},
{"4a45", "Transcend Information"},
{"4a54", "Sony"},
{"4a60", "Transcend Information"},
{"5041", "Panasonic"},
{"5048", "Phison"},
{"5054", "SanDisk"},
{"5344", "SanDisk"},
{"534d", "Samsung"},
{"534f", "Angelbird / Hoodman"},
{"5350", "Silicon Power"},
{"544d", "Toshiba"}
};

DeviceStorage::DeviceStorage()
: DeviceBaseInfo()
, m_Model("")
Expand Down Expand Up @@ -268,6 +303,35 @@ bool DeviceStorage::setHwinfoInfo(const QMap<QString, QString> &mapInfo)
setAttribute(mapInfo, "VID_PID", m_VID_PID);
m_PhysID = m_VID_PID;

if (m_Driver.contains("mmcblk")) {
QString sysfsDeviceLink = mapInfo.value("SysFS Device Link");

if (!sysfsDeviceLink.isEmpty())
{
if (!sysfsDeviceLink.startsWith("/sys/"))
{
sysfsDeviceLink = "/sys" + sysfsDeviceLink;
}

QString err;
QString name = Common::safeReadSysFsFile(sysfsDeviceLink, "name", err);
if (err.isEmpty())
m_Name = name;

QString manfIdRaw = Common::safeReadSysFsFile(sysfsDeviceLink, "manfid", err);
if (err.isEmpty()) {
m_Vendor = getManfName(manfIdRaw);
}

QString oemIdRaw = Common::safeReadSysFsFile(sysfsDeviceLink, "oemid", err);
if (err.isEmpty()) {
if (m_Vendor.isEmpty()) {
m_Vendor = getOemName(oemIdRaw);
}
}
}
}

getOtherMapInfo(mapInfo);
return true;
}
Expand Down Expand Up @@ -787,3 +851,19 @@ void DeviceStorage::getInfoFromsmartctl(const QMap<QString, QString> &mapInfo)
if (m_Name.startsWith("RS") && m_Vendor.isEmpty())
m_Vendor = "Longsys";
}

// 通过manfid获取厂商名称
QString DeviceStorage::getManfName(const QString &rawManfId)
{
QString cleanId = rawManfId.trimmed().toLower();
cleanId.remove("0x"); // 去除0x前缀
Comment on lines +856 to +859

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Removing "0x" this way will also strip any '0' and 'x' characters inside the ID

cleanId.remove("0x") removes all occurrences of that substring, not just a leading prefix, so IDs containing 0x in the middle will be corrupted (e.g. "0x10x1b""11b"), breaking lookups. Restrict this to a leading prefix only, e.g.:

QString cleanId = rawManfId.trimmed().toLower();
if (cleanId.startsWith("0x")) {
    cleanId.remove(0, 2);
}

Apply the same fix to getOemName.

return MANFID_TABLE.value(cleanId, rawManfId);
}

// 通过oemid获取厂商名称
QString DeviceStorage::getOemName(const QString &rawOemId)
{
QString cleanId = rawOemId.trimmed().toLower();
cleanId.remove("0x");
return OEMID_TABLE.value(cleanId, rawOemId);
}
2 changes: 2 additions & 0 deletions deepin-devicemanager/src/DeviceManager/DeviceStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class DeviceStorage: public DeviceBaseInfo

const QString &mediaType() const;

QString getManfName(const QString &rawManfId);
QString getOemName(const QString &rawOemId);
protected:

/**
Expand Down
56 changes: 56 additions & 0 deletions deepin-devicemanager/src/commonfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

// 其它头文件
#include <QString>
#include <QMap>

Check warning on line 15 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMap> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QMap> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QProcess>

Check warning on line 16 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 16 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFile>

Check warning on line 17 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 17 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 18 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 18 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDir>

Check warning on line 19 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 19 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QLoggingCategory>

Check warning on line 20 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 20 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QRegularExpression>

Check warning on line 21 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 21 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <sys/utsname.h>
#include <cmath>
Expand Down Expand Up @@ -394,3 +396,57 @@
// 4. Return original string if no known suffix matches
return speed;
}

/**
* 安全读取sysfs下指定文件内容
* @param baseDir 根目录 /sys/xxx/mmc0:0001
* @param fileName 待读取的文件名
* @return 文件原始字符串(去换行空格)
*/
QString Common::safeReadSysFsFile(const QString &baseDir, const QString &fileName, QString &errOut)
{
errOut.clear();

if (!baseDir.startsWith("/sys/") && baseDir != "/sys") {
errOut = QString("baseDir not under /sys: %1").arg(baseDir);
return "";
}
if (baseDir.contains("..")) {
errOut = QString("Path traversal detected in baseDir: %1").arg(baseDir);
return "";
}

if (fileName.contains('/') || fileName.contains("..") || fileName.contains('\\')) {
errOut = QString("Path traversal detected in fileName: %1").arg(fileName);
return "";
}

QFileInfo fullPathInfo(QDir(baseDir), fileName);
QString fullPath = fullPathInfo.canonicalFilePath();

if (!fullPath.startsWith("/sys/"))
{
errOut = QString("Path escape to non-sys dir: %1").arg(fullPath);
return "";
}

if (!fullPathInfo.isFile() || fullPathInfo.isSymLink())
{
errOut = QString("Not a regular file or symlink: %1").arg(fullPath);
Comment on lines +433 to +435

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Logic and message around regular file vs symlink are inconsistent and may reject valid sysfs paths

The condition !fullPathInfo.isFile() || fullPathInfo.isSymLink() actually rejects symlinks, while the message claims regular files or symlinks are allowed, and sysfs commonly exposes attributes via symlinks.

To allow both, invert the logic:

if (!fullPathInfo.isFile() && !fullPathInfo.isSymLink()) {
    errOut = QString("Not a regular file or symlink: %1").arg(fullPath);
    return "";
}

If symlinks are meant to be disallowed, adjust the error message to match that behavior instead.

return "";
}

QFile file(fullPath);
if (!file.open(QIODevice::ReadOnly))
{
errOut = QString("Open failed: %1, err=%2").arg(fullPath, file.errorString());
return "";
}

const qint64 MAX_READ_LEN = 64;
QByteArray rawData = file.read(MAX_READ_LEN);
file.close();

QString content = QString::fromUtf8(rawData).trimmed();
return content;
}
9 changes: 9 additions & 0 deletions deepin-devicemanager/src/commonfunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,14 @@ class Common
* Note: Case-sensitive - "Gbit/s" -> "Mbps", "Mbit/s" -> "Mbps"
*/
static QString formatNetworkSpeed(const QString& speed);

/**
* @brief safeReadSysFsFile: 安全读取 sysfs 下指定文件内容
* @param baseDir 根目录,如 /sys/devices/xxx/mmc0:0001
* @param fileName 待读取的文件名(禁止包含路径分隔符)
* @param errOut 错误输出参数,成功时为空
* @return 文件内容(去换行空格),失败返回空字符串
*/
static QString safeReadSysFsFile(const QString &baseDir, const QString &fileName, QString &errOut);
};
#endif // COMMONFUNCTION_H
Loading