Received: by 10.192.165.148 with SMTP id m20csp73796imm; Fri, 4 May 2018 06:56:55 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpPBX9bdUYR7e3rAsTPQcIjBgPi+A3LYwI6aTLi5SLilT2+OeGHzPY7CgdUGLwJqnNo54UG X-Received: by 10.167.130.22 with SMTP id k22mr27107031pfi.73.1525442215341; Fri, 04 May 2018 06:56:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525442215; cv=none; d=google.com; s=arc-20160816; b=0HtQtdrLpsS22oHQdxF+2cMXs+lawF7FnLrH6putCiSMGTvcWM3zhgknlcAm/M/OUb tlEiCN1FDTEF2tXEpc6DFb8JWw6igl53Xe2ZVntF8ygIbEtj9aABxdTzos4w+Mjdxv4y VvZatKg25SIu/W9t1SzJFdd+i8ZOGvxQN+y0CjTzyCpuniqAZ3mBlCdqCsC9MlOfts7n CvgM76KO92Mx+Zk/FdrCqrbKu6d+XkDfvrQlzI1Ojq8oGknMkUNm4sgWfB1fxYEN8JC/ UDG3zPhQ/WVa2H04Ura59thdGvkdQAI5wSZSUZligbo4CXxW6LumdZYSA3JFcY3bWP6r VoRw== 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:arc-authentication-results; bh=vJCkCLeozTTSpN1PdLfGExEdxJfOA2HLJfltzQIBJ1o=; b=Ydf70vgdQc41x2lNT7CvkI3kNjqQcmLMLAW0ExhFc6SXH5XInZfEeXdd/QQf45RIZA 9644OoFlaJIOI4r8KsE22NYCr2D1DHtU3w64ga5CWu2ZWBcjuqRCL06poSE79RVWAYiH OgFzkk5vuHCDurn/w0YMzSAPpi19bevYaeD1h1vfiCy0cl+X7dWMj3Q2S3j2n3NsIJjn sp+p6mArYI6O17HDEEZkFOqgTGyqDSql+pY8YFk8Ce+rrhYWY7wGy9JBmcN6fGIvgti/ 3b1WAB7id8WtwGgXeU9NXH5eu1H1ZqxhhMaDd1ehIiC0V2asfGsGo03No5+zqfpIgugk 0U1Q== 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 r12si15921096pfk.83.2018.05.04.06.56.41; Fri, 04 May 2018 06:56:55 -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 S1752030AbeEDNzp (ORCPT + 99 others); Fri, 4 May 2018 09:55:45 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:53656 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751573AbeEDNzn (ORCPT ); Fri, 4 May 2018 09:55:43 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5F2111596; Fri, 4 May 2018 06:55:43 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 0A67A3F487; Fri, 4 May 2018 06:55:41 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: akpm@linux-foundation.org, aryabinin@virtuozzo.com, dvyukov@google.com, mark.rutland@arm.com, mingo@redhat.com, peterz@infradead.org Subject: [PATCH 1/3] kcov: ensure irq code sees a valid area Date: Fri, 4 May 2018 14:55:33 +0100 Message-Id: <20180504135535.53744-2-mark.rutland@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180504135535.53744-1-mark.rutland@arm.com> References: <20180504135535.53744-1-mark.rutland@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For kernels built with CONFIG_PREEMPT, some C code may execute before or after the interrupt handler, while the hardirq count is zero. In these cases, in_task() can return true. A task can be interrupted in the middle of a KCOV_DISABLE ioctl while it resets the task's kcov data via kcov_task_init(). Instrumented code executed during this period will call __sanitizer_cov_trace_pc(), and as in_task() returns true, will inspect t->kcov_mode before trying to write to t->kcov_area. In kcov_init_task() Since we update t->kcov_{mode,area,size} with plain stores, which may be re-ordered, torn, etc. Thus __sanitizer_cov_trace_pc() may see bogus values for any of these fields, and may attempt to write to memory which is not mapped. Let's avoid this by using WRITE_ONCE() to set t->kcov_mode, with a barrier() to ensure this is ordered before we clear t->kov_{area,size}. This ensures that any code execute while kcov_init_task() is preempted will either see valid values for t->kcov_{area,size}, or will see that t->kcov_mode is KCOV_MODE_DISABLED, and bail out without touching t->kcov_area. Signed-off-by: Mark Rutland Cc: Andrew Morton Cc: Andrey Ryabinin Cc: Dmitry Vyukov --- kernel/kcov.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/kcov.c b/kernel/kcov.c index 2c16f1ab5e10..5be9a60a959f 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -241,7 +241,8 @@ static void kcov_put(struct kcov *kcov) void kcov_task_init(struct task_struct *t) { - t->kcov_mode = KCOV_MODE_DISABLED; + WRITE_ONCE(t->kcov_mode, KCOV_MODE_DISABLED); + barrier(); t->kcov_size = 0; t->kcov_area = NULL; t->kcov = NULL; -- 2.11.0