cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

zalloc-simple.cocci (8574B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2///
      3/// Use zeroing allocator rather than allocator followed by memset with 0
      4///
      5/// This considers some simple cases that are common and easy to validate
      6/// Note in particular that there are no ...s in the rule, so all of the
      7/// matched code has to be contiguous
      8///
      9// Confidence: High
     10// Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU.
     11// Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6.
     12// Copyright: (C) 2017 Himanshu Jha
     13// URL: http://coccinelle.lip6.fr/rules/kzalloc.html
     14// Options: --no-includes --include-headers
     15//
     16// Keywords: kmalloc, kzalloc
     17// Version min: < 2.6.12 kmalloc
     18// Version min:   2.6.14 kzalloc
     19//
     20
     21virtual context
     22virtual patch
     23virtual org
     24virtual report
     25
     26//----------------------------------------------------------
     27//  For context mode
     28//----------------------------------------------------------
     29
     30@depends on context@
     31type T, T2;
     32expression x;
     33expression E1;
     34statement S;
     35@@
     36
     37* x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\|
     38  kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\|
     39  devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|kvmalloc_node(E1,...)\);
     40  if ((x==NULL) || ...) S
     41* memset((T2)x,0,E1);
     42
     43//----------------------------------------------------------
     44//  For patch mode
     45//----------------------------------------------------------
     46
     47@depends on patch@
     48type T, T2;
     49expression x;
     50expression E1,E2,E3,E4;
     51statement S;
     52@@
     53
     54(
     55- x = kmalloc(E1,E2);
     56+ x = kzalloc(E1,E2);
     57|
     58- x = (T *)kmalloc(E1,E2);
     59+ x = kzalloc(E1,E2);
     60|
     61- x = (T)kmalloc(E1,E2);
     62+ x = (T)kzalloc(E1,E2);
     63|
     64- x = vmalloc(E1);
     65+ x = vzalloc(E1);
     66|
     67- x = (T *)vmalloc(E1);
     68+ x = vzalloc(E1);
     69|
     70- x = (T)vmalloc(E1);
     71+ x = (T)vzalloc(E1);
     72|
     73- x = kmalloc_node(E1,E2,E3);
     74+ x = kzalloc_node(E1,E2,E3);
     75|
     76- x = (T *)kmalloc_node(E1,E2,E3);
     77+ x = kzalloc_node(E1,E2,E3);
     78|
     79- x = (T)kmalloc_node(E1,E2,E3);
     80+ x = (T)kzalloc_node(E1,E2,E3);
     81|
     82- x = kmem_cache_alloc(E3,E4);
     83+ x = kmem_cache_zalloc(E3,E4);
     84|
     85- x = (T *)kmem_cache_alloc(E3,E4);
     86+ x = kmem_cache_zalloc(E3,E4);
     87|
     88- x = (T)kmem_cache_alloc(E3,E4);
     89+ x = (T)kmem_cache_zalloc(E3,E4);
     90|
     91- x = kmem_alloc(E1,E2);
     92+ x = kmem_zalloc(E1,E2);
     93|
     94- x = (T *)kmem_alloc(E1,E2);
     95+ x = kmem_zalloc(E1,E2);
     96|
     97- x = (T)kmem_alloc(E1,E2);
     98+ x = (T)kmem_zalloc(E1,E2);
     99|
    100- x = devm_kmalloc(E2,E1,E3);
    101+ x = devm_kzalloc(E2,E1,E3);
    102|
    103- x = (T *)devm_kmalloc(E2,E1,E3);
    104+ x = devm_kzalloc(E2,E1,E3);
    105|
    106- x = (T)devm_kmalloc(E2,E1,E3);
    107+ x = (T)devm_kzalloc(E2,E1,E3);
    108|
    109- x = kvmalloc(E1,E2);
    110+ x = kvzalloc(E1,E2);
    111|
    112- x = (T *)kvmalloc(E1,E2);
    113+ x = kvzalloc(E1,E2);
    114|
    115- x = (T)kvmalloc(E1,E2);
    116+ x = (T)kvzalloc(E1,E2);
    117|
    118- x = kvmalloc_node(E1,E2,E3);
    119+ x = kvzalloc_node(E1,E2,E3);
    120|
    121- x = (T *)kvmalloc_node(E1,E2,E3);
    122+ x = kvzalloc_node(E1,E2,E3);
    123|
    124- x = (T)kvmalloc_node(E1,E2,E3);
    125+ x = (T)kvzalloc_node(E1,E2,E3);
    126)
    127  if ((x==NULL) || ...) S
    128- memset((T2)x,0,E1);
    129
    130@depends on patch@
    131type T, T2;
    132expression x;
    133expression E1,E2,E3,E4;
    134statement S;
    135@@
    136  x = (T)dma_alloc_coherent(E1, E2, E3, E4);
    137  if ((x==NULL) || ...) S
    138- memset((T2)x, 0, E2);
    139
    140//----------------------------------------------------------
    141//  For org mode
    142//----------------------------------------------------------
    143
    144@r depends on org || report@
    145type T, T2;
    146expression x;
    147expression E1,E2;
    148statement S;
    149position p;
    150@@
    151
    152 x = (T)kmalloc@p(E1,E2);
    153 if ((x==NULL) || ...) S
    154 memset((T2)x,0,E1);
    155
    156@script:python depends on org@
    157p << r.p;
    158x << r.x;
    159@@
    160
    161msg="%s" % (x)
    162msg_safe=msg.replace("[","@(").replace("]",")")
    163coccilib.org.print_todo(p[0], msg_safe)
    164
    165@script:python depends on report@
    166p << r.p;
    167x << r.x;
    168@@
    169
    170msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
    171coccilib.report.print_report(p[0], msg)
    172
    173//-----------------------------------------------------------------
    174@r1 depends on org || report@
    175type T, T2;
    176expression x;
    177expression E1;
    178statement S;
    179position p;
    180@@
    181
    182 x = (T)vmalloc@p(E1);
    183 if ((x==NULL) || ...) S
    184 memset((T2)x,0,E1);
    185
    186@script:python depends on org@
    187p << r1.p;
    188x << r1.x;
    189@@
    190
    191msg="%s" % (x)
    192msg_safe=msg.replace("[","@(").replace("]",")")
    193coccilib.org.print_todo(p[0], msg_safe)
    194
    195@script:python depends on report@
    196p << r1.p;
    197x << r1.x;
    198@@
    199
    200msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
    201coccilib.report.print_report(p[0], msg)
    202
    203//-----------------------------------------------------------------
    204@r2 depends on org || report@
    205type T, T2;
    206expression x;
    207expression E1,E2,E3,E4;
    208statement S;
    209position p;
    210@@
    211
    212 x = (T)dma_alloc_coherent@p(E1,E2,E3,E4);
    213 if ((x==NULL) || ...) S
    214 memset((T2)x,0,E2);
    215
    216@script:python depends on org@
    217p << r2.p;
    218x << r2.x;
    219@@
    220
    221msg="%s" % (x)
    222msg_safe=msg.replace("[","@(").replace("]",")")
    223coccilib.org.print_todo(p[0], msg_safe)
    224
    225@script:python depends on report@
    226p << r2.p;
    227x << r2.x;
    228@@
    229
    230msg="WARNING: dma_alloc_coherent used in %s already zeroes out memory, so memset is not needed" % (x)
    231coccilib.report.print_report(p[0], msg)
    232
    233//-----------------------------------------------------------------
    234@r3 depends on org || report@
    235type T, T2;
    236expression x;
    237expression E1,E2,E3;
    238statement S;
    239position p;
    240@@
    241
    242 x = (T)kmalloc_node@p(E1,E2,E3);
    243 if ((x==NULL) || ...) S
    244 memset((T2)x,0,E1);
    245
    246@script:python depends on org@
    247p << r3.p;
    248x << r3.x;
    249@@
    250
    251msg="%s" % (x)
    252msg_safe=msg.replace("[","@(").replace("]",")")
    253coccilib.org.print_todo(p[0], msg_safe)
    254
    255@script:python depends on report@
    256p << r3.p;
    257x << r3.x;
    258@@
    259
    260msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
    261coccilib.report.print_report(p[0], msg)
    262
    263//-----------------------------------------------------------------
    264@r4 depends on org || report@
    265type T, T2;
    266expression x;
    267expression E1,E2,E3;
    268statement S;
    269position p;
    270@@
    271
    272 x = (T)kmem_cache_alloc@p(E2,E3);
    273 if ((x==NULL) || ...) S
    274 memset((T2)x,0,E1);
    275
    276@script:python depends on org@
    277p << r4.p;
    278x << r4.x;
    279@@
    280
    281msg="%s" % (x)
    282msg_safe=msg.replace("[","@(").replace("]",")")
    283coccilib.org.print_todo(p[0], msg_safe)
    284
    285@script:python depends on report@
    286p << r4.p;
    287x << r4.x;
    288@@
    289
    290msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
    291coccilib.report.print_report(p[0], msg)
    292
    293//-----------------------------------------------------------------
    294@r5 depends on org || report@
    295type T, T2;
    296expression x;
    297expression E1,E2;
    298statement S;
    299position p;
    300@@
    301
    302 x = (T)kmem_alloc@p(E1,E2);
    303 if ((x==NULL) || ...) S
    304 memset((T2)x,0,E1);
    305
    306@script:python depends on org@
    307p << r5.p;
    308x << r5.x;
    309@@
    310
    311msg="%s" % (x)
    312msg_safe=msg.replace("[","@(").replace("]",")")
    313coccilib.org.print_todo(p[0], msg_safe)
    314
    315@script:python depends on report@
    316p << r5.p;
    317x << r5.x;
    318@@
    319
    320msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
    321coccilib.report.print_report(p[0], msg)
    322
    323//-----------------------------------------------------------------
    324@r6 depends on org || report@
    325type T, T2;
    326expression x;
    327expression E1,E2,E3;
    328statement S;
    329position p;
    330@@
    331
    332 x = (T)devm_kmalloc@p(E2,E1,E3);
    333 if ((x==NULL) || ...) S
    334 memset((T2)x,0,E1);
    335
    336@script:python depends on org@
    337p << r6.p;
    338x << r6.x;
    339@@
    340
    341msg="%s" % (x)
    342msg_safe=msg.replace("[","@(").replace("]",")")
    343coccilib.org.print_todo(p[0], msg_safe)
    344
    345@script:python depends on report@
    346p << r6.p;
    347x << r6.x;
    348@@
    349
    350msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
    351coccilib.report.print_report(p[0], msg)
    352
    353//-----------------------------------------------------------------
    354@r7 depends on org || report@
    355type T, T2;
    356expression x;
    357expression E1,E2;
    358statement S;
    359position p;
    360@@
    361
    362 x = (T)kvmalloc@p(E1,E2);
    363 if ((x==NULL) || ...) S
    364 memset((T2)x,0,E1);
    365
    366@script:python depends on org@
    367p << r7.p;
    368x << r7.x;
    369@@
    370
    371msg="%s" % (x)
    372msg_safe=msg.replace("[","@(").replace("]",")")
    373coccilib.org.print_todo(p[0], msg_safe)
    374
    375@script:python depends on report@
    376p << r7.p;
    377x << r7.x;
    378@@
    379
    380msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
    381coccilib.report.print_report(p[0], msg)
    382
    383//-----------------------------------------------------------------
    384@r9 depends on org || report@
    385type T, T2;
    386expression x;
    387expression E1,E2,E3;
    388statement S;
    389position p;
    390@@
    391
    392 x = (T)kvmalloc_node@p(E1,E2,E3);
    393 if ((x==NULL) || ...) S
    394 memset((T2)x,0,E1);
    395
    396@script:python depends on org@
    397p << r9.p;
    398x << r9.x;
    399@@
    400
    401msg="%s" % (x)
    402msg_safe=msg.replace("[","@(").replace("]",")")
    403coccilib.org.print_todo(p[0], msg_safe)
    404
    405@script:python depends on report@
    406p << r9.p;
    407x << r9.x;
    408@@
    409
    410msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
    411coccilib.report.print_report(p[0], msg)