This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository enlightenment.

View the commit online.

commit 46dee0eb81839b97e31b2442fb29963f49e9cdd0
Author: Cedric BAIL <[email protected]>
AuthorDate: Wed Nov 25 18:17:56 2026 -0600

    e_comp_wl - only programs the user allows may be an input method
    
    zwp_input_method_v2 reads every key typed into a text field, so any
    program the user merely ran could become a keylogger. Nothing on a bare
    Wayland connection tells an input method from any other client, so the
    user names the programs they trust, in a .desktop file of
    <prefix>/share/enlightenment/data/wayland-privileges or
    ~/.e/e/wayland-privileges:
    
      Exec=fcitx5
      X-Enlightenment-Wayland-Privileges=input_method;
    
    The key grants, not the directory, so a desktop file copied there by
    mistake grants nothing and the next privileged protocol only needs a new
    name. Exec's program is resolved as a shell would, through PATH, and
    compared with the client's /proc/<pid>/exe. When it cannot be resolved
    the file grants nothing: matching on the name alone would let any
    program called fcitx5 in, on a machine without fcitx5.
    
    A refused client does not see the global, through
    wl_display_set_global_filter, which also refuses a bind to it. To an
    input method this looks like a compositor without the protocol, which
    they all handle; a protocol error would kill its connection instead.
    
    The files are read again for each check, which is once per client
    registry, so editing one does not need a restart of E, which on Wayland
    is the end of the session.
    
    fcitx5.desktop is installed, as the default is to refuse and an upgrade
    would otherwise silently stop a working input method. ibus is not
    listed: it does not speak input-method v2.
    
    Two limits. Anyone who can write ~/.e/e can add a program, but they can
    already add a startup application. And sandboxed clients cannot be
    matched, since their exe is the sandbox's; wp_security_context_v1 is the
    answer for them and is not implemented. /proc/<pid>/exe is also Linux
    only, elsewhere every client is refused.
    
    The test harness sets E_HOME, so E's user directory is the throwaway one
    even when the caller has E_HOME set, and tells the client where it is.
    Tests that act as an input method write a real allow-file there, and
    e_wlcs.c does the same for wlcs, so the mechanism under test is the one
    users get.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 data/wayland-privileges/fcitx5.desktop |  13 +++
 data/wayland-privileges/meson.build    |   3 +
 meson.build                            |   1 +
 src/bin/e_comp_wl.h                    |   1 +
 src/bin/e_comp_wl_extensions.c         |   1 +
 src/bin/e_comp_wl_privileges.c         | 152 +++++++++++++++++++++++++++++++++
 src/bin/meson.build                    |   1 +
 src/tests/e-test-compositor.sh.in      |   1 +
 src/tests/wayland/e_wl_testkit.c       |  44 ++++++++++
 src/tests/wayland/e_wl_testkit.h       |   4 +
 src/tests/wayland/globals.expected     |   1 -
 src/tests/wayland/meson.build          |   8 +-
 src/tests/wayland/run-nested.sh        |   3 +-
 src/tests/wayland/test_privileges.c    |  66 ++++++++++++++
 src/tests/wayland/test_text_input.c    |   1 +
 src/tests/wlcs/e_wlcs.c                |  34 ++++++++
 16 files changed, 331 insertions(+), 3 deletions(-)

diff --git a/data/wayland-privileges/fcitx5.desktop b/data/wayland-privileges/fcitx5.desktop
new file mode 100644
index 000000000..b4d7123a9
--- /dev/null
+++ b/data/wayland-privileges/fcitx5.desktop
@@ -0,0 +1,13 @@
+# Programs named by a .desktop file in this directory, or in
+# ~/.e/e/wayland-privileges, may use the Wayland protocols listed in their
+# X-Enlightenment-Wayland-Privileges key. No other program is told they
+# exist:
+#   input_method  zwp_input_method_v2, which reads the keys typed in a
+#                 text field
+# Exec names the program, as an absolute path or a name looked up in PATH.
+[Desktop Entry]
+Type=Application
+Name=Fcitx 5
+Exec=fcitx5
+NoDisplay=true
+X-Enlightenment-Wayland-Privileges=input_method;
diff --git a/data/wayland-privileges/meson.build b/data/wayland-privileges/meson.build
new file mode 100644
index 000000000..4dc49e261
--- /dev/null
+++ b/data/wayland-privileges/meson.build
@@ -0,0 +1,3 @@
+install_data('fcitx5.desktop',
+             install_dir: join_paths(dir_data, 'enlightenment/data/wayland-privileges')
+            )
diff --git a/meson.build b/meson.build
index 9c6c1b139..4012b0ff3 100644
--- a/meson.build
+++ b/meson.build
@@ -415,6 +415,7 @@ subdir('data/icons')
 subdir('data/images')
 subdir('data/input_methods')
 subdir('data/units')
+subdir('data/wayland-privileges')
 subdir('data/session')
 subdir('data/valgrind')
 
diff --git a/src/bin/e_comp_wl.h b/src/bin/e_comp_wl.h
index 518793b89..f0208c24d 100644
--- a/src/bin/e_comp_wl.h
+++ b/src/bin/e_comp_wl.h
@@ -501,6 +501,7 @@ EINTERN void e_comp_wl_presentation_feedback_discard(Eina_List **feedbacks);
 EINTERN void e_comp_wl_presentation_feedback_commit(E_Client *ec, Eina_List **feedbacks);
 EINTERN Eina_Bool e_comp_wl_extension_idle_inhibited(void);
 
+EINTERN void e_comp_wl_privileges_init(void);
 EINTERN Eina_Bool e_comp_wl_text_input_init(void);
 /* Called with the client wl_keyboard enters or leaves, after the walk from
  * an xdg popup up to its toplevel. */
diff --git a/src/bin/e_comp_wl_extensions.c b/src/bin/e_comp_wl_extensions.c
index 428a14608..625a096c4 100644
--- a/src/bin/e_comp_wl_extensions.c
+++ b/src/bin/e_comp_wl_extensions.c
@@ -1819,6 +1819,7 @@ e_comp_wl_extensions_init(void)
    GLOBAL_CREATE_OR_RETURN(wp_presentation, wp_presentation_interface, 1);
    GLOBAL_CREATE_OR_RETURN(zwp_keyboard_shortcuts_inhibit_manager_v1, zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
    if (!e_comp_wl_text_input_init()) return EINA_FALSE;
+   e_comp_wl_privileges_init();
 
    ecore_event_handler_add(ECORE_WL2_EVENT_SYNC_DONE, _dmabuf_add, NULL);
 
diff --git a/src/bin/e_comp_wl_privileges.c b/src/bin/e_comp_wl_privileges.c
new file mode 100644
index 000000000..c7926b78f
--- /dev/null
+++ b/src/bin/e_comp_wl_privileges.c
@@ -0,0 +1,152 @@
+/* Globals a client can use to read or type the user's keys are advertised
+ * only to the programs named in a .desktop file of
+ * <prefix>/share/enlightenment/data/wayland-privileges or
+ * ~/.e/e/wayland-privileges with, for instance:
+ *
+ *   Exec=fcitx5
+ *   X-Enlightenment-Wayland-Privileges=input_method;
+ *
+ * A client that is refused does not see the global at all, as on a
+ * compositor without the protocol, which every input method handles; a
+ * protocol error would kill its connection instead.
+ *
+ * The program is the client's /proc/<pid>/exe, so this cannot tell apart
+ * sandboxed clients, which all are the sandbox's binary. */
+#define E_COMP_WL
+#include "e.h"
+#include "input-method-unstable-v2-server-protocol.h"
+
+static const struct
+{
+   const struct wl_interface *iface;
+   const char *privilege;
+} _privileged_globals[] =
+{
+   { &zwp_input_method_manager_v2_interface, "input_method" },
+};
+
+/* The program the Exec key starts, found in PATH as a shell would, or NULL */
+static char *
+_e_comp_wl_privileges_exec_resolve(const char *exec)
+{
+   char prog[PATH_MAX], buf[PATH_MAX];
+   const char *path, *end;
+   char stop = ' ';
+   size_t n = 0;
+
+   while (*exec == ' ') exec++;
+   if (*exec == '"') stop = *exec++;
+   while (*exec && (*exec != stop) && (n < sizeof(prog) - 1))
+     prog[n++] = *exec++;
+   prog[n] = 0;
+   if (prog[0] == '/') return realpath(prog, NULL);
+   if ((!prog[0]) || strchr(prog, '/')) return NULL;
+
+   for (path = getenv("PATH"); path && *path; path = *end ? end + 1 : end)
+     {
+        for (end = path; *end && (*end != ':'); end++);
+        if (snprintf(buf, sizeof(buf), "%.*s/%s", (int)(end - path), path, prog) >=
+            (int)sizeof(buf))
+          continue;
+        if (!access(buf, X_OK)) return realpath(buf, NULL);
+     }
+   return NULL;
+}
+
+static Eina_Bool
+_e_comp_wl_privileges_list_has(const char *list, const char *privilege)
+{
+   size_t len = strlen(privilege);
+   const char *p;
+
+   for (p = strstr(list, privilege); p; p = strstr(p + len, privilege))
+     {
+        if ((p != list) && (p[-1] != ';')) continue;
+        if ((p[len] == ';') || (!p[len])) return EINA_TRUE;
+     }
+   return EINA_FALSE;
+}
+
+static Eina_Bool
+_e_comp_wl_privileges_file_grants(const char *file, const char *exe, const char *privilege)
+{
+   Efreet_Ini *ini;
+   const char *list, *exec;
+   char *prog = NULL;
+   Eina_Bool granted;
+
+   ini = efreet_ini_new(file);
+   if (!ini) return EINA_FALSE;
+   if (efreet_ini_section_set(ini, "Desktop Entry"))
+     {
+        list = efreet_ini_string_get(ini, "X-Enlightenment-Wayland-Privileges");
+        exec = efreet_ini_string_get(ini, "Exec");
+        if (list && exec && _e_comp_wl_privileges_list_has(list, privilege))
+          prog = _e_comp_wl_privileges_exec_resolve(exec);
+     }
+   efreet_ini_free(ini);
+   granted = prog && (!strcmp(prog, exe));
+   free(prog);
+   return granted;
+}
+
+static Eina_Bool
+_e_comp_wl_privileges_dir_grants(const char *dir, const char *exe, const char *privilege)
+{
+   Eina_Iterator *it;
+   const char *file;
+   Eina_Bool granted = EINA_FALSE;
+
+   it = eina_file_ls(dir);
+   if (!it) return EINA_FALSE;
+   EINA_ITERATOR_FOREACH(it, file)
+     {
+        if ((!granted) && eina_str_has_extension(file, ".desktop"))
+          granted = _e_comp_wl_privileges_file_grants(file, exe, privilege);
+        eina_stringshare_del(file);
+     }
+   eina_iterator_free(it);
+   return granted;
+}
+
+/* The files are read at every check, so a change to them applies to the
+ * next client without restarting E. */
+static bool
+_e_comp_wl_privileges_global_filter(const struct wl_client *client, const struct wl_global *global, void *data EINA_UNUSED)
+{
+   const struct wl_interface *iface = wl_global_get_interface(global);
+   const char *privilege = NULL;
+   char dir[PATH_MAX], *exe;
+   Eina_Bool granted;
+   unsigned int i;
+   pid_t pid;
+
+   for (i = 0; i < EINA_C_ARRAY_LENGTH(_privileged_globals); i++)
+     if (_privileged_globals[i].iface == iface)
+       privilege = _privileged_globals[i].privilege;
+   if (!privilege) return true;
+
+   wl_client_get_credentials((struct wl_client *)client, &pid, NULL, NULL);
+   snprintf(dir, sizeof(dir), "/proc/%d/exe", (int)pid);
+   exe = realpath(dir, NULL);
+   if (!exe) return false;
+
+   e_prefix_data_concat_static(dir, "data/wayland-privileges");
+   granted = _e_comp_wl_privileges_dir_grants(dir, exe, privilege);
+   if (!granted)
+     {
+        e_user_dir_concat_static(dir, "wayland-privileges");
+        granted = _e_comp_wl_privileges_dir_grants(dir, exe, privilege);
+     }
+   if (!granted)
+     INF("%s is not allowed %s, see wayland-privileges", exe, privilege);
+   free(exe);
+   return granted;
+}
+
+EINTERN void
+e_comp_wl_privileges_init(void)
+{
+   wl_display_set_global_filter(e_comp_wl->wl.disp,
+                                _e_comp_wl_privileges_global_filter, NULL);
+}
diff --git a/src/bin/meson.build b/src/bin/meson.build
index 9623e3a4d..e192f2232 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -423,6 +423,7 @@ if config_h.has('HAVE_WAYLAND') == true
     'e_comp_wl.c',
     'e_comp_wl_extensions.c',
     'e_comp_wl_extensions_tizen.c',
+    'e_comp_wl_privileges.c',
     'e_comp_wl_shm.c',
     'e_comp_wl_text_input.c',
     wayland_proto_c,
diff --git a/src/tests/e-test-compositor.sh.in b/src/tests/e-test-compositor.sh.in
index 3c432d713..2a0f684a7 100644
--- a/src/tests/e-test-compositor.sh.in
+++ b/src/tests/e-test-compositor.sh.in
@@ -93,6 +93,7 @@ exec env \
     E_START=e-test-compositor \
     XDG_RUNTIME_DIR="$RUNDIR" \
     HOME="$RUNDIR" \
+    E_HOME="$RUNDIR/.e" \
     E_PREFIX="$STAGE@PREFIX@" \
     E_BIN_DIR="$STAGE@BIN_DIR@" \
     E_LIB_DIR="$STAGE@LIB_DIR@" \
diff --git a/src/tests/wayland/e_wl_testkit.c b/src/tests/wayland/e_wl_testkit.c
index 9aa09c0b4..2be3cde92 100644
--- a/src/tests/wayland/e_wl_testkit.c
+++ b/src/tests/wayland/e_wl_testkit.c
@@ -8,7 +8,9 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <limits.h>
 #include <sys/mman.h>
+#include <sys/stat.h>
 
 #include "e_wl_testkit.h"
 
@@ -766,3 +768,45 @@ tk_touch_up(Tk *tk, int id)
    wl_test_touch_up(tk->tester, id);
    tk_settle(tk);
 }
+
+void
+tk_privileges_file(const char *name, const char *exec, const char *privileges)
+{
+   const char *dir = getenv("E_TEST_PRIVILEGES_DIR");
+   char path[PATH_MAX], self[PATH_MAX], *p;
+   ssize_t n;
+   FILE *f;
+
+   if (!dir)
+     {
+        fprintf(stderr, "tk_privileges_file: E_TEST_PRIVILEGES_DIR is unset\n");
+        exit(1);
+     }
+   if (!exec)
+     {
+        n = readlink("/proc/self/exe", self, sizeof(self) - 1);
+        if (n <= 0)
+          {
+             perror("tk_privileges_file: /proc/self/exe");
+             exit(1);
+          }
+        self[n] = 0;
+        exec = self;
+     }
+   snprintf(path, sizeof(path), "%s/%s.desktop", dir, name);
+   for (p = strchr(path + 1, '/'); p; p = strchr(p + 1, '/'))
+     {
+        *p = 0;
+        mkdir(path, 0700);
+        *p = '/';
+     }
+   f = fopen(path, "w");
+   if (!f)
+     {
+        perror(path);
+        exit(1);
+     }
+   fprintf(f, "[Desktop Entry]\nType=Application\nName=%s\nExec=%s\n", name, exec);
+   if (privileges) fprintf(f, "X-Enlightenment-Wayland-Privileges=%s\n", privileges);
+   fclose(f);
+}
diff --git a/src/tests/wayland/e_wl_testkit.h b/src/tests/wayland/e_wl_testkit.h
index 56169bfed..ef9c4d0ea 100644
--- a/src/tests/wayland/e_wl_testkit.h
+++ b/src/tests/wayland/e_wl_testkit.h
@@ -170,4 +170,8 @@ void tk_pointer_button(Tk *tk, unsigned int button, int pressed);
 void tk_touch_down(Tk *tk, int id, int x, int y);
 void tk_touch_up(Tk *tk, int id);
 
+/* Write name.desktop in E's wayland-privileges directory, granting
+ * privileges (no key if NULL) to exec (this test if NULL). */
+void tk_privileges_file(const char *name, const char *exec, const char *privileges);
+
 #endif
diff --git a/src/tests/wayland/globals.expected b/src/tests/wayland/globals.expected
index cda3b98f7..3c2b3ff91 100644
--- a/src/tests/wayland/globals.expected
+++ b/src/tests/wayland/globals.expected
@@ -13,7 +13,6 @@ xdg_toplevel_drag_manager_v1	1
 xdg_wm_base	6
 zwp_e_session_recovery	1
 zwp_idle_inhibit_manager_v1	1
-zwp_input_method_manager_v2	1
 zwp_keyboard_shortcuts_inhibit_manager_v1	1
 zwp_pointer_constraints_v1	1
 zwp_primary_selection_device_manager_v1	1
diff --git a/src/tests/wayland/meson.build b/src/tests/wayland/meson.build
index c4fee488a..04f5b7fa3 100644
--- a/src/tests/wayland/meson.build
+++ b/src/tests/wayland/meson.build
@@ -91,6 +91,8 @@ wl_protocol_tests = [
   ['primary-selection', 'test_primary_selection.c'],
   ['idle-inhibit', 'test_idle_inhibit.c'],
   ['text-input', 'test_text_input.c'],
+  ['privileges', 'test_privileges.c',
+   {'PATH': meson.current_build_dir()}],
   ['client-iconify', 'test_client_iconify.c',
    {'ELM_TRANSITION_DURATION_FACTOR': '4'}],
 ]
@@ -106,7 +108,11 @@ foreach t: wl_protocol_tests
   if t.length() > 2
     env = environment({'E_TEST_COMPOSITOR': e_test_compositor})
     foreach k, v: t[2]
-      env.set(k, v)
+      if k == 'PATH'
+        env.prepend(k, v)
+      else
+        env.set(k, v)
+      endif
     endforeach
   endif
   test('wl-' + t[0],
diff --git a/src/tests/wayland/run-nested.sh b/src/tests/wayland/run-nested.sh
index 590a9d098..0ed2b242b 100755
--- a/src/tests/wayland/run-nested.sh
+++ b/src/tests/wayland/run-nested.sh
@@ -143,7 +143,8 @@ if [ -z "$SOCKET" ]; then
 fi
 
 set +e
-XDG_RUNTIME_DIR="$RUNDIR" WAYLAND_DISPLAY="$SOCKET" E_TEST_PID="$E_PID" "$@"
+XDG_RUNTIME_DIR="$RUNDIR" WAYLAND_DISPLAY="$SOCKET" E_TEST_PID="$E_PID" \
+    E_TEST_PRIVILEGES_DIR="$RUNDIR/.e/e/wayland-privileges" "$@"
 client_status=$?
 set -e
 
diff --git a/src/tests/wayland/test_privileges.c b/src/tests/wayland/test_privileges.c
new file mode 100644
index 000000000..6b03e969d
--- /dev/null
+++ b/src/tests/wayland/test_privileges.c
@@ -0,0 +1,66 @@
+/* zwp_input_method_manager_v2 is only advertised to a program a .desktop
+ * file in wayland-privileges grants input_method to. The files are read
+ * again for every client, so each case is a new connection. The test runs
+ * with its directory in E's PATH. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <limits.h>
+
+#include "e_wl_testkit.h"
+
+#define PROG "test-privileges"
+#define FILE_NAME "test-privileges"
+
+static void
+_expect(int advertised, const char *what)
+{
+   Tk *tk = tk_connect(PROG);
+   uint32_t version = tk_global_version(tk, "zwp_input_method_manager_v2");
+
+   if (!tk_global_version(tk, "zwp_text_input_manager_v3"))
+     tk_fail(tk, "%s: zwp_text_input_manager_v3 is hidden", what);
+   if (advertised && (!version))
+     tk_fail(tk, "%s: zwp_input_method_manager_v2 is hidden", what);
+   if ((!advertised) && version)
+     tk_fail(tk, "%s: zwp_input_method_manager_v2 is advertised", what);
+   tk_disconnect(tk);
+}
+
+int
+main(void)
+{
+   char path[PATH_MAX], quoted[PATH_MAX + 16];
+   ssize_t n;
+
+   n = readlink("/proc/self/exe", path, sizeof(path) - 1);
+   if (n <= 0)
+     {
+        perror("/proc/self/exe");
+        return 1;
+     }
+   path[n] = 0;
+   snprintf(quoted, sizeof(quoted), "\"%s\" --flag %%U", path);
+
+   _expect(0, "no file");
+   tk_privileges_file(FILE_NAME, NULL, NULL);
+   _expect(0, "a file without the key");
+   tk_privileges_file(FILE_NAME, NULL, "input_methods;virtual_keyboard;");
+   _expect(0, "a file without input_method");
+   tk_privileges_file(FILE_NAME, "/bin/true", "input_method;");
+   _expect(0, "a file for another program");
+   tk_privileges_file(FILE_NAME, "true", "input_method;");
+   _expect(0, "a file for another program found in PATH");
+   tk_privileges_file(FILE_NAME, quoted, "virtual_keyboard;input_method");
+   _expect(1, "a file for this program");
+   tk_privileges_file(FILE_NAME, strrchr(path, '/') + 1, "input_method;");
+   _expect(1, "a file for this program found in PATH");
+
+   snprintf(path, sizeof(path), "%s/" FILE_NAME ".desktop", getenv("E_TEST_PRIVILEGES_DIR"));
+   unlink(path);
+   _expect(0, "the file is gone");
+
+   printf(PROG ": ok\n");
+   return 0;
+}
diff --git a/src/tests/wayland/test_text_input.c b/src/tests/wayland/test_text_input.c
index 07c4d2776..95ca93038 100644
--- a/src/tests/wayland/test_text_input.c
+++ b/src/tests/wayland/test_text_input.c
@@ -428,6 +428,7 @@ main(void)
    Ti_Watch tw = { 0 };
    Im_Watch iw = { 0 };
 
+   tk_privileges_file("test-text-input", NULL, "input_method");
    app = tk_connect(PROG);
    ime = tk_connect(PROG);
    app_seat = tk_bind(app, &wl_seat_interface, 1);
diff --git a/src/tests/wlcs/e_wlcs.c b/src/tests/wlcs/e_wlcs.c
index 6906e6d4a..da4977637 100644
--- a/src/tests/wlcs/e_wlcs.c
+++ b/src/tests/wlcs/e_wlcs.c
@@ -218,6 +218,39 @@ _wait_for_socket(E_Server *s)
    return -1;
 }
 
+/* wlcs is its own input method, and E only advertises
+ * zwp_input_method_manager_v2 to the programs wayland-privileges names. */
+static void
+_privileges_grant(E_Server *s)
+{
+   static const char *dirs[] = { ".e", ".e/e", ".e/e/wayland-privileges" };
+   char path[PATH_MAX], exe[PATH_MAX];
+   unsigned int i;
+   ssize_t n;
+   FILE *f;
+
+   n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
+   if (n <= 0) return;
+   exe[n] = 0;
+   for (i = 0; i < sizeof(dirs) / sizeof(dirs[0]); i++)
+     {
+        snprintf(path, sizeof(path), "%s/%s", s->runtime_dir, dirs[i]);
+        mkdir(path, 0700);
+     }
+   if (snprintf(path, sizeof(path), "%s/.e/e/wayland-privileges/wlcs.desktop",
+                s->runtime_dir) >= (int)sizeof(path))
+     return;
+   f = fopen(path, "w");
+   if (!f)
+     {
+        fprintf(stderr, "e_wlcs: %s: %s\n", path, strerror(errno));
+        return;
+     }
+   fprintf(f, "[Desktop Entry]\nType=Application\nName=wlcs\nExec=%s\n"
+           "X-Enlightenment-Wayland-Privileges=input_method;\n", exe);
+   fclose(f);
+}
+
 static void
 _server_start(WlcsDisplayServer *server)
 {
@@ -233,6 +266,7 @@ _server_start(WlcsDisplayServer *server)
         s->runtime_dir[0] = 0;
         return;
      }
+   _privileges_grant(s);
 
    pid = fork();
    if (pid < 0)

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to