|  | 
| 9 | 9 | 
 | 
| 10 | 10 | #include "common.h" | 
| 11 | 11 | #include "lxml.h" | 
|  | 12 | +#include "exception.h" | 
| 12 | 13 | 
 | 
| 13 | 14 | #include <etree_defs.h> | 
| 14 | 15 | #include <etree_api.h> | 
|  | 
| 17 | 18 | #include <libxml/parser.h> | 
| 18 | 19 | #include <libxml/dict.h> | 
| 19 | 20 | 
 | 
|  | 21 | +#define XMLSEC_EXTRACT_VERSION(x, y) ((x / (y)) % 100) | 
|  | 22 | + | 
|  | 23 | +#define XMLSEC_EXTRACT_MAJOR(x) XMLSEC_EXTRACT_VERSION(x, 100 * 100) | 
|  | 24 | +#define XMLSEC_EXTRACT_MINOR(x) XMLSEC_EXTRACT_VERSION(x, 100) | 
|  | 25 | +#define XMLSEC_EXTRACT_PATCH(x) XMLSEC_EXTRACT_VERSION(x, 1) | 
|  | 26 | + | 
|  | 27 | +static long PyXmlSec_GetLibXmlVersionLong() { | 
|  | 28 | +    return PyOS_strtol(xmlParserVersion, NULL, 10); | 
|  | 29 | +} | 
|  | 30 | +long PyXmlSec_GetLibXmlVersionMajor() { | 
|  | 31 | +    return XMLSEC_EXTRACT_MAJOR(PyXmlSec_GetLibXmlVersionLong()); | 
|  | 32 | +} | 
|  | 33 | +long PyXmlSec_GetLibXmlVersionMinor() { | 
|  | 34 | +    return XMLSEC_EXTRACT_MINOR(PyXmlSec_GetLibXmlVersionLong()); | 
|  | 35 | +} | 
|  | 36 | +long PyXmlSec_GetLibXmlVersionPatch() { | 
|  | 37 | +    return XMLSEC_EXTRACT_PATCH(PyXmlSec_GetLibXmlVersionLong()); | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +long PyXmlSec_GetLibXmlCompiledVersionMajor() { | 
|  | 41 | +    return XMLSEC_EXTRACT_MAJOR(LIBXML_VERSION); | 
|  | 42 | +} | 
|  | 43 | +long PyXmlSec_GetLibXmlCompiledVersionMinor() { | 
|  | 44 | +    return XMLSEC_EXTRACT_MINOR(LIBXML_VERSION); | 
|  | 45 | +} | 
|  | 46 | +long PyXmlSec_GetLibXmlCompiledVersionPatch() { | 
|  | 47 | +    return XMLSEC_EXTRACT_PATCH(LIBXML_VERSION); | 
|  | 48 | +} | 
|  | 49 | + | 
|  | 50 | +static int PyXmlSec_CheckLibXmlLibraryVersion(void) { | 
|  | 51 | +    // Make sure that the version of libxml2 that we were compiled against is the same as the one | 
|  | 52 | +    // that is loaded. If there is a version mismatch, we could run into segfaults. | 
|  | 53 | + | 
|  | 54 | +    if (PyXmlSec_GetLibXmlVersionMajor() != PyXmlSec_GetLibXmlCompiledVersionMajor() || | 
|  | 55 | +        PyXmlSec_GetLibXmlVersionMinor() != PyXmlSec_GetLibXmlCompiledVersionMinor()) { | 
|  | 56 | +        return -1; | 
|  | 57 | +    } | 
|  | 58 | + | 
|  | 59 | +    return 0; | 
|  | 60 | +} | 
|  | 61 | + | 
|  | 62 | +static int PyXmlSec_CheckLxmlLibraryVersion(void) { | 
|  | 63 | +    // Make sure that the version of libxml2 lxml is using is the same as the one we are using. Because | 
|  | 64 | +    // we pass trees between the two libraries, we need to make sure that they are using the same version | 
|  | 65 | +    // of libxml2, or we could run into difficult to debug segfaults. | 
|  | 66 | +    // See: https://github.com/xmlsec/python-xmlsec/issues/283 | 
|  | 67 | + | 
|  | 68 | +    PyObject* lxml = NULL; | 
|  | 69 | +    PyObject* version = NULL; | 
|  | 70 | + | 
|  | 71 | +    // Default to failure | 
|  | 72 | +    int result = -1; | 
|  | 73 | + | 
|  | 74 | +    lxml = PyImport_ImportModule("lxml.etree"); | 
|  | 75 | +    if (lxml == NULL) { | 
|  | 76 | +        goto FINALIZE; | 
|  | 77 | +    } | 
|  | 78 | +    version = PyObject_GetAttrString(lxml, "LIBXML_VERSION"); | 
|  | 79 | +    if (version == NULL) { | 
|  | 80 | +        goto FINALIZE; | 
|  | 81 | +    } | 
|  | 82 | +    if (!PyTuple_Check(version) || PyTuple_Size(version) != 3) { | 
|  | 83 | +        goto FINALIZE; | 
|  | 84 | +    } | 
|  | 85 | + | 
|  | 86 | +    PyObject* major = PyTuple_GetItem(version, 0); | 
|  | 87 | +    PyObject* minor = PyTuple_GetItem(version, 1); | 
|  | 88 | + | 
|  | 89 | +    if (!PyLong_Check(major) || !PyLong_Check(minor)) { | 
|  | 90 | +        goto FINALIZE; | 
|  | 91 | +    } | 
|  | 92 | + | 
|  | 93 | +    if (PyLong_AsLong(major) != PyXmlSec_GetLibXmlVersionMajor() || PyLong_AsLong(minor) != PyXmlSec_GetLibXmlVersionMinor()) { | 
|  | 94 | +        goto FINALIZE; | 
|  | 95 | +    } | 
|  | 96 | + | 
|  | 97 | +    result = 0; | 
|  | 98 | + | 
|  | 99 | +FINALIZE: | 
|  | 100 | +    // Cleanup our references, and return the result | 
|  | 101 | +    Py_XDECREF(lxml); | 
|  | 102 | +    Py_XDECREF(version); | 
|  | 103 | +    return result; | 
|  | 104 | +} | 
| 20 | 105 | 
 | 
| 21 | 106 | int PyXmlSec_InitLxmlModule(void) { | 
|  | 107 | +    if (PyXmlSec_CheckLibXmlLibraryVersion() < 0) { | 
|  | 108 | +        PyXmlSec_SetLastError("xmlsec libxml2 library compiled version vs runtime version mismatch"); | 
|  | 109 | +        return -1; | 
|  | 110 | +    } | 
|  | 111 | + | 
|  | 112 | +    if (PyXmlSec_CheckLxmlLibraryVersion() < 0) { | 
|  | 113 | +        PyXmlSec_SetLastError("lxml & xmlsec libxml2 library version mismatch"); | 
|  | 114 | +        return -1; | 
|  | 115 | +    } | 
|  | 116 | + | 
| 22 | 117 |     return import_lxml__etree(); | 
| 23 | 118 | } | 
| 24 | 119 | 
 | 
|  | 
0 commit comments