|
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 we are using. If the versions we are using are not internally consistent, we could run |
| 53 | + // into segfaults. |
| 54 | + |
| 55 | + if (PyXmlSec_GetLibXmlVersionMajor() != PyXmlSec_GetLibXmlCompiledVersionMajor() || |
| 56 | + PyXmlSec_GetLibXmlVersionMinor() != PyXmlSec_GetLibXmlCompiledVersionMinor()) { |
| 57 | + return -1; |
| 58 | + } |
| 59 | + |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +static int PyXmlSec_CheckLxmlLibraryVersion(void) { |
| 64 | + // Make sure that the version of libxml2 that lxml was compiled against is the same as the one |
| 65 | + // that we are using. Since we pass trees between the two libraries, if there is a version mismatch |
| 66 | + // we could run into segfaults. |
| 67 | + // See: https://github.com/xmlsec/python-xmlsec/issues/283 |
| 68 | + |
| 69 | + PyObject* lxml = NULL; |
| 70 | + PyObject* version = NULL; |
| 71 | + |
| 72 | + // Default to failure |
| 73 | + int result = -1; |
| 74 | + |
| 75 | + lxml = PyImport_ImportModule("lxml.etree"); |
| 76 | + if (lxml == NULL) { |
| 77 | + goto FINALIZE; |
| 78 | + } |
| 79 | + version = PyObject_GetAttrString(lxml, "LIBXML_VERSION"); |
| 80 | + if (version == NULL) { |
| 81 | + goto FINALIZE; |
| 82 | + } |
| 83 | + if (!PyTuple_Check(version) || PyTuple_Size(version) != 3) { |
| 84 | + goto FINALIZE; |
| 85 | + } |
| 86 | + |
| 87 | + PyObject* major = PyTuple_GetItem(version, 0); |
| 88 | + PyObject* minor = PyTuple_GetItem(version, 1); |
| 89 | + |
| 90 | + if (!PyLong_Check(major) || !PyLong_Check(minor)) { |
| 91 | + goto FINALIZE; |
| 92 | + } |
| 93 | + |
| 94 | + if (PyLong_AsLong(major) != PyXmlSec_GetLibXmlVersionMajor() || PyLong_AsLong(minor) != PyXmlSec_GetLibXmlVersionMinor()) { |
| 95 | + goto FINALIZE; |
| 96 | + } |
| 97 | + |
| 98 | + result = 0; |
| 99 | + |
| 100 | +FINALIZE: |
| 101 | + // Cleanup our references, and return the result |
| 102 | + Py_XDECREF(lxml); |
| 103 | + Py_XDECREF(version); |
| 104 | + return result; |
| 105 | +} |
20 | 106 |
|
21 | 107 | int PyXmlSec_InitLxmlModule(void) {
|
| 108 | + if (PyXmlSec_CheckLibXmlLibraryVersion() < 0) { |
| 109 | + PyXmlSec_SetLastError("xmlsec libxml2 library compiled version vs runtime version mismatch"); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + if (PyXmlSec_CheckLxmlLibraryVersion() < 0) { |
| 114 | + PyXmlSec_SetLastError("lxml & xmlsec libxml2 library version mismatch"); |
| 115 | + return -1; |
| 116 | + } |
| 117 | + |
22 | 118 | return import_lxml__etree();
|
23 | 119 | }
|
24 | 120 |
|
|
0 commit comments