如何为 Linux 用户设置 CAP_SYS_NICE 能力?

发布于 2024-12-08 03:10:38 字数 216 浏览 0 评论 0原文

我的程序使用 Linux 系统调用 setpriority() 来更改它创建的线程的优先级。它需要设置负优先级(-10),但是,正如文档中提到的,以普通用户身份运行时会失败。

用户需要 CAP_SYS_NICE 能力才能根据自己的需要设置优先级,但我不知道如何向用户提供这种能力。

所以我的问题是:如何为 Linux 用户设置 CAP_SYS_NICE 功能?

My program is using the Linux system call setpriority() to change the priorities of the threads it creates. It needs to set negative priorities (-10) but, as mentioned on the documentation, this fails when run as a normal user.

The user needs the CAP_SYS_NICE capability to be able to set the priorities as he wants, but I have no idea how to give such capability to the user.

So my question: how to set CAP_SYS_NICE capability to a Linux user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

赠我空喜 2024-12-15 03:10:38

有一个非常方便的实用程序用于设置二进制文件的功能:setcap。这需要在应用程序二进制文件上以 root 身份运行,但一旦设置,就可以以普通用户身份运行。示例:

$ sudo setcap 'cap_sys_nice=eip' <application>

您可以使用 getcap 确认应用程序具有哪些功能:

$ getcap <application>
<application> = cap_sys_nice+eip

我建议将这些功能集成到安装行中的 makefile 中,无论如何,该安装行通常以 root 身份运行。请注意,功能不能存储在 TAR 文件或任何衍生包格式中。如果您稍后打包应用程序,则需要一个脚本(Debian 软件包的 postinst)来在部署时应用该功能。

There is a nice handy utility for setting capabilities on a binary: setcap. This needs to be run as root on your application binary, but once set, can be run as a normal user. Example:

$ sudo setcap 'cap_sys_nice=eip' <application>

You can confirm what capabilities are on an application using getcap:

$ getcap <application>
<application> = cap_sys_nice+eip

I'd suggest integrating the capabilities into your makefile in the install line, which is typically run as root anyhow. Note that capabilities cannot be stored in a TAR file or any derivative package formats. If you do package your application later on, you will need a script (postinst for Debian packages) to apply the capability on deploy.

梦醒灬来后我 2024-12-15 03:10:38

Jan Hudec 是对的,进程不能只给自己一种能力,而 setuid 包装器是获得该能力的明显方法。另外,请记住,当您删除 root 权限时,您需要 prctl(PR_SET_KEEPCAPS, ...)。 (有关详细信息,请参阅 prctl 手册页。)否则,当您转换为非 root 真实用户 ID 时,您将放弃该功能。

如果您确实只想启动具有不同允许的良好级别的用户会话,您可能会看到 pam_limitslimits.conf 手册页,如 pam_limits code> 模块允许您更改硬性限制。它可能是这样的一行:

yourspecialusername hard nice -10

Jan Hudec is right that a process can't just give itself a capability, and a setuid wrapper is the obvious way get the capability. Also, keep in mind that you'll need to prctl(PR_SET_KEEPCAPS, ...) when you drop root. (See the prctl man page for details.) Otherwise, you'll drop the capability when you transition to your non-root real user id.

If you really just want to launch user sessions with a different allowed nice level, you might see the pam_limits and limits.conf man pages, as the pam_limits module allows you to change the hard nice limit. It could be a line like:

yourspecialusername hard nice -10
羞稚 2024-12-15 03:10:38

AFAIK 不可能获得能力。根进程拥有所有能力,可以放弃它们,但一旦放弃,就无法重新获得。因此,您需要一个 suid-root 包装器,它将放弃所有其他功能并运行该进程。

AFAIK It's not possible to get a capability. Root processes have all capabilities and can give them up, but once given up, they can't be regained. So you'll need a suid-root wrapper that will give up all other capabilities and run the process.

归属感 2024-12-15 03:10:38

关于 sudo,我像这样添加了用户:

niceuser ALL=NOPASSWD:/usr/bin/nice

然后它工作正常:

niceuser@localhost $ nice
0
niceuser@localhost $ sudo nice -n -10 nice
-10

Regarding sudo, I added the user like this:

niceuser ALL=NOPASSWD:/usr/bin/nice

And then it worked fine:

niceuser@localhost $ nice
0
niceuser@localhost $ sudo nice -n -10 nice
-10
心奴独伤 2024-12-15 03:10:38

如果您通过 sudo 获得 root 访问权限,
您可以使用 setpriv 将任何您想要的功能添加到命令中。
不过,使用它确实很痛苦,所以我为它编写了一个脚本。

#!/bin/bash

uid="$(id -u)"
gid="$(id -g)"

sudo --preserve-env \
    setpriv --reuid="$uid" --regid="$gid" --init-groups \
    --inh-caps +sys_nice --ambient-caps +sys_nice \
    "$@"

将其保存为 /usr/local/bin/chyortchmod +x 它。

要使用该脚本,请在需要实时权限的命令前加上 chyort 前缀:(

$ chyort jackd -d alsa -r 44100 -p 256

该名称的灵感来自 chrt 和 чёрт。)

If you have root access through sudo,
you can use setpriv to add any capability you want to a command.
Using it is a real pain in the ass, though, so I wrote a script for it.

#!/bin/bash

uid="$(id -u)"
gid="$(id -g)"

sudo --preserve-env \
    setpriv --reuid="$uid" --regid="$gid" --init-groups \
    --inh-caps +sys_nice --ambient-caps +sys_nice \
    "$@"

Save this as /usr/local/bin/chyort and chmod +x it.

To use the script, prefix the command that requires realtime permissions with chyort:

$ chyort jackd -d alsa -r 44100 -p 256

(The name was inspired by chrt and чёрт.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文