devm_free.cocci 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /// Find uses of standard freeing functons on values allocated using devm_
  2. /// functions. Values allocated using the devm_functions are freed when
  3. /// the device is detached, and thus the use of the standard freeing
  4. /// function would cause a double free.
  5. /// See Documentation/driver-model/devres.txt for more information.
  6. ///
  7. /// A difficulty of detecting this problem is that the standard freeing
  8. /// function might be called from a different function than the one
  9. /// containing the allocation function. It is thus necessary to make the
  10. /// connection between the allocation function and the freeing function.
  11. /// Here this is done using the specific argument text, which is prone to
  12. /// false positives. There is no rule for the request_region and
  13. /// request_mem_region variants because this heuristic seems to be a bit
  14. /// less reliable in these cases.
  15. ///
  16. // Confidence: Moderate
  17. // Copyright: (C) 2011 Julia Lawall, INRIA/LIP6. GPLv2.
  18. // Copyright: (C) 2011 Gilles Muller, INRIA/LiP6. GPLv2.
  19. // URL: http://coccinelle.lip6.fr/
  20. // Comments:
  21. // Options: --no-includes --include-headers
  22. virtual org
  23. virtual report
  24. virtual context
  25. @r depends on context || org || report@
  26. expression x;
  27. @@
  28. (
  29. x = devm_kmalloc(...)
  30. |
  31. x = devm_kvasprintf(...)
  32. |
  33. x = devm_kasprintf(...)
  34. |
  35. x = devm_kzalloc(...)
  36. |
  37. x = devm_kmalloc_array(...)
  38. |
  39. x = devm_kcalloc(...)
  40. |
  41. x = devm_kstrdup(...)
  42. |
  43. x = devm_kmemdup(...)
  44. |
  45. x = devm_get_free_pages(...)
  46. |
  47. x = devm_request_irq(...)
  48. |
  49. x = devm_ioremap(...)
  50. |
  51. x = devm_ioremap_nocache(...)
  52. |
  53. x = devm_ioport_map(...)
  54. )
  55. @safe depends on context || org || report exists@
  56. expression x;
  57. position p;
  58. @@
  59. (
  60. x = kmalloc(...)
  61. |
  62. x = kvasprintf(...)
  63. |
  64. x = kasprintf(...)
  65. |
  66. x = kzalloc(...)
  67. |
  68. x = kmalloc_array(...)
  69. |
  70. x = kcalloc(...)
  71. |
  72. x = kstrdup(...)
  73. |
  74. x = kmemdup(...)
  75. |
  76. x = get_free_pages(...)
  77. |
  78. x = request_irq(...)
  79. |
  80. x = ioremap(...)
  81. |
  82. x = ioremap_nocache(...)
  83. |
  84. x = ioport_map(...)
  85. )
  86. ...
  87. (
  88. kfree@p(x)
  89. |
  90. kzfree@p(x)
  91. |
  92. __krealloc@p(x, ...)
  93. |
  94. krealloc@p(x, ...)
  95. |
  96. free_pages@p(x, ...)
  97. |
  98. free_page@p(x)
  99. |
  100. free_irq@p(x)
  101. |
  102. iounmap@p(x)
  103. |
  104. ioport_unmap@p(x)
  105. )
  106. @pb@
  107. expression r.x;
  108. position p != safe.p;
  109. @@
  110. (
  111. * kfree@p(x)
  112. |
  113. * kzfree@p(x)
  114. |
  115. * __krealloc@p(x, ...)
  116. |
  117. * krealloc@p(x, ...)
  118. |
  119. * free_pages@p(x, ...)
  120. |
  121. * free_page@p(x)
  122. |
  123. * free_irq@p(x)
  124. |
  125. * iounmap@p(x)
  126. |
  127. * ioport_unmap@p(x)
  128. )
  129. @script:python depends on org@
  130. p << pb.p;
  131. @@
  132. msg="WARNING: invalid free of devm_ allocated data"
  133. coccilib.org.print_todo(p[0], msg)
  134. @script:python depends on report@
  135. p << pb.p;
  136. @@
  137. msg="WARNING: invalid free of devm_ allocated data"
  138. coccilib.report.print_report(p[0], msg)