Skip to content

Commit

Permalink
Check access to /proc/PID/root for FileChooser portal usage outside s…
Browse files Browse the repository at this point in the history
…andbox
  • Loading branch information
grulja committed Oct 19, 2020
1 parent d87c71b commit d5a01ea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
46 changes: 46 additions & 0 deletions common/gnomehintssettings.cpp
Expand Up @@ -31,10 +31,19 @@
#include <QStyleFactory>
#include <QSettings>
#include <QStandardPaths>
#include <QTimer>

#include <QDBusArgument>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

Q_LOGGING_CATEGORY(QGnomePlatform, "qt.qpa.qgnomeplatform")

Expand Down Expand Up @@ -78,6 +87,7 @@ void gtkMessageHandler(const gchar *log_domain,
GnomeHintsSettings::GnomeHintsSettings()
: QObject(0)
, m_usePortal(checkUsePortalSupport())
, m_canUseFileChooserPortal(!m_usePortal)
, m_gnomeDesktopSettings(g_settings_new("org.gnome.desktop.wm.preferences"))
, m_settings(g_settings_new("org.gnome.desktop.interface"))
{
Expand Down Expand Up @@ -140,6 +150,42 @@ GnomeHintsSettings::GnomeHintsSettings()
loadStaticHints();
loadTheme();
loadTitlebar();

if (m_canUseFileChooserPortal) {
QTimer::singleShot(0, this, [this] () {
const QString filePath = QStringLiteral("/proc/%1/root").arg(QCoreApplication::applicationPid());
qWarning() << filePath;
struct stat info;
if (lstat(filePath.toStdString().c_str(), &info) == 0) {
if (!static_cast<int>(info.st_uid)) {
m_canUseFileChooserPortal = false;
}
} else {
// Do not use FileChooser portal if we fail to get information about the file
m_canUseFileChooserPortal = false;
}
});

if (m_canUseFileChooserPortal) {
// Get information about portal version
QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"),
QLatin1String("/org/freedesktop/portal/desktop"),
QLatin1String("org.freedesktop.DBus.Properties"),
QLatin1String("Get"));
message << QLatin1String("org.freedesktop.portal.FileChooser") << QLatin1String("version");
QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(message);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, [this] (QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<QVariant> reply = *watcher;
if (reply.isValid()) {
uint fileChooserPortalVersion = reply.value().toUInt();
if (fileChooserPortalVersion < 3) {
m_canUseFileChooserPortal = false;
}
}
});
}
}
}

GnomeHintsSettings::~GnomeHintsSettings()
Expand Down
6 changes: 6 additions & 0 deletions common/gnomehintssettings.h
Expand Up @@ -77,6 +77,11 @@ class GnomeHintsSettings : public QObject
return palette;
}

inline bool canUseFileChooserPortal() const
{
return m_canUseFileChooserPortal;
}

inline bool gtkThemeDarkVariant() const
{
return m_gtkThemeDarkVariant;
Expand Down Expand Up @@ -167,6 +172,7 @@ private Q_SLOTS:
void configureKvantum(const QString &theme) const;

bool m_usePortal;
bool m_canUseFileChooserPortal = false;
bool m_gtkThemeDarkVariant = false;
TitlebarButtons m_titlebarButtons = TitlebarButton::CloseButton;
TitlebarButtonsPlacement m_titlebarButtonPlacement = TitlebarButtonsPlacement::RightPlacement;
Expand Down
28 changes: 3 additions & 25 deletions theme/qgnomeplatformtheme.cpp
Expand Up @@ -27,12 +27,6 @@
#include <QGuiApplication>
#include <QStyleFactory>

#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>

#if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON)
#include <private/qdbustrayicon_p.h>
#endif
Expand All @@ -51,22 +45,6 @@ QGnomePlatformTheme::QGnomePlatformTheme()
*/
g_type_ensure(PANGO_TYPE_FONT_FAMILY);
g_type_ensure(PANGO_TYPE_FONT_FACE);


// Get information about portal version
QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"),
QLatin1String("/org/freedesktop/portal/desktop"),
QLatin1String("org.freedesktop.DBus.Properties"),
QLatin1String("Get"));
message << QLatin1String("org.freedesktop.portal.FileChooser") << QLatin1String("version");
QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(message);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, [this] (QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<QVariant> reply = *watcher;
if (reply.isValid()) {
fileChooserPortalVersion = reply.value().toUInt();
}
});
}

QGnomePlatformTheme::~QGnomePlatformTheme()
Expand Down Expand Up @@ -115,10 +93,10 @@ QPlatformDialogHelper *QGnomePlatformTheme::createPlatformDialogHelper(QPlatform
{
switch (type) {
case QPlatformTheme::FileDialog: {
if (fileChooserPortalVersion < 3) {
return new QGtk3FileDialogHelper;
} else {
if (m_hints->canUseFileChooserPortal()) {
return new QXdgDesktopPortalFileDialog;
} else {
return new QGtk3FileDialogHelper;
}
}
case QPlatformTheme::FontDialog:
Expand Down
1 change: 0 additions & 1 deletion theme/qgnomeplatformtheme.h
Expand Up @@ -46,7 +46,6 @@ class QGnomePlatformTheme : public QPlatformTheme
void loadSettings();

GnomeHintsSettings *m_hints;
uint fileChooserPortalVersion = 0;
};

#endif // QGNOME_PLATFORM_THEME_HH

0 comments on commit d5a01ea

Please sign in to comment.