我正在使用evdev和uinput编写伪谱键盘重映射器here。我不想让我的普通用户有权读取和写入/ dev / input / event *和/ dev / uinput,所以我创建了一个新用户(hopr),其唯一目的是运行程序。在尝试使其发挥作用的同时,我注意到一些非常特殊的行为,我希望有人可以解释。
为了确保这是关于uinput而不是其他内容,我使用python-evdev编写了一个简单的测试脚本:
from evdev import UInput, ecodes
KEY_RELEASE = 0L
KEY_PRESS = 1L
ui = UInput()
ui.write(ecodes.EV_KEY, ecodes.KEY_A, KEY_PRESS)
ui.write(ecodes.EV_KEY, ecodes.KEY_A, KEY_RELEASE)
ui.syn()
ui.close()
我还创建了一个名为hopr的新用户和一个名为uinput的新组,并在组输入和输入(读取事件)中添加了hopr。计划是将/ dev / uinput的组更改为uinput,并且只为组提供rw permissio。这两个用户的组是:
user: user adm cdrom sudo dip plugdev lpadmin sambashare
hopr: hopr input uinput
首先,我尝试运行脚本而不更改组,并且在/etc/udev/rules.d中没有额外的规则。默认情况下,XUbuntu 16.04中/ dev / uinput的权限是用户root和组root的权限。
crw-rw----+ 1 root root 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # evdev.uinput.UInputError: "/dev/uinput" cannot be opened for writing
我有点惊讶地看到我的普通用户能够运行脚本但新用户却无法运行。接下来,我为所有人使用chmod 添加了rw权限:
crw-rw-rw-+ 1 root root 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # OK
好的,那是预期的。接下来,我再次删除了所有人的rw,并使用chown 将组更改为uinput :
crw-rw----+ 1 root uinput 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # evdev.uinput.UInputError: "/dev/uinput" cannot be opened for writing
同样,这是意料之外的,但它变得更糟。接下来,我再次为每个人添加了rw权限,但保留了组uinput
crw-rw-rw-+ 1 root uinput 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # evdev.uinput.UInputError: "/dev/uinput" cannot be opened for writing
现在我真的很困惑,但是使用chown 将组改回root 会让它再次起作用
crw-rw-rw-+ 1 root root 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # OK
显然我在这里有些不明白的事情,所以我也尝试使用/dev/udev/rules.d中的规则正确设置:
KERNEL=="uinput*", GROUP="uinput", MODE="0660"
令我惊讶的是,现在一切正常(几乎)如预期的那样!受限制的hopr用户可以在没有rw权限的情况下运行程序。唯一的问题是为什么我的普通用户也可以运行它。
crw-rw----+ 1 root uinput 10, 223 mar 23 09:58 /dev/uinput
user> python test.py # OK
hopr> python test.py # OK
所以,我的问题是: