何时使用pkexec与gksu / gksudo?

有两种通用方式以图形方式以root身份运行应用程序(或者更常见的是,作为另一个用户)。 像gksugksudokdesudo这样的程序是sudo图形前端。 相比之下, pkexec是PolicyKit的图形前端。

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

虽然pkexec没有使用这种可配置性,但PolicyKit更pkexec配置。 此外, pkexec向用户显示将要启动的程序的完整路径,以便用户更确定将发生什么。 PolicyKit的所谓“策略”可用于设置更多预付款设置。 例如,是否应该记住密码。

我从pkexec手册中得到的pkexec

PROGRAM将运行它的环境将被设置为最小的已知和安全环境,以避免通过LD_LIBRARY_PATH或类似机制注入代码。 此外,PKEXEC_UID环境变量设置为调用pkexec的进程的用户标识。 因此,pkexec将不允许您作为另一个用户运行例如X11应用程序,因为未设置$ DISPLAY环境变量。

有关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    Examples for the PolicyKit Project http://hal.freedesktop.org/docs/PolicyKit/  Run the PolicyKit example program Frobnicate Kør PolicyKit eksemplet Frobnicate Authentication is required to run the PolicyKit example program Frobnicate Autorisering er påkrævet for at afvikle PolicyKit eksemplet Frobnicate audio-x-generic  no no auth_self_keep  /usr/bin/pk-example-frobnicate   and drop it in the /usr/share/polkit-1/actions directory under a suitable name (eg 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 (eg like any other well-written suid program). 

使用sudo,您可以设置每个用户和每个程序策略,以保留或重置sudo上下文中的调用者环境。 默认设置env_reset策略。

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

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

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

虽然pkexec可能不会对传递给PROGRAM的ARGUMENTS进行validation,但sudo确实具有此function。 虽然承认,你可以很容易搞砸这个,通常没有完成。

您可以稍微调整一下如何通过pkexec运行程序:图标,要显示的文本,甚至可以拥有本地化的东西等等。 根据具体情况,这确实很有趣。 可悲的是,有人觉得有必要为这个function重新发明轮子。 这可能是放入图形gtksudo / kdesu包装器的东西。

Policykit只是一个集中配置框架。 不幸的是不是一个漂亮的。 PKs XML文件比应用程序本身缺少二进制文件的任何东西都要复杂得多。 并且没有人会如此疯狂地使用二进制…哦gconf …没关系。

pkexecsudo及其前端pkexec不同之处:

  1. 您无法通过pkexec运行图形应用程序而无需明确配置它。
  2. 您可以稍微调整一下如何通过pkexec运行pkexec :图标,要显示的文本,是否记住密码,是否允许它以图形方式运行等等。
  3. 任何人都可以运行“运行为”超级用户(前提是他们可以进行身份​​validation),使用sudo你必须在管理员sudoers文件中列出。
  4. gksudo在要求输入密码时会锁定键盘,鼠标和焦点,而pkexec则没有。 在这两种情况下,击键都是可以嗅探的 。
  5. 使用pkexec您可以在稍微消毒的环境中工作。

试试例子:

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