diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index e8fb638bf31..c9c3eeb8974 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -79,7 +79,7 @@ final class Bootstrap { } /** initialize native resources */ - public static void initializeNatives(boolean mlockAll, boolean ctrlHandler) { + public static void initializeNatives(boolean mlockAll, boolean seccomp, boolean ctrlHandler) { final ESLogger logger = Loggers.getLogger(Bootstrap.class); // check if the user is running as root, and bail @@ -91,6 +91,11 @@ final class Bootstrap { } } + // enable secure computing mode + if (seccomp) { + Natives.trySeccomp(); + } + // mlockall if requested if (mlockAll) { if (Constants.WINDOWS) { @@ -134,7 +139,8 @@ final class Bootstrap { private void setup(boolean addShutdownHook, Settings settings, Environment environment) throws Exception { initializeNatives(settings.getAsBoolean("bootstrap.mlockall", false), - settings.getAsBoolean("bootstrap.ctrlhandler", true)); + settings.getAsBoolean("bootstrap.seccomp", true), + settings.getAsBoolean("bootstrap.ctrlhandler", true)); // initialize probes before the security manager is installed initializeProbes(); diff --git a/core/src/main/java/org/elasticsearch/bootstrap/BootstrapInfo.java b/core/src/main/java/org/elasticsearch/bootstrap/BootstrapInfo.java index 829c45f074e..76485bb86e5 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/BootstrapInfo.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/BootstrapInfo.java @@ -43,4 +43,11 @@ public final class BootstrapInfo { public static boolean isMemoryLocked() { return Natives.isMemoryLocked(); } + + /** + * Returns true if secure computing mode is enabled (linux/amd64 only) + */ + public static boolean isSeccompInstalled() { + return Natives.isSeccompInstalled(); + } } diff --git a/core/src/main/java/org/elasticsearch/bootstrap/JNANatives.java b/core/src/main/java/org/elasticsearch/bootstrap/JNANatives.java index 7a76d37bf12..647f923d5c7 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/JNANatives.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/JNANatives.java @@ -41,6 +41,8 @@ class JNANatives { // Set to true, in case native mlockall call was successful static boolean LOCAL_MLOCKALL = false; + // Set to true, in case native seccomp call was successful + static boolean LOCAL_SECCOMP = false; static void tryMlockall() { int errno = Integer.MIN_VALUE; @@ -170,4 +172,19 @@ class JNANatives { } } + static void trySeccomp() { + if (Constants.LINUX && "amd64".equals(Constants.OS_ARCH)) { + try { + Seccomp.installFilter(); + LOCAL_SECCOMP = true; + } catch (Exception e) { + // this is likely to happen unless the kernel is newish, its a best effort at the moment + // so we log stacktrace at debug for now... + if (logger.isDebugEnabled()) { + logger.debug("unable to install seccomp filter", e); + } + logger.warn("unable to install seccomp filter: " + e.getMessage()); + } + } + } } diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Natives.java b/core/src/main/java/org/elasticsearch/bootstrap/Natives.java index 05b0088ae11..1bf9bb80617 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Natives.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Natives.java @@ -88,4 +88,19 @@ final class Natives { } return JNANatives.LOCAL_MLOCKALL; } + + static void trySeccomp() { + if (!JNA_AVAILABLE) { + logger.warn("cannot install seccomp filters because JNA is not available"); + return; + } + JNANatives.trySeccomp(); + } + + static boolean isSeccompInstalled() { + if (!JNA_AVAILABLE) { + return false; + } + return JNANatives.LOCAL_SECCOMP; + } } diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Seccomp.java b/core/src/main/java/org/elasticsearch/bootstrap/Seccomp.java new file mode 100644 index 00000000000..409ebc2181f --- /dev/null +++ b/core/src/main/java/org/elasticsearch/bootstrap/Seccomp.java @@ -0,0 +1,271 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.bootstrap; + +import com.sun.jna.Library; +import com.sun.jna.Memory; +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import com.sun.jna.Structure; + +import org.apache.lucene.util.Constants; +import org.elasticsearch.common.logging.ESLogger; +import org.elasticsearch.common.logging.Loggers; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; +import java.util.List; + +/** + * Installs a limited form of Linux secure computing mode (filter mode). + * This filters system calls to block process execution. + *
+ * This is only supported on the amd64 architecture, on Linux kernels 3.5 or above, and requires + * {@code CONFIG_SECCOMP} and {@code CONFIG_SECCOMP_FILTER} compiled into the kernel. + *
+ * Filters are installed using either {@code seccomp(2)} (3.17+) or {@code prctl(2)} (3.5+). {@code seccomp(2)} + * is preferred, as it allows filters to be applied to any existing threads in the process, and one motivation + * here is to protect against bugs in the JVM. Otherwise, code will fall back to the {@code prctl(2)} method + * which will at least protect elasticsearch application threads. + *
+ * The filters will return {@code EACCES} (Access Denied) for the following system calls: + *
+ * This is not intended as a sandbox. It is another level of security, mostly intended to annoy
+ * security researchers and make their lives more difficult in achieving "remote execution" exploits.
+ * @see
+ * http://www.kernel.org/doc/Documentation/prctl/seccomp_filter.txt
+ */
+// only supported on linux/amd64
+// not an example of how to write code!!!
+final class Seccomp {
+ private static final ESLogger logger = Loggers.getLogger(Seccomp.class);
+
+ /** we use an explicit interface for native methods, for varargs support */
+ static interface LinuxLibrary extends Library {
+ /**
+ * maps to prctl(2)
+ */
+ int prctl(int option, long arg2, long arg3, long arg4, long arg5);
+ /**
+ * used to call seccomp(2), its too new...
+ * this is the only way, DONT use it on some other architecture unless you know wtf you are doing
+ */
+ long syscall(long number, Object... args);
+ };
+
+ // null if something goes wrong.
+ static final LinuxLibrary libc;
+
+ static {
+ LinuxLibrary lib = null;
+ try {
+ lib = (LinuxLibrary) Native.loadLibrary("c", LinuxLibrary.class);
+ } catch (UnsatisfiedLinkError e) {
+ logger.warn("unable to link C library. native methods (seccomp) will be disabled.", e);
+ }
+ libc = lib;
+ }
+
+ /** the preferred method is seccomp(2), since we can apply to all threads of the process */
+ static final int SECCOMP_SYSCALL_NR = 317; // since Linux 3.17
+ static final int SECCOMP_SET_MODE_FILTER = 1; // since Linux 3.17
+ static final int SECCOMP_FILTER_FLAG_TSYNC = 1; // since Linux 3.17
+
+ /** otherwise, we can use prctl(2), which will at least protect ES application threads */
+ static final int PR_GET_NO_NEW_PRIVS = 39; // since Linux 3.5
+ static final int PR_SET_NO_NEW_PRIVS = 38; // since Linux 3.5
+ static final int PR_GET_SECCOMP = 21; // since Linux 2.6.23
+ static final int PR_SET_SECCOMP = 22; // since Linux 2.6.23
+ static final int SECCOMP_MODE_FILTER = 2; // since Linux Linux 3.5
+
+ /** corresponds to struct sock_filter */
+ static final class SockFilter {
+ short code; // insn
+ byte jt; // number of insn to jump (skip) if true
+ byte jf; // number of insn to jump (skip) if false
+ int k; // additional data
+
+ SockFilter(short code, byte jt, byte jf, int k) {
+ this.code = code;
+ this.jt = jt;
+ this.jf = jf;
+ this.k = k;
+ }
+ }
+
+ /** corresponds to struct sock_fprog */
+ public static final class SockFProg extends Structure implements Structure.ByReference {
+ public short len; // number of filters
+ public Pointer filter; // filters
+
+ public SockFProg(SockFilter filters[]) {
+ len = (short) filters.length;
+ // serialize struct sock_filter * explicitly, its less confusing than the JNA magic we would need
+ Memory filter = new Memory(len * 8);
+ ByteBuffer bbuf = filter.getByteBuffer(0, len * 8);
+ bbuf.order(ByteOrder.nativeOrder()); // little endian
+ for (SockFilter f : filters) {
+ bbuf.putShort(f.code);
+ bbuf.put(f.jt);
+ bbuf.put(f.jf);
+ bbuf.putInt(f.k);
+ }
+ this.filter = filter;
+ }
+
+ @Override
+ protected List