为什么在使用 system() 调用管理程序的 setuid-root C 程序中需要 setuid(0)?

发布于 2024-07-25 03:48:38 字数 534 浏览 3 评论 0原文

我必须为某人做一次肮脏的 Linux 黑客攻击,这样他们就可以在非 root 用户身份的情况下使用 cupsenable Printername shell 命令启动打印机。 我不希望他们能够以根身份使用整个 cupsenable 语法,因此我只编写了一个 C 包装器来清理 argv[1] 中的输入并调用system("cupsenable sanitizedprintername")

我将程序设为 setuid root,但即便如此,cupsenable 仍因“权限被拒绝”而失败。 然后我在 system() 之前插入了一个 setuid(0) 调用,你瞧,它起作用了。

忽略是否​​有更好的方法让用户控制打印机的问题。 可能有更好的方法。 我感兴趣的是 chmod u+ssetuid(0)system() 的复杂性。 为什么它会这样?

I had to do a dirty Linux hack for somebody so they could start a printer with the cupsenable printername shell command while being a non-root user. I didn't want them to be able to use the entirety of the cupsenable syntax as root, so I just wrote a C wrapper that sanitizes the input in argv[1] and calls system("cupsenable sanitizedprintername").

I made the program setuid root, but even so, cupsenable failed with "permission denied". Then I inserted a setuid(0) call before system() and, lo and behold, it worked.

Disregard the issue of there being a better way to give users control of the printer. There probably is a better way. What I'm interested in are the intricacies of chmod u+s vs. setuid(0) vs. system(). Why did it behave that way?

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

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

发布评论

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

评论(1

¢好甜 2024-08-01 03:48:38

来自man系统

不要在具有 set-user-ID 或 set-group-ID 权限的程序中使用 system(),因为某些环境变量的奇怪值可能会被用来破坏系统完整性。 请改用 exec(3) 系列函数,但不要使用 execlp(3)execvp(3)。 事实上,在 /bin/sh 为 bash 的系统上,system() 无法在具有 set-user-ID 或 set-group-ID 权限的程序中正常工作版本 2,因为 bash 2 在启动时放弃特权。

来自 man bash

如果 shell 启动时有效用户(组)id 不等于真实用户(组)id,并且未提供 -p 选项,则不会读取启动文件,shell函数不是从环境继承的,SHELLOPTS 变量如果出现在环境中,将被忽略,有效用户 id 将设置为真实用户 id。

看来您的 setuid(0) 调用绕过了该保护。

From man system:

Do not use system() from a program with set-user-ID or set-group-ID privileges, because strange values for some environment variables might be used to subvert system integrity. Use the exec(3) family of functions instead, but not execlp(3) or execvp(3). system() will not, in fact, work properly from programs with set-user-ID or set-group-ID privileges on systems on which /bin/sh is bash version 2, since bash 2 drops privileges on startup.

And from man bash:

If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS variable, if it appears in the environment, is ignored, and the effective user id is set to the real user id.

It appears your setuid(0) call circumvented that protection.

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