Received: by 10.192.165.148 with SMTP id m20csp3942405imm; Mon, 30 Apr 2018 08:59:49 -0700 (PDT) X-Google-Smtp-Source: AB8JxZp33/zzJVtlXToQ9w4glXZWyKRxk4lzX97bnGSwPw2uF4NUkRT2sn11pGwmvoYnt1DBj9cP X-Received: by 2002:a17:902:ab93:: with SMTP id f19-v6mr11978665plr.392.1525103989661; Mon, 30 Apr 2018 08:59:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525103989; cv=none; d=google.com; s=arc-20160816; b=eQdJvq+M2YuPg1g4wpBja+eqUev2txhY5q4T5D8mQI1FFbZv3OoOZGeFrUZcEfM8Gw IU/EneHkJaUTQswQh60vJ+M2W68EfOl0gsI0cLAuYiBn9SWEZzGKk0IqfYVu/Og7Kj+1 4CsYWoPxIG6TVuzLFU92YXONdb9qvyMvs+sa7OZEDFMViYJQU31Js2cMhl1kK04kW5A5 MbFc7adHk+ppYgBgnXhOxWqcPkHRfqWDoKb+m4wuCtqTUEZwFthXGpCQzA/V/KCEm7os e2f+8Iqzu+WQBmSy2hr6Ut25P+1ideQEhehkumdNK5V05RQwr2lHM1EXoLdg3meRnSKJ ZvoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dmarc-filter:dkim-signature:dkim-signature :arc-authentication-results; bh=Nr0XzDIfZb0pbrgcZeuyvXrSiN0UqyWTjQjuC47K5cY=; b=W8hpvdchZ6TLyNdy1tR5tFfMICXYe5al76Sq2nQaKfMS9mexr0bNj2dJ9c/O9A3tvx /wsrdajFlMygxQSGtbVNM0kNSKOQfJJtgAe54TOGngKUJQrYBCnLpnN56O5QmZkWv5N4 4REyktUKMwSSscYUilRPAMFicA6Dix+qyk4BvnZQBzg76cQWfMr2HW1tgk6K2mAWvRkc JqjFd+ZI8r+52KLMz54/Y/xueCv0wcpG/Pfj5Au4fDkbVdP+Li7MzS+GGOTVR4IR0i1o loXS86/+nc4SG/hxuumKzwiNUC/uEUw4lY4f1xyJCuPWBo95enrqlw8ufMLFy86XnTHC 0SHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=o4Jk464I; dkim=pass header.i=@codeaurora.org header.s=default header.b=JX1mPTeh; 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 u10-v6si6319451pgp.45.2018.04.30.08.59.34; Mon, 30 Apr 2018 08:59:49 -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=@codeaurora.org header.s=default header.b=o4Jk464I; dkim=pass header.i=@codeaurora.org header.s=default header.b=JX1mPTeh; 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 S1754601AbeD3P7Y (ORCPT + 99 others); Mon, 30 Apr 2018 11:59:24 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:39284 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754474AbeD3P7X (ORCPT ); Mon, 30 Apr 2018 11:59:23 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id CAB6A60A4E; Mon, 30 Apr 2018 15:59:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1525103962; bh=7XxLOxQieNW4KYNtXfy4PdaxPA0eyJHWU6d03YHBE+4=; h=From:To:Cc:Subject:Date:From; b=o4Jk464IrM8WKzNC5Iph0Tjvq6x84266frgvsblA3nLlaTMEyw/7vd9fXbd/DDjXD uffiHDZaUmjXhmn9bXIGoYrvJdlr/SwN22VO9ycAbulfnldxYhXjOYn8+KYkc8aONQ opRLMr6LEYIeaJLOMwiBFKiq/7t1oDYZw6GftFKU= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 Received: from jhugo-perf-lnx.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: jhugo@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 174BB60271; Mon, 30 Apr 2018 15:59:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1525103961; bh=7XxLOxQieNW4KYNtXfy4PdaxPA0eyJHWU6d03YHBE+4=; h=From:To:Cc:Subject:Date:From; b=JX1mPTeh4FiRqZTjtyMHwlB446hL67PGrtPZcpMqMxbr0qdk6vmrLwZ0PKZIC8LWp Z0olaLkR3f4RV8Vv9u3psIFfDVf6Cw5p0+wXeANVFJQyihBD21XfkJCXFufDEMvVFN md8whNP6lAcbTCuEgqTqezzQ6Wch/mTbpbdrczKI= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 174BB60271 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=jhugo@codeaurora.org From: Jeffrey Hugo To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Mark Rutland , Jan Glauber , Kees Cook , Ard Biesheuvel , Catalin Marinas , Will Deacon , Laura Abbott , Timur Tabi , Stephen Smalley , Andrew Morton , Ingo Molnar , Thomas Gleixner , Peter Zijlstra , Jeffrey Hugo Subject: [PATCH v3] init: Fix false positives in W+X checking Date: Mon, 30 Apr 2018 09:59:06 -0600 Message-Id: <1525103946-29526-1-git-send-email-jhugo@codeaurora.org> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org load_module() creates W+X mappings via __vmalloc_node_range() (from layout_and_allocate()->move_module()->module_alloc()) by using PAGE_KERNEL_EXEC. These mappings are later cleaned up via "call_rcu_sched(&freeinit->rcu, do_free_init)" from do_init_module(). This is a problem because call_rcu_sched() queues work, which can be run after debug_checkwx() is run, resulting in a race condition. If hit, the race results in a nasty splat about insecure W+X mappings, which results in a poor user experience as these are not the mappings that debug_checkwx() is intended to catch. This issue is observed on multiple arm64 platforms, and has been artificially triggered on an x86 platform. Address the race by flushing the queued work before running the arch-defined mark_rodata_ro() which then calls debug_checkwx(). Reported-by: Timur Tabi Reported-by: Jan Glauber Fixes: e1a58320a38d ("x86/mm: Warn on W^X mappings") Signed-off-by: Jeffrey Hugo Acked-by: Kees Cook Acked-by: Ingo Molnar Acked-by: Will Deacon --- v3: -added comment to module code to establish matching connection -added acks by Kees, Ingo, and Will v2: -was "arm64: mm: Fix false positives in W+X checking" (see [1]) -moved to common code based on review and confirmation of issue on x86 [1] http://lists.infradead.org/pipermail/linux-arm-kernel/2018-April/573776.html init/main.c | 7 +++++++ kernel/module.c | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/init/main.c b/init/main.c index b795aa3..499d957 100644 --- a/init/main.c +++ b/init/main.c @@ -1034,6 +1034,13 @@ static int __init set_debug_rodata(char *str) static void mark_readonly(void) { if (rodata_enabled) { + /* + * load_module() results in W+X mappings, which are cleaned up + * with call_rcu_sched(). Let's make sure that queued work is + * flushed so that we don't hit false positives looking for + * insecure pages which are W+X. + */ + rcu_barrier_sched(); mark_rodata_ro(); rodata_test(); } else diff --git a/kernel/module.c b/kernel/module.c index a6e43a5..7b71c3c 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -3516,6 +3516,11 @@ static noinline int do_init_module(struct module *mod) * walking this with preempt disabled. In all the failure paths, we * call synchronize_sched(), but we don't want to slow down the success * path, so use actual RCU here. + * Note that module_alloc() on most architectures creates W+X page + * mappings which won't be cleaned up until do_free_init() runs. Any + * code such as mark_rodata_ro() which depends on those mappings to + * be cleaned up needs to sync with the queued work - ie + * rcu_barrier_sched() */ call_rcu_sched(&freeinit->rcu, do_free_init); mutex_unlock(&module_mutex); -- Qualcomm Datacenter Technologies as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.