** Description changed:

  [ Impact ]
  
  When using smartcard authentication (or another authentication method
  where the user's name isn't known by gdm at startup), gdm may hang
  forever on a login attempt due to a memory management bug.
  
  This occurs when the accounts-daemon process doesn't respond with a
  user's information via DBus until after the athentication &
  authorization has completed.
  
  This bug has not been observed on Ubuntu without the following synthetic
  reproducer.
  
  The bug originally manifested as a crash in RHEL 10 [1]; I've been
  unable to reproduce that behavior in Ubuntu.
  
  [1] https://gitlab.gnome.org/GNOME/gdm/-/merge_requests/392
  
  [ Test plan ]
  
  For resolute, use `images:ubuntu/26.04/desktop` and the
  `ubuntu/resolute-devel` branch.
  
  ```sh
  lxc launch images:ubuntu/noble/desktop n0 --vm -c limits.cpu=2 -c 
limits.memory=4GiB
  ```
  
  ```sh
  apt install -y softhsm2 pamtester openssl sssd libpam-sss gnutls-bin git
  
  # The sssd user must be able to read the scripts, so don't clone into ~/ubuntu
- git clone --depth 1 -b ubuntu/noble-devel 
https://git.launchpad.net/ubuntu/+source/gdm3 /tmp/gdm
+ git clone --depth 1 -b ubuntu/noble-devel 
https://git.launchpad.net/ubuntu/+source/gdm3 /usr/local/src/gdm
  ```
  
  Disable the image's automatic login and enable GDM debug logging:
  
  ```sh
  cat >/etc/gdm3/custom.conf <<'EOF'
  [daemon]
  AutomaticLoginEnable=false
  
  [debug]
  Enable=true
  EOF
  ```
  
  Create the virtual smart card for user `ubuntu` (PIN `554433`). The script 
runs
  its own PAM checks, restarts GDM on the login screen and ends its checks with
  `Press any key and enter to continue`. Leave it running for the rest of the 
test.
  
  ```sh
  cd /tmp
- sudo -u ubuntu sudo env PIN=554433 WAIT=1 bash 
/tmp/gdm/debian/tests/sssd-gdm-smartcard-pam-auth-tester.sh
+ sudo -u ubuntu sudo env PIN=554433 WAIT=1 bash 
/usr/local/src/gdm/debian/tests/sssd-gdm-smartcard-pam-auth-tester.sh
  ```
  
  In a second terminal pause accounts-daemon just before logging in:
  
  ```sh
  systemctl kill -s STOP accounts-daemon
  ```
  
  On the graphical console, at the smart card prompt, enter the user name
  `ubuntu` and then the PIN `554433`.
  
  Wait 3 seconds after submitting the PIN, then resume accounts-daemon:
  
  ```sh
  systemctl kill -s CONT accounts-daemon
- sleep 15
+ ```
  
- loginctl list-sessions
- journalctl -b --no-pager -t gdm-smartcard] \
-     | grep -E "new-username=|not fully loaded|destroyed while still being 
fetched|SESSION_OPENED"
- ```
+ Expected behavior (patched): Login completes successfully
+ 
+ Actual behavior (unpatched): Login hangs forever
  
  [ Where problems could occur ]
  
  The gdm-session-worker is responsible for driving authentication in gdm; if 
the
  change is wrong/broken, we'd expect to see authentication issues similar to
  this bug (hangs, failures, etc).
  
  [ Other info ]
  
  Upstream fix (51.0):
  
https://gitlab.gnome.org/GNOME/gdm/-/commit/5a0c2afd7e2001a848101a8154d38fcf1b092963
  
  Introduced by (45.0):
  
https://gitlab.gnome.org/GNOME/gdm/-/commit/95736e372a11d00f02054906e8286b6f35cf5cd3
  
  On each login attempt, gdm spawns a new gdm-session-worker process,
  which initializes a GdmSessionWorker (`session-worker-main.c:156`). At
  multiple times during its lifecycle, GdmSessionWorker attempts to load
  the session settings for the user that is logging in (`gdm-session-
  worker.c:436,2600,2905`).
  
  gdm uses the libaccountsservice ActUserManager singleton to interact
  with users on the system (ActUser). To load a user's session settings,
  gdm-session-worker calls `act_user_manager_get_user` (`gdm-session-
  settings.c:351`), which returns an unowned reference to an ActUser. That
  unowned reference may be retained in the GdmSessionSettings after it is
  freed by the ActUserManager; the call to `g_object_ref` is needed to
  satisfy the ownership annotation on `act_user_manager_get_user`'s return
  value [1][2].
  
  I was able to coax an LLM to generate the following description of the
  hang, which I believe to be accurate:
  
  gdm-session-worker walks a linear state machine. Each transition is driven by 
a
  D-Bus call from the GDM daemon (`gdm-session.c`) which spawned the worker:
  
-     NONE                  (worker spawned)
-     SETUP_COMPLETE        reached via Initialize()
-     AUTHENTICATED         reached via Authenticate()
-     AUTHORIZED            reached via Authorize()
-     ACCREDITED            reached via EstablishCredentials()
-     ACCOUNT_DETAILS_SAVED reached via Open()
-     SESSION_OPENED
-     SESSION_STARTED
+     NONE                  (worker spawned)
+     SETUP_COMPLETE        reached via Initialize()
+     AUTHENTICATED         reached via Authenticate()
+     AUTHORIZED            reached via Authorize()
+     ACCREDITED            reached via EstablishCredentials()
+     ACCOUNT_DETAILS_SAVED reached via Open()
+     SESSION_OPENED
+     SESSION_STARTED
  
  The bug stalls the worker at the ACCREDITED -> ACCOUNT_DETAILS_SAVED 
transition.
  The Open() call never completes, so the worker never reaches SESSION_OPENED
  and the login hangs.
  
  On smartcard login the username is still empty during NONE -> SETUP_COMPLETE
  (the Initialize() call), so no settings are loaded yet and the worker proceeds
  straight to the PAM setup.
  
  The username first appears during SETUP_COMPLETE -> AUTHENTICATED, inside
  do_authenticate(), which blocks in the PAM conversation:
  
-     do_authenticate()
-       gdm_session_worker_authenticate_user()
-         pam_authenticate()
-           ...
-             gdm_session_worker_pam_new_messages_handler()
-               gdm_session_worker_process_pam_message()
-                 gdm_session_worker_update_username()
-                   attempt_to_load_user_settings()
-                     gdm_session_settings_load()
-                       act_user_manager_get_user()
+     do_authenticate()
+       gdm_session_worker_authenticate_user()
+         pam_authenticate()
+           ...
+             gdm_session_worker_pam_new_messages_handler()
+               gdm_session_worker_process_pam_message()
+                 gdm_session_worker_update_username()
+                   attempt_to_load_user_settings()
+                     gdm_session_settings_load()
+                       act_user_manager_get_user()
  
  act_user_manager_get_user() returns a (transfer none) pointer.
  
  For a not-yet-loaded user, that ActUser is held by exactly one reference — the
  manager's internal new_users list — while an async FindUserByName request to
  accounts-daemon is in flight.
  
  GDM stores the pointer in settings->user without taking its own reference, but
  gdm_session_settings_load() nevertheless treats it as an owned one:
  
-     g_autoptr(ActUser) old_user = g_steal_pointer(&settings->user);
+     g_autoptr(ActUser) old_user = g_steal_pointer(&settings->user);
  
  That cleanup unrefs the unowned pointer on the next call to
  gdm_session_settings_load().
  
  Authentication completes and the worker advances through AUTHORIZED to
  ACCREDITED. The next transition, ACCREDITED -> ACCOUNT_DETAILS_SAVED, is
  driven by Open():
  
-     do_save_account_details_when_ready()
-       g_signal_connect(user_settings, "notify::is-loaded", 
on_settings_is_loaded_changed)
-       gdm_session_settings_load()
-         g_steal_pointer(&settings->user)
-         act_user_manager_get_user()
+     do_save_account_details_when_ready()
+       g_signal_connect(user_settings, "notify::is-loaded", 
on_settings_is_loaded_changed)
+       gdm_session_settings_load()
+         g_steal_pointer(&settings->user)
+         act_user_manager_get_user()
  
  On that second call, g_steal_pointer() moves the borrowed ActUser into the
  g_autoptr old_user, act_user_manager_get_user() resolves to the same ActUser,
  and returning runs g_object_unref(old_user): refcount 1 -> 0.
  
  The ActUser is finalized while its accounts-daemon fetch is still in flight
  (libaccountsservice logs "destroyed while still being fetched" and cancels the
  request).
  
  GDM's on_user_is_loaded_changed is connected to that freed object, so its
  notify::is-loaded can never fire again. The worker's 
on_settings_is_loaded_changed
  never runs, save_account_details_now() is never called, and the worker stays 
in
  ACCREDITED forever.
  
  The fix takes a reference that GDM actually owns:
  
-     settings->user = act_user_manager_get_user(settings->user_manager, 
username);
-     g_object_ref(settings->user);
+     settings->user = act_user_manager_get_user(settings->user_manager, 
username);
+     g_object_ref(settings->user);
  
  This balances the steal/autoptr pair. The manager's own reference keeps the
  ActUser alive long enough for the accounts-daemon fetch to complete and 
deliver
  is-loaded, which unblocks the worker so it can reach SESSION_OPENED.
  
  [1] 
https://gitlab.freedesktop.org/accountsservice/accountsservice/-/blob/23.13.9/src/libaccountsservice/act-user-manager.c?ref_type=tags#L1968
  [2] 
https://gi.readthedocs.io/en/latest/annotations/giannotations.html#memory-and-lifecycle-management

-- 
You received this bug notification because you are a member of Ubuntu
Desktop Bugs, which is subscribed to gdm3 in Ubuntu.
https://bugs.launchpad.net/bugs/2174023

Title:
  smartcard login attempt hangs when accounts-daemon is slow

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/gdm3/+bug/2174023/+subscriptions


-- 
desktop-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/desktop-bugs

Reply via email to