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

imagewriter: Force permissions on settings to 600 #772

Open
wants to merge 2 commits into
base: qml
Choose a base branch
from
Open
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
29 changes: 7 additions & 22 deletions src/imagewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,15 @@ ImageWriter::ImageWriter(QObject *parent)
#ifdef Q_OS_WIN
_taskbarButton = nullptr;
#endif

if (!_settings.isWritable() && !_settings.fileName().isEmpty())
{
/* Settings file is not writable, probably run by root previously */
QString settingsFile = _settings.fileName();
qDebug() << "Settings file" << settingsFile << "not writable. Recreating it";
QFile f(_settings.fileName());
QByteArray oldsettings;
const auto expectedPermissions = (QFileDevice::WriteOwner | QFileDevice::ReadOwner);
auto settingsFile = QFile(_settings.fileName());

if (f.open(f.ReadOnly))
{
oldsettings = f.readAll();
f.close();
}
f.remove();
if (f.open(f.WriteOnly))
{
f.write(oldsettings);
f.close();
_settings.sync();
}
else
{
qDebug() << "Error deleting and recreating settings file. Please remove manually.";
if (settingsFile.exists() && settingsFile.permissions() != expectedPermissions) {
if (!settingsFile.setPermissions(expectedPermissions)) {
qDebug() << "Failed to set expected permissions for settings file. Errant behaviour may follow, including settings failing to persist";
qDebug() << "To prevent future messages of this form, please delete " << _settings.fileName();
}
}
}

Expand Down