名字
pkexec - 以其他用户身份执行命令
概要
pkexec [--version] [--help]
pkexec [--用户用户名]程序[参数...]
描述
pkexec允许授权用户以其他用户身份执行程序。如果未指定用户名,则程序将以管理超级用户root 身份执行。
返回值
成功完成后,返回值是PROGRAM的返回值。如果调用进程未获得授权,或者无法通过身份验证获得授权或发生错误,则 pkexec将退出,返回值为 127。
安全说明
以其他用户身份执行程序是特权操作。默认情况下,所需的授权(请参阅"所需授权"部分)需要管理员身份验证。此外,呈现给用户的身份验证对话框将显示要执行的程序的完整路径,以便用户了解将发生的情况:
[图片][注1]
+----------------------------------------------------------+
| Authenticate [X] |
+----------------------------------------------------------+
| |
| [Icon] Authentication is needed to run '/bin/bash' |
| as the super user |
| |
| An application is attempting to perform an |
| action that requires privileges. Authentication |
| as the super user is required to perform this |
| action. |
| |
| Password for root: [_________________________] |
| |
| [V] Details: |
| Command: /bin/bash |
| Run As: Super User (root) |
| Action: org.freedesktop.policykit.exec |
| Vendor: The PolicyKit Project |
| |
| [Cancel] [Authenticate] |
+----------------------------------------------------------+
PROGRAM将运行它的环境将被设置为最小的已知和安全环境,以避免通过LD_LIBRARY_PATH或类似机制注入代码。此外,PKEXEC_UID环境变量设置为调用pkexec的进程的用户 ID。因此,pkexec将不允许您以其他用户身份运行例如X11应用程序,因为未设置$DISPLAY环境变量。
所需授权
默认情况下,除非相关程序存在操作定义文件,否则需要org.freedesktop.policykit.exec授权。要需要其他授权,可以使用org.freedesktop.policykit.exec.path注释对操作进行指定(有关详细信息,请参阅名为"示例"的部分)。
例
要指定以其他用户身份执行程序 /usr/bin/pk-example-frobnicate 所需的授权类型,只需编写一个类似这样的操作定义文件
<?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>
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>
并将其放在 /usr/share/polkit-1/actions 目录中,使用合适的名称(例如,匹配操作的命名空间)。请注意,除了指定程序之外,还可以指定身份验证消息、说明、图标和默认值。例如,对于上面定义的操作,将显示以下身份验证对话框:
[图片][注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] |
+----------------------------------------------------------+
如果用户使用的是da_DK区域设置,则对话框如下所示:
[图片][注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] |
+----------------------------------------------------------+
请注意,pkexec不会对传递给PROGRAM的参数进行验证。在正常情况下(每次使用pkexec都需要管理员身份验证),这不是问题,因为如果用户是管理员,他也可以运行pkexec bash来获取 root。
但是,如果使用用户可以保留授权的操作(或者如果用户已隐式授权),例如上面的 pk-example-frobnicate,则这可能是一个安全漏洞。因此,根据经验,更改了默认所需授权的程序永远不应该隐式信任用户输入(例如,像任何其他编写良好的suid程序一样)。