20.12.2022 20:01 Frederick Virchanza Gotham kirjutas:
>
> I've written a GUI program for desktop PC's that acts as a 'man in the middle', it intercepts traffic and modifies it before forwarding it.
>
> I was thinking it would be cool to allow the user to write some C++ code in a text box in my GUI application to describe a text filter,
>
> So then I would compile this translation unit to a dynamic shared library, e.g. "custom_filter.dll" on MS-Windows or "libcustom_filter.so" on Linux/Mac. Then I would load this library into my program using "LoadLibrary" or "dlopen". I would compile the library with "-fsanitize" to make sure it dies as soon as there's a memory access violation.
You only get memory access violation if the program attempts to write
into non-mapped memory. Nothing prevents it to just write over your own
vital data structures in your own program. Suggesting to run it as a
separate process, not as a shared library.
>
> So the question is how can I make it as safe as possible?
You don't need to do any anything. If this is a desktop app, only the
desktop user can enter code pieces, and they can delete their files or
format their drives easily without the help of your program. You just
need to take care the compiled code runs with the same privileges than
the user has.
If your program has a web interface and untrusted remote users can enter
code, you are screwed anyway. There is no way to sanitize or sandbox a
piece of general C++ code, so there is no point to bother. If you really
want to do this, compile and run the program in a short-living docker
container, or something like that.