0003-memory-errors-CVE2012-1502.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. [PATCH] Fix Double Free Corruption (CVE2012-1502)
  2. Downloaded from:
  3. http://pkgs.fedoraproject.org/cgit/PyPAM.git/plain/PyPAM-0.5.0-memory-errors.patch
  4. For details, see: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-1502
  5. Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
  6. diff -up PyPAM-0.5.0/PAMmodule.c.memory PyPAM-0.5.0/PAMmodule.c
  7. --- PyPAM-0.5.0/PAMmodule.c.memory 2012-05-07 17:22:54.503914026 +0200
  8. +++ PyPAM-0.5.0/PAMmodule.c 2012-05-07 17:23:15.644381942 +0200
  9. @@ -37,33 +37,48 @@ static void PyPAM_Err(PyPAMObject *self,
  10. err_msg = pam_strerror(self->pamh, result);
  11. error = Py_BuildValue("(si)", err_msg, result);
  12. - Py_INCREF(PyPAM_Error);
  13. PyErr_SetObject(PyPAM_Error, error);
  14. + Py_XDECREF(error);
  15. }
  16. static int PyPAM_conv(int num_msg, const struct pam_message **msg,
  17. struct pam_response **resp, void *appdata_ptr)
  18. {
  19. - PyObject *args;
  20. -
  21. + PyObject *args, *msgList, *respList, *item;
  22. + struct pam_response *response, *spr;
  23. PyPAMObject* self = (PyPAMObject *) appdata_ptr;
  24. +
  25. if (self->callback == NULL)
  26. return PAM_CONV_ERR;
  27. Py_INCREF(self);
  28. - PyObject* msgList = PyList_New(num_msg);
  29. -
  30. + msgList = PyList_New(num_msg);
  31. + if (msgList == NULL) {
  32. + Py_DECREF(self);
  33. + return PAM_CONV_ERR;
  34. + }
  35. +
  36. for (int i = 0; i < num_msg; i++) {
  37. - PyList_SetItem(msgList, i,
  38. - Py_BuildValue("(si)", msg[i]->msg, msg[i]->msg_style));
  39. + item = Py_BuildValue("(si)", msg[i]->msg, msg[i]->msg_style);
  40. + if (item == NULL) {
  41. + Py_DECREF(msgList);
  42. + Py_DECREF(self);
  43. + return PAM_CONV_ERR;
  44. + }
  45. + PyList_SetItem(msgList, i, item);
  46. }
  47. -
  48. +
  49. args = Py_BuildValue("(OO)", self, msgList);
  50. - PyObject* respList = PyEval_CallObject(self->callback, args);
  51. + if (args == NULL) {
  52. + Py_DECREF(self);
  53. + Py_DECREF(msgList);
  54. + return PAM_CONV_ERR;
  55. + }
  56. + respList = PyEval_CallObject(self->callback, args);
  57. Py_DECREF(args);
  58. Py_DECREF(self);
  59. -
  60. +
  61. if (respList == NULL)
  62. return PAM_CONV_ERR;
  63. @@ -71,11 +86,15 @@ static int PyPAM_conv(int num_msg, const
  64. Py_DECREF(respList);
  65. return PAM_CONV_ERR;
  66. }
  67. -
  68. - *resp = (struct pam_response *) malloc(
  69. +
  70. + response = (struct pam_response *) malloc(
  71. PyList_Size(respList) * sizeof(struct pam_response));
  72. + if (response == NULL) {
  73. + Py_DECREF(respList);
  74. + return PAM_CONV_ERR;
  75. + }
  76. + spr = response;
  77. - struct pam_response* spr = *resp;
  78. for (int i = 0; i < PyList_Size(respList); i++, spr++) {
  79. PyObject* respTuple = PyList_GetItem(respList, i);
  80. char* resp_text;
  81. @@ -85,7 +104,7 @@ static int PyPAM_conv(int num_msg, const
  82. free((--spr)->resp);
  83. --i;
  84. }
  85. - free(*resp);
  86. + free(response);
  87. Py_DECREF(respList);
  88. return PAM_CONV_ERR;
  89. }
  90. @@ -95,7 +114,8 @@ static int PyPAM_conv(int num_msg, const
  91. }
  92. Py_DECREF(respList);
  93. -
  94. + *resp = response;
  95. +
  96. return PAM_SUCCESS;
  97. }
  98. @@ -122,7 +142,11 @@ static PyObject * PyPAM_pam(PyObject *se
  99. PyPAMObject_Type.ob_type = &PyType_Type;
  100. p = (PyPAMObject *) PyObject_NEW(PyPAMObject, &PyPAMObject_Type);
  101. + if (p == NULL)
  102. + return NULL;
  103. +
  104. if ((spc = (struct pam_conv *) malloc(sizeof(struct pam_conv))) == NULL) {
  105. + Py_DECREF((PyObject *)p);
  106. PyErr_SetString(PyExc_MemoryError, "out of memory");
  107. return NULL;
  108. }
  109. @@ -455,9 +479,15 @@ static PyObject * PyPAM_getenvlist(PyObj
  110. }
  111. retval = PyList_New(0);
  112. + if (retval == NULL)
  113. + return NULL;
  114. while ((cp = *(result++)) != NULL) {
  115. entry = Py_BuildValue("s", cp);
  116. + if (entry == NULL) {
  117. + Py_DECREF(retval);
  118. + return NULL;
  119. + }
  120. PyList_Append(retval, entry);
  121. Py_DECREF(entry);
  122. }