https://bugs.kde.org/show_bug.cgi?id=528336

            Bug ID: 528336
           Summary: ksecretd crashes on login when using the Union style
    Classification: Frameworks and Libraries
           Product: frameworks-kwallet
      Version First unspecified
       Reported In:
          Platform: NixOS
                OS: Linux
            Status: REPORTED
          Severity: crash
          Priority: NOR
         Component: general
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]
  Target Milestone: ---

Created attachment 198067
  --> https://bugs.kde.org/attachment.cgi?id=198067&action=edit
Reproduction script for the bug

DESCRIPTION

When pam_kwallet starts ksecretd at login, ksecretd truncates each environment
variable that pam_kwallet_init sends it at 999 bytes. As a result, the wallet
daemon runs with incomplete search paths such as XDG_DATA_DIRS.

The cause is waitForEnvironment() in src/runtime/ksecretd/main.cpp. It reads
each line from the socket with fgets() into a buffer of BSIZE (1000) bytes and
passes the result to putenv(). If a line is longer than the buffer, the first
999 bytes become the value of the variable, and the remaining characters are
passed to putenv() as if they were a separate line.

On NixOS, the variables of a Plasma session are much longer than 999 bytes,
because each package adds its own directory to them. On my system,
XDG_DATA_DIRS for example is 5689 bytes and QT_PLUGIN_PATH is 3506 bytes. The
system-wide data directory /run/current-system/sw/share is at the very end of
XDG_DATA_DIRS, so ksecretd misses it entirely.

With the Union widget style, losing that directory crashes ksecretd at every
login. Union's plugin directory is in the first 999 bytes of QT_PLUGIN_PATH, so
ksecretd still loads the plugin, but Union can't find any of its styles, and it
crashes in Union::Style::matches() while QApplication starts (see bug 526278
for the same crash in Flatpak apps). ksecretd logs `Icon theme "breeze-dark"
not found.` and `Could not find style "breeze"` just before the crash. The
password hash from PAM is lost with the crashed process, so the next
application that uses the wallet starts a new ksecretd through D-Bus, and that
one asks for the password. Chromium and Electron applications such as Slack ask
the wallet for their encryption key once at startup, and they give up if the
user doesn't answer the prompt within about 25 seconds. They then can't decrypt
their saved cookies, so they start logged out after every login.

The Union crash is a separate bug, and should be fixed separately. Even without
Union, though, ksecretd runs with the wrong paths for the whole session.

STEPS TO REPRODUCE

The attached script long_variable.py starts ksecretd the way pam_kwallet5 and
pam_kwallet_init do. It sends the password hash over a pipe and the environment
over a unix socket, and the environment includes a variable KWALLET_TEST_LONG
with a 1503-byte value. The script then reads the variable back from the
running ksecretd with gdb. It needs Python 3 and gdb, and it uses an empty
scratch directory as HOME, so it doesn't touch a real wallet.

1. Run the script on a private session bus, with the path of the ksecretd
binary and a scratch directory:
   dbus-run-session -- python3 long_variable.py /path/to/ksecretd
/path/to/scratch-dir
2. Read the length of KWALLET_TEST_LONG that the script prints.

To see the crash at login instead:

1. Use a system where XDG_DATA_DIRS in the Plasma session is longer than 999
bytes and the system data directory comes after the first 999 bytes, for
example NixOS with Plasma 6.
2. Set the widget style to Union.
3. Log in with a display manager that uses pam_kwallet5.

OBSERVED RESULT

The script prints a length of 981. That is 999 bytes minus the 18 bytes of
"KWALLET_TEST_LONG=".

At login, ksecretd crashes in Union::Style::matches(), and a new ksecretd asks
for the wallet password.

EXPECTED RESULT

ksecretd keeps the whole value of every variable, so the script prints a length
of 1503, and at login PAM unlocks the wallet without asking for the password.

SOFTWARE/OS VERSIONS

Operating System: NixOS 26.11
KDE Plasma Version: 6.8.2 (kwallet-pam 6.8.2, Union 6.8.2)
KDE Frameworks Version: 6.31.0
Qt Version: 6.12.0
Kernel Version: 7.3.1 (64-bit)
Graphics Platform: Wayland
Display manager: plasma-login-manager 6.8.2

ADDITIONAL INFORMATION

Reading each line with getline() fixes the bug. With this patch, the script
prints 1503. ksecretd also no longer crashes when I replay my whole session
environment to it the same way, with the Union style set. I can open a merge
request with it.

--- a/src/runtime/ksecretd/main.cpp
+++ b/src/runtime/ksecretd/main.cpp
@@ -31,7 +31,6 @@
 #include <sys/un.h>
 #include <unistd.h>

-#define BSIZE 1000
 static int pipefd = 0;
 static int socketfd = 0;
 #endif
@@ -74,19 +73,19 @@
     }
     qCDebug(KSECRETD_LOG) << "ksecretd5: client connected";

-    char str[BSIZE] = {'\0'};
-
-    int chop = 0;
+    // Read each line whole, because on some systems (for example NixOS)
variables such as XDG_DATA_DIRS are
+    // several thousand characters long, and a fixed-size buffer would cut
them off.
     FILE *s3 = fdopen(dup(s2), "r");
-    while (!feof(s3)) {
-        if (fgets(str, BSIZE, s3)) {
-            chop = strlen(str) - 1;
-            if (str[chop] == '\n') {
-                str[chop] = '\0';
-            }
-            putenv(strdup(str));
+    char *line = nullptr;
+    size_t lineCapacity = 0;
+    ssize_t lineLength;
+    while ((lineLength = getline(&line, &lineCapacity, s3)) != -1) {
+        if (lineLength > 0 && line[lineLength - 1] == '\n') {
+            line[lineLength - 1] = '\0';
         }
+        putenv(strdup(line));
     }
+    free(line);
     fclose(s3);

     qCDebug(KSECRETD_LOG) << "ksecretd5: client disconnected";

AI DISCLOSURE

Claude Opus 5.5 found this bug on its own in Claude Code on my machine. I had
only asked it why Slack and Claude Desktop logged me out after every boot.
Claude read the logs and core dumps, traced the crash to the fixed-size buffer
in ksecretd, and wrote the reproduction script and the patch. It also drafted
the beginning of this report, which I then edited. I'm a human, and I'm the one
submitting the report and answering questions about it.

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to