"static_wrapper" imports in the FFI

Iavor Diatchki iavor.diatchki at gmail.com
Wed Mar 17 19:13:36 EDT 2010


Hello,

My point was that in many cases we can simply avoid run-time code
generation, rather then trying to work around the system's SE Linux
policy.  While I believe that using the foreign "export" gives us the
same functional behavior, the generated code is certainly not the
same.  I wrote a small benchmark to compare the two approaches and it
seems that the generated code is ~30% slower when using a foreign
export.  As I mentioned in a previous post, I think that this is
because of the extra indirection that we have to follow.  I am
attaching my GHC patch and the benchmark if anyone is interested in
playing around with it (Ganesh, thanks for the darcs tip---using
"darcs optimize --reorder" did the trick!)

-Iavor

PS: As a concrete example, Gtk2Hs uses numerous 'import "wrapper"'
declarations, which means that just adding a GUI to a Haskell app
requires you to allow making executable memory at run time.  I've had
fairly serious difficulties when trying to explain why this happens to
programmers who are not Haskell implementors.



On Wed, Mar 17, 2010 at 8:21 AM, Tyson Whitehead <twhitehead at gmail.com> wrote:
> On March 16, 2010 20:01:32 Iavor Diatchki wrote:
>> Optionally disabling executable heap blocks would be a separate patch.
>>  As far as I know, the only reason that the heap is executable is to
>> support the adjustor thunks used to implement "wrapper" imports.  The
>> "static_wrapper" patch provides a way to install Haskell callbacks in
>> many C libraries without the need for adjustor thunks.
>
> I believe this is the code in "rts/Adjustor.c" and "rts/sm/Storage.c".  It (or
> it gets ffi to) write a small bit of assembler that adds a hard coded pointer
> (to a StablePtr) to the argument list and jump to a hard coded address.  It
> then has to fiddle with the executable bits on the memory page it wrote the
> code into in order to allow the system the execute it.
>
> This leaves me to ask though, could you not also tighten up the security here
> by just getting the the system to turn off the writable bit when it also turns
> on the executable one?  I realize this implies that you will only get one of
> these per page, but still that might not be that bad if  you don't generate
> very many and recycle them.
>
> As a compromise, you could also just temporarily make pages writable when you
> add to them, thus greatly minimizing the attack window.  If you could get the
> OS could freeze all other threads while doing this there would be no window.
> If there generation and usage is/could be localized to OS threads, then
> modification would always be safe if OS thread works on their own page.
>
> I scanned the ghc source (all c, h, cmm, hs, and lhs files), and the only usage
> of import "wrappers" seems to be in System.Console.Terminfo.Base.
>
> Cheers!  -Tyson
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: static_wrapper_patch.tar.gz
Type: application/x-gzip
Size: 5644 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20100317/f2b55ebf/static_wrapper_patch.tar.gz


More information about the Glasgow-haskell-users mailing list