-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Run evaluation of inlined quotes in secured sandbox #4111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
nicolasstucki
wants to merge
3
commits into
scala:master
from
dotty-staging:add-splicer-security-manager
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package dotty.tools.dotc.util | ||
|
||
|
||
import java.lang.reflect.{InvocationTargetException, ReflectPermission} | ||
import java.security.Permission | ||
|
||
import scala.quoted.QuoteError | ||
|
||
object Sandbox { | ||
|
||
def runInSecuredThread[T](timeout: Int)(thunk: => T): T = { | ||
runWithSandboxSecurityManager { securityManager => | ||
class SandboxThread extends Thread { | ||
var result: scala.util.Try[T] = | ||
scala.util.Failure(new Exception("Sandbox failed with a fatal error")) | ||
override def run(): Unit = { | ||
result = scala.util.Try { | ||
securityManager.enable() // Enable security manager on this thread | ||
thunk | ||
} | ||
} | ||
} | ||
val thread = new SandboxThread | ||
thread.start() | ||
thread.join(timeout) | ||
if (thread.isAlive) { | ||
// TODO kill the thread? | ||
throw new InvocationTargetException(new QuoteError(s"Failed to evaluate inlined quote. Caused by timeout ($timeout ms).")) | ||
} else thread.result.fold[T](throw _, identity) | ||
} | ||
} | ||
|
||
private def runWithSandboxSecurityManager[T](run: SandboxSecurityManager => T): T = { | ||
val ssm: SandboxSecurityManager = synchronized { | ||
System.getSecurityManager match { | ||
case ssm: SandboxSecurityManager => | ||
assert(ssm.running > 0) | ||
ssm.running += 1 | ||
ssm | ||
case sm => | ||
assert(sm == null) | ||
val ssm = new SandboxSecurityManager | ||
System.setSecurityManager(ssm) | ||
ssm | ||
} | ||
} | ||
try run(ssm) | ||
finally synchronized { | ||
ssm.running -= 1 | ||
assert(ssm.running >= 0) | ||
if (ssm.running == 0) { | ||
assert(System.getSecurityManager eq ssm) | ||
System.setSecurityManager(null) | ||
} | ||
} | ||
} | ||
|
||
/** A security manager that can be enabled on individual threads. | ||
* | ||
* Inspired by https://github.com/alphaloop/selective-security-manager | ||
*/ | ||
private class SandboxSecurityManager extends SecurityManager { | ||
|
||
@volatile private[Sandbox] var running: Int = 1 | ||
|
||
private[this] val enabledFlag: ThreadLocal[Boolean] = new ThreadLocal[Boolean]() { | ||
override protected def initialValue(): Boolean = false | ||
} | ||
|
||
def enable(): Unit = { | ||
enabledFlag.set(true) | ||
} | ||
|
||
override def checkPermission(permission: Permission): Unit = { | ||
if (enabledFlag.get() && !isClassLoading) | ||
super.checkPermission(permission) | ||
} | ||
|
||
override def checkPermission(permission: Permission, context: Object): Unit = { | ||
if (enabledFlag.get()) | ||
super.checkPermission(permission, context) | ||
} | ||
|
||
private def isClassLoading: Boolean = { | ||
try { | ||
enabledFlag.set(false) // Disable security to do the check | ||
Thread.currentThread().getStackTrace.exists(elem => elem.getClassName == "java.lang.ClassLoader") | ||
} finally { | ||
enabledFlag.set(true) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import java.io.File | ||
|
||
import scala.quoted._ | ||
object Macros { | ||
inline def foo(): Int = ~fooImpl() | ||
def fooImpl(): Expr[Int] = { | ||
System.setSecurityManager(null) | ||
'(1) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Macros._ | ||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
Macros.foo() // error: Failed to evaluate inlined quote. Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "setSecurityManager") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import java.io.File | ||
|
||
import scala.quoted._ | ||
object Macros { | ||
inline def foo(): Int = ~fooImpl() | ||
def fooImpl(): Expr[Int] = { | ||
(new File("dfsdafsd")).exists() | ||
'(1) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Macros._ | ||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
Macros.foo() // error: Failed to evaluate inlined quote. Caused by: access denied ("java.util.PropertyPermission" "user.dir" "read") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import java.io.File | ||
|
||
import scala.quoted._ | ||
object Macros { | ||
inline def foo(): Int = ~fooImpl() | ||
def fooImpl(): Expr[Int] = { | ||
(new File("dfsdafsd")).createNewFile() | ||
'(1) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Macros._ | ||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
Macros.foo() // error: Failed to evaluate inlined quote. Caused by: access denied ("java.util.PropertyPermission" "user.dir" "read") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import java.io.File | ||
|
||
import scala.quoted._ | ||
object Macros { | ||
inline def foo(): Int = ~fooImpl() | ||
def fooImpl(): Expr[Int] = { | ||
while (true) () | ||
'(1) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Macros._ | ||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
Macros.foo() // error: Failed to evaluate inlined quote. Caused by timeout (3000 ms). | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any chance to add a test with an infinite loop to test this case?
Because Dotty can be embedded in build tools VMs, this means we must kill the thread I think.
I'd use
thread.interrupt()
first, and specify/hope that the thread must not catchInterruptedException
. If the thread doesn't handleinterrupt
by exiting, I'm not sure what's a sensible behavior — usingstop
mightleave monitors held hence cause a deadlockrelease monitors while data is inconsistent causing corruption, not usingstop
might leave a thread wasting CPU, so I suspect the thread should bestop
ed and the compiler should not be reused.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shared data that might be corrupted by
Thread.stop
includesAfter some discussion on Gitter and with Allan, I learned that build tools usually fork Scala compilers in the same JVM (though that's configurable) — they typically create fresh compiler instances (which is safer) though @smarter suggests it could be faster to reuse them sometimes (which is only safe for instances which didn't get corrupted).