<<
path:
root/public/0x2lib.git/html/tools/seccomp_collect_syscalls.py
blob: 2611bb92deee3746b87ae4489e055f59904b824d
[raw]
[clear marker]
2# This program records syscalls. Either from a specific point in your program
3# or just from the start.
5# It works in two steps: Recording syscalls and creating an white-list, ready to paste into your
8# In case you want to only restrict syscalls from a specific point on, set this marker
9# in any place of your program:
12# log("---- SECCOMP BOUNDARY ----");
16# First step, recording:
19# ./seccomp_collect_syscalls.py -r your_program <optional args for your program>
22# After that, it depends if you have a boundary marker in your program.
23# With boundary marker:
26# ./seccomp_collect_syscalls.py -p strace.txt
29# Without boundary marker:
32# ./seccomp_collect_syscalls.py -i -p strace.txt
35# Well, you can pass `-i` even with a boundary marker, it will ignore it anyway.
41from sys import exit, argv
42from pathlib import Path
48HELP = """Usage: syscalls.py [OPTIONS] [FP]
51 -r <program path> Records syscalls
52 -p <strace output file> Prints the used syscalls from your 'strace.txt'.
53 If the path is omitted, it looks after 'strace.txt'
54 in your current directory.
56 -i Ignore boundary marker when using `-p`
59STRACE_OUTPUT_FP = Path("strace.txt")
60SCMP_BOUNDARY_MARKER = "---- SECCOMP BOUNDARY ----"
71 if "h" in args[1] or "help" in args[1]:
77 print("Need filepath to program")
83 fp = STRACE_OUTPUT_FP if len(args) == 2 else args[2]
85 ignore_marker = "-i" in args
86 gather_unique_syscalls(fp)
88 print("Unknown argument")
92def strace_run(program):
103def gather_unique_syscalls(fp):
107 print("File does not exist:", fp)
110 content = open_file_or_exit(fp)
112 (i for i, s in enumerate(content) if SCMP_BOUNDARY_MARKER in s), None
115 if not ignore_marker:
116 if boundary_idx == None:
117 print("Cannot find seccomp boundary marker")
121 content = content[boundary_idx:]
127 for i, line in enumerate(content):
131 syscall = f"[ERR: at line {i + boundary_idx}]"
136 syscalls.add(syscall)
138 program_fn = os.path.basename(__file__)
139 print(f"/** Copy pasta from {program_fn} */")
140 print("SECCOMP_ALLOWED_SYSCALLS :: string.[")
142 for item in syscalls:
143 code = f' "{item.upper()}",'
152 subprocess.run(cmd, text=True, check=True)
153 except subprocess.CalledProcessError as e:
154 print(f"Command failed {e.returncode}: {e.stderr}")
158def open_file_or_exit(fp):
160 with open(fp, 'r', encoding="utf8") as f:
162 except Exception as e:
167if __name__ == "__main__":
172 except KeyboardInterrupt:
173 print("Terminated by user")
178# ------------------------------------------------------------------------------
179# This software is available under 2 licenses -- choose whichever you prefer.
180# ------------------------------------------------------------------------------
181# ALTERNATIVE A - MIT License
182# Copyright (c) 2026 Adam Blazeowsky
183# Permission is hereby granted, free of charge, to any person obtaining a copy of
184# this software and associated documentation files (the "Software"), to deal in
185# the Software without restriction, including without limitation the rights to
186# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
187# of the Software, and to permit persons to whom the Software is furnished to do
188# so, subject to the following conditions:
189# The above copyright notice and this permission notice shall be included in all
190# copies or substantial portions of the Software.
191# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
192# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
194# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
195# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
196# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
198# ------------------------------------------------------------------------------
199# ALTERNATIVE B - Public Domain (www.unlicense.org)
200# This is free and unencumbered software released into the public domain.
201# Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
202# software, either in source code form or as a compiled binary, for any purpose,
203# commercial or non-commercial, and by any means.
204# In jurisdictions that recognize copyright laws, the author or authors of this
205# software dedicate any and all copyright interest in the software to the public
206# domain. We make this dedication for the benefit of the public at large and to
207# the detriment of our heirs and successors. We intend this dedication to be an
208# overt act of relinquishment in perpetuity of all present and future rights to
209# this software under copyright law.
210# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
211# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
212# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
213# AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
214# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
215# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
216# ------------------------------------------------------------------------------