访问另一个用户的 D-Bus 会话

发布于 2024-11-17 18:19:12 字数 293 浏览 9 评论 0原文

让我们假设这种情况:我们有一个用户登录,通过 sudosu 执行某个应用程序。该用户已运行 dbus-daemon
然而,当以root权限运行的应用程序尝试访问D-Bus时,它只会生成另一个由root用户拥有的dbus-daemon。这不是我们想要的情况。

有没有办法获得通过 sudosu 运行应用程序的用户的 D-Bus 会话的访问权限?

Let's assume this kind of situation: we have one user logged in, executing some application through sudo or su. This user has got a dbus-daemon running.
However, when an application running with root privileges tries to access D-Bus, it just spawns another dbus-daemon, owned by root user. That's not a desired situation.

Is there a way to gain access to D-Bus session of user who ran the application through sudo or su?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

调妓 2024-11-24 18:19:12

如果您使用的是 systemd 发行版,相对较新 machinectl shell 命令可以完成 su/sudo 的工作,它还会设置会话变量,如 XDG_RUNTIME_DIRDBUS_SESSION_BUS_ADDRESS。例如,如果我想以用户 test 身份运行 systemctl --user,正常方法将失败:

$ sudo --user=test systemctl --user
Failed to connect to bus: No such file or directory

但这种方法有效:

$ sudo machinectl shell --uid=test .host -- /usr/bin/systemctl --user

如果您需要“返回”在调用 sudo 脚本的用户会话中,您可以使用 SUDO_USER/SUDO_UID 来一起破解某些内容。

If you're on a systemd distro, the relatively new machinectl shell command can do the work of su/sudo, and it will also set session variables like XDG_RUNTIME_DIR and DBUS_SESSION_BUS_ADDRESS. So for example, if I want to run systemctl --user as user test, the normal approach will fail:

$ sudo --user=test systemctl --user
Failed to connect to bus: No such file or directory

But this way works:

$ sudo machinectl shell --uid=test .host -- /usr/bin/systemctl --user

If you need to "reach back" into the user session that invoked a sudo script, you could use the SUDO_USER/SUDO_UID to hack something together.

碍人泪离人颜 2024-11-24 18:19:12

首先,当使用 susudo 调用应用程序时,您需要保留 DBUS_SESSION_BUS_ADDRESS 环境变量。不幸的是,这还不够,因为 DBus 总是检查(作为安全措施)调用进程和会话守护进程的 UID 是否相同。唯一的解决方法是在连接到会话总线之前从此应用程序调用 seteuid。然后您可以使用 seteuid(0) 重新获得您的权限。

First, you need DBUS_SESSION_BUS_ADDRESS environment variable to be preserved when invoking application with su or sudo. Unfortunately, this is not enough, because DBus always checks (as a security measure) whether UIDs of the calling process and the session daemon are the same. The only workaround is to call seteuid from this application before connecting to the session bus. You can regain your privileges then with seteuid(0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文