Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to implement plugin framework - are DLLs the only way (C/C++ project)?

Introduction:

I am currently developing a document classifier software in C/C++ and I will be using Naive-Bayesian model for classification. But I wanted the users to use any algorithm that they want(or I want in the future), hence I went to separate the algorithm part in the architecture as a plugin that will be attached to the main app @ app start-up. Hence any user can write his own algorithm as a plugin and use it with my app.

Problem Statement:

The way I am intending to develop this is to have each of the algorithms that user wants to use to be made into a DLL file and put into a specific directory. And at the start, my app will search for all the DLLs in that directory and load them.

My Questions:

(1) What if a malicious code is made as a DLL (and that will have same functions mandated by plugin framework) and put into my plugins directory? In that case, my app will think that its a plugin and picks it and calls its functions, so the malicious code can easily bring down my entire app down (In the worst case could make my app as a malicious code launcher!!!).

(2) Is using DLLs the only way available to implement plugin design pattern? (Not only for the fear of malicious plugin, but its a generic question out of curiosity :) )

(3) I think a lot of softwares are written with plugin model for extendability, if so, how do they defend against such attacks?

(4) In general what do you think about my decision to use plugin model for extendability (do you think I should look at any other alternatives?)

Thank you

-MicroKernel :)

like image 589
Microkernel Avatar asked Dec 03 '22 05:12

Microkernel


2 Answers

  1. Do not worry about malicious plugins. If somebody managed to sneak a malicious DLL into that folder, they probably also have the power to execute stuff directly.

  2. As an alternative to DLLs, you could hook up a scripting language like Python or Lua, and allow scripted plugins. But maybe in this case you need the speed of compiled code?

    For embedding Python, see here. The process is not very difficult. You can link statically to the interpreter, so users won't need to install Python on their system. However, any non-builtin modules will need to be shipped with your application.

    However, if the language does not matter much to you, embedding Lua is probably easier because it was specifically designed for that task. See this section of its manual.

  3. See 1. They don't.

  4. Using a plugin model sounds like a fine solution, provided that a lack of extensibility really is a problem at this point. It might be easier to hard-code your current model, and add the plugin interface later, if it turns out that there is actually a demand for it. It is easy to add, but hard to remove once people started using it.

like image 72
Thomas Avatar answered Dec 06 '22 10:12

Thomas


Malicious code is not the only problem with DLLs. Even a well-meaning DLL might contain a bug that could crash your whole application or gradually leak memory.

Loading a module in a high-level language somewhat reduces the risk. If you want to learn about embedding Python for example, the documentation is here.

Another approach would be to launch the plugin in a separate process. It does require a bit more effort on your part to implement, but it's much safer. The seperate process approach is used by Google's Chrome web browser, and they have a document describing the architecture.

The basic idea is to provide a library for plugin writers that includes all the logic for communicating with the main app. That way, the plugin author has an API that they use, just as if they were writing a DLL. Wikipedia has a good list of ways for inter-process communication (IPC).

like image 32
Daniel Stutzbach Avatar answered Dec 06 '22 10:12

Daniel Stutzbach