1 // SPDX-License-Identifier: GPL-2.0-only
2 /// Use memdup_user rather than duplicating its implementation
3 /// This is a little bit restricted to reduce false positives
6 // Copyright: (C) 2010-2012 Nicolas Palix.
7 // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
8 // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
9 // URL: http://coccinelle.lip6.fr/
11 // Options: --no-includes --include-headers
20 filter = frozenset(['memdup_user', 'vmemdup_user'])
23 return not (filter & {el.current_element for el in p})
26 expression from,to,size;
28 position p : script:python() { relevant(p) };
31 - to = \(kmalloc@p\|kzalloc@p\)
32 - (size,\(GFP_KERNEL\|GFP_USER\|
33 - \(GFP_KERNEL\|GFP_USER\)|__GFP_NOWARN\));
34 + to = memdup_user(from,size);
39 <+... when != goto l1;
44 - if (copy_from_user(to, from, size) != 0) {
45 - <+... when != goto l2;
51 expression from,to,size;
53 position p : script:python() { relevant(p) };
56 - to = \(kvmalloc@p\|kvzalloc@p\)(size,\(GFP_KERNEL\|GFP_USER\));
57 + to = vmemdup_user(from,size);
62 <+... when != goto l1;
67 - if (copy_from_user(to, from, size) != 0) {
68 - <+... when != goto l2;
74 expression from,to,size;
75 position p : script:python() { relevant(p) };
79 * to = \(kmalloc@p\|kzalloc@p\)
80 (size,\(GFP_KERNEL\|GFP_USER\|
81 \(GFP_KERNEL\|GFP_USER\)|__GFP_NOWARN\));
82 if (to==NULL || ...) S1
83 if (copy_from_user(to, from, size) != 0)
86 @rv depends on !patch@
87 expression from,to,size;
88 position p : script:python() { relevant(p) };
92 * to = \(kvmalloc@p\|kvzalloc@p\)(size,\(GFP_KERNEL\|GFP_USER\));
93 if (to==NULL || ...) S1
94 if (copy_from_user(to, from, size) != 0)
97 @script:python depends on org@
101 coccilib.org.print_todo(p[0], "WARNING opportunity for memdup_user")
103 @script:python depends on report@
107 coccilib.report.print_report(p[0], "WARNING opportunity for memdup_user")
109 @script:python depends on org@
113 coccilib.org.print_todo(p[0], "WARNING opportunity for vmemdup_user")
115 @script:python depends on report@
119 coccilib.report.print_report(p[0], "WARNING opportunity for vmemdup_user")