sudo的工作原理

sudo的工作原理

此文不介绍sudoers文件的配置方法,只是简单介绍sudo工作流程。

工作流程

工作流程如下图,第一次用markdown写流程图,不太精细。

认证文件简介

认证文件可以理解为cookie,里面记录认证时间,在终端的PID,用户ID,终端的ttydev。
解析代码参考Github
认证文件以二进制保存,每个用户会有一个自己的cookie文件,放在/var/run/sudo/ts文件夹下,文件名为用户名。
文件保存结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct timestamp_entry_v1 {
unsigned short version; /* version number */
unsigned short size; /* entry size */
unsigned short type; /* TS_GLOBAL, TS_TTY, TS_PPID */
unsigned short flags; /* TS_DISABLED, TS_ANYUID */
uid_t auth_uid; /* uid to authenticate as */
pid_t sid; /* session ID associated with tty/ppid */
struct timespec ts; /* time stamp (CLOCK_MONOTONIC) */
union {
dev_t ttydev; /* tty device number */
pid_t ppid; /* parent pid */
} u;
};

struct timestamp_entry {
unsigned short version; /* version number */
unsigned short size; /* entry size */
unsigned short type; /* TS_GLOBAL, TS_TTY, TS_PPID */
unsigned short flags; /* TS_DISABLED, TS_ANYUID */
uid_t auth_uid; /* uid to authenticate as */
pid_t sid; /* session ID associated with tty/ppid */
struct timespec start_time; /* session/ppid start time */
struct timespec ts; /* time stamp (CLOCK_MONOTONIC) */
union {
dev_t ttydev; /* tty device number */
pid_t ppid; /* parent pid */
} u;
};

有两种保存方式,通过version进行区分,当version值为1时使用上面的结构体;剩下的使用下面的结构体。
type字段表示这下方的union中使用哪个字段,type会使用以下几个值:

1
2
3
4
#define TS_GLOBAL               0x01    /* not restricted by tty or ppid */
#define TS_TTY 0x02 /* restricted by tty */
#define TS_PPID 0x03 /* restricted by ppid */
#define TS_LOCKEXCL 0x04 /* special lock record */

flags字段自行研究吧,没有太关注。
auth_uid这个是授权用户的UID。
sid这个表示所在终端的PID,但是这个有一个东西需要注意,如果这个账户是通过别的账户su过来的,那么他的值为最上层终端的PID,不太清楚原因。