Skip to content

Commit 29089d3

Browse files
committed
refactor: secure password transmission by QDBusUnixFileDescriptor
Changed from using base64-encoded password strings to using QDBusUnixFileDescriptor for transmitting passwords during network mount operations Log: Enhanced security of password transmission in network mounting Influence: 1. Test network mount with password to ensure successful authentication 2. Verify anonymous mount still works without password 3. Validate password saving functionality when savePasswd is enabled 4. Test mount failure scenarios (wrong password, network error) to ensure error handling 5. Ensure compatibility with existing D-Bus service (MountControl) that expects file descriptor feat: 在网络挂载中使用文件描述符安全传输密码 将密码传输从 base64 编码字符串改为使用 QDBusUnixFileDescriptor Log: 改进了网络挂载中密码传输的安全性 Task: https://pms.uniontech.com/task-view-389921.html Influence: 1. 测试带密码的网络挂载,确保认证成功 2. 验证匿名挂载在无密码时仍能正常工作 3. 验证启用 savePasswd 时的密码保存功能 4. 测试挂载失败场景(错误密码、网络错误)以确保错误处理正常 5. 确保与现有 D-Bus 服务(MountControl)的兼容性,该服务期望接收文件描述符
1 parent 66ea1a8 commit 29089d3

1 file changed

Lines changed: 46 additions & 12 deletions

File tree

src/dfm-mount/private/dnetworkmounter.cpp

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <QDebug>
1414
#include <QTimer>
1515
#include <QtConcurrent>
16+
#include <QDBusUnixFileDescriptor>
1617

1718
#include <libmount.h>
1819

@@ -148,9 +149,7 @@ QList<QVariantMap> DNetworkMounter::loginPasswd(const QString &address)
148149
if (err)
149150
qDebug() << "query password failed: " << passwd << err->message;
150151
else {
151-
// since daemon accept base64-ed passwd to mount cifs, cleartext should be encoded with base64
152-
// see commit of dde-file-manager: 3b50664d4034754b15c1a516cfaab8c7fbdd3db9
153-
passwd.insert(kLoginPasswd, QString(QByteArray(pwd).toBase64()));
152+
passwd.insert(kLoginPasswd, pwd);
154153
}
155154
}
156155
return passwds;
@@ -471,12 +470,52 @@ void DNetworkMounter::mountByGvfsCallback(GObject *srcObj, GAsyncResult *res, gp
471470
delete finalize;
472471
}
473472

473+
static QVariant preparePasswd(const QString &passwd)
474+
{
475+
if (passwd.isEmpty()) {
476+
qDebug() << "Created empty QVariant for empty passwd";
477+
return QVariant("");
478+
}
479+
480+
// Prepare passwd
481+
const QByteArray passwdBytes = passwd.toLocal8Bit();
482+
483+
// Create pipe
484+
int pipefds[2];
485+
if (pipe(pipefds) == -1) {
486+
qCritical() << "Failed to create pipe:" << strerror(errno);
487+
return QVariant("");
488+
}
489+
490+
// pipefds[0] is for reading
491+
// pipefds[1] is for writing
492+
int read_fd = pipefds[0];
493+
int write_fd = pipefds[1];
494+
495+
// Write passwd to pipe
496+
qint64 bytesWritten = write(write_fd, passwdBytes.constData(), passwdBytes.size());
497+
close(write_fd);
498+
if (bytesWritten != passwdBytes.size()) {
499+
qCritical() << "Failed to write passwd to pipe.";
500+
close(read_fd);
501+
return QVariant("");
502+
}
503+
504+
// Create file descriptor wrapper
505+
QDBusUnixFileDescriptor dbusFd(read_fd);
506+
// read_fd has been copied to QDBusUnixFileDescriptor
507+
close(read_fd);
508+
509+
qDebug() << "Successfully created fd for passwd transmission";
510+
return QVariant::fromValue(dbusFd);
511+
}
512+
474513
DNetworkMounter::MountRet DNetworkMounter::mountWithUserInput(const QString &address,
475514
const MountPassInfo info)
476515
{
477516
QVariantMap param { { kLoginUser, info.userName },
478517
{ kLoginDomain, info.domain },
479-
{ kLoginPasswd, info.passwd },
518+
{ kLoginPasswd, preparePasswd(info.passwd) },
480519
{ kLoginTimeout, info.timeout },
481520
{ kMountFsType, "cifs" } };
482521

@@ -495,13 +534,8 @@ DNetworkMounter::MountRet DNetworkMounter::mountWithUserInput(const QString &add
495534
if (ok) {
496535
err = DeviceError::kNoError;
497536

498-
if (!info.anonymous && info.savePasswd != NetworkMountPasswdSaveMode::kNeverSavePasswd) {
499-
// since passwd from user input is base64-ed data, so the passwd should be decoded into cleartext for saving.
500-
// associated commit of dde-file-manager: 3b50664d4034754b15c1a516cfaab8c7fbdd3db9
501-
auto _info = info;
502-
_info.passwd = QByteArray::fromBase64(info.passwd.toLocal8Bit());
503-
savePasswd(address, _info);
504-
}
537+
if (!info.anonymous && info.savePasswd != NetworkMountPasswdSaveMode::kNeverSavePasswd)
538+
savePasswd(address, info);
505539
}
506540

507541
return { ok, err, mpt };
@@ -517,7 +551,7 @@ DNetworkMounter::MountRet DNetworkMounter::mountWithSavedInfos(const QString &ad
517551
for (const auto &login : infos) {
518552
QVariantMap param { { kLoginUser, login.value(kSchemaUser, "") },
519553
{ kLoginDomain, login.value(kSchemaDomain, "") },
520-
{ kLoginPasswd, login.value(kLoginPasswd, "") },
554+
{ kLoginPasswd, preparePasswd(login.value(kLoginPasswd, "").toString()) },
521555
{ kLoginTimeout, secs },
522556
{ kMountFsType, "cifs" } };
523557

0 commit comments

Comments
 (0)