From 6b528c26a05f1c3f969a9896328be408bcfd6064 Mon Sep 17 00:00:00 2001 From: NavaL Date: Tue, 31 May 2016 15:17:25 +0200 Subject: added custom c extension to set mutex locking for openssl rand generation --- service/extension/extension.c | 106 +++++++++++++++++++++++++++++++++++++++ service/pixelated/application.py | 29 +++++++++++ service/setup.py | 5 +- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 service/extension/extension.c diff --git a/service/extension/extension.c b/service/extension/extension.c new file mode 100644 index 00000000..4b695d72 --- /dev/null +++ b/service/extension/extension.c @@ -0,0 +1,106 @@ +#include "Python.h" + +#include "openssl/crypto.h" +#include "stdio.h" + +static PyObject *SpamError; + +static PyObject *IdCallback; +static PyObject *LockingCallback; + + +//-------------------------- + +static void locking_function(int mode, int n, const char * file, int line) +{ + PyObject *arglist; + PyObject *result; + + printf("Enter locking_function\n"); + + arglist = Py_BuildValue("(i, i, s, i)", mode, n, file, line); + result = PyObject_CallObject(LockingCallback, arglist); +// if(mode & CRYPTO_LOCK) +// +// result = PyObject_CallObject(IdCallback, arglist); +// else +// a--; + + Py_DECREF(arglist); + Py_DECREF(result); + + printf("Leave locking_function\n"); +} + +static unsigned long id_function(void) +{ + PyObject *arglist; + PyObject *result; + int value; + + arglist = Py_BuildValue(NULL); + result = PyObject_CallObject(IdCallback, arglist); + + if (!PyArg_ParseTuple(result, "i", &value)) + return 0; + + Py_DECREF(arglist); + Py_DECREF(result); + + return ((unsigned long)value); +} + + + +//-------------------------- + + + +static PyObject * +spam_system(PyObject *self, PyObject *args) +{ + const char *command; + int sts; + + if (!PyArg_ParseTuple(args, "s", &command)) + return NULL; + sts = system(command); + if (sts < 0) { + PyErr_SetString(SpamError, "System command failed"); + return NULL; + } + return PyLong_FromLong(sts); +} + +static PyObject * enable_mutexes(PyObject *self, PyObject *args) { + PyObject *pIdCallback, *pLockingCallback; + + if (!PyArg_UnpackTuple(args, "enable_mutexes", 2, 2, &pIdCallback, &pLockingCallback)) { + return NULL; + } + IdCallback = pIdCallback; + LockingCallback = pLockingCallback; + + CRYPTO_set_id_callback(id_function); + CRYPTO_set_locking_callback(locking_function); + + printf("Enabled mutexes\n"); + + Py_RETURN_NONE; +} + + +static PyMethodDef SpamMethods[] = { + {"system", spam_system, METH_VARARGS, + "Execute a shell command."}, + {"enable_mutexes", enable_mutexes, METH_VARARGS, + "Enable mutexes for openssl"}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + + +PyMODINIT_FUNC +initfoobar(void) +{ + (void) Py_InitModule("foobar", SpamMethods); +} diff --git a/service/pixelated/application.py b/service/pixelated/application.py index ce1e0258..8b128cd3 100644 --- a/service/pixelated/application.py +++ b/service/pixelated/application.py @@ -40,6 +40,10 @@ from pixelated.resources.root_resource import RootResource log = logging.getLogger(__name__) +from multiprocessing import Lock +from threading import current_thread +import foobar + class ServicesFactory(object): @@ -136,8 +140,33 @@ def _create_service_factory(args): else: return ServicesFactory(UserAgentMode(is_single_user=False)) +CRYPTO_LOCK=1 +CRYPTO_UNLOCK=2 +CRYPTO_READ=4 +CRYPTO_WRITE=8 + + +def idfunc(): + return current_thread().ident + + +locks = [Lock(), Lock(), Lock(), Lock(), Lock(),Lock(), Lock(), Lock(), Lock(), Lock()] + + +def lockfunc(mode, n, file, line): + if mode & CRYPTO_LOCK == CRYPTO_LOCK: + print "acquire lock %d" % n + locks[n].acquire() + elif mode & CRYPTO_UNLOCK == CRYPTO_UNLOCK: + print "release lock %d" % n + locks[n].release() + else: + print "unexpected call with mode %d and n %d" % (mode, n) + def initialize(): + foobar.enable_mutexes(idfunc, lockfunc) + log.info('Starting the Pixelated user agent') args = arguments.parse_user_agent_args() logger.init(debug=args.debug) diff --git a/service/setup.py b/service/setup.py index 53a8f715..b04e86c7 100644 --- a/service/setup.py +++ b/service/setup.py @@ -14,6 +14,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . +from setuptools import Extension from setuptools import setup import os @@ -55,4 +56,6 @@ setup(name='pixelated-user-agent', 'pixelated-register = pixelated.register:initialize' ] }, - include_package_data=True) + include_package_data=True, + ext_modules=[ + Extension("foobar", ["extension/extension.c"], libraries=["crypto"])]) -- cgit v1.2.3