Skip to content

Commit

Permalink
First minor push of security-related changes.
Browse files Browse the repository at this point in the history
* Check JRubyPermission("eval.string") for String evals
* Check JRubyPermission("eval.block") for Block evals
  • Loading branch information
headius committed Dec 13, 2011
1 parent 762fb99 commit b8f17f2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.jruby.runtime.builtin.Variable;
import org.jruby.runtime.component.VariableEntry;
import org.jruby.runtime.marshal.CoreObjectType;
import org.jruby.security.SecurityUtil;
import org.jruby.util.IdUtil;
import org.jruby.util.TypeConverter;
import org.jruby.util.log.Logger;
Expand Down Expand Up @@ -1815,6 +1816,8 @@ private Block setupBlock(Block block) {
* with this implementation.
*/
protected IRubyObject yieldUnder(final ThreadContext context, RubyModule under, Block block) {
SecurityUtil.checkEvalBlock(context);

context.preExecuteUnder(under, block);

Visibility savedVisibility = block.getBinding().getVisibility();
Expand Down
13 changes: 8 additions & 5 deletions src/org/jruby/evaluator/ASTInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyInstanceConfig.CompileMode;
import org.jruby.RubyLocalJumpError;
import org.jruby.RubyModule;
import org.jruby.RubyString;
Expand All @@ -44,19 +45,19 @@
import org.jruby.ast.util.ArgsUtil;
import org.jruby.common.IRubyWarnings.ID;
import org.jruby.exceptions.JumpException;
import org.jruby.interpreter.Interpreter;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Binding;
import org.jruby.runtime.Block;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.Frame;
import org.jruby.runtime.InterpretedBlock;
import org.jruby.runtime.RubyEvent;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.Binding;
import org.jruby.runtime.Frame;
import org.jruby.runtime.InterpretedBlock;
import org.jruby.security.SecurityUtil;
import org.jruby.util.ByteList;
import org.jruby.RubyInstanceConfig.CompileMode;
import org.jruby.interpreter.Interpreter;

public class ASTInterpreter {
public static IRubyObject INTERPRET_METHOD(
Expand All @@ -83,6 +84,7 @@ public static IRubyObject INTERPRET_METHOD(
}
}
public static IRubyObject INTERPRET_EVAL(Ruby runtime, ThreadContext context, Node node, String name, IRubyObject self, Block block) {
SecurityUtil.checkEvalString(context);
try {
ThreadContext.pushBacktrace(context, name, node.getPosition());
return node.interpret(runtime, context, self, block);
Expand All @@ -91,6 +93,7 @@ public static IRubyObject INTERPRET_EVAL(Ruby runtime, ThreadContext context, No
}
}
public static IRubyObject INTERPRET_EVAL(Ruby runtime, ThreadContext context, String file, int line, Node node, String name, IRubyObject self, Block block) {
SecurityUtil.checkEvalString(context);
try {
ThreadContext.pushBacktrace(context, name, file, line);
return node.interpret(runtime, context, self, block);
Expand Down
39 changes: 39 additions & 0 deletions src/org/jruby/security/JRubyPermission.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
******************************************************************************
* BEGIN LICENSE BLOCK *** Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public License Version
* 1.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.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in
* which case the provisions of the GPL or the LGPL are applicable instead of
* those above. If you wish to allow use of your version of this file only under
* the terms of either the GPL or the LGPL, and not to allow others to use your
* version of this file under the terms of the CPL, indicate your decision by
* deleting the provisions above and replace them with the notice and other
* provisions required by the GPL or the LGPL. If you do not delete the
* provisions above, a recipient may use your version of this file under the
* terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ****
******************************************************************************/

package org.jruby.security;

import java.security.BasicPermission;

public class JRubyPermission extends BasicPermission {
public static final JRubyPermission EVAL_STRING = new JRubyPermission("eval.string");
public static final JRubyPermission EVAL_BLOCK = new JRubyPermission("eval.block");

private String name;
public JRubyPermission(String name) {
super(name);
}
}
66 changes: 66 additions & 0 deletions src/org/jruby/security/SecurityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
******************************************************************************
* BEGIN LICENSE BLOCK *** Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public License Version
* 1.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.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in
* which case the provisions of the GPL or the LGPL are applicable instead of
* those above. If you wish to allow use of your version of this file only under
* the terms of either the GPL or the LGPL, and not to allow others to use your
* version of this file under the terms of the CPL, indicate your decision by
* deleting the provisions above and replace them with the notice and other
* provisions required by the GPL or the LGPL. If you do not delete the
* provisions above, a recipient may use your version of this file under the
* terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ****
******************************************************************************/

package org.jruby.security;

import org.jruby.runtime.ThreadContext;

/**
* Utility class for checking various JRuby permissions.
*/
public class SecurityUtil {
/**
* Check if the given thread (context) has permission to eval strings.
*
* @param context the thread context to confirm
*/
public static void checkEvalString(ThreadContext context) {
try {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(JRubyPermission.EVAL_STRING);
}
} catch (SecurityException se) {
throw context.runtime.newSecurityError(se.getLocalizedMessage());
}
}

/**
* Check if the given thread (context) has permission to eval blocks.
*
* @param context the thread context to confirm
*/
public static void checkEvalBlock(ThreadContext context) {
try {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(JRubyPermission.EVAL_BLOCK);
}
} catch (SecurityException se) {
throw context.runtime.newSecurityError(se.getLocalizedMessage());
}
}
}

0 comments on commit b8f17f2

Please sign in to comment.