Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Security reinforcement only authentication(TWB) #338

Merged
merged 1 commit into from
Nov 23, 2024
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
52 changes: 34 additions & 18 deletions logViewerService/logviewerservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ LogViewerService::~LogViewerService()
*/
QString LogViewerService::readLog(const QDBusUnixFileDescriptor &fd)
{
m_actionId = s_Action_View;

if(!isValidInvoker(true)) {
if(!checkAuth(s_Action_View)) {
return " ";
}

Expand Down Expand Up @@ -174,9 +172,7 @@ static QByteArray processCmdWithArgs(const QString &cmdStr, const QStringList &a
*/
QString LogViewerService::readLog(const QString &filePath)
{
m_actionId = s_Action_View;

if(!isValidInvoker(true)) {
if(!checkAuth(s_Action_View)) {
return " ";
}

Expand Down Expand Up @@ -291,9 +287,7 @@ bool LogViewerService::checkAuthorization(const QString &actionId, qint64 applic
*/
QStringList LogViewerService::readLogLinesInRange(const QDBusUnixFileDescriptor &fd, qint64 startLine, qint64 lineCount, bool bReverse)
{
m_actionId = s_Action_View;

if(!isValidInvoker(true)) {
if(!checkAuth(s_Action_View)) {
return QStringList();
}

Expand Down Expand Up @@ -327,10 +321,8 @@ QStringList LogViewerService::readLogLinesInRange(const QString &filePath, qint6
{
QStringList lines;

m_actionId = s_Action_View;

// 开启鉴权
if (!isValidInvoker(true))
if (!checkAuth(s_Action_View))
return lines;

//增加服务黑名单,只允许通过提权接口读取/var/log下,家目录下和临时目录下的文件
Expand Down Expand Up @@ -601,9 +593,7 @@ QString LogViewerService::readLogInStream(const QString &token)

QString LogViewerService::isFileExist(const QString &filePath)
{
m_actionId = s_Action_View;

if (!isValidInvoker(true))
if (!checkAuth(s_Action_View))
return QString("");

if (QFile::exists(filePath))
Expand Down Expand Up @@ -914,9 +904,7 @@ static bool processExportLog(const QString &cmdStr, const QString &outFullPath,c

bool LogViewerService::exportLog(const QString &outDir, const QString &in, bool isFile)
{
m_actionId = s_Action_Export;

if(!isValidInvoker(true)) { //非法调用
if(!checkAuth(s_Action_Export)) { //非法调用
return false;
}

Expand Down Expand Up @@ -1140,3 +1128,31 @@ bool LogViewerService::isValidInvoker(bool checkAuth/* = true*/)
}
return true;
}

bool LogViewerService::checkAuth(const QString &actionId)
{
if (!calledFromDBus()) {
qWarning() << "called not from dbus.";
return false;
}

bool isRoot = connection().interface()->serviceUid(message().service()).value() == 0;
if (isRoot) {
qInfo() << "dbus caller is root progress.";
return true;
}

uint pid = connection().interface()->servicePid(message().service()).value();

bool bAuthVaild = false;
bAuthVaild = checkAuthorization(actionId, pid);
if (!bAuthVaild) {
qWarning() << "checkAuthorization failed.";
sendErrorReply(QDBusError::ErrorType::Failed,
QString("(pid: %1) is not allowed to configrate firewall. %3")
.arg(pid)
.arg("checkAuthorization failed."));
}

return bAuthVaild;
}
1 change: 1 addition & 0 deletions logViewerService/logviewerservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public Q_SLOTS:
* @return
*/
bool isValidInvoker(bool checkAuth = false);
bool checkAuth(const QString &actionId);
QByteArray processCatFile(const QString &filePath);
void processCmdArgs(const QString &cmdStr, const QStringList &args);
};
Expand Down
Loading