moveToTrash/Unix: avoid mkdir/chmod race condition for the trash dir

QDir::mkdir() followed by QFile::setPermissions() is a race condition
because an attacker could enter the directory before we set the
permissions. QDir::mkdir() got an overload with the permissions in 6.3,
but I decided to go a level lower and use QFileSystemEngine directly
here.

Pick-to: 6.5 6.6
Change-Id: I9d43e5b91eb142d6945cfffd1786c338e21c129e
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Thiago Macieira 2023-09-20 17:42:38 -07:00
parent 772ad60425
commit a71f556830

View File

@ -1193,8 +1193,10 @@ static QString freeDesktopTrashLocation(const QString &sourcePath)
| QFileDevice::ExeOwner;
QString targetDir = topDir.filePath(trashDir);
// deliberately not using mkpath, since we want to fail if topDir doesn't exist
if (topDir.mkdir(trashDir))
QFile::setPermissions(targetDir, ownerPerms);
bool created = QFileSystemEngine::createDirectory(QFileSystemEntry(targetDir), false, ownerPerms);
if (created)
return targetDir;
// maybe it already exists and is a directory
if (QFileInfo(targetDir).isDir())
return targetDir;
return QString();