Skip to content

Redesign Xauth handling #1230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2023
Merged

Redesign Xauth handling #1230

merged 2 commits into from
Feb 8, 2023

Conversation

Vogtinator
Copy link
Contributor

@Vogtinator Vogtinator commented Feb 10, 2020

This commit moves Xauthority handling over to libXau.
Advantage is that this allows use of FamilyWild, is faster, more reliable
and easier to read. However, we lose the ability to merge the new cookie into
an existing Xauthority file, so support for using a non-temporary file is
dropped. Even if merging was implemented manually, use of FamilyWild would
"infect" such a file and break it for DMs which don't write it.

@plfiorini plfiorini added this to the 0.19 milestone Feb 10, 2020
@tpgxyz
Copy link
Contributor

tpgxyz commented Feb 25, 2020

I've applied this PR to OpenMandrivaAssociation/sddm@5384de2

I'm running sddm on OpenMandriva with this PR, found no issues.

Copy link
Member

@davidedmundson davidedmundson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally fine, one minor comment/question

The process env part was a bit confusing, but makes sense after IRC explanation.

FILE *fp = popen(qPrintable(cmd), "w");
QString dir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
if (!dir.isEmpty()) {
m_xauthFile.setFileTemplate(dir + QStringLiteral("/xauth_XXXXXX"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat weird.

setupChildProcess is after we've forked, we have the same memory contents, but it's not shared. It's COW.

Does it work as intended and actually clean up?

I suspect it would seem to work because:

the m_xauthFile of the parent that's actually scoped to the process stays effectively uninitialised

the child sets up m_xauthFile but in it's cloned memory, we would then effectively leak the temporary file as we don't call the destructor when we exec()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - I totally forgot to mention that.

It leaks the temporary file currently, so relies on /run getting cleaned up by something else.

Not sure how to improve that though, the deletion would need to happen in the user context as well to avoid any arbitrary deletion vulnerabilities.

There's also #1083 which needs a way to clean up stuff after a user session ends.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about redesigning sddm-helper away from setupChildProcess? It could just do the setup in the main process itself (like setuid) and then use QProcess as usual. This means that sddm-helper would effectively run as the target user at that point and proper cleanup can be performed.

It would not help with #1083 though as that's managed by sddm, not sddm-helper IIRC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically possible, but currently sddm-helper performs utmp and PAM session handling after the user process quites, so that would have to be moved into the daemon first...

I tested how this behaves on FreeBSD and there the session setup is done as part of Xsession: https://github.com/freebsd/freebsd-ports-kde/blob/d70b3c9023dfe8414ea91c31881209d16c15b229/x11/sddm/files/patch-data_scripts_Xsession#L18
This also includes setting up XDG_RUNTIME_DIR, which means that it's not available inside sddm-helper at all.
While using the fallback directory there is technically fine, it definitely needs manual cleanup then.

plfiorini
plfiorini previously approved these changes Apr 4, 2020
@Vogtinator
Copy link
Contributor Author

plfiorini approved these changes 1 minute ago

Note that there are currently open topics, like the authority file leak and the manpage update.

@plfiorini plfiorini self-requested a review April 4, 2020 18:23
@plfiorini plfiorini dismissed their stale review April 4, 2020 18:24

still need some changes

@Vogtinator
Copy link
Contributor Author

I changed the design somewhat. Instead of putting the temporary xauth_XXXXX file into the user's runtime dir, it now ends up in sddm's RUNTIME_DIR and gets its ownership changed to the target user. This means that cleanup by sddm-helper is possible now and it should also work on FreeBSD.

@plfiorini plfiorini modified the milestones: 0.19, 0.20 Nov 3, 2020
youngker pushed a commit to youngker/apparmor that referenced this pull request Nov 23, 2020
Future sddm version will use $XDG_RUNTIME_DIR/xauth_XXXXXX

References:
- https://bugzilla.opensuse.org/show_bug.cgi?id=1174290
- https://bugzilla.suse.com/show_bug.cgi?id=1174293
- sddm/sddm#1230
- jonls/redshift#763

This is the 2.13 version of 35f033c /
https://gitlab.com/apparmor/apparmor/-/merge_requests/581

The difference is that this commit avoids using the @{run} variable.
youngker pushed a commit to youngker/apparmor that referenced this pull request Nov 23, 2020
Future sddm version will use $XDG_RUNTIME_DIR/xauth_XXXXXX

References:

* https://bugzilla.opensuse.org/show_bug.cgi?id=1174290
* https://bugzilla.suse.com/show_bug.cgi?id=1174293
* sddm/sddm#1230
* jonls/redshift#763

This is the 2.13 version of 35f033c / https://gitlab.com/apparmor/apparmor/-/merge_requests/581

The difference is that this commit avoids using the @{run} variable.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/582
Acked-by: John Johansen <john.johansen@canonical.com>
youngker pushed a commit to youngker/apparmor that referenced this pull request Nov 23, 2020
Future sddm version will use $XDG_RUNTIME_DIR/xauth_XXXXXX

References:
- https://bugzilla.opensuse.org/show_bug.cgi?id=1174290
- https://bugzilla.suse.com/show_bug.cgi?id=1174293
- sddm/sddm#1230
- jonls/redshift#763

This is the 2.13 version of 35f033c /
https://gitlab.com/apparmor/apparmor/-/merge_requests/582

The difference is that this commit avoids using the @{run} variable.

(cherry picked from commit 02b9090)
youngker pushed a commit to youngker/apparmor that referenced this pull request Nov 23, 2020
Future sddm version will use $XDG_RUNTIME_DIR/xauth_XXXXXX

References:
- https://bugzilla.opensuse.org/show_bug.cgi?id=1174290
- https://bugzilla.suse.com/show_bug.cgi?id=1174293
- sddm/sddm#1230
- jonls/redshift#763

This is the 2.13 version of 35f033c /
https://gitlab.com/apparmor/apparmor/-/merge_requests/582

The difference is that this commit avoids using the @{run} variable.

(cherry picked from commit 02b9090)
Signed-off-by: John Johansen <john.johansen@canonical.com>
@tvolin
Copy link

tvolin commented Jan 31, 2021

Placing file xauth_XXXXX has bad side-effects. The user cannot write to the containing directory, so xauth (the program) cannot create the lockfile RUNTIME_DIR/xauth_XXXXX-c
(it appends -c to the original filename and tries to create the lock file)
This makes all executions of xauth take 20 seconds to time out (and set RC=1). This is most painful whenever you ssh with X forwarding enabled.
See downstream bug https://bugzilla.redhat.com/show_bug.cgi?id=1922772

I changed the design somewhat. Instead of putting the temporary xauth_XXXXX file into the user's runtime dir, it now ends up in sddm's RUNTIME_DIR and gets its ownership changed to the target user. This means that cleanup by sddm-helper is possible now and it should also work on FreeBSD.

@tpgxyz
Copy link
Contributor

tpgxyz commented Feb 2, 2021

@tvolin make sure you have set proper permissions on /run/sddm
see here OpenMandrivaAssociation/sddm@3cf5678

@Vogtinator
Copy link
Contributor Author

Placing file xauth_XXXXX has bad side-effects. The user cannot write to the containing directory, so xauth (the program) cannot create the lockfile RUNTIME_DIR/xauth_XXXXX-c
(it appends -c to the original filename and tries to create the lock file)
This makes all executions of xauth take 20 seconds to time out (and set RC=1). This is most painful whenever you ssh with X forwarding enabled.
See downstream bug https://bugzilla.redhat.com/show_bug.cgi?id=1922772

Great, back to the drawing board. Apparently this approach doesn't work either then.

@Vogtinator
Copy link
Contributor Author

@tvolin make sure you have set proper permissions on /run/sddm
see here OpenMandrivaAssociation/sddm@3cf5678

O.o

You realize that way you're giving EVERY USER on the system FULL ACCESS to your sessions and the login screen, which means they can just run xev and grab your passwords?

@Vogtinator Vogtinator marked this pull request as draft February 2, 2021 08:34
@tpgxyz
Copy link
Contributor

tpgxyz commented Feb 2, 2021

You realize that way you're giving EVERY USER on the system FULL ACCESS to your sessions and the login screen, which means they can just run xev and grab your passwords?

oops, thanks for spotting this ⭕

@Vogtinator
Copy link
Contributor Author

Actually, I missed that the directory has the sticky bit set - in that case it should actually be fine (but no guarantee).

There are other issues with this approach though, for instance that there's still no explicit cleanup in case of leftover files (sddm crash, power outage, etc.).

@tvolin
Copy link

tvolin commented Feb 2, 2021

Permissions 1733 seem to do the job. Non-root users can't even see the name of each other's xauth files. And with 0600 as the file permission (which it is), they cannot read each other's files either.
Cleanup of /var/run on Fedora is a non-issue since it's on tmpfs. Not sure about other distros.
Bob Richmond proposed this solution for Fedora. Since Mandriva did something similar I'll suggest we fix this downstream.

@Vogtinator
Copy link
Contributor Author

Permissions 1733 seem to do the job. Non-root users can't even see the name of each other's xauth files. And with 0600 as the file permission (which it is), they cannot read each other's files either.
Cleanup of /var/run on Fedora is a non-issue since it's on tmpfs. Not sure about other distros.

I'm wondering mostly about FreeBSD actually.

Bob Richmond proposed this solution for Fedora. Since Mandriva did something similar I'll suggest we fix this downstream.

@Vogtinator
Copy link
Contributor Author

@Vogtinator Can you rebase this on the current mainline to make it mergeable?

Well, I did yesterday but just shortly afterwards major changes got merged...

I don't currently have time to get familiar with them and redo this PR.

@tpgxyz
Copy link
Contributor

tpgxyz commented Jun 11, 2021

@Vogtinator can you please rebase it on top current head ?

@Conan-Kudo
Copy link
Contributor

@Vogtinator @davidedmundson Can someone please look at rebasing this on top of current mainline? We need this and I couldn't figure out how to do it myself. 😢

Kangie added a commit to Kangie/sddm that referenced this pull request Jun 19, 2022
This is a rebase of sddm#1230 by @Vogtinator

> This commit moves Xauthority handling over to libXau.
> Advantage is that this allows use of FamilyWild, is faster, more reliable
> and easier to read. However, we lose the ability to merge the new cookie into
> an existing Xauthority file, so support for using a non-temporary file is
> dropped. Even if merging was implemented manually, use of FamilyWild would
> "infect" such a file and break it for DMs which don't write it.
Kangie added a commit to Kangie/sddm that referenced this pull request Jun 19, 2022
This is a rebase of sddm#1230 by @Vogtinator

> This commit moves Xauthority handling over to libXau.
> Advantage is that this allows use of FamilyWild, is faster, more reliable
> and easier to read. However, we lose the ability to merge the new cookie into
> an existing Xauthority file, so support for using a non-temporary file is
> dropped. Even if merging was implemented manually, use of FamilyWild would
> "infect" such a file and break it for DMs which don't write it.
@Kangie Kangie mentioned this pull request Jun 19, 2022
peremen pushed a commit to peremen/sddm that referenced this pull request Jun 30, 2022
By moving this in display we don't need sddm-helper to perform anything
as root after the initial setup.

This allows us to drop the priveleges in UserSession before forking.

This should unblock sddm#1230 as well as give us a path to simplify the
paths where the helper is running the display manager.
Vogtinator pushed a commit to Vogtinator/sddm that referenced this pull request Feb 3, 2023
This is a rebase of sddm#1230 by @Vogtinator

> This commit moves Xauthority handling over to libXau.
> Advantage is that this allows use of FamilyWild, is faster, more reliable
> and easier to read. However, we lose the ability to merge the new cookie into
> an existing Xauthority file, so support for using a non-temporary file is
> dropped. Even if merging was implemented manually, use of FamilyWild would
> "infect" such a file and break it for DMs which don't write it.
Vogtinator pushed a commit to Vogtinator/sddm that referenced this pull request Feb 3, 2023
This is a rebase of sddm#1230 by @Vogtinator

> This commit moves Xauthority handling over to libXau.
> Advantage is that this allows use of FamilyWild, is faster, more reliable
> and easier to read. However, we lose the ability to merge the new cookie into
> an existing Xauthority file, so support for using a non-temporary file is
> dropped. Even if merging was implemented manually, use of FamilyWild would
> "infect" such a file and break it for DMs which don't write it.
@Vogtinator Vogtinator force-pushed the libxau branch 3 times, most recently from bb0a3a5 to 6e012fb Compare February 4, 2023 20:20
@Vogtinator Vogtinator marked this pull request as ready for review February 4, 2023 20:28
@Vogtinator
Copy link
Contributor Author

Tested to work as expected with x11, x11-user and wayland. For x11, XAUTHORITY is set to /tmp/xauth_rAnDoM, for x11-user to /run/user/1337/xauth_rAnDoM.

Copy link
Member

@davidedmundson davidedmundson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks

// Check file
if (!fp)
if (XauWriteAuth(authFp, &auth) == 0) {
fclose(authFp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest using QScopeGuard rather than having to fClose in all the paths

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Vogtinator and others added 2 commits February 7, 2023 18:54
This commit moves Xauthority handling over to libXau.
Advantage is that this allows use of FamilyWild, is faster, more reliable
and easier to read. However, we lose the ability to merge the new cookie into
an existing Xauthority file, so support for using a non-temporary file is
dropped. Even if merging was implemented manually, use of FamilyWild would
"infect" such a file and break it for DMs which don't write it.

Co-authored-by: Matt Jolly <Matt.Jolly@footclan.ninja>
Use proper temporary files instead of UUIDs. This makes the file's purpose more
obvious and also takes care of cleanup.
@Vogtinator Vogtinator merged commit eb4a7d5 into sddm:develop Feb 8, 2023
@Vogtinator Vogtinator deleted the libxau branch February 8, 2023 07:12
@Vogtinator Vogtinator mentioned this pull request Feb 9, 2023
githubkusi pushed a commit to githubkusi/sddm that referenced this pull request Jul 6, 2023
By moving this in display we don't need sddm-helper to perform anything
as root after the initial setup.

This allows us to drop the priveleges in UserSession before forking.

This should unblock sddm#1230 as well as give us a path to simplify the
paths where the helper is running the display manager.
Rob--W added a commit to Rob--W/sddm that referenced this pull request Oct 2, 2023
Starting from sddm 0.20 (by sddm#1230),
the xauth file is at `/tmp/xauth_*` instead of `~/.Xauthority`.
The new location is subject to systemd-tmpfiles's cleanup, which
deletes files from /tmp/ that are older than 10 days:
https://github.com/systemd/systemd/blob/v254/tmpfiles.d/tmp.conf

The xauth file should not be removed while the session is active,
because it is needed for starting X applications. When removed,
X applications won't start any more.

This patch fixes the issue by declaring these files as "ignored"
in sddm-tmpfiles.conf, so that systemd-tmpfiles doesn't remove them.

Minimal test case:

```
touch /tmp/xauth_testonly
SYSTEMD_LOG_LEVEL=debug systemd-tmpfiles --prefix=/tmp/ --clean
```

Pre-patch output:
File "/tmp/xauth_testonly": change time Mon 2023-10-02 01:27:38.395466 CEST is too new.

(meaning that the file was considered for removal)

Post-patch output:
Ignoring "/tmp/xauth_testonly": a separate glob exists.

(meaning that the file is ignored and not considered for removal)
Vogtinator pushed a commit to Rob--W/sddm that referenced this pull request Dec 7, 2023
Starting from sddm 0.20 (by sddm#1230),
the xauth file is at `/tmp/xauth_*` instead of `~/.Xauthority`.
The new location is subject to systemd-tmpfiles's cleanup, which
deletes files from /tmp/ that are older than 10 days:
https://github.com/systemd/systemd/blob/v254/tmpfiles.d/tmp.conf

The xauth file should not be removed while the session is active,
because it is needed for starting X applications. When removed,
X applications won't start any more.

This patch fixes the issue by declaring these files as "ignored"
in sddm-tmpfiles.conf, so that systemd-tmpfiles doesn't remove them.

Minimal test case:

```
touch /tmp/xauth_testonly
SYSTEMD_LOG_LEVEL=debug systemd-tmpfiles --prefix=/tmp/ --clean
```

Pre-patch output:
File "/tmp/xauth_testonly": change time Mon 2023-10-02 01:27:38.395466 CEST is too new.

(meaning that the file was considered for removal)

Post-patch output:
Ignoring "/tmp/xauth_testonly": a separate glob exists.

(meaning that the file is ignored and not considered for removal)
Vogtinator pushed a commit that referenced this pull request Dec 7, 2023
Starting from sddm 0.20 (by #1230),
the xauth file is at `/tmp/xauth_*` instead of `~/.Xauthority`.
The new location is subject to systemd-tmpfiles's cleanup, which
deletes files from /tmp/ that are older than 10 days:
https://github.com/systemd/systemd/blob/v254/tmpfiles.d/tmp.conf

The xauth file should not be removed while the session is active,
because it is needed for starting X applications. When removed,
X applications won't start any more.

This patch fixes the issue by declaring these files as "ignored"
in sddm-tmpfiles.conf, so that systemd-tmpfiles doesn't remove them.

Minimal test case:

```
touch /tmp/xauth_testonly
SYSTEMD_LOG_LEVEL=debug systemd-tmpfiles --prefix=/tmp/ --clean
```

Pre-patch output:
File "/tmp/xauth_testonly": change time Mon 2023-10-02 01:27:38.395466 CEST is too new.

(meaning that the file was considered for removal)

Post-patch output:
Ignoring "/tmp/xauth_testonly": a separate glob exists.

(meaning that the file is ignored and not considered for removal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants