Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp66606ybe; Wed, 4 Sep 2019 15:15:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqxO2OU/jxrkga6DEeOZkZ7z90F1zqtSJPKHHDytrQdyIX9O4EGWkQKXXXMB3rnY/eQtNJsO X-Received: by 2002:a17:902:16e:: with SMTP id 101mr21107plb.139.1567635339066; Wed, 04 Sep 2019 15:15:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567635339; cv=none; d=google.com; s=arc-20160816; b=CQG3TbYpJFf7QtVhX1pF3mCoEWTKP9jdYykxb4zaW409EvdTA+pjaLXSKrXhXOMU6d weGZFkjyqEyK6dcK9IpjHcdHg0MiIMTVVP/mgA5txlEAD2LntUrnecV3KQ2nuvp5O9vC wPZ/BHudacFPZ5CmnpXzDaInx1s8z3tBHAjiF2R/Qy3A+bOLQ3+p+SvBVI5NTLJe+cY5 kLtGkto8Ah6+TJ//XomcbUbMmfgbICb5lw10sJ3rkgLBvXIlr2Quao3Zml5rcPjR9kwT zsIaopEazwsyx5UZ0lqwNimeox88/V+ChWb8MQbiid0acqT4DtlEjzkzJ7Ngm004Es5u NjTg== 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 :message-id:date:subject:cc:to:from; bh=RVYDeHbKBGun4f+UKrUmf76hgNLAoFPf0BMYGjpN1us=; b=rYoXUuCewEbzWRjXQAnTl/A/2UPPOWyqPP8AxGNFo4rabYMVte0luLK+BQqhVIr5B+ P68XLS0qBtstw75/mt5ETgHMunJ3YAjWrEohQd8FkdKnrvMG1QIegytQrWkxeCYau3la r0liUwrMGGrJvvCRWTzBdm9LyrJkCmzlPYfYlITylh6cZdIBu/0U5VYhd/hzIuUvzzYn QW66IanHmn0wfBvZQEJxrJ3GFwkazCwwfmfmy4PgeE3bYQfgTaiMN3R9iZJ/v///QjPr 1LG63cMhjOhrkGSFbV/z/vETAwu184IpSDg/IRalQD8GFQb2MWPArusXOZ+Fz5bUsl85 lW+w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n3si34280pgb.68.2019.09.04.15.15.22; Wed, 04 Sep 2019 15:15:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729919AbfIDWMm (ORCPT + 99 others); Wed, 4 Sep 2019 18:12:42 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:38939 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726495AbfIDWMm (ORCPT ); Wed, 4 Sep 2019 18:12:42 -0400 Received: by mail-wr1-f68.google.com with SMTP id t16so403078wra.6 for ; Wed, 04 Sep 2019 15:12:40 -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:mime-version :content-transfer-encoding; bh=RVYDeHbKBGun4f+UKrUmf76hgNLAoFPf0BMYGjpN1us=; b=eJecNKUFSF4Sj5nDpm6pK6SgQLRG3WSWZuOf6y6esFn/dBQM7GBUNpNSd6uJ36Id98 OHjhUTTO91S+6jpvxaZvzJB15nSxrFu2z5zy6eeQ8nJqg5TuZryc74OjbPtTqNSrBhNx 0/Gl+CdJQBVNpvRa1u/ugW0LRAsprPHBDe0VEo0qADkqxCAuZipJRLlaJSBb7Kn1azf1 aSxuszAyH8L58+V5/OhKzH8pcP+MFXjuyeo9RIFeB7mBmlNVliTRFOQb87yVx6p2VfNK hZY6lzHt3t3efTp93Jhnq2JlgvvTMRirGnxx4jasX0oaIcSrTkGvZGRrDsq6BRi7oVhV eShA== X-Gm-Message-State: APjAAAUvwx/vNYNwljmU0KEQdaCtlH+0vYi3h8qxVwnR9T/T9H54/Hwk YbWGYi9al2Pp5lmgoKmkmynZ2aZH X-Received: by 2002:adf:f011:: with SMTP id j17mr34814430wro.131.1567635160085; Wed, 04 Sep 2019 15:12:40 -0700 (PDT) Received: from green.intra.ispras.ru (bran.ispras.ru. [83.149.199.196]) by smtp.googlemail.com with ESMTPSA id s26sm156204wrs.63.2019.09.04.15.12.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2019 15:12:39 -0700 (PDT) From: Denis Efremov To: Coccinelle Cc: Denis Efremov , Julia Lawall , Gilles Muller , Nicolas Palix , Michal Marek , linux-kernel@vger.kernel.org Subject: [RFC PATCH] coccinelle: check for integer overflow in binary search Date: Thu, 5 Sep 2019 01:12:23 +0300 Message-Id: <20190904221223.5281-1-efremov@linux.com> X-Mailer: git-send-email 2.21.0 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 This is an RFC. I will resend the patch after feedback. Currently I'm preparing big patchset with bsearch warnings fixed. The rule will be a part of this patchset if it will be considered good enough for checking. There is a known integer overflow error [1] in the binary search algorithm. Google faced it in 2006 [2]. This rule checks midpoint calculation in binary search for overflow, i.e., (l + h) / 2. Not every match is an actual error since the array could be small enough. However, a custom implementation of binary search is error-prone and it's better to use the library function (lib/bsearch.c) or to apply defensive programming for midpoint calculation. [1] https://en.wikipedia.org/wiki/Binary_search_algorithm#Implementation_issues [2] https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html Signed-off-by: Denis Efremov --- scripts/coccinelle/misc/bsearch.cocci | 80 +++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 scripts/coccinelle/misc/bsearch.cocci diff --git a/scripts/coccinelle/misc/bsearch.cocci b/scripts/coccinelle/misc/bsearch.cocci new file mode 100644 index 000000000000..a99d9a8d3ee5 --- /dev/null +++ b/scripts/coccinelle/misc/bsearch.cocci @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// Check midpoint calculation in binary search algorithm for integer overflow +/// error [1]. Google faced it in 2006 [2]. Not every match is an actual error +/// since the array can be small enough. However, a custom implementation of +/// binary search is error-prone and it's better to use the library function +/// (lib/bsearch.c) or to apply defensive programming for midpoint calculation. +/// +/// [1] https://en.wikipedia.org/wiki/Binary_search_algorithm#Implementation_issues +/// [2] https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html +// +// Confidence: Medium +// Copyright: (C) 2019 Denis Efremov, ISPRAS +// Comments: +// Options: --no-includes --include-headers + +virtual report +virtual org + +@r depends on org || report@ +identifier l, h, m; +statement S; +position p; +// to match 1 in << +// to match 2 in / +// Can't use exact values, e.g. 2, because it fails to match 2L. +// TODO: Is there an isomorphism for 2, 2L, 2U, 2UL, 2ULL, etc? +constant c; +// TODO: Is there an isomorphism for (a / 2) === (a >> 1)? +@@ +( + while (\(l < h\|l <= h\|(h - l) > 1\|(l + 1) < h\|l < (h - 1)\)) { + ... +( + ((l + h)@p / c) +| + ((l + h)@p >> c) +) + ... + } +//TODO: Is it possible to match "do {} while ();" loops? +// | +// do { +// ... +// ( +// ((l + h)@p / c) +// | +// ((l + h)@p >> c) +// ) +// ... +// } while (\(l < h\|l <= h\|(h - l) > 1\|(l + 1) < h\|l < (h - 1)\)); +| + for (...; \(l < h\|l <= h\|(h - l) > 1\|(l + 1) < h\|l < (h - 1)\); + m = \(((l + h)@p / c)\|((l + h)@p >> c)\)) S +| + for (...; \(l < h\|l <= h\|(h - l) > 1\|(l + 1) < h\|l < (h - 1)\); ...) { + ... +( + ((l + h)@p / c) +| + ((l + h)@p >> c) +) + ... + } +) + +@script:python depends on org@ +p << r.p; +@@ + +coccilib.org.print_todo(p[0], "WARNING opportunity for bsearch() or 'm = l + (h - l) / 2;'") + +@script:python depends on report@ +p << r.p; +@@ + +msg="WARNING: custom implementation of bsearch is error-prone. " +msg+="Consider using lib/bsearch.c or fix the midpoint calculation " +msg+="as 'm = l + (h - l) / 2;' to prevent the arithmetic overflow." +coccilib.report.print_report(p[0], msg) + -- 2.21.0