Restrict CreateFile method to WriteFstab method in polkit helper.

This commit is contained in:
Andrius Štikonas 2022-02-20 19:42:25 +00:00
parent 27b85117c4
commit e483bab0d5
6 changed files with 9 additions and 13 deletions

View File

@ -295,7 +295,7 @@ static void writeEntry(QTextStream& s, const FstabEntry& entry, std::array<unsig
<< entry.comment() << "\n"; << entry.comment() << "\n";
} }
bool writeMountpoints(const FstabEntryList& fstabEntries, const QString& filename) bool writeMountpoints(const FstabEntryList& fstabEntries)
{ {
QString fstabContents; QString fstabContents;
QTextStream out(&fstabContents); QTextStream out(&fstabContents);
@ -306,5 +306,5 @@ bool writeMountpoints(const FstabEntryList& fstabEntries, const QString& filenam
writeEntry(out, e, columnWidth); writeEntry(out, e, columnWidth);
ExternalCommand cmd; ExternalCommand cmd;
return cmd.createFile(fstabContents.toLocal8Bit(), filename); return cmd.writeFstab(fstabContents.toLocal8Bit());
} }

View File

@ -116,6 +116,6 @@ QString unescapeSpaces(const QString& mountPoint);
LIBKPMCORE_EXPORT FstabEntryList readFstabEntries(const QString& fstabPath = QStringLiteral("/etc/fstab")); LIBKPMCORE_EXPORT FstabEntryList readFstabEntries(const QString& fstabPath = QStringLiteral("/etc/fstab"));
LIBKPMCORE_EXPORT QStringList possibleMountPoints(const QString& deviceNode, const QString& fstabPath = QStringLiteral("/etc/fstab")); LIBKPMCORE_EXPORT QStringList possibleMountPoints(const QString& deviceNode, const QString& fstabPath = QStringLiteral("/etc/fstab"));
LIBKPMCORE_EXPORT bool writeMountpoints(const FstabEntryList& fstabEntries, const QString& filename = QStringLiteral("/etc/fstab")); LIBKPMCORE_EXPORT bool writeMountpoints(const FstabEntryList& fstabEntries);
#endif #endif

View File

@ -231,13 +231,13 @@ bool ExternalCommand::writeData(Report& commandReport, const QByteArray& buffer,
return waitForDbusReply(pcall); return waitForDbusReply(pcall);
} }
bool ExternalCommand::createFile(const QByteArray& fileContents, const QString& filePath) bool ExternalCommand::writeFstab(const QByteArray& fileContents)
{ {
auto interface = helperInterface(); auto interface = helperInterface();
if (!interface) if (!interface)
return false; return false;
QDBusPendingCall pcall = interface->CreateFile(filePath, fileContents); QDBusPendingCall pcall = interface->WriteFstab(fileContents);
return waitForDbusReply(pcall); return waitForDbusReply(pcall);
} }

View File

@ -57,7 +57,7 @@ public:
bool copyBlocks(const CopySource& source, CopyTarget& target); bool copyBlocks(const CopySource& source, CopyTarget& target);
QByteArray readData(const CopySourceDevice& source); QByteArray readData(const CopySourceDevice& source);
bool writeData(Report& commandReport, const QByteArray& buffer, const QString& deviceNode, const quint64 firstByte); // same as copyBlocks but from QByteArray bool writeData(Report& commandReport, const QByteArray& buffer, const QString& deviceNode, const quint64 firstByte); // same as copyBlocks but from QByteArray
bool createFile(const QByteArray& filePath, const QString& fileContents); // similar to writeData but creates a new file bool writeFstab(const QByteArray& fileContents);
/**< @param cmd the command to run */ /**< @param cmd the command to run */
void setCommand(const QString& cmd); void setCommand(const QString& cmd);

View File

@ -126,19 +126,15 @@ bool ExternalCommandHelper::writeData(QFile& device, const QByteArray& buffer, c
} }
/** Creates a new file with given contents. /** Creates a new file with given contents.
@param filePath file to write to
@param fileContents the data that we write @param fileContents the data that we write
@return true on success @return true on success
*/ */
bool ExternalCommandHelper::CreateFile(const QString &filePath, const QByteArray& fileContents) bool ExternalCommandHelper::WriteFstab(const QByteArray& fileContents)
{ {
if (!isCallerAuthorized()) { if (!isCallerAuthorized()) {
return false; return false;
} }
// Do not allow using this helper for writing to arbitrary location QString filePath = QStringLiteral("/etc/fstab");
if ( filePath != QStringLiteral("/etc/fstab") )
return false;
QFile device(filePath); QFile device(filePath);
auto flags = QIODevice::WriteOnly | QIODevice::Unbuffered; auto flags = QIODevice::WriteOnly | QIODevice::Unbuffered;

View File

@ -43,7 +43,7 @@ public Q_SLOTS:
const QString& targetDevice, const qint64 targetOffset, const qint64 blockSize); const QString& targetDevice, const qint64 targetOffset, const qint64 blockSize);
Q_SCRIPTABLE QByteArray ReadData(const QString& device, const qint64 offset, const qint64 length); Q_SCRIPTABLE QByteArray ReadData(const QString& device, const qint64 offset, const qint64 length);
Q_SCRIPTABLE bool WriteData(const QByteArray& buffer, const QString& targetDevice, const qint64 targetOffset); Q_SCRIPTABLE bool WriteData(const QByteArray& buffer, const QString& targetDevice, const qint64 targetOffset);
Q_SCRIPTABLE bool CreateFile(const QString& filePath, const QByteArray& fileContents); Q_SCRIPTABLE bool WriteFstab(const QByteArray& fileContents);
private: private:
bool isCallerAuthorized(); bool isCallerAuthorized();