mueval: Safely evaluate pure Haskell expressions

[ bsd3, development, language, library, program ] [ Propose Tags ] [ Report a vulnerability ]

Mueval is a Haskell interpreter. It uses the GHC API to evaluate arbitrary Haskell expressions. Importantly, mueval takes many precautions to defang and avoid "evil" code. It uses resource limits, whitelisted modules and Safe Haskell, special Show instances for IO, threads, processes, and changes of directory to sandbox the Haskell code. . It is, in short, intended to be a standalone version of Lambdabot's famous evaluation functionality. For examples and explanations, please see the README file. . Mueval is POSIX-only.


[Skip to Readme]

Modules

[Last Documentation]

  • Mueval
    • Mueval.ArgsParse
    • Mueval.Context
    • Mueval.Interpreter
    • Mueval.Parallel
    • Mueval.Resources

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.2, 0.2.1, 0.3, 0.3.1, 0.4, 0.4.5, 0.4.6, 0.5, 0.5.1, 0.6, 0.6.1, 0.6.2, 0.6.3, 0.6.4, 0.7.0, 0.7.1, 0.8, 0.8.1, 0.8.2, 0.9, 0.9.1, 0.9.1.1, 0.9.1.1.2, 0.9.3, 0.9.4
Dependencies base (>=4.5 && <5), Cabal (>=3.8.1.0 && <3.9), containers (>=0.6.7 && <0.7), directory (>=1.3.7.1 && <1.4), extensible-exceptions (>=0.1.1.4 && <0.2), filepath (>=1.4.2.2 && <1.5), hint (>=0.9.0.7 && <0.10), mtl (>=2.2.2 && <2.3), mueval, process (>=1.6.16.0 && <1.7), QuickCheck (>=2.14.3 && <2.16), show (>=0.6 && <0.7), simple-reflect (>=0.3.3 && <0.4), unix (>=2.7.3 && <2.8) [details]
Tested with ghc ==6.10.1
License BSD-3-Clause
Author Gwern <gwern@gwern.net>
Maintainer Terence Ng <stoicism03@gmail.com>
Revised Revision 1 made by ncfavier at 2024-04-22T14:51:18Z
Category Development, Language
Home page https://github.com/TerenceNg03/mueval#readme
Bug tracker https://github.com/TerenceNg03/mueval/issues
Source repo head: git clone https://github.com/TerenceNg03/mueval
Uploaded by TerenceNg03 at 2023-12-04T21:54:55Z
Distributions Arch:0.9.3, Debian:0.9.3, FreeBSD:0.9.1.1.2, NixOS:0.9.4
Reverse Dependencies 5 direct, 1 indirect [details]
Executables mueval
Downloads 27174 total (105 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2023-12-05 [all 2 reports]

Readme for mueval-0.9.4

[back to package description]

What

Mueval grew out of my discontent with Lambdabot: it's really neat to be able to run expressions in #haskell like this:

07:53 < ivanm> > filter (\ x -> isLetter x || x == '\t') "asdf$#$     dfs"
07:55 < lambdabot>  "asdfdfs"

But Lambdabot is crufty and very difficult to install or run. IMO, we need a replacement or rewrite, but one of the things that make this difficult is that Lambdabot uses hs-plugins to get that sort of evaluation functionality, and hs-plugins is half the problem. We want some sort of standalone executable which provides that functionality. Now, ghc -e is obviously unsuited because there is no sandboxing, so what I've done is basically marry the GHC API (as rendered less sharp-edged by Hint) with a bunch of resource limits and sandboxing (as largely stolen from Lambdabot).

Examples

The end result is an adorable little program, which you can use like this:

$ mueval --expression '1*100+1'
Expression type: (Num t) => t
result: "101"

$ mueval --expression "filter (\`notElem\` ['A'..'Z']) \"abcXsdzWEE\""
Expression type: [Char]
result: "\"abcsdz\""

Note that mueval will avoid all the attacks I've been able to test on it:

$ mueval --expression 'let x = x in x'
Expression type: t
result: "mueval: Time limit exceeded"

$ mueval --expression "let foo = readFile \"/etc/passwd\" >>= print in foo"
Expression type: IO ()
result: "<IO ()>"

$ mueval --module System.IO.Unsafe --expression "let foo = unsafePerformIO readFile \"/etc/passwd\" in foo"
mueval: Unknown or untrusted module supplied! Aborting.

Loading definitions from files

Like Lambdabot, Mueval is capable of loading a file and its definitions. This is useful to get a kind of persistence. Suppose you have a file L.hs, with a function bar = (+1) in it; then mueval --loadfile=L.hs --expression="bar 1" will evaluate to, as one would expect, 2.

It's worth noting that definitions and module imports in the loaded are not fully checked like the expression is. The resource limits and timeouts still apply, but little else. So if you are dynamically adding functions and module imports, you must secure them yourself or accept the loss of security. Currently, all known 'evil' expressions cause Mueval to exit with an error (a non-zero exit code), so my advice is to do something like mueval --expression foo && echo "\n" >> L.hs && echo foo >> L.hs. (That is, only accept new expressions which evaluate successfully.)

Summary

Anyway, it's my hope that this will be useful as an example or useful in itself for people endeavoring to fix the Lambdabot situation or just in safely running code period.

Getting

You can download Mueval at Hackage: http://hackage.haskell.org/package/mueval. Mueval has a public Git repository, at https://github.com/gwern/mueval. Contributions & updates are of course welcomed.

Installing

Mueval depends on a few of the standard libraries, which you should have installed already, and also on the 'Hint' library http://hackage.haskell.org/package/hint; Hint is particularly essential as it is the very capable wrapper around the GHC API which Mueval uses. (Without Hint, this would've been much more painful to write). All of this is cabalized, so ideally installation will be as simple as:

$ cabal install mueval

However, you can still manually download and unpack the Mueval tarball, and do the usual Cabal dance:

$ runhaskell Setup configure
$ runhaskell Setup build
$ runhaskell Setup install

See also

  • Chris Done's interactive Haskell REPL website, Try Haskell!

Bugs

Mueval uses a number of techniques for security; particularly problematic seem to be the resource limits, as they have to be specified manually & statically in the source code and so will probably be broken somewhere somewhen. For this reason, they are not enabled by default. Experiment with --rlimits for hours of fun!

Mueval also cannot do qualified imports. This is due to limitations in the GHC API; see https://ghc.haskell.org/trac/ghc/ticket/1895 & https://ghc.haskell.org/trac/ghc/ticket/2362.

As of 2010 or so, compiling Mueval (or any Hint-using executable) with profiling support seems to lead to runtime crashes.

Finally, under GHC 6.10.1 (and higher?), you must run Mueval with +RTS -N2 -RTS as otherwise the watchdog threads will not get run and DoS attacks are possible. (Compare mueval -e "let x = x + 1 in x" against mueval -e "let x = x + 1 in x" +RTS -N2 -RTS.)

Contributions

So, you've discovered a bug or other infelicity? If you can successfully build & install Mueval, but running it on expressions leads to errors, please send me an email at gwern@gwern.net. Include in the email all the output you see if you run the informal test suite:

$ sh tests.sh

If this script does not terminate with a success message, then there's probably something wrong. One of the properties Mueval strives to have is that on every bad expression, it errors out with an exit code of 1, and on every good expression, an exit code of 0.

Also good is making sure cabal check and hlint are happy; but that's not as important as tests.sh passing.

License

BSD-3.