Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2102072rwn; Fri, 9 Sep 2022 08:33:01 -0700 (PDT) X-Google-Smtp-Source: AA6agR5PgMAFauRJjIPFyOYlhcu/Gb2ZrJN6CZb27vYydYzzKu6LIb9PYlEV39ISFE+ttVnHWx0P X-Received: by 2002:a05:6a00:801:b0:53e:5e35:336c with SMTP id m1-20020a056a00080100b0053e5e35336cmr14974441pfk.62.1662737580909; Fri, 09 Sep 2022 08:33:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662737580; cv=none; d=google.com; s=arc-20160816; b=Z8e8NrLBNp9tgNEWZS0rLUjsweRZOsVoeNPK8kEOOGxDHXVLvHHFcTfxC7eJyybzDg LlAG5qNUo1hWIRy2hjmIcl0VF7meairEPUKC3bP2KhUGluKaW17QvFdWkZye3exv+SXO rr5bWxzdnnDxXKw6UJLkaijSJdIbXSI/kLNdNtfhQbwAcK+F6ohmXMHomDfkogzfFEso fog+nCaVJG9VK/RGzWQT27WFcgLDnk2lDH/oT7ghf8oOKfciL25Ahn1y5DWUdtFY7+Vg S0jF5sAx6DNmYStMEIxfW+krd9d2qfN3iGaXEPsmIN1MxRDvI0amoGTduXWKF+jsvpKT eCzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=VEx+Q7TcFeydOo2XsJxkVkD2MQ3yxmn/oc5Gpjuo2WE=; b=GFtKlE45zy68NLLKTOyNhfYGd5zjvbsOUajPVflw+cp+7VF79+PP4vPROtSRJSKHOA 9IxIoe235yskQQKH+XU97KU8VtQBSStvCGUNRvB9vGnNe9Z/SjHi3n/P220tpAmJ4wA/ ThWjMKxucaGWlUAiaPDnrtisssb3x0tHB8uO7eZqF3IBPMIziEKP+2bcCyvcRTEsprno aAD4X7+VGg4pZzbjKZCoccm7nDTFp3nfTI5ubXeNMzOuI0EeQJ34XqbhD4P8M29c1FTl W525x0sHVcrP40qp4VV7MVrNr+l9XyjngpxNt+RGYrtfIB9lBTdcGlTQz1/4Y+aaHkzK ygvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XrbQMOQX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ng11-20020a17090b1a8b00b00202641c4969si1075330pjb.0.2022.09.09.08.32.47; Fri, 09 Sep 2022 08:33:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XrbQMOQX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229810AbiIIPaE (ORCPT + 99 others); Fri, 9 Sep 2022 11:30:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229930AbiIIP3c (ORCPT ); Fri, 9 Sep 2022 11:29:32 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E887AB5326 for ; Fri, 9 Sep 2022 08:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662737325; x=1694273325; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=RuBa4e3QmLHKQ2j7H+ky5v61IXa2A+iP22YYziucdtU=; b=XrbQMOQXu0Vps2P9+lbUSIP0B///STxzn8vh0aEGct+AaXMW6FFiL5iA 5XfKWmLGv/lQmXsrCybEbtTwzGIKTmylK9BFsINlVtuYTCcX/LCoeGUfN nBvHZsKPR40xCMKu+3sosYwNwlCaxm+cRGbndILUYbyV1QmX2cKh0Llb2 Y2fdP4bjxUyTULiZEbSZWHJsLw2RUHw6SwbFi1OKXbO1Qgwvs3iSrTK4X hq5TCvvzfBqDLDUGLr4VfnPsen+mrv9slbnxFpyi5EeTnaFY19MuYUkp4 cPjxL8hMpb7t+31mGg/3iRiIv78deODujxATtVFyUFtgvWEaeMSz/BhrS w==; X-IronPort-AV: E=McAfee;i="6500,9779,10465"; a="296236316" X-IronPort-AV: E=Sophos;i="5.93,303,1654585200"; d="scan'208";a="296236316" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Sep 2022 08:27:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,303,1654585200"; d="scan'208";a="615303870" Received: from viggo.jf.intel.com (HELO ray2.amr.corp.intel.com) ([10.54.77.144]) by orsmga002.jf.intel.com with ESMTP; 09 Sep 2022 08:27:29 -0700 From: Dave Hansen To: linux-kernel@vger.kernel.org Cc: Dave Hansen , kernel test robot , Peter Zijlstra , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH] x86/mm: Set NX bit when making pages present Date: Fri, 9 Sep 2022 08:27:21 -0700 Message-Id: <20220909152721.1685334-1-dave.hansen@linux.intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The x86 mm code now actively refuses to create writable, executable mappings and warns when there is an attempt to create one. 0day ran across a case triggered by module unloading, but that looks to be a generic problem. It presumably goes like this: 1. Load module with direct map, P=1,W=1,NX=1 2. Map module executable, set P=1,W=0,NX=0 3. Free module, land in vfree()->vm_remove_mappings() 4. Set P=0 during alias processing, P=0,W=0,NX=0 5. Restore kernel mapping via set_direct_map_default_noflush(), set P=1,W=1, resulting in P=1,W=1,NX=0 That's clearly a writable, executable mapping which is a no-no. The new W^X code is clearly doing its job. Fix it by actively setting _PAGE_NX when creating writable mappings. One concern: I haven't been able to actually reproduce this, even by loading and unloading the module that 0day hit it with. I'd like to be able to reproduce this before committing a fix. Reported-by: kernel test robot Signed-off-by: Dave Hansen Cc: Peter Zijlstra (Intel) Cc: Andy Lutomirski Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: x86@kernel.org Cc: "H. Peter Anvin" Link: https://lore.kernel.org/all/fcf89147-440b-e478-40c9-228c9fe56691@intel.com/ -- 0day folks, please do share these as they come up. We want to keep fixing them. --- arch/x86/mm/pat/set_memory.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 1a2d6376251c..5fb5874ea2c6 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2247,6 +2247,12 @@ static int __set_pages_p(struct page *page, int numpages) .mask_clr = __pgprot(0), .flags = 0}; + /* + * Avoid W^X mappings that occur if the old + * mapping was !_PAGE_RW and !_PAGE_NX. + */ + pgprot_val(cpa.mask_set) |= __supported_pte_mask & _PAGE_NX; + /* * No alias checking needed for setting present flag. otherwise, * we may need to break large pages for 64-bit kernel text -- 2.34.1