SDL: dbus: Use the minimum privileges when opening an fd for the OpenURI portal

From fb8cdf0aa8c176c4935c5c007f66e83739f1c4c1 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Sun, 13 Sep 2026 12:33:57 -0400
Subject: [PATCH] dbus: Use the minimum privileges when opening an fd for the
 OpenURI portal

Open files and directories as read-only, as the fd is only used as proof of access by the portal, and a mismatch between fd write permissions and the portal write parameter will cause the fd to be rejected.
---
 src/core/linux/SDL_dbus.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/core/linux/SDL_dbus.c b/src/core/linux/SDL_dbus.c
index 2416907fc68d8..33a4467c146de 100644
--- a/src/core/linux/SDL_dbus.c
+++ b/src/core/linux/SDL_dbus.c
@@ -27,6 +27,7 @@
 #include "SDL_dbus.h"
 
 #include <fcntl.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
 #ifdef SDL_USE_LIBDBUS
@@ -543,7 +544,19 @@ bool SDL_DBus_OpenURI(const char *uri, const char *window_id, const char *activa
             }
             uri = decoded_path;
         }
-        fd = open(uri, O_RDWR | O_CLOEXEC);
+
+        struct stat st;
+        if (stat(uri, &st) == 0) {
+            /* Open files and directories as read-only, as the fd is only used as proof
+             * of access by the portal, and a mismatch between fd write permissions and
+             * the portal write parameter will cause the fd to be rejected.
+             */
+            int oflags = O_RDONLY | O_CLOEXEC;
+            if (S_ISDIR(st.st_mode)) {
+                oflags |= O_DIRECTORY;
+            }
+            fd = open(uri, oflags);
+        }
         SDL_free(decoded_path);
         if (fd >= 0) {
             msg = dbus.message_new_method_call(bus_name, path, interface, "OpenFile");