Repository navigation
fix(kubernetes): 非特权 workload 显式禁止权限提升 #44
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
Open
zbb88888
wants to merge
2
commits into
gpustack:main
Choose a base branch
from
zbb88888:security/kubernetes-no-new-privs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/gpustack_runtime/deployer/test_privilege_escalation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from __future__ import annotations as __future_annotations__ | ||
|
|
||
| import kubernetes | ||
| import pytest | ||
|
|
||
| from gpustack_runtime.deployer.kubernetes import _set_privilege_escalation | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "security, expected", | ||
| [ | ||
| (None, False), | ||
| (kubernetes.client.V1SecurityContext(privileged=False), False), | ||
| (kubernetes.client.V1SecurityContext(privileged=True), True), | ||
| *[ | ||
| (kubernetes.client.V1SecurityContext( | ||
| capabilities=kubernetes.client.V1Capabilities(add=[value]), | ||
| ), True) | ||
| for value in ["SYS_ADMIN", "CAP_SYS_ADMIN", "sys_admin", "cap_sys_admin", "ALL", "CAP_ALL"] | ||
| ], | ||
| ( | ||
| kubernetes.client.V1SecurityContext( | ||
| capabilities=kubernetes.client.V1Capabilities(add=["IPC_LOCK"]), | ||
| run_as_user=1000, | ||
| ), | ||
| False, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_privilege_escalation_matches_final_security_context(security, expected): | ||
| container = kubernetes.client.V1Container(name="model", security_context=security) | ||
| _set_privilege_escalation(container) | ||
| assert container.security_context.allow_privilege_escalation is expected | ||
| if security is not None: | ||
| assert container.security_context is security | ||
| assert container.security_context.run_as_user == security.run_as_user |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 Kubernetes 中,用房声明的 Capability 可能会带有
CAP_前缀(例如CAP_SYS_ADMIN),或者使用ALL/CAP_ALL来启用所有权限。此外,这些字焦串可能是大小写不敏感的。如果用户指定了
CAP_SYS_ADMIN、ALL或CAP_ALL,当前的检查"SYS_ADMIN" in (capabilities.add or [])将无法识别,从而导致allow_privilege_escalation被错误地设置为False,这可能会导致容器因权限不足而无法正常运行。建议对 Capability 进行不区分大小写的处理,并同时支持
CAP_SYS_ADMIN、ALL和CAP_ALL。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修复。新增 capability 规范化:大小写不敏感,去除可选 CAP_ 前缀,并识别 SYS_ADMIN、CAP_SYS_ADMIN、ALL、CAP_ALL。其他 capability(如 IPC_LOCK)仍可显式关闭权限提升。新增 6 个覆盖前缀/大小写/ALL 的测试,连同既有特权测试共 23 项通过。未把不确定的 capability 字符串扩大为允许提权。提交已推送到 PR 分支。