]> granicus.if.org Git - file/commitdiff
Add and enable libseccomp support (Paul Moore)
authorChristos Zoulas <christos@zoulas.com>
Sun, 24 Sep 2017 16:04:55 +0000 (16:04 +0000)
committerChristos Zoulas <christos@zoulas.com>
Sun, 24 Sep 2017 16:04:55 +0000 (16:04 +0000)
ChangeLog
configure.ac
src/Makefile.am
src/file.c
src/file.h
src/seccomp.c [new file with mode: 0644]

index 2063a23befeded521bfae60fce40ae6f33cc78d4..f76093c12a11b7f175004de750b70e1c1120371f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-09-24  12:02  Christos Zoulas <christos@zoulas.com>
+
+       * seccomp support (Paul Moore)
+
 2017-09-02  11:53  Christos Zoulas <christos@zoulas.com>
 
        * release 5.32
index 946198be643d669fd1a7394b40de21df14d8f22e..fee4eded8a7ad5cfa7650c1bd85fac742122c17f 100644 (file)
@@ -39,6 +39,11 @@ AC_ARG_ENABLE(zlib,
 [AS_HELP_STRING([--disable-zlib], [disable zlib compression support @<:@default=auto@:>@])])
 AC_MSG_RESULT($enable_zlib)
 
+AC_MSG_CHECKING(for libseccomp support)
+AC_ARG_ENABLE(libseccomp,
+[AS_HELP_STRING([--disable-libseccomp], [disable libseccomp sandboxing @<:@default=auto@:>@])])
+AC_MSG_RESULT($enable_libseccomp)
+
 AC_MSG_CHECKING(for file formats in man section 5)
 AC_ARG_ENABLE(fsect-man5,
 [  --enable-fsect-man5      enable file formats in man section 5],
@@ -158,6 +163,9 @@ dnl Checks for libraries
 if test "$enable_zlib" != "no"; then
   AC_CHECK_LIB(z, gzopen)
 fi
+if test "$enable_libseccomp" != "no"; then
+    AC_CHECK_LIB(seccomp, seccomp_init)
+fi
 if test "$MINGW" = 1; then
   AC_CHECK_LIB(gnurx,regexec,,AC_MSG_ERROR([libgnurx is required to build file(1) with MinGW]))
 fi
index 155aec44ee67be3584a3af629f9b5af77fafcd94..cbdccac2a61f5d1adfed57856154bb241e4e70aa 100644 (file)
@@ -19,7 +19,7 @@ MINGWLIBS =
 endif
 libmagic_la_LIBADD = $(LTLIBOBJS) $(MINGWLIBS)
 
-file_SOURCES = file.c
+file_SOURCES = file.c seccomp.c
 file_LDADD = libmagic.la
 CLEANFILES = magic.h
 EXTRA_DIST = magic.h.in
index 90e0b7e7ca6d9dd9eee8a414918be74b5f5b0341..5152f4234194655417d2b35aa3eb5d19ff159510 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.173 2017/09/18 20:40:10 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.174 2017/09/24 16:04:56 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -186,6 +186,15 @@ main(int argc, char *argv[])
 
        file_setprogname(progname);
 
+#ifdef HAVE_LIBSECCOMP
+#if 0
+       if (enable_sandbox_basic() == -1)
+#else
+       if (enable_sandbox_full() == -1)
+#endif
+               file_err(EXIT_FAILURE, "SECCOMP initialisation failed");
+#endif /* HAVE_LIBSECCOMP */
+
 #ifdef S_IFLNK
        posixly = getenv("POSIXLY_CORRECT") != NULL;
        flags |=  posixly ? MAGIC_SYMLINK : 0;
index 3a993a3e6c991a69ce29157603c63f4eddbc403a..79490508ce5045c03dbedc86d3912b0e8396e8bf 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.184 2017/09/18 20:40:10 christos Exp $
+ * @(#)$File: file.h,v 1.185 2017/09/24 16:04:56 christos Exp $
  */
 
 #ifndef __file_h__
@@ -590,6 +590,18 @@ const char *fmtcheck(const char *, const char *)
      __attribute__((__format_arg__(2)));
 #endif
 
+#ifdef HAVE_LIBSECCOMP
+// basic filter 
+// this mode should not interfere with normal operations
+// only some dangerous syscalls are blacklisted
+int enable_sandbox_basic(void);
+
+// enhanced filter 
+// this mode allows only the necessary syscalls used during normal operation
+// extensive testing required !!!
+int enable_sandbox_full(void);
+#endif
+
 protected const char *file_getprogname(void);
 protected void file_setprogname(const char *);
 protected void file_err(int, const char *, ...)
diff --git a/src/seccomp.c b/src/seccomp.c
new file mode 100644 (file)
index 0000000..88c7381
--- /dev/null
@@ -0,0 +1,246 @@
+/*
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice immediately at the beginning of the file, without modification,
+ *    this list of conditions, and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+/*
+ * libseccomp hooks.
+ */
+#include "file.h"
+
+#ifndef        lint
+FILE_RCSID("@(#)$File: seccomp.c,v 1.1 2017/09/24 16:04:56 christos Exp $")
+#endif /* lint */
+
+#if HAVE_LIBSECCOMP
+#include <seccomp.h> /* libseccomp */
+#include <sys/prctl.h> /* prctl */
+#include <sys/socket.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#define DENY_RULE(call) \
+    do \
+       if (seccomp_rule_add (ctx, SCMP_ACT_KILL, SCMP_SYS(call), 0) == -1) \
+           goto out; \
+    while (/*CONSTCOND*/0)
+#define ALLOW_RULE(call) \
+    do \
+       if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, SCMP_SYS(call), 0) == -1) \
+           goto out; \
+    while (/*CONSTCOND*/0)
+
+static scmp_filter_ctx ctx;
+
+
+int
+enable_sandbox_basic(void)
+{
+
+       if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
+               return -1;
+
+#if 0
+       // prevent escape via ptrace
+       prctl(PR_SET_DUMPABLE, 0);
+#endif
+
+       if (prctl (PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
+               return -1;
+
+       // initialize the filter
+       ctx = seccomp_init(SCMP_ACT_ALLOW);
+       if (ctx == NULL)
+           return 1;
+
+       DENY_RULE(_sysctl);
+       DENY_RULE(acct);
+       DENY_RULE(add_key);
+       DENY_RULE(adjtimex);
+       DENY_RULE(chroot);
+       DENY_RULE(clock_adjtime);
+       DENY_RULE(create_module);
+       DENY_RULE(delete_module);
+       DENY_RULE(fanotify_init);
+       DENY_RULE(finit_module);
+       DENY_RULE(get_kernel_syms);
+       DENY_RULE(get_mempolicy);
+       DENY_RULE(init_module);
+       DENY_RULE(io_cancel);
+       DENY_RULE(io_destroy);
+       DENY_RULE(io_getevents);
+       DENY_RULE(io_setup);
+       DENY_RULE(io_submit);
+       DENY_RULE(ioperm);
+       DENY_RULE(iopl);
+       DENY_RULE(ioprio_set);
+       DENY_RULE(kcmp);
+#ifdef __NR_kexec_file_load
+       DENY_RULE(kexec_file_load);
+#endif
+       DENY_RULE(kexec_load);
+       DENY_RULE(keyctl);
+       DENY_RULE(lookup_dcookie);
+       DENY_RULE(mbind);
+       DENY_RULE(nfsservctl);
+       DENY_RULE(migrate_pages);
+       DENY_RULE(modify_ldt);
+       DENY_RULE(mount);
+       DENY_RULE(move_pages);
+       DENY_RULE(name_to_handle_at);
+       DENY_RULE(open_by_handle_at);
+       DENY_RULE(perf_event_open);
+       DENY_RULE(pivot_root);
+       DENY_RULE(process_vm_readv);
+       DENY_RULE(process_vm_writev);
+       DENY_RULE(ptrace);
+       DENY_RULE(reboot);
+       DENY_RULE(remap_file_pages);
+       DENY_RULE(request_key);
+       DENY_RULE(set_mempolicy);
+       DENY_RULE(swapoff);
+       DENY_RULE(swapon);
+       DENY_RULE(sysfs);
+       DENY_RULE(syslog);
+       DENY_RULE(tuxcall);
+       DENY_RULE(umount2);
+       DENY_RULE(uselib);
+       DENY_RULE(vmsplice);
+
+       // blocking dangerous syscalls that file should not need
+       DENY_RULE (execve);
+       DENY_RULE (socket);
+       // ...
+
+       
+       // applying filter...
+       if (seccomp_load (ctx) == -1)
+               goto out;
+       // free ctx after the filter has been loaded into the kernel
+       seccomp_release(ctx);
+       return 0;
+       
+out:
+       seccomp_release(ctx);
+       return -1;
+}
+
+
+int
+enable_sandbox_full(void)
+{
+
+       // prevent child processes from getting more priv e.g. via setuid,
+       // capabilities, ...
+       if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
+               return -1;
+
+       if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
+               return -1;
+       
+       // initialize the filter
+       ctx = seccomp_init(SCMP_ACT_KILL);
+       if (ctx == NULL)
+               return -1;
+
+       ALLOW_RULE(access);
+       ALLOW_RULE(brk);
+       ALLOW_RULE(close);
+       ALLOW_RULE(dup2);
+       ALLOW_RULE(exit);
+       ALLOW_RULE(exit_group);
+       ALLOW_RULE(fcntl);  
+       ALLOW_RULE(fstat);
+       ALLOW_RULE(getdents);
+       ALLOW_RULE(ioctl);
+       ALLOW_RULE(lseek);
+       ALLOW_RULE(lstat);
+       ALLOW_RULE(mmap);
+       ALLOW_RULE(mprotect);
+       ALLOW_RULE(mremap);
+       ALLOW_RULE(munmap);
+       ALLOW_RULE(open);
+       ALLOW_RULE(openat);
+       ALLOW_RULE(pread64);
+       ALLOW_RULE(read);
+       ALLOW_RULE(rt_sigaction);
+       ALLOW_RULE(rt_sigprocmask);
+       ALLOW_RULE(rt_sigreturn);
+       ALLOW_RULE(select);
+       ALLOW_RULE(stat);
+       ALLOW_RULE(sysinfo);
+       ALLOW_RULE(unlink);
+       ALLOW_RULE(write);
+
+
+#if 0
+       // needed by valgrind
+       ALLOW_RULE(gettid);
+       ALLOW_RULE(getpid);
+       ALLOW_RULE(readlink);
+       ALLOW_RULE(rt_sigtimedwait);
+#endif
+
+#if 0
+        /* special restrictions for socket, only allow AF_UNIX/AF_LOCAL */
+        if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
+            SCMP_CMP(0, SCMP_CMP_EQ, AF_UNIX)) == -1)
+               goto out;
+
+        if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
+            SCMP_CMP(0, SCMP_CMP_EQ, AF_LOCAL)) == -1)
+               goto out;
+
+
+        /* special restrictions for open, prevent opening files for writing */
+        if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
+            SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) == -1)
+               goto out;
+
+        if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
+            SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) == -1)
+               goto out;
+
+        if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
+            SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) == -1)
+               goto out;
+
+
+        /* allow stderr */
+        if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
+            SCMP_CMP(0, SCMP_CMP_EQ, 2)) == -1)
+                goto out;
+#endif
+
+       // applying filter...
+       if (seccomp_load(ctx) == -1)
+               goto out;
+       // free ctx after the filter has been loaded into the kernel
+       seccomp_release(ctx);
+       return 0;
+
+out:
+       // something went wrong
+       seccomp_release(ctx);
+       return -1;
+}
+#endif