Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2745287ybh; Mon, 9 Mar 2020 12:07:47 -0700 (PDT) X-Google-Smtp-Source: ADFU+vt0vj0GEc06T8LIOlVNjZFMzn9TUEnpYDOlMLRAYE+uu+5s0JbJkSx6UanzO8UWuIv+G+9n X-Received: by 2002:a05:6830:4a6:: with SMTP id l6mr14733087otd.61.1583780867229; Mon, 09 Mar 2020 12:07:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583780867; cv=none; d=google.com; s=arc-20160816; b=v2q8L3+IBJhOEFRXi3xTsIRD4x/2+m50I+wE/gcXK6Lm5/2HkUM+LcTH8bYwpuiHMl dhrC336tK+Lbyny3f6nl8fuBjm+XxYEVPPeJduCrfV/VaE6oyMc5S9f2IKTzRO0Yvfik zk3Zp/4vUnEGQapqOq1guNpXlSVKPQAfS4HzFH8DDMIGe/KEjxFZH4xAlXRVhF49Slun NptNoLSIgQT/tJgjqAtgcIwG0CRdF7+bK+IxXsrnGX84QjZNL0M/VgFuiwP6BFw/NAMG Gb7655xC3mUp81uxK/FE2VGD/CpljqP0Nh6xbmU0Y5a/b4UDbiPu4ML2Wmly9Zx2woUs NKTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=iTBirUbRM2fpi6ZrkpyYsTtPtCRdflYdovd3S/OOiBM=; b=GdtEe+GE7A7X+HP/VRVMGGdPPD7YZ3woHH3T/vmJo3AJqCVge9UAQg4RZSLiUcyyga GsEwDjhT2owvXfCzSPxNQVcuLfIygP5TYCgV1hvw7VsiXeqoiUXlkcha34OtmNKBeSew yvHI6sX6nz01h7108KAWjosmE2bFbwIuFYUhQfFA69FU7iYd86HEcyREXWPFySh4370I DRzwCIqc5U6V3tg+GPGBniLOMQvdap/MXC2a7DcwV8550keV/k+aSxW2J5U8HuOnrIbt pIzEfV9oDAvMFflddzu4pAobgjd705kUE2MR83P5BQ4JiuTWMw4IHxbe2oMtpHCUc6hE YOMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ou5GJgW0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x16si6356357otp.180.2020.03.09.12.07.35; Mon, 09 Mar 2020 12:07:47 -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; dkim=pass header.i=@kernel.org header.s=default header.b=ou5GJgW0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727885AbgCITFg (ORCPT + 99 others); Mon, 9 Mar 2020 15:05:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:47662 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727594AbgCITE0 (ORCPT ); Mon, 9 Mar 2020 15:04:26 -0400 Received: from paulmck-ThinkPad-P72.home (50-39-105-78.bvtn.or.frontiernet.net [50.39.105.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CDDCA2253D; Mon, 9 Mar 2020 19:04:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583780666; bh=heicvJqO/GtMXfDPR7KvF5QrLChk/HgFXlTA7BeTKGM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ou5GJgW0GlYbv+6WTSYcS/resHBac/SWpXBuHWhW1DZmxhCsLo4x1vNl/+V/zEXEO URPH3tQ9xhwCbGTT+i0kz3vTO4qpIDxJx9hGGacw8b1eTMMWP4Ns99GOgNpgO82A8A xe4lHWnG2HfDfgcieEAIDYof1H+DK5euQE6C443k= From: paulmck@kernel.org To: linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, kernel-team@fb.com, mingo@kernel.org Cc: elver@google.com, andreyknvl@google.com, glider@google.com, dvyukov@google.com, cai@lca.pw, boqun.feng@gmail.com, "Paul E . McKenney" Subject: [PATCH kcsan 15/32] kcsan: Fix 0-sized checks Date: Mon, 9 Mar 2020 12:04:03 -0700 Message-Id: <20200309190420.6100-15-paulmck@kernel.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20200309190359.GA5822@paulmck-ThinkPad-P72> References: <20200309190359.GA5822@paulmck-ThinkPad-P72> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marco Elver Instrumentation of arbitrary memory-copy functions, such as user-copies, may be called with size of 0, which could lead to false positives. To avoid this, add a comparison in check_access() for size==0, which will be optimized out for constant sized instrumentation (__tsan_{read,write}N), and therefore not affect the common-case fast-path. Signed-off-by: Marco Elver Signed-off-by: Paul E. McKenney --- kernel/kcsan/core.c | 7 +++++++ kernel/kcsan/test.c | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c index e3c7d8f..82c2bef 100644 --- a/kernel/kcsan/core.c +++ b/kernel/kcsan/core.c @@ -456,6 +456,13 @@ static __always_inline void check_access(const volatile void *ptr, size_t size, long encoded_watchpoint; /* + * Do nothing for 0 sized check; this comparison will be optimized out + * for constant sized instrumentation (__tsan_{read,write}N). + */ + if (unlikely(size == 0)) + return; + + /* * Avoid user_access_save in fast-path: find_watchpoint is safe without * user_access_save, as the address that ptr points to is only used to * check if a watchpoint exists; ptr is never dereferenced. diff --git a/kernel/kcsan/test.c b/kernel/kcsan/test.c index cc60002..d26a052 100644 --- a/kernel/kcsan/test.c +++ b/kernel/kcsan/test.c @@ -92,6 +92,16 @@ static bool test_matching_access(void) return false; if (WARN_ON(matching_access(9, 1, 10, 1))) return false; + + /* + * An access of size 0 could match another access, as demonstrated here. + * Rather than add more comparisons to 'matching_access()', which would + * end up in the fast-path for *all* checks, check_access() simply + * returns for all accesses of size 0. + */ + if (WARN_ON(!matching_access(8, 8, 12, 0))) + return false; + return true; } -- 2.9.5