Seccomp.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. var TEST_PROGRAM = [
  16. "#include <sys/resource.h>",
  17. "#include <sys/prctl.h>",
  18. "#include <linux/filter.h>",
  19. "#include <linux/seccomp.h>",
  20. "#include <linux/audit.h>",
  21. "#include <sys/syscall.h>",
  22. "int main() {",
  23. " return __NR_read",
  24. " | PR_SET_NO_NEW_PRIVS | PR_SET_SECCOMP | AUDIT_ARCH_X86_64",
  25. " | BPF_K | SECCOMP_MODE_FILTER;",
  26. "}"
  27. ].join('\n');
  28. var pushLinks = function (file, builder) {
  29. if (typeof(builder.config.HAS_SECCOMP) !== 'undefined') {
  30. if (builder.config.HAS_SECCOMP) {
  31. file.links.push("util/Seccomp.c");
  32. } else {
  33. file.links.push("util/Seccomp_dummy.c");
  34. }
  35. return true;
  36. }
  37. return false;
  38. };
  39. var detect = module.exports.detect = function (async, file, builder) {
  40. if (pushLinks(file, builder)) { return; }
  41. console.log("Searching for SECCOMP");
  42. var hasSeccomp = false;
  43. if (builder.config.systemName !== 'linux') {
  44. console.log("SECCOMP is only available on linux");
  45. } else if (process.env['Seccomp_NO']) {
  46. console.log("SECCOMP disabled");
  47. } else {
  48. var done = async();
  49. var CanCompile = require('../node_build/CanCompile');
  50. var cflags = [ builder.config.cflags, '-x', 'c' ];
  51. CanCompile.check(builder, TEST_PROGRAM, cflags, function (err, can) {
  52. builder.config.HAS_SECCOMP = !!can;
  53. if (!can) {
  54. console.log("Failed to get SECCOMP, compile failure: [" + err + "]");
  55. }
  56. pushLinks(file, builder);
  57. done();
  58. });
  59. return;
  60. }
  61. builder.config.HAS_SECCOMP = hasSeccomp;
  62. pushLinks(file, builder);
  63. };