Просмотр исходного кода

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.
Frank Praznik 5 дней назад
Родитель
Сommit
fb8cdf0aa8
1 измененных файлов с 14 добавлено и 1 удалено
  1. 14 1
      src/core/linux/SDL_dbus.c

+ 14 - 1
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");