kfree.cocci 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /// Find a use after free.
  2. //# Values of variables may imply that some
  3. //# execution paths are not possible, resulting in false positives.
  4. //# Another source of false positives are macros such as
  5. //# SCTP_DBG_OBJCNT_DEC that do not actually evaluate their argument
  6. ///
  7. // Confidence: Moderate
  8. // Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
  9. // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
  10. // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
  11. // URL: http://coccinelle.lip6.fr/
  12. // Comments:
  13. // Options: --no-includes --include-headers
  14. virtual org
  15. virtual report
  16. @free@
  17. expression E;
  18. position p1;
  19. @@
  20. (
  21. * kfree@p1(E)
  22. |
  23. * kzfree@p1(E)
  24. )
  25. @print expression@
  26. constant char [] c;
  27. expression free.E,E2;
  28. type T;
  29. position p;
  30. identifier f;
  31. @@
  32. (
  33. f(...,c,...,(T)E@p,...)
  34. |
  35. E@p == E2
  36. |
  37. E@p != E2
  38. |
  39. E2 == E@p
  40. |
  41. E2 != E@p
  42. |
  43. !E@p
  44. |
  45. E@p || ...
  46. )
  47. @sz@
  48. expression free.E;
  49. position p;
  50. @@
  51. sizeof(<+...E@p...+>)
  52. @loop exists@
  53. expression E;
  54. identifier l;
  55. position ok;
  56. @@
  57. while (1) { ...
  58. (
  59. * kfree@ok(E)
  60. |
  61. * kzfree@ok(E)
  62. )
  63. ... when != break;
  64. when != goto l;
  65. when forall
  66. }
  67. @r exists@
  68. expression free.E, subE<=free.E, E2;
  69. expression E1;
  70. iterator iter;
  71. statement S;
  72. position free.p1!=loop.ok,p2!={print.p,sz.p};
  73. @@
  74. (
  75. * kfree@p1(E,...)
  76. |
  77. * kzfree@p1(E,...)
  78. )
  79. ...
  80. (
  81. iter(...,subE,...) S // no use
  82. |
  83. list_remove_head(E1,subE,...)
  84. |
  85. subE = E2
  86. |
  87. subE++
  88. |
  89. ++subE
  90. |
  91. --subE
  92. |
  93. subE--
  94. |
  95. &subE
  96. |
  97. BUG(...)
  98. |
  99. BUG_ON(...)
  100. |
  101. return_VALUE(...)
  102. |
  103. return_ACPI_STATUS(...)
  104. |
  105. E@p2 // bad use
  106. )
  107. @script:python depends on org@
  108. p1 << free.p1;
  109. p2 << r.p2;
  110. @@
  111. cocci.print_main("kfree",p1)
  112. cocci.print_secs("ref",p2)
  113. @script:python depends on report@
  114. p1 << free.p1;
  115. p2 << r.p2;
  116. @@
  117. msg = "ERROR: reference preceded by free on line %s" % (p1[0].line)
  118. coccilib.report.print_report(p2[0],msg)