Skip to content

Commit b65cfaa

Browse files
pppanghu77deepin-bot[bot]
authored andcommitted
fix(security): use QProcess instead of system() to prevent command injection
- Replace system() calls with QProcess in ioctlOperateNetworkLogicalName() - Use QStringList for parameterized command execution - Split complex rfkill shell pipeline into separate QProcess calls - Add QRegularExpression header for output parsing - Prevents potential command injection via malicious network interface names Log: fix(security): use QProcess instead of system() to prevent command injection Task: https://pms.uniontech.com/task-view-386839.html
1 parent 2f02873 commit b65cfaa

1 file changed

Lines changed: 46 additions & 9 deletions

File tree

  • deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol

deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QFile>
1111
#include <QProcess>
1212
#include <QCryptographicHash>
13+
#include <QRegularExpression>
1314
#include <QDebug>
1415

1516
#include <net/if.h>
@@ -140,17 +141,53 @@ void EnableUtils::disableInDevice()
140141
bool EnableUtils::ioctlOperateNetworkLogicalName(const QString &logicalName, bool enable)
141142
{
142143
if (logicalName.startsWith("wlan") || logicalName.startsWith("wlp")) { // Wireless LAN
143-
QString cmd = QString("rfkill %1 $(rfkill list | grep -A 2 \"phy$(iw dev %2 info 2>/dev/null | awk '/wiphy/{print $2}')\" | awk 'NR==1{print $1}' | tr -d ':')")
144-
.arg(enable ? "unblock" : "block")
145-
.arg(logicalName);
146-
int ret = system(cmd.toStdString().c_str());
147-
if (ret != 0) {
148-
qCritical() << "Failed to block/unblock wifi: " << " error code: " << ret ;
144+
// 第一步:获取 wiphy 编号
145+
QProcess iwProcess;
146+
iwProcess.start("iw", QStringList() << "dev" << logicalName << "info");
147+
iwProcess.waitForFinished();
148+
QString iwOutput = QString::fromUtf8(iwProcess.readAllStandardOutput());
149+
150+
// 解析 wiphy 编号
151+
QRegularExpression wiphyRe("wiphy\\s+(\\d+)");
152+
QRegularExpressionMatch wiphyMatch = wiphyRe.match(iwOutput);
153+
if (!wiphyMatch.hasMatch()) {
154+
qCritical() << "Failed to get wiphy number for interface: " << logicalName;
155+
return false;
149156
}
150-
cmd = QString("/sbin/ifconfig %1 %2").arg(logicalName).arg(enable ? "up" : "down");
151-
ret = system(cmd.toStdString().c_str());
157+
QString phyNum = wiphyMatch.captured(1);
158+
159+
// 第二步:获取 rfkill 设备编号
160+
QProcess rfkillListProcess;
161+
rfkillListProcess.start("rfkill", QStringList() << "list");
162+
rfkillListProcess.waitForFinished();
163+
QString rfkillOutput = QString::fromUtf8(rfkillListProcess.readAllStandardOutput());
164+
165+
// 查找对应的 rfkill 编号
166+
QRegularExpression rfkillRe("^(\\d+):.*\\n.*\\n.*phy" + phyNum);
167+
QRegularExpressionMatch rfkillMatch = rfkillRe.match(rfkillOutput);
168+
QString rfkillId;
169+
if (rfkillMatch.hasMatch()) {
170+
rfkillId = rfkillMatch.captured(1);
171+
}
172+
173+
// 第三步:执行 rfkill block/unblock
174+
if (!rfkillId.isEmpty()) {
175+
QProcess rfkillBlockProcess;
176+
rfkillBlockProcess.start("rfkill", QStringList() << (enable ? "unblock" : "block") << rfkillId);
177+
rfkillBlockProcess.waitForFinished();
178+
int ret = rfkillBlockProcess.exitCode();
179+
if (ret != 0) {
180+
qCritical() << "Failed to block/unblock wifi: error code: " << ret;
181+
}
182+
}
183+
184+
// 第四步:执行 ifconfig up/down
185+
QProcess ifconfigProcess;
186+
ifconfigProcess.start("/sbin/ifconfig", QStringList() << logicalName << (enable ? "up" : "down"));
187+
ifconfigProcess.waitForFinished();
188+
int ret = ifconfigProcess.exitCode();
152189
if (ret != 0) {
153-
qCritical() << "Failed to up/down network: " << logicalName << enable << " error code: " << ret ;
190+
qCritical() << "Failed to up/down network: " << logicalName << enable << " error code: " << ret;
154191
return false;
155192
}
156193
} else {

0 commit comments

Comments
 (0)