有时需要用root运行kate,然而使用下面的命令:
sudo kate 文件名
会得到下面一段话:
Running Kate with sudo can cause bugs and expose you to security vulnerabilities
. Instead use Kate normally and you will be prompted for elevated privileges whe
n saving documents if needed.
也就是建议使用下面的命令运行:
kate 文件名
但是这样做只适用于/etc/fstab这种普通用户可以读取的文件,原因是kate打开时会用普通用户读取文件,保存时再通过polkit提示输入密码(su用的root密码或sudo用的当前用户密码)以root保存文件,然而如果是像/root/.bashrc这种的普通用户没有读取权限的文件,就会报错或者按照空文件打开。
有的教程建议使用下面的命令:
SUDO_EDITOR=kate sudoedit 文件名
然而这会生成临时文件再编辑,这会使得自动语法着色等功能失效,用起来体验也不是很好。
其实很简单,使用下面的命令即可成功以root打开kate:
sudo SUDO_USER= kate 文件名
如果是kdesu的话(kdesu已不推荐使用,因此不在$PATH中),可使用下面的命令:
/usr/lib/x86_64-linux-gnu/libexec/kf5/kdesu SUDO_USER= KDESU_USER= kate 文件名
原理是kate会检查当前UID是不是0(root),是的话会再检查SUDO_USER和KDESU_USER环境变量是否有值,如果有值的话说明是使用sudo或kdesu以root运行的,kate会拒绝运行,因此清除这两个环境变量即可绕过这个限制。
kwrite/dolphin也有类似的限制,同样可以使用这种方法以root运行。
注:
kate/kwrite/dolphin在2017年的17.04开始加入不允许以root运行的限制,但由于当时Kali等发行版会以root运行整个桌面(2020年Kali 2020.1改为默认非root运行桌面),这个改变造成了这些发行版不能正常使用kate/kwrite/dolphin,2018年的18.08改为不允许使用sudo和kdesu运行,但允许以root运行。
17.04、17.08、17.12、18.04这几个版本的kate/kwrite/dolphin使用任何方法均无法以root运行(当前仍在支持的系统中主要影响Ubuntu/Kubuntu 18.04以及其衍生版),除非重新编译或二进制修改,也可以安装并使用其它编辑器,或使用sudoedit等方式解决这个问题。
可使用下面的命令获得kate/kwrite/dolphin版本:
kate -v
kwrite -v
dolphin -v
参考资料:
kate源码(apps/kate/main.cpp或apps/lib/kateapp.cpp)
#if !defined(Q_OS_WIN) && !defined(Q_OS_HAIKU)
// Prohibit using sudo or kdesu (but allow using the root user directly)
if (getuid() == 0) {
setlocale(LC_ALL, "");
bindtextdomain("kate", KDE_INSTALL_FULL_LOCALEDIR);
if (!qEnvironmentVariableIsEmpty("SUDO_USER")) {
auto message = kli18n(
"Running this editor with sudo can cause bugs and expose you to security vulnerabilities. "
"Instead use this editor normally and you will be prompted for elevated privileges when "
"saving documents if needed.");
std::cout << dgettext("kate", message.untranslatedText()) << std::endl;
exit(EXIT_FAILURE);
} else if (!qEnvironmentVariableIsEmpty("KDESU_USER")) {
auto message = kli18n(
"Running this editor with kdesu can cause bugs and expose you to security vulnerabilities. "
"Instead use this editor normally and you will be prompted for elevated privileges when "
"saving documents if needed.");
std::cout << dgettext("kate", message.untranslatedText()) << std::endl;
exit(EXIT_FAILURE);
}
}
#endif
本文介绍了如何在Ubuntu及其衍生版的某些KDE文本编辑器(如kate、kwrite和dolphin)中,避免因root运行导致的安全漏洞。推荐使用正常方式获取权限,仅在必要时被提示,而非直接使用sudo或kdesu。
484

被折叠的 条评论
为什么被折叠?



