linux-safety.lists.elisa.tech archive mirror
 help / color / mirror / Atom feed
* Re: [linux-safety] [PATCH] coccinelle: misc: Check for hard-coded constants
@ 2020-08-13 15:16 Lukas Bulwahn
  2020-08-13 15:23 ` Mohammed Billoo
  0 siblings, 1 reply; 10+ messages in thread
From: Lukas Bulwahn @ 2020-08-13 15:16 UTC (permalink / raw)
  To: mab; +Cc: skhan, linux-safety

To your questions, here is my opinion...

1. Is the header format in the semantic patch acceptable (i.e. referencing the CWE that this particular semantic patch aims to address)?

Actually, I think we should that for the existing rules as well.

I was thinking of the following format:

# Addresses: CWE-414 ("Missing Lock Check")

or

# Contributes-to: Missing Lock Check [CWE-414]

I think it is good discussion to have with Julia Lawall, Dan Carpenter, Luc Van Oostenryck, Joe Perches, etc. to see how they would want to maintain such information within their tools.


2. Should we create a separate directory for ELISA within coccinelle?

No, we do not structure according to the contributor, then the kernel architecture would be "linus directory", "andrew directory", "shuah directory", etc.

I would suggest that we could roughly structure according the existing structure for coccinelle and the CWE structure.


Lukas

P.S.: We need to set groups.io not to generate HTML emails on responses etc. when we want to engage with the kernel community. Let us check if we get that set up.
 

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: [linux-safety] [PATCH] coccinelle: misc: Check for hard-coded constants
@ 2020-08-13 15:45 Lukas Bulwahn
  0 siblings, 0 replies; 10+ messages in thread
From: Lukas Bulwahn @ 2020-08-13 15:45 UTC (permalink / raw)
  To: sudip.mukherjee, mab; +Cc: skhan, linux-safety

> imho, misc is ok for this one, but when you actually make a cocci script for
> CWE-414 ("Missing Lock Check"), that should be going to
> scripts/coccinelle/locks/
> 

Agree. Locking checks go to locks.

The example of CWE-414 was just to show a suitable format for adding the CWE mapping into the tools/rules as comments and maintain the mapping there.


Lukas

^ permalink raw reply	[flat|nested] 10+ messages in thread
* [PATCH] coccinelle: misc: Check for hard-coded constants
@ 2020-08-12 23:43 Mohammed Billoo
  2020-08-13 14:39 ` [linux-safety] " Shuah Khan
  0 siblings, 1 reply; 10+ messages in thread
From: Mohammed Billoo @ 2020-08-12 23:43 UTC (permalink / raw)
  To: linux-safety; +Cc: Mohammed Billoo

This semantic patch looks for variables that are initialized to
constants, arrays that are both declared and indexed with constants.
A false positive will occur  when a variable is initialized to 0, which
must happen for auto variables. This will be resolved in a future patch.

The patch was tested against the following snippet:

int main()
{
    int iarr[54]; /* instance 1 */
    int j = 0;    /* instance 2 */
    int i = 1;    /* instance 3 */
    iarr[0] = 3;  /* instance 4 */
    return 0;
}

and it correctly identified instances 1, 3, and 4. It incorrectly
identified instance 2, which will be addressed in a future patch.

Signed-off-by: Mohammed Billoo <mab@mab-labs.com>
---
 scripts/coccinelle/misc/magic_numbers.cocci | 45 +++++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100644 scripts/coccinelle/misc/magic_numbers.cocci

diff --git a/scripts/coccinelle/misc/magic_numbers.cocci b/scripts/coccinelle/misc/magic_numbers.cocci
new file mode 100644
index 000000000000..be6df33d28e4
--- /dev/null
+++ b/scripts/coccinelle/misc/magic_numbers.cocci
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Capture and instances of CWE-547 (https://cwe.mitre.org/data/definitions/547.html)
+///
+//# This attempts to capture instances of magic numbers and report them
+
+virtual report
+
+@r1 depends on report@
+type T;
+constant C;
+identifier var;
+position p;
+@@
+* T var@p = C;
+
+@script:python depends on report@
+p << r1.p;
+@@
+coccilib.report.print_report(p[0], "Hard-coded constant, consider using #define")
+
+@r2 depends on report@
+type T;
+identifier var;
+constant C;
+position p;
+@@
+* T var[C];
+
+@script:python depends on report@
+p << r2.p;
+@@
+coccilib.report.print_report(p[0], "Hard-coded constant, consider using #define")
+
+@r3 depends on report@
+type T;
+constant C;
+position p;
+T[] E;
+@@
+* E[C]@p = ... ;
+
+@script:python depends on report@
+p << r3.p;
+@@
+coccilib.report.print_report(p[0], "Hard-coded constant, consider using #define")
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-08-13 19:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-13 15:16 [linux-safety] [PATCH] coccinelle: misc: Check for hard-coded constants Lukas Bulwahn
2020-08-13 15:23 ` Mohammed Billoo
2020-08-13 15:33   ` Lukas Bulwahn
2020-08-13 15:41     ` Sudip Mukherjee
2020-08-13 17:02       ` Shuah Khan
     [not found]       ` <162AE2925F9D984B.16363@lists.elisa.tech>
2020-08-13 19:43         ` Shuah Khan
  -- strict thread matches above, loose matches on Subject: below --
2020-08-13 15:45 Lukas Bulwahn
2020-08-12 23:43 Mohammed Billoo
2020-08-13 14:39 ` [linux-safety] " Shuah Khan
2020-08-13 14:41   ` Mohammed Billoo
     [not found]   ` <162ADAEB16525C4A.3117@lists.elisa.tech>
2020-08-13 14:45     ` Mohammed Billoo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).