{"id":20882709,"url":"https://github.com/ochko/safeexec","last_synced_at":"2026-02-26T18:40:17.482Z","repository":{"id":502736,"uuid":"129946","full_name":"ochko/safeexec","owner":"ochko","description":"safe executor for programming contest auto judge system","archived":false,"fork":false,"pushed_at":"2024-09-06T14:54:24.000Z","size":53,"stargazers_count":25,"open_issues_count":0,"forks_count":22,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-01T09:05:14.097Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://coder.mn","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ochko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2009-02-16T10:22:38.000Z","updated_at":"2024-09-06T14:54:28.000Z","dependencies_parsed_at":"2024-11-18T07:34:35.489Z","dependency_job_id":"8b587e22-66fa-4dd2-8d42-574597a8bba4","html_url":"https://github.com/ochko/safeexec","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ochko%2Fsafeexec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ochko%2Fsafeexec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ochko%2Fsafeexec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ochko%2Fsafeexec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ochko","download_url":"https://codeload.github.com/ochko/safeexec/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253797963,"owners_count":21965984,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T07:33:08.798Z","updated_at":"2026-02-26T18:40:12.436Z","avatar_url":"https://github.com/ochko.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# safeexec(safe execution environment)\n\nA general-purpose lightweight sandbox for safely executing user programs.\nsafeexec provides a sandbox environment which you can set limits on system resources like memory, cpu time effectively preventing intended or unintented breach of system.\nHowever, running untrusted code on your server should be taken at *your own risk*.\n\n## How it works\n\n- Safeexec forks child process.\n- Sets various limitations on the child process via `setrlimit`. So safeexec should have setuid bit on, and uid root.\n- Parent process polls memory usage looking into `/proc` filesystem on linux or via `kvm` on freebsd.\n- Wall clock limit is enforced with alarm() \u0026 signal().\n- Reports usage stats to `stdout` or file specified by `--usage` argument.\n\n### Resource usage report format\n\nResource usage is in 4 line plain text.\n\nFor memory limit:\n```\nMemory Limit Exceeded\nelapsed time: 0 seconds\nmemory usage: 32884 kbytes\ncpu usage: 0.416 seconds\n```\n\nFor time limit:\n```\nTime Limit Exceeded\nelapsed time: 2 seconds\nmemory usage: 1424 kbytes\ncpu usage: 1.000 seconds\n```\n\nFile write attempt:\n```\nCommand exited with non-zero status (1)\nelapsed time: 0 seconds\nmemory usage: 64 kbytes\ncpu usage: 0.000 seconds\n```\n\n## Usage notes\n\nRunning it with no argument gives:\n\n```\nusage: ./safeexec \u003coptions\u003e --exec \u003ccommand\u003e\nAvailable options:\n\t--cpu     \u003cseconds\u003e           Default: 1 second(s)\n\t--mem     \u003ckbytes\u003e            Default: 32768 kbyte(s)\n\t--space   \u003ckbytes\u003e            Default: 0 kbyte(s)\n\t--uids    \u003cminuid\u003e \u003cmaxuid\u003e   Default: 5000-65535\n\t--minuid  \u003cuid\u003e               Default: 5000\n\t--maxuid  \u003cuid\u003e               Default: 65535\n\t--core    \u003ckbytes\u003e            Default: 0 kbyte(s)\n\t--nproc   \u003cnumber\u003e            Default: 0 proccess(es)\n\t--fsize   \u003ckbytes\u003e            Default: 8192 kbyte(s)\n\t--stack   \u003ckbytes\u003e            Default: 8192 kbyte(s)\n\t--clock   \u003cseconds\u003e           Wall clock timeout (default: 10)\n\t--usage   \u003cfilename\u003e          Report statistics to ... (default: stderr)\n\t--chroot  \u003cpath\u003e              Directory to chrooted (default: /tmp)\n\t--error   \u003cpath\u003e              Print stderr to file (default: /dev/null)\n```\n\nKeep in mind that `--exec` must be last option.\n\n### Memory limits\n\nFor compiled languages like C or C++ you can set strict memory limitation.\nBut for interpreted languages(python, ruby, java etc..) you can't set strict memory limits. Instead you have to give big enough memory that your language can load.\n\n### Unpriviliged uid range\n\nSafeexec runs users program as a random uid. Default gid/uid range is: 5000-65535.\nSo please check if any uid in this range is in use. If then you can chage the range with arguments. `--minuid` and `--maxuid`.\nWhole user range is treated as \"unpriviliged\" (\"other\" for all files).\n\n### Network access\n\nOn Freebsd network access blocked by setting limit on socket buffer size.\nBut on Linux it doesn't block network access. Use other tools(iptables,..) for network access blockage.\n\n### Number of open files\n\nMaximum number of open files is not limited because vm(java) or interpreted(ruby, python) languages open a lots of files.\n\n### Chroot\n\nFrom ubuntu's [BasicChroot](https://help.ubuntu.com/community/BasicChroot) page:\n\n\u003e A chroot is basically a special directory on your computer which prevents applications, if run from inside that directory, from accessing files outside the directory.\n\u003e In many ways, a chroot is like installing another operating system inside your existing operating system.\n\u003e Technically-speaking, chroot temporarily changes the root directory (which is normally /) to the chroot directory (for example, /var/chroot). As the root directory is the top of the filesystem hierarchy, applications are unable to access directories higher up than the root directory, and so are isolated from the rest of the system. This prevents applications inside the chroot from interfering with files elsewhere on your computer.\n\nYou can use safeexec without chroot if your OS doesn't allow arbitrary users to write filesystems.\n\n## Build\n\nClone this repository and run:\n\n```\n% cmake .\n% make\n```\n\n## Install\n\nYou'll need root to install binary to set root setuid bit.\n\n```\n% sudo make install\n```\n\n## Test\n\n`% sudo make permission \u0026\u0026 make test`\n\n```\nRunning tests...\nTest project /usr/home/ochko/safeexec\n    Start 1: memory-limit-exceeded\n1/8 Test #1: memory-limit-exceeded ............   Passed    1.16 sec\n    Start 2: time-limit-exceeded\n2/8 Test #2: time-limit-exceeded ..............   Passed    2.10 sec\n    Start 3: output-limit-exceeded\n3/8 Test #3: output-limit-exceeded ............   Passed    2.10 sec\n    Start 4: fork-attemp\n4/8 Test #4: fork-attemp ......................   Passed    0.59 sec\n    Start 5: file-write-attemp\n5/8 Test #5: file-write-attemp ................   Passed    0.13 sec\n    Start 6: file-read-attemp\n6/8 Test #6: file-read-attemp .................   Passed    0.13 sec\n    Start 7: return-code\n7/8 Test #7: return-code ......................   Passed    0.13 sec\n    Start 8: wall-time-limit-exceeded\n8/8 Test #8: wall-time-limit-exceeded .........   Passed    3.13 sec\n\n100% tests passed, 0 tests failed out of 8\n\nTotal Test time (real) =   9.48 sec\n```\n\n## Todo\n\n* Use getrusage for getting memory usage(becase this is same on both bsd and linux)\n* Fix file read/write permissions on linux when file owner is test runner and group permission is on:\n  - can read chmod 0640 file, but can't read 0620 file.\n  - can write chmod 0620 file, but can't write 0644 file.\n* Use [Getopt](http://www.gnu.org/software/libc/manual/html_node/Getopt.html) similar argument parsing library.\n\n## Changelog\n\n* FreeBSD support\n\n### May 17, 2015.\n\n* Fix linux specifics\n\n### Sep 23, 2014.\n\n* chmod/chown task for cmake\n* Add more test\n\n### Sep 8, 2014.\n\n* Introduce cmake\n* Terminate only process under controll. kill(pid,..) instead of kill(-1,...)\n\n### Apr 5-6, 2013\n\n* Apparmor profile for linux file read/write permission workaround.\n* setgit to protect files with group permission\n* Redirect stderr of user program\n\n### Dec 10, 2012.\n\n* Add address space limit\n* Error handling for permission setter\n\n### Dec 8, 2012.\n\n* FreeBSD support using `kvm.h`\n* Add some error handling\n\n### Feb 16, 2009.\n\n* Copied from mooshak's SafeExec.\n* Add chroot feature\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) Lkhagva Ochirkhuyag, 2009-2015\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fochko%2Fsafeexec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fochko%2Fsafeexec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fochko%2Fsafeexec/lists"}