简体   繁体   English

为什么很多服务器都改变它的uid和gid,有什么好处?

[英]Why many servers change its uid and gid,what's the benefit?

I see such logic in many open source projects:我在很多开源项目中看到了这样的逻辑:

if (setuid() == 0) {
   if (setgid(ccf->group) == -1) {
   ...
   if (initgroups(ccf->username, ccf->group) == -1) {

I have 2 questions on this:我对此有两个问题:

  1. What's the benifit to change to another gid and uid?换成另一个gid和uid有什么好处?
  2. And what's initgroups for? initgroups有什么用? IMO,to change gid and uid, setuid() and setgid() will be enough. IMO,要更改 gid 和 uid, setuid()setgid()就足够了。

Most of the time, system daemons are spawned by init scripts and therefore run as root .大多数时候,系统守护进程是由 init 脚本生成的,因此以root身份运行。 Calling setuid() and setgid() allows them to drop their superuser privileges and impersonate another user on the system (generally far less powerful than root ).调用setuid()setgid()允许他们放弃他们的超级用户权限并在系统上模拟另一个用户(通常远不如root强大)。 That way, bugs and security holes become less lethal to the system.这样,错误和安全漏洞对系统的致命性就会降低。

Concerning the second part of your question, initgroups() is called to reinitialize the group access list and add ccf->group to the list of groups that ccf->username belongs to.关于问题的第二部分,调用initgroups()来重新初始化组访问列表并将ccf->group添加到ccf->username所属的组列表中。 That's probably done because calling setgid() is not sufficient for the access rights associated with the new group to be propagated to the process.这可能是因为调用setgid()不足以将与新组关联的访问权限传播到进程。

Generally, you need administrative permission to listen on ports 1023 and below.通常,您需要管理权限才能侦听 1023 及以下端口。 (There are other reasons to start as administrator, but that's the big one.) But here's the thing: You can start as administrator, bind the socket, then drop down to be a user. (以管理员身份开始还有其他原因,但这是最重要的。)但事情是这样的:您可以以管理员身份开始,绑定套接字,然后下拉成为用户。

Now, why would you want to be a user?现在,你为什么想成为用户? Well, if you run with the smallest amount of permissions possible, and your program is compromised, then the damage will be contained.好吧,如果您以尽可能少的权限运行,并且您的程序受到损害,那么损害将得到控制。

On some OS setgid() toasts the supplementary groups.在某些操作系统上setgid()为补充组敬酒。 Calling initgroups() before setgid() is thus ineffective.因此在setgid() )之前调用initgroups() ) 是无效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM