Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mount-cgroup in Golang style #40

Merged
merged 7 commits into from Mar 9, 2022
Merged

Implement mount-cgroup in Golang style #40

merged 7 commits into from Mar 9, 2022

Conversation

kmahyyg
Copy link
Contributor

@kmahyyg kmahyyg commented Mar 9, 2022

feat(exp/mount_cgroup.go): completely fix #35 in golang-style

This implemented mount-cgroup exploit totally in Golang.

Detailed information:

  • Change whole "create-mount-write" process using Golang native Unix API.
  • To avoid conflict, increase length of random string from 3 to 4.
    Tasks sub-cgroup are also in the same kind of random name.
  • Read mounted filesystem information from /proc/self/mountinfo, instead of /etc/mtab .
  • Due to the limitation of exec.Command, output redirect is implemented in another way: manually get pid and write to cgroups.procs

Further enhancement:

  • To read the container filesystem path on the host, implement to read superBlockOptions of MountInfo.
  • To implement CVE-2022-0492 in this PR further, I modified EscapeCgroup function, so that it can be re-used for other subsystems.

Chores:

  • go mod tidy
  • Fix Typo
  • Run a much quicker random string generator
1b94046 (HEAD -> main, origin/main, origin/HEAD) feat(exp/mount_cgroup.go): completely fix #35 in golang-style
043d6b6 feat(util/cgroup.go): add superBlockOptions when parsing /proc/self/mountinfo
4c640ae fix(util/cgroup.go): typo: marjor -> major
60b44e5 fix(exp/mount-cgroup): fix #35 in shell-style in a simple way
ecfadba optimize(exp/mount-cgroup): update build constraint, cgroup is linux only
ef056df optimize(util/common.go): Quicker Random String Generator

Signed-off-by: kmahyyg <16604643+kmahyyg@users.noreply.github.com>
…only

Signed-off-by: kmahyyg <16604643+kmahyyg@users.noreply.github.com>
Signed-off-by: kmahyyg <16604643+kmahyyg@users.noreply.github.com>
Signed-off-by: kmahyyg <16604643+kmahyyg@users.noreply.github.com>
…ountinfo

Signed-off-by: kmahyyg <16604643+kmahyyg@users.noreply.github.com>
This implemented mount-cgroup exploit totally in Golang.

Detailed information:
- Change whole "create-mount-write" process using Golang native Unix API.
- To avoid conflict, increase length of random string from 3 to 4.
Tasks sub-cgroup are also in the same kind of random name.
- Read mounted filesystem information from /proc/self/mountinfo, instead of /etc/mtab .
- Due to the limitation of exec.Command, output redirect is implemented in another way: manually get pid and write to cgroups.proc

Further enhancement:
- To read the container filesystem path on the host, implement to read `superBlockOptions` of MountInfo.
- To implement CVE-2022-0492 in this PR further, I modified EscapeCgroup function, so that it can be re-used for other subsystems.

Chores:
- `go mod tidy`

Signed-off-by: kmahyyg <16604643+kmahyyg@users.noreply.github.com>
@kmahyyg
Copy link
Contributor Author

kmahyyg commented Mar 9, 2022

CleanShot 2022-03-09 at 20 40 49@2x

@neargle neargle self-requested a review March 9, 2022 13:13
@neargle
Copy link
Member

neargle commented Mar 9, 2022

Awesome! Elegant implementation ~ more elegant than half of the code I wrote in TODO.

@neargle neargle merged commit 05c907b into cdk-team:main Mar 9, 2022
Copy link
Member

@neargle neargle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM~


// trigger release
// sleep 2s for debug purpose
addProcCmd := exec.Command("/bin/sh", "-c", "sleep 2")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why sleep here?

Copy link
Contributor Author

@kmahyyg kmahyyg Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it need some time to read the PID of spawned /bin/sh, And also use /bin/sh -c echo $$ then put the command stdout to cgroup.procs will cause unknown reason error with exit code 1. So I use another way to achieve this.

You could also monitor this behaviour by adding auditd rule -a always,exit -F arch=b64 -S execve -F key=procmon which made debug much more easier than before.

@kmahyyg kmahyyg changed the title [Draft] Implement mount-cgroup in Golang style and CVE-2022-0492 Implement mount-cgroup in Golang style Mar 9, 2022
neargle pushed a commit that referenced this pull request Mar 10, 2022
* feat(exploit/abuse_unpriv_userns.go): exploit of CVE-2022-0942

co-operate with PR #40.

Signed-off-by: kmahyyg <16604643+kmahyyg@users.noreply.github.com>
Reviewer: neargle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mount-cgroup无法多次执行命令
2 participants