2020-07-17 19:57:09 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
///
|
2020-08-11 15:49:53 +08:00
|
|
|
/// Use kfree_sensitive, kvfree_sensitive rather than memset or
|
|
|
|
/// memzero_explicit followed by kfree.
|
2020-07-17 19:57:09 +08:00
|
|
|
///
|
|
|
|
// Confidence: High
|
|
|
|
// Copyright: (C) 2020 Denis Efremov ISPRAS
|
|
|
|
// Options: --no-includes --include-headers
|
|
|
|
//
|
2020-08-11 15:49:53 +08:00
|
|
|
// Keywords: kfree_sensitive, kvfree_sensitive
|
2020-07-17 19:57:09 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
virtual context
|
|
|
|
virtual patch
|
|
|
|
virtual org
|
|
|
|
virtual report
|
|
|
|
|
|
|
|
@initialize:python@
|
|
|
|
@@
|
|
|
|
# kmalloc_oob_in_memset uses memset to explicitly trigger out-of-bounds access
|
2020-08-11 15:49:53 +08:00
|
|
|
filter = frozenset(['kmalloc_oob_in_memset',
|
|
|
|
'kfree_sensitive', 'kvfree_sensitive'])
|
2020-07-17 19:57:09 +08:00
|
|
|
|
|
|
|
def relevant(p):
|
|
|
|
return not (filter & {el.current_element for el in p})
|
|
|
|
|
|
|
|
@cond@
|
|
|
|
position ok;
|
|
|
|
@@
|
|
|
|
|
|
|
|
if (...)
|
|
|
|
\(memset@ok\|memzero_explicit@ok\)(...);
|
|
|
|
|
|
|
|
@r depends on !patch forall@
|
|
|
|
expression E;
|
|
|
|
position p : script:python() { relevant(p) };
|
|
|
|
position m != cond.ok;
|
|
|
|
type T;
|
|
|
|
@@
|
|
|
|
|
|
|
|
(
|
|
|
|
* memset@m((T)E, 0, ...);
|
|
|
|
|
|
|
|
|
* memzero_explicit@m((T)E, ...);
|
|
|
|
)
|
|
|
|
... when != E
|
|
|
|
when strict
|
|
|
|
* \(kfree\|vfree\|kvfree\)(E)@p;
|
|
|
|
|
|
|
|
@rp_memzero depends on patch@
|
|
|
|
expression E, size;
|
|
|
|
position p : script:python() { relevant(p) };
|
|
|
|
position m != cond.ok;
|
|
|
|
type T;
|
|
|
|
@@
|
|
|
|
|
|
|
|
- memzero_explicit@m((T)E, size);
|
|
|
|
... when != E
|
|
|
|
when strict
|
2020-08-11 15:49:53 +08:00
|
|
|
(
|
|
|
|
- kfree(E)@p;
|
|
|
|
+ kfree_sensitive(E);
|
|
|
|
|
|
2020-07-17 19:57:09 +08:00
|
|
|
- \(vfree\|kvfree\)(E)@p;
|
|
|
|
+ kvfree_sensitive(E, size);
|
2020-08-11 15:49:53 +08:00
|
|
|
)
|
2020-07-17 19:57:09 +08:00
|
|
|
|
|
|
|
@rp_memset depends on patch@
|
|
|
|
expression E, size;
|
|
|
|
position p : script:python() { relevant(p) };
|
|
|
|
position m != cond.ok;
|
|
|
|
type T;
|
|
|
|
@@
|
|
|
|
|
|
|
|
- memset@m((T)E, 0, size);
|
|
|
|
... when != E
|
|
|
|
when strict
|
|
|
|
(
|
|
|
|
- kfree(E)@p;
|
2020-08-11 15:49:53 +08:00
|
|
|
+ kfree_sensitive(E);
|
2020-07-17 19:57:09 +08:00
|
|
|
|
|
|
|
|
- \(vfree\|kvfree\)(E)@p;
|
|
|
|
+ kvfree_sensitive(E, size);
|
|
|
|
)
|
|
|
|
|
|
|
|
@script:python depends on report@
|
|
|
|
p << r.p;
|
2020-10-09 20:54:53 +08:00
|
|
|
m << r.m;
|
2020-07-17 19:57:09 +08:00
|
|
|
@@
|
|
|
|
|
2020-10-09 20:54:53 +08:00
|
|
|
msg = "WARNING opportunity for kfree_sensitive/kvfree_sensitive (memset at line %s)"
|
|
|
|
coccilib.report.print_report(p[0], msg % (m[0].line))
|
2020-07-17 19:57:09 +08:00
|
|
|
|
|
|
|
@script:python depends on org@
|
|
|
|
p << r.p;
|
2020-10-09 20:54:53 +08:00
|
|
|
m << r.m;
|
2020-07-17 19:57:09 +08:00
|
|
|
@@
|
|
|
|
|
2020-10-09 20:54:53 +08:00
|
|
|
msg = "WARNING opportunity for kfree_sensitive/kvfree_sensitive (memset at line %s)"
|
|
|
|
coccilib.org.print_todo(p[0], msg % (m[0].line))
|