Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp468578ybt; Fri, 19 Jun 2020 06:16:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcr4O1YK64Gato0/UhDJ4PHH166XItv03/9qhx2UOG6T+ZEBkiOk6iDQtyqXpHeEMkp18j X-Received: by 2002:a05:6402:148f:: with SMTP id e15mr3379226edv.368.1592572565583; Fri, 19 Jun 2020 06:16:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592572565; cv=none; d=google.com; s=arc-20160816; b=bjr3FerM+p/mfiufT1W2da1l1n2KkdTw3xPK8t6djEbUvKDg+449OYjE6y9QPrZ95n yeT+ERabaumn6/nKUDwFjCq3VT61pXKSZG4QEkfmRhNT78BrTG1R1V3/cTRR0hXBEK2J ATMwK8siJB9L83SAB2+nWuJF6eBNUVEl+ncg0jkUKtyYlQ0ai3L4JPERPCXNvFl2+d7y xwdInDis+beKvydrMTJtsLaIfiguUxHbDFM/OjUqlph1l4cJI/0iWz2GZ3V5zJX+A08R A+gTkBb07RxCayT4UfdvvhWy/7RBKN8fOIGedKVfbjrChUY2JZmG5ZlYmQLRPbN6RoO/ XXRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=iV0NmQFNmutz16eLhCxLK2NGxLJ1R4f6BmCpmiGCcRI=; b=CCv/GS0+4h2R04JsPUbFuFUw1fV8LI1RnsyWGya/dxdK82B7DRI469HSp0uzJE0yyZ FTtVxURNbJ03uU1k/Oab0xLRnlWS8Y5tXC6qc7wwa89enkRWBgd/0W0v6oud9G8ZaWky FuBRiSsSJPPTWBzDbiVqY8sigHfWjoWeW35U936jJyq+CQb0hKyARlY571/xGOzNCho/ tl64VKfhHWx0vQKiMu3JcRpoRBQcLV9Y+o8rH1OKMFbFjKJb3V1CVc1tyW8VTKAEoc+C 1jZiAtLB7fNFWbg6M+Rz19utDxFVupsqvU/uG1ZzXU4l/DphnnPcTvRxjAXij0krKjoI sQAA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dk14si3799895edb.0.2020.06.19.06.15.42; Fri, 19 Jun 2020 06:16:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731083AbgFSNNj (ORCPT + 99 others); Fri, 19 Jun 2020 09:13:39 -0400 Received: from mail-lj1-f174.google.com ([209.85.208.174]:34417 "EHLO mail-lj1-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726124AbgFSNNg (ORCPT ); Fri, 19 Jun 2020 09:13:36 -0400 Received: by mail-lj1-f174.google.com with SMTP id x18so11432592lji.1 for ; Fri, 19 Jun 2020 06:13:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iV0NmQFNmutz16eLhCxLK2NGxLJ1R4f6BmCpmiGCcRI=; b=PoyH9IYJaZdO34/7YYADnnLsBmuL/VSArK4gj5Tq+J5+yEStV1MUfhq45yJpKsZqBL S9zCi1slEYf7gEcZlI/JxfFTvllYDiWgvu9aNq0PIFMBwwR7XB34HARmvV6SVcKjL30F yT8ki7Eqah3l397lLTyl1zoz4NyucnuiW2vqUcYbDAqYq2s3RgWtHsgh98XfQOjxb0v2 bpS1j3EgUHZyAYVhKLxZ+I+RuSB9DjaqOU1uDVtY2da0QnQ0gMC0d7qqvhOJYsF0olZu wVd/CnDj7uw97kYoetiFKwlBlDsE/kbpjCucjaPS1I9COsAuOUWk1siBaSNfLZEpugG0 fVqQ== X-Gm-Message-State: AOAM531JHerhF5/u+egaG305U9iekczfyvOl3yu3O6ssAPhOuD+UiwDN I9ZQTGYz0e7DJuhtpDP6Q/0= X-Received: by 2002:a2e:6f19:: with SMTP id k25mr1705672ljc.247.1592572412343; Fri, 19 Jun 2020 06:13:32 -0700 (PDT) Received: from localhost.localdomain ([213.87.137.195]) by smtp.googlemail.com with ESMTPSA id p19sm1170166lji.113.2020.06.19.06.13.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jun 2020 06:13:31 -0700 (PDT) From: Denis Efremov To: Julia Lawall Cc: Denis Efremov , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org, "Gustavo A . R . Silva" , Kees Cook Subject: [PATCH v3] coccinelle: misc: add array_size_dup script to detect missed overflow checks Date: Fri, 19 Jun 2020 16:13:13 +0300 Message-Id: <20200619131313.15468-1-efremov@linux.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200615102045.4558-1-efremov@linux.com> References: <20200615102045.4558-1-efremov@linux.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Detect an opencoded expression that is used before or after array_size()/array3_size()/struct_size() to compute the same size. Cc: Gustavo A. R. Silva Cc: Kees Cook Signed-off-by: Denis Efremov --- Changes in v2: - python rules moved next to SmPL patterns - assignment operator used - struct_size patterns fixed to check only E3, since E1, E2 are sizeofs of a structure and a member of a structure Changes in v3: - s/overlow/overflow/ typo fixed (thanks, Markus) - \(&E1\|&E2\) changed to &\(E1\|E2\) - print strings breaks removed scripts/coccinelle/misc/array_size_dup.cocci | 297 +++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 scripts/coccinelle/misc/array_size_dup.cocci diff --git a/scripts/coccinelle/misc/array_size_dup.cocci b/scripts/coccinelle/misc/array_size_dup.cocci new file mode 100644 index 000000000000..d03740257e97 --- /dev/null +++ b/scripts/coccinelle/misc/array_size_dup.cocci @@ -0,0 +1,297 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// Check for array_size(), array3_size(), struct_size() duplicates. +/// Three types of patterns for these functions: +/// 1. An opencoded expression is used before array_size() to compute the same size +/// 2. An opencoded expression is used after array_size() to compute the same size +/// 3. Consecutive calls of array_size() with the same values +/// From security point of view only first case is relevant. These functions +/// perform arithmetic overflow check. Thus, if we use an opencoded expression +/// before a call to the *_size() function we can miss an overflow. +/// +// Confidence: High +// Copyright: (C) 2020 Denis Efremov ISPRAS +// Options: --no-includes --include-headers --no-loops + +virtual context +virtual report +virtual org + +@as@ +expression E1, E2; +@@ + +array_size(E1, E2) + +@as_next@ +expression subE1 <= as.E1; +expression subE2 <= as.E2; +expression as.E1, as.E2, E3; +assignment operator aop; +position p1, p2; +@@ + +* E1 * E2@p1 + ... when != \(E1\|E2\|subE1\|subE2\) aop E3 + when != &\(E1\|E2\|subE1\|subE2\) +* array_size(E1, E2)@p2 + +@script:python depends on report@ +p1 << as_next.p1; +p2 << as_next.p2; +@@ + +coccilib.report.print_report(p1[0], +f"WARNING: array_size is used down the code (line {p2[0].line}) to compute the same size") + +@script:python depends on org@ +p1 << as_next.p1; +p2 << as_next.p2; +@@ + +coccilib.org.print_todo(p1[0], +f"WARNING: array_size is used down the code (line {p2[0].line}) to compute the same size") + +@as_prev@ +expression subE1 <= as.E1; +expression subE2 <= as.E2; +expression as.E1, as.E2, E3; +assignment operator aop; +position p1, p2; +@@ + +* array_size(E1, E2)@p1 + ... when != \(E1\|E2\|subE1\|subE2\) aop E3 + when != &\(E1\|E2\|subE1\|subE2\) +* E1 * E2@p2 + +@script:python depends on report@ +p1 << as_prev.p1; +p2 << as_prev.p2; +@@ + +coccilib.report.print_report(p2[0], +f"WARNING: array_size is already used (line {p1[0].line}) to compute the same size") + +@script:python depends on org@ +p1 << as_prev.p1; +p2 << as_prev.p2; +@@ + +coccilib.org.print_todo(p2[0], +f"WARNING: array_size is already used (line {p1[0].line}) to compute the same size") + +@as_dup@ +expression subE1 <= as.E1; +expression subE2 <= as.E2; +expression as.E1, as.E2, E3; +assignment operator aop; +position p1, p2; +@@ + +* array_size(E1, E2)@p1 + ... when != \(E1\|E2\|subE1\|subE2\) aop E3 + when != &\(E1\|E2\|subE1\|subE2\) +* array_size(E1, E2)@p2 + +@script:python depends on report@ +p1 << as_dup.p1; +p2 << as_dup.p2; +@@ + +coccilib.report.print_report(p2[0], +f"WARNING: same array_size (line {p1[0].line})") + +@script:python depends on org@ +p1 << as_dup.p1; +p2 << as_dup.p2; +@@ + +coccilib.org.print_todo(p2[0], +f"WARNING: same array_size (line {p1[0].line})") + +@as3@ +expression E1, E2, E3; +@@ + +array3_size(E1, E2, E3) + +@as3_next@ +expression subE1 <= as3.E1; +expression subE2 <= as3.E2; +expression subE3 <= as3.E3; +expression as3.E1, as3.E2, as3.E3, E4; +assignment operator aop; +position p1, p2; +@@ + +* E1 * E2 * E3@p1 + ... when != \(E1\|E2\|E3\|subE1\|subE2\|subE3\) aop E4 + when != &\(E1\|E2\|E3\|subE1\|subE2\|subE3\) +* array3_size(E1, E2, E3)@p2 + +@script:python depends on report@ +p1 << as3_next.p1; +p2 << as3_next.p2; +@@ + +coccilib.report.print_report(p1[0], +f"WARNING: array3_size is used down the code (line {p2[0].line}) to compute the same size") + +@script:python depends on org@ +p1 << as3_next.p1; +p2 << as3_next.p2; +@@ + +coccilib.org.print_todo(p1[0], +f"WARNING: array3_size is used down the code (line {p2[0].line}) to compute the same size") + +@as3_prev@ +expression subE1 <= as3.E1; +expression subE2 <= as3.E2; +expression subE3 <= as3.E3; +expression as3.E1, as3.E2, as3.E3, E4; +assignment operator aop; +position p1, p2; +@@ + +* array3_size(E1, E2, E3)@p1 + ... when != \(E1\|E2\|E3\|subE1\|subE2\|subE3\) aop E4 + when != &\(E1\|E2\|E3\|subE1\|subE2\|subE3\) +* E1 * E2 * E3@p2 + +@script:python depends on report@ +p1 << as3_prev.p1; +p2 << as3_prev.p2; +@@ + +coccilib.report.print_report(p2[0], +f"WARNING: array3_size is already used (line {p1[0].line}) to compute the same size") + +@script:python depends on org@ +p1 << as3_prev.p1; +p2 << as3_prev.p2; +@@ + +coccilib.org.print_todo(p2[0], +f"WARNING: array3_size is already used (line {p1[0].line}) to compute the same size") + +@as3_dup@ +expression subE1 <= as3.E1; +expression subE2 <= as3.E2; +expression subE3 <= as3.E3; +expression as3.E1, as3.E2, as3.E3, E4; +assignment operator aop; +position p1, p2; +@@ + +* array3_size(E1, E2, E3)@p1 + ... when != \(E1\|E2\|E3\|subE1\|subE2\|subE3\) aop E4 + when != &\(E1\|E2\|E3\|subE1\|subE2\|subE3\) +* array3_size(E1, E2, E3)@p2 + +@script:python depends on report@ +p1 << as3_dup.p1; +p2 << as3_dup.p2; +@@ + +coccilib.report.print_report(p2[0], +f"WARNING: same array3_size (line {p1[0].line})") + +@script:python depends on org@ +p1 << as3_dup.p1; +p2 << as3_dup.p2; +@@ + +coccilib.org.print_todo(p2[0], +f"WARNING: same array3_size (line {p1[0].line})") + +@ss@ +expression E1, E2, E3; +@@ + +struct_size(E1, E2, E3) + +@ss_next@ +expression subE3 <= ss.E3; +expression ss.E1, ss.E2, ss.E3, E4; +assignment operator aop; +position p1, p2; +@@ + +* E1 * E2 + E3@p1 + ... when != \(E3\|subE3\) aop E4 + when != &\(E3\|subE3\) +* struct_size(E1, E2, E3)@p2 + +@script:python depends on report@ +p1 << ss_next.p1; +p2 << ss_next.p2; +@@ + +coccilib.report.print_report(p1[0], +f"WARNING: struct_size is used down the code (line {p2[0].line}) to compute the same size") + +@script:python depends on org@ +p1 << ss_next.p1; +p2 << ss_next.p2; +@@ + +coccilib.org.print_todo(p1[0], +f"WARNING: struct_size is used down the code (line {p2[0].line}) to compute the same size") + +@ss_prev@ +expression subE3 <= ss.E3; +expression ss.E1, ss.E2, ss.E3, E4; +assignment operator aop; +position p1, p2; +@@ + +* struct_size(E1, E2, E3)@p1 + ... when != \(E3\|subE3\) aop E4 + when != &\(E3\|subE3\) +* E1 * E2 + E3@p2 + +@script:python depends on report@ +p1 << ss_prev.p1; +p2 << ss_prev.p2; +@@ + +coccilib.report.print_report(p2[0], +f"WARNING: struct_size is already used (line {p1[0].line}) to compute the same size") + +@script:python depends on org@ +p1 << ss_prev.p1; +p2 << ss_prev.p2; +@@ + +coccilib.org.print_todo(p2[0], +f"WARNING: struct_size is already used (line {p1[0].line}) to compute the same size") + +@ss_dup@ +expression subE3 <= ss.E3; +expression ss.E1, ss.E2, ss.E3, E4; +assignment operator aop; +position p1, p2; +@@ + +* struct_size(E1, E2, E3)@p1 + ... when != \(E3\|subE3\) aop E4 + when != &\(E3\|subE3\) +* struct_size(E1, E2, E3)@p2 + +@script:python depends on report@ +p1 << ss_dup.p1; +p2 << ss_dup.p2; +@@ + +coccilib.report.print_report(p2[0], +f"WARNING: same struct_size (line {p1[0].line})") + +@script:python depends on org@ +p1 << ss_dup.p1; +p2 << ss_dup.p2; +@@ + +coccilib.org.print_todo(p2[0], +f"WARNING: same struct_size (line {p1[0].line})") -- 2.26.2