当前位置: 首页>>技术教程>>正文


何时使用pkexec与gksu/gksudo?

, , , , ,

问题描述

有两种通用的方法以root身份(或者更一般地说,作为另一个用户)以图形方式运行应用程序。像gksugksudokdesudo这样的程序是sudo的图形前端。相反,pkexecPolicyKit的图形前端。

以root用户(或另一用户,non-root用户)手动运行程序时,与使用sudo前端的更传统方法相比,使用pkexec有哪些优点/缺点(如果有的话)?

最佳解决办法

PolicyKit更具可配置性,但pkexec不使用此可配置性。另外,pkexec向用户显示将要启动的程序的完整路径,以便用户更确定会发生什么。 PolicyKit的so-called’策略’可用于设置更多高级设置。例如,是否应该记住密码。

我从pkexec手册中获得的一些内容:

The environment that PROGRAM will run it, will be set to a minimal known and safe environment in order to avoid injecting code through LD_LIBRARY_PATH or similar mechanisms. In addition the PKEXEC_UID environment variable is set to the user id of the process invoking pkexec. As a result, pkexec will not allow you to run e.g. X11 applications as another user since the $DISPLAY environment variable is not set.

有关pkexec手册中的策略或操作定义的更多信息:

   To specify what kind of authorization is needed to execute the program    /usr/bin/pk-example-frobnicate as another user, simply write an action    definition file like this         <?xml version="1.0" encoding="UTF-8"?>        <!DOCTYPE policyconfig PUBLIC         "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"         "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">        <policyconfig>           <vendor>Examples for the PolicyKit Project</vendor>          <vendor_url>http://hal.freedesktop.org/docs/PolicyKit/</vendor_url>           <action id="org.freedesktop.policykit.example.pkexec.run-frobnicate">            <description>Run the PolicyKit example program Frobnicate</description>            <description xml:lang="da">Kør PolicyKit eksemplet Frobnicate</description>            <message>Authentication is required to run the PolicyKit example program Frobnicate</message>            <message xml:lang="da">Autorisering er påkrævet for at afvikle PolicyKit eksemplet Frobnicate</message>            <icon_name>audio-x-generic</icon_name>            <defaults>              <allow_any>no</allow_any>              <allow_inactive>no</allow_inactive>              <allow_active>auth_self_keep</allow_active>            </defaults>            <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/pk-example-frobnicate</annotate>          </action>         </policyconfig>     and drop it in the /usr/share/polkit-1/actions directory under a    suitable name (e.g. matching the namespace of the action). Note that in    addition to specifying the program, the authentication message,    description, icon and defaults can be specified. For example, for the    action defined above, the following authentication dialog will be    shown:         [IMAGE][2]             +----------------------------------------------------------+            |                     Authenticate                     [X] |            +----------------------------------------------------------+            |                                                          |            |  [Icon]  Authentication is required to run the PolicyKit |            |          example program Frobnicate                      |            |                                                          |            |          An application is attempting to perform an      |            |          action that requires privileges. Authentication |            |          is required to perform this action.             |            |                                                          |            |          Password: [__________________________________]  |            |                                                          |            | [V] Details:                                             |            |  Command: /usr/bin/pk-example-frobnicate                 |            |  Run As:  Super User (root)                              |            |  Action:  org.fd.pk.example.pkexec.run-frobnicate        |            |  Vendor:  Examples for the PolicyKit Project             |            |                                                          |            |                                  [Cancel] [Authenticate] |            +----------------------------------------------------------+     If the user is using the da_DK locale, the dialog looks like this:         [IMAGE][3]             +----------------------------------------------------------+            |                     Autorisering                     [X] |            +----------------------------------------------------------+            |                                                          |            |  [Icon]  Autorisering er påkrævet for at afvikle         |            |          PolicyKit eksemplet Frobnicate                  |            |                                                          |            |          Et program forsøger at udføre en handling der   |            |          kræver privilegier. Autorisering er påkrævet.   |            |                                                          |            |          Kodeord: [___________________________________]  |            |                                                          |            | [V] Detaljer:                                            |            |  Bruger:   Super User (root)                             |            |  Program:  /usr/bin/pk-example-frobnicate                |            |  Handling: org.fd.pk.example.pkexec.run-frobnicate       |            |  Vendor:   Examples for the PolicyKit Project            |            |                                                          |            |                                [Annullér] [Autorisering] |            +----------------------------------------------------------+     Note that pkexec does no validation of the ARGUMENTS passed to PROGRAM.    In the normal case (where administrator authentication is required    every time pkexec is used), this is not a problem since if the user is    an administrator he might as well just run pkexec bash to get root.     However, if an action is used for which the user can retain    authorization (or if the user is implicitly authorized), such as with    pk-example-frobnicate above, this could be a security hole. Therefore,    as a rule of thumb, programs for which the default required    authorization is changed, should never implicitly trust user input    (e.g. like any other well-written suid program). 

次佳解决办法

使用sudo,您可以设置每个用户和每个计划策略,以便在sudo环境中保留或重置呼叫者环境。 env_reset策略默认设置。

您不能通过pkexec运行图形应用程序,而无需显式配置它。因为这只是环境重置的结果,所以sudo显然也是如此。但是请注意,pkexec和sudo都不能阻止以root用户身份运行的恶意应用程序从显示管理器或用户X11-cookie文件中检索所有必要的信息。后者,既可以是相似的,也可以是根据情况由非根应用程序完成的。

Sudo不需要明确列出用户。列出任何用户组或者甚至为所有用户设置权限都可以完成。 target_pw指令允许这些用户在他们想要运行应用程序的whoose上下文中(即root)使用用户凭证进行身份验证。除此之外,同样传统的su(su /gtksu /kdesu)程序可以用来做同样的事情,而无需特殊配置。

sudo也允许用户在指定的时间内保持身份验证。该选项名为timeout,可全局配置,每个用户或每个应用程序。每个用户可以保留每个用户的身份验证或全局用户身份

尽管pkexec可能不会对传递给PROGRAM的ARGUMENTS进行验证,但sudo确实具有此功能。虽然承认,你可以很容易地搞砸了,而且通常没有完成。

您可以稍微调整一下您希望通过pkexec运行程序的方式:图标,要显示的文本,甚至可以拥有本地化内容等等。根据情况,这可能确实很漂亮。可悲的是,有人觉得有必要重新发明这个功能。这可能会被放入图形gtksudo /kdesu包装中。

那么Policykit只是一个集中的配置框架。不幸的是不是一个漂亮的。 PK XML-Files比任何应用程序本身可以提供的二进制文件都要复杂得多。而且没有人会使用二进制代码…哦gconf …没关系。

第三种解决办法

pkexecsudo及其前端不同之处:

  1. 您无法通过pkexec运行图形应用程序,而无需显式配置它。

  2. 您可以稍微调整一下您希望通过pkexec运行程序的方式:图标,要显示的文本,是否记住密码,是否允许它以图形方式运行等等。

  3. 任何人都可以运行”Run as”超级用户(假设他们可以进行身份​​验证),使用sudo您必须以管理员身份在sudoers文件中列出。

  4. gksudo在请求密码时锁定键盘,鼠标和焦点,pkexec不会。在这两种情况下the keystrokes are sniffable though

  5. 使用pkexec,您可以在稍微更加卫生的环境中工作。

尝试例如:

cd /etc/init.d
sudo cat README
# and now the same with pkexec
pkexec cat README
# nice, huh?

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/977.html,未经允许,请勿转载。