Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370547
  • 博文数量: 56
  • 博客积分: 1449
  • 博客等级: 中尉
  • 技术积分: 822
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-08 10:24
文章分类

全部博文(56)

文章存档

2014年(7)

2012年(13)

2011年(10)

2010年(26)

分类: C/C++

2014-07-31 11:04:29

今天在看nginx代码的时候发现调用initgroups这个函数. man了一下对描述不甚了解. 翻阅APUE也没太搞懂到底有什么作用.

点击(此处)折叠或打开

  1. if (initgroups(ccf->username, ccf->group) == -1) {
联系到该处代码的上下文, 应该是与权限相关. 于是google了一下附加组ID.

点击(此处)折叠或打开

  1. This form of group membership persisted until it was changed in 4.2BSD (circa 1983). With 4.2BSD, the concept of supplementary group IDs was introduced. Not only did we belong to the group corresponding to the group ID in our password file entry, but we also could belong to up to 16 additional groups. The file access permission checks were modified so that not only was the effective group ID compared to the file's group ID, but also all the supplementary group IDs were compared to the file's group ID.
摘自~eweiss/222_book/222_book/0201433079/ch06lev1sec5.html

也就是说一个用户可以最多同时属于16个其他的组. 查看系统文件/etc/group, 该文件每行有四个字段.
group_name: encrpyted_passwd: group_id: group_member
第四个字段即为其他属于这个组的用户名. 这时候再来看man手册关于initgroups就比较容易理解了.

点击(此处)折叠或打开

  1. The initgroups() function initializes the group access list by reading the group database /etc/group and using all groups of which user is a member. The additional group group is also added to the list.
也就是说这个函数会去查看/etc/group, 找出调用进程所属的其他组加入到附加组, 并且将指定的group参数也加入到附加组.
但是其作用依旧晦涩难懂. 为此我写了些代码做点实验来理解. 注意initgroups函数只能由root用户或者有CAP_SETGID权限的进程调用.

既然这个函数是关于权限的. 那么首先我以root用户创建一个-rw-rw----权限的普通文件.

点击(此处)折叠或打开

  1. # touch testfile
  2. # chmod 660 testfile
该文件只有属于root用户或者root组的进程能够读写. 那么普通进程要打开这个文件一定会返回Permission denied错误. 假如一个普通进程的附加组里有root组ID.
那么该进程是可以访问这个文件的.于是我对/etc/group进行修改. 将我所使用的普通用户名加入到root组的组成员中. 不妨将用户名称为foobar.

点击(此处)折叠或打开

  1. root:X:0:foobar
但是foobar用户依然无法访问testfile文件. 猜想可能需要initgroups函数的介入才行了. 于是我以root用户写了一个set-user-id的程序.

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <grp.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. static char buf[BUFSIZ];
  9. /* 打印此时进程的各种ID */
  10. void show_id()
  11. {
  12.     uid_t uid = getuid();
  13.     gid_t gid = getgid();
  14.     uid_t euid = geteuid();
  15.     gid_t egid = getegid();
  16.     printf("UID: %d\n", uid);
  17.     printf("GID: %d\n", gid);
  18.     printf("EUID: %d\n", euid);
  19.     printf("EGID: %d\n", egid);
  20. }
  21. int main(int argc, char *argv[])
  22. {

  23.     int fd;
  24.     int n;
  25.     int i;
  26.     if (argc != 2) {
  27.         fprintf(stderr, "Usage %s filename\n", argv[0]);
  28.         return -1;
  29.     }
  30.     show_id();
  31.     /* euid = 0才能执行initgroups函数调用 */
  32.     if (geteuid() == 0) {
  33.         if (initgroups("foobar", getgid()) == -1) {
  34.             perror("initgroups failed");
  35.             return -1;
  36.         }
  37.         /* 由于是set-user-id程序, 还原到进程的真实ID */
  38.         if (setuid(getuid()) == -1) {
  39.             perror("setuid failed");
  40.             return -1;
  41.         }
  42.     }
  43.     show_id();
  44.     fd = open(argv[1], O_RDONLY);
  45.     if (fd == -1) {
  46.         perror("open failed");
  47.         return -1;
  48.     }
  49.     while ( (n = read(fd, buf, BUFSIZ)) > 0) {
  50.         write(STDIN_FILENO, buf, n);
  51.     }
  52.     return 0;
  53. }
以foobar身份运行这个程序可以访问并且输出testfile的内容. 说明这个进程的附加组进程中存在root组ID. 才得以通过权限的检查.
运行结果如下:

点击(此处)折叠或打开

  1. UID: 1001
  2. GID: 1001
  3. EUID: 0
  4. EGID: 1001
  5. UID: 1001
  6. GID: 1001
  7. EUID: 1001
  8. EGID: 1001
  9. hello world
到此: initgroups函数的意义就是讲组文件/etc/group中username参数所属的其他组ID以及group参数加入到进程的附加组ID当中.  关于组权限检查, 附加组ID和进程组ID是等效的.






阅读(4664) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~