[Linux] 与进程相关的ID

Linux下与 进程相关联的ID有这么三种:
1. 实际用户ID,实际组ID
这在用户登录系统时就确定了
2. 有效用户ID,有效用户组ID添加组ID
这一类ID决定着我们的进程运行时对文件的访问权限;
添加组ID是说,一个用户可以属于多个 组;
3. 设置-用户-ID,设置-组-ID
它们的作用是什么呢?简单的说就是让程序在执行时(exec)拥有一个和有效用户/组ID不同的副本。

一个比较好的例子是程序passwd能帮助用户修改密码,新的密码又必须保存到/etc/passwd中,而只有超级用户才拥有对该文件的写入权限?怎么办呢? 程序passwd拥有设置-用户-ID位:
ls `which passwd` -l
-rwsr-xr-x 1 root root 29104 2008-04-03 09:08 /usr/bin/passwd

当我们以普通用户身份运行passwd时,这可以让passwd拥有的有效用户ID变成超级用户,这样就可以完成对/etc/passwd的写操作了。
系统中类型拥有设置用户ID的程序还有ping。

命令行下可以使用chmod 完成对设置用户/组ID的修改,比如:chmod u+s file, chmod 4755 file

getuid/setuid, seteuid,setegid之类的函数可以帮助我们修改进程的各种ID,但实际上,setuid并非那么简单,他的行为:

1. If the process has superuser privileges, the setuid function sets the real user ID, effective user ID, and saved set-user-ID to uid;
2. If the process does not have superuser privileges, but uid equals either the real user ID or the saved set-user-ID, setuid sets only the effective user ID to uid. The real user ID and the saved set-user-ID are not changed.
3. If neither of these two conditions is true, errno is set to EPERM, and 1 is returned.

最后说一说今天刚看到一些关于脚本程序能否设置euid的讨论,
cat test-suid.sh
#!/bin/sh
echo world > /hello
 ll test-suid.sh
-rwxr-xr-x 1 root roger 30 2008-12-23 13:42 test-suid.sh
可以看到test-suid.sh有设置用户ID
但当我以普通用户身份运行test-suid.sh时,
roger@roger-desktop:~/sandbox$ ./test-suid.sh
./test-suid.sh: 2: cannot create /hello: Permission denied
我们只是得到了Permission Denied!
Linux下,对于脚本(包括perl,python)是忽略设置-用户/组ID位的!!!

注意,貌似Perl中如果想修改设置用户ID,得安装suidperl,这个我还没有尝试过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值