kfree.cocci 1.8 KB

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