Skip to content

Commit ab5b545

Browse files
committed
refactor: improve credential transmission security
The changes refactor the credential transmission logic for disk encryption/decryption operations to use a common helper function. Previously each operation (re-encryption and decryption) had duplicate pipe creation and credential serialization code. Now this logic is centralized in sendCredentialsViaFd() which handles: 1. Pipe creation for secure credentials transfer 2. Credentials serialization using QDataStream 3. FD creation and cleanup 4. Synchronous/asynchronous D-Bus calls Benefits include: 1. Reduced code duplication 2. More consistent error handling 3. Improved log messages with method context 4. Proper pipe cleanup in all cases 5. Better separation of concerns Log: Improved security message when transmitting encryption credentials Influence: 1. Verify disk encryption still works with valid credentials 2. Test with invalid credentials to check error handling 3. Verify decryption works for encrypted devices 4. Check logs for credential transmission errors 5. Test cancellation during credential transmission refactor: 改进凭据传输安全性 该提交重构了磁盘加密/解密操作的凭据传输逻辑,改用通用辅助函数。之前每个 操作(重新加密和解密)都有重复的管道创建和凭据序列化代码。现在这些逻辑集 中在sendCredentialsViaFd()中,处理: 1. 用于安全凭据传输的管道创建 2. 使用QDataStream进行凭据序列化 3. 文件描述符创建和清理 4. 同步/异步D-Bus调用 优点包括: 1. 减少代码重复 2. 更一致的错误处理 3. 改进的带方法上下文的日志消息 4. 所有情况下正确的管道清理 5. 更好的关注点分离 Log: 改进传输加密凭据时的安全消息显示 Influence: 1. 验证使用有效凭据时磁盘加密仍能工作 2. 使用无效凭据测试错误处理 3. 验证对加密设备的解密功能 4. 检查凭据传输错误的日志 5. 测试凭据传输期间的取消操作
1 parent a828a0f commit ab5b545

File tree

2 files changed

+84
-89
lines changed

2 files changed

+84
-89
lines changed

src/plugins/filemanager/dfmplugin-disk-encrypt-entry/menu/diskencryptmenuscene.cpp

Lines changed: 79 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -398,51 +398,24 @@ void DiskEncryptMenuScene::doReencryptDevice(const DeviceEncryptParam &param)
398398
kDaemonBusPath,
399399
kDaemonBusIface,
400400
QDBusConnection::systemBus());
401-
if (iface.isValid()) {
402-
// Create anonymous pipe for secure credential transmission
403-
int pipefd[2];
404-
if (pipe(pipefd) == -1) {
405-
fmCritical() << "Failed to create anonymous pipe for credentials";
406-
return;
407-
}
408-
409-
// Prepare credentials data using QDataStream for reliable serialization
410-
QByteArray credentials;
411-
QDataStream stream(&credentials, QIODevice::WriteOnly);
412-
QVariantMap params {
413-
{ encrypt_param_keys::kKeyDevice, param.devDesc },
414-
{ encrypt_param_keys::kKeyPassphrase, toBase64(param.key) },
415-
{ encrypt_param_keys::kKeyExportToPath, param.exportPath },
416-
};
417-
if (!tpmToken.isEmpty()) params.insert(encrypt_param_keys::kKeyTPMToken, tpmToken);
418-
stream << params;
419-
420-
// Write credentials to pipe and close write end immediately
421-
ssize_t written = write(pipefd[1], credentials.constData(), credentials.size());
422-
close(pipefd[1]); // Close write end immediately after writing
423-
424-
if (written != credentials.size()) {
425-
fmCritical() << "Failed to write credentials to pipe, written:" << written << "expected:" << credentials.size();
426-
close(pipefd[0]);
427-
return;
428-
}
401+
if (!iface.isValid()) {
402+
fmCritical() << "Failed to create D-Bus interface for re-encryption";
403+
return;
404+
}
429405

430-
// Create file descriptor for D-Bus transmission
431-
QDBusUnixFileDescriptor fd(pipefd[0]);
432-
if (!fd.isValid()) {
433-
fmCritical() << "Failed to create valid file descriptor from pipe";
434-
close(pipefd[0]);
435-
return;
436-
}
406+
// Prepare credentials data
407+
QVariantMap params {
408+
{ encrypt_param_keys::kKeyDevice, param.devDesc },
409+
{ encrypt_param_keys::kKeyPassphrase, toBase64(param.key) },
410+
{ encrypt_param_keys::kKeyExportToPath, param.exportPath },
411+
};
412+
if (!tpmToken.isEmpty())
413+
params.insert(encrypt_param_keys::kKeyTPMToken, tpmToken);
437414

438-
fmDebug() << "Starting device re-encryption via fd";
439-
iface.asyncCall("SetupAuthArgs", QVariant::fromValue(fd));
415+
// Send credentials via fd
416+
fmDebug() << "Starting device re-encryption via fd";
417+
if (sendCredentialsViaFd(iface, "SetupAuthArgs", params, true)) {
440418
QApplication::setOverrideCursor(Qt::WaitCursor);
441-
442-
// Close read end (D-Bus service will have its own copy)
443-
close(pipefd[0]);
444-
} else {
445-
fmCritical() << "Failed to create D-Bus interface for re-encryption";
446419
}
447420
}
448421

@@ -452,57 +425,26 @@ void DiskEncryptMenuScene::doDecryptDevice(const DeviceEncryptParam &param)
452425
kDaemonBusPath,
453426
kDaemonBusIface,
454427
QDBusConnection::systemBus());
455-
if (iface.isValid()) {
456-
// Create anonymous pipe for secure credential transmission
457-
int pipefd[2];
458-
if (pipe(pipefd) == -1) {
459-
fmCritical() << "Failed to create anonymous pipe for credentials";
460-
return;
461-
}
462-
463-
// Prepare credentials data using QDataStream for reliable serialization
464-
QByteArray credentials;
465-
QDataStream stream(&credentials, QIODevice::WriteOnly);
466-
QVariantMap params {
467-
{ encrypt_param_keys::kKeyJobType, param.jobType },
468-
{ encrypt_param_keys::kKeyDevice, param.devDesc },
469-
{ encrypt_param_keys::kKeyDeviceName, param.deviceDisplayName },
470-
{ encrypt_param_keys::kKeyPassphrase, toBase64(param.key) }
471-
};
472-
stream << params;
473-
474-
// Write credentials to pipe and close write end immediately
475-
ssize_t written = write(pipefd[1], credentials.constData(), credentials.size());
476-
close(pipefd[1]); // Close write end immediately after writing
477-
478-
if (written != credentials.size()) {
479-
fmCritical() << "Failed to write credentials to pipe, written:" << written << "expected:" << credentials.size();
480-
close(pipefd[0]);
481-
return;
482-
}
483-
484-
// Create file descriptor for D-Bus transmission
485-
QDBusUnixFileDescriptor fd(pipefd[0]);
486-
if (!fd.isValid()) {
487-
fmCritical() << "Failed to create valid file descriptor from pipe";
488-
close(pipefd[0]);
489-
return;
490-
}
491-
492-
fmDebug() << "Calling Decryption D-Bus method via fd";
493-
QDBusReply<bool> ret = iface.call("Decryption", QVariant::fromValue(fd));
494-
if (ret.value()) {
495-
QApplication::setOverrideCursor(Qt::WaitCursor);
496-
} else {
497-
fmCritical() << "Decryption failed to start";
498-
}
428+
if (!iface.isValid()) {
429+
fmCritical() << "Failed to create D-Bus interface for decryption";
430+
return;
431+
}
499432

500-
// Close read end (D-Bus service will have its own copy)
501-
close(pipefd[0]);
433+
// Prepare credentials data
434+
QVariantMap params {
435+
{ encrypt_param_keys::kKeyJobType, param.jobType },
436+
{ encrypt_param_keys::kKeyDevice, param.devDesc },
437+
{ encrypt_param_keys::kKeyDeviceName, param.deviceDisplayName },
438+
{ encrypt_param_keys::kKeyPassphrase, toBase64(param.key) }
439+
};
502440

441+
// Send credentials via fd
442+
fmDebug() << "Calling Decryption D-Bus method via fd";
443+
if (sendCredentialsViaFd(iface, "Decryption", params, false)) {
444+
QApplication::setOverrideCursor(Qt::WaitCursor);
503445
EventsHandler::instance()->autoStartDFM();
504446
} else {
505-
fmCritical() << "Failed to create D-Bus interface for decryption";
447+
fmCritical() << "Decryption failed to start";
506448
}
507449
}
508450

@@ -658,6 +600,54 @@ QString DiskEncryptMenuScene::getBase64Of(const QString &fileName)
658600
return QString(contents.toBase64());
659601
}
660602

603+
bool DiskEncryptMenuScene::sendCredentialsViaFd(QDBusInterface &iface, const QString &method,
604+
const QVariantMap &params, bool asyncCall)
605+
{
606+
// Create anonymous pipe for secure credential transmission
607+
int pipefd[2];
608+
if (pipe(pipefd) == -1) {
609+
fmCritical() << "[sendCredentialsViaFd] Failed to create anonymous pipe for credentials";
610+
return false;
611+
}
612+
613+
// Prepare credentials data using QDataStream for reliable serialization
614+
QByteArray credentials;
615+
QDataStream stream(&credentials, QIODevice::WriteOnly);
616+
stream << params;
617+
618+
// Write credentials to pipe and close write end immediately
619+
ssize_t written = write(pipefd[1], credentials.constData(), credentials.size());
620+
close(pipefd[1]); // Close write end immediately after writing
621+
622+
if (written != credentials.size()) {
623+
fmCritical() << "[sendCredentialsViaFd] Failed to write credentials to pipe, written:" << written << "expected:" << credentials.size();
624+
close(pipefd[0]);
625+
return false;
626+
}
627+
628+
// Create file descriptor for D-Bus transmission
629+
QDBusUnixFileDescriptor fd(pipefd[0]);
630+
if (!fd.isValid()) {
631+
fmCritical() << "[sendCredentialsViaFd] Failed to create valid file descriptor from pipe";
632+
close(pipefd[0]);
633+
return false;
634+
}
635+
636+
// Call D-Bus method with file descriptor
637+
fmDebug() << "[sendCredentialsViaFd] Calling D-Bus method:" << method << "via fd";
638+
if (asyncCall) {
639+
iface.asyncCall(method, QVariant::fromValue(fd));
640+
} else {
641+
QDBusReply<bool> reply = iface.call(method, QVariant::fromValue(fd));
642+
close(pipefd[0]);
643+
return reply.value();
644+
}
645+
646+
// Close read end (D-Bus service will have its own copy)
647+
close(pipefd[0]);
648+
return true;
649+
}
650+
661651
void DiskEncryptMenuScene::onUnlocked(bool ok, dfmmount::OperationErrorInfo info, QString clearDev)
662652
{
663653
QApplication::restoreOverrideCursor();

src/plugins/filemanager/dfmplugin-disk-encrypt-entry/menu/diskencryptmenuscene.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <dfm-mount/dmount.h>
1414

1515
#include <QUrl>
16+
#include <QDBusInterface>
1617

1718
class QAction;
1819

@@ -60,6 +61,10 @@ class DiskEncryptMenuScene : public dfmbase::AbstractMenuScene
6061
static QString generateTPMToken(const QString &device, bool pin);
6162
static QString getBase64Of(const QString &fileName);
6263

64+
// Send credentials via file descriptor for secure D-Bus transmission
65+
static bool sendCredentialsViaFd(QDBusInterface &iface, const QString &method,
66+
const QVariantMap &params, bool asyncCall = false);
67+
6368
static void onUnlocked(bool ok, dfmmount::OperationErrorInfo, QString);
6469
static void onMounted(bool ok, dfmmount::OperationErrorInfo, QString);
6570

0 commit comments

Comments
 (0)