用户权限和组权限是 分作 多种方式来存放的。
uid:
实际用户ID,有效用户ID,保存的设置用户ID
gid:
实际组ID,有效组ID,保存的设置组ID
前面第四章 文件和目录中 4.4节中 有说明,与一个进程相关联的ID 有如下几个:
实际用户ID:告诉我们实际是谁,一般是用户登陆时,取自口令文件的ID
有效用户ID:主要是进程用于校验文件的使用权限
保存的设置用户ID:执行程序时保存有效用户ID的副本。
设置实际用户ID 和 有效用户ID /组
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
int setuid(uid_t uid);
DESCRIPTION
setuid() sets the effective user ID of the calling process. If the effective UID of the caller is root (more precisely: if the caller has the CAP_SETUID capability), the real UID and saved set-user-ID are also set.
设置 有效用户ID,如果是超级用户root,则也会设置 实际用户ID 和 保存的设置用户ID
更改三个 用户ID 的不同方法: exec / setuid(uid)
交换 实际用户ID 和 有效用户ID
设置 有效用户ID/组
超级用户root 和 非特权用户 两种用户 设置不同ID 的各个函数
setuid()实验:以谁的身份来执行什么样的操作
mhr@ubuntu:~/Desktop/xitongbiancheng/test$ ./a.out 0 cat /etc/shadow
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
int main(int argc,char* argv[])
{
pid_t pid;
if(argc < 3)// 至少三个参数
{
fprintf(stderr,"Usage: ...\n");
exit(1);
}
fflush(NULL);
pid = fork();
if(pid < 0)
{
perror("fork()");
exit(1);
}
else if(pid == 0) //child
{
setuid(atoi(argv[1]));//设置用户ID 为 root,前提是 root权限下执行该可执行程序
fflush(NULL);
execvp(argv[2],argv+2);//变参 用 execvp()
perror("execvp");
exit(1);
}
wait(NULL);
exit(0);
}
root@ubuntu:/home/mhr/Desktop/xitongbiancheng/test# ./a.out 0 cat /etc/shadow
前提是 root权限下执行该可执行程序
解释器文件
本质就是脚本文件,其起始行的形式是:
#! pathname [optional-argument]
常见的 解释器文件如:
#! /bin/sh
解释器文件 对后缀名不做要求,如:
p.exec
#! /bin/bash
ls
cat /etc/shadow
ps
mhr@ubuntu:~/Desktop/xitongbiancheng/test$
mhr@ubuntu:~/Desktop/xitongbiancheng/test$ chmod u+x p.exec // 给 可执行权限x
mhr@ubuntu:~/Desktop/xitongbiancheng/test$
mhr@ubuntu:~/Desktop/xitongbiancheng/test$
mhr@ubuntu:~/Desktop/xitongbiancheng/test$ ls -l p.exec
-rwxrw-r-- 1 mhr mhr 37 Feb 21 22:51 p.exec
mhr@ubuntu:~/Desktop/xitongbiancheng/test$
mhr@ubuntu:~/Desktop/xitongbiancheng/test$ ./p.exec
a.out p.exec test.c TEST_FILE wait.c
cat: /etc/shadow: Permission denied
PID TTY TIME CMD
7179 pts/2 00:00:00 bash
7671 pts/2 00:00:00 bash
7700 pts/2 00:00:00 p.exec
7703 pts/2 00:00:00 ps
mhr@ubuntu:~/Desktop/xitongbiancheng/test$
即执行的是 /bin/bash,用 /bin/bash 解释
ls
cat /etc/shadow
ps