Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3932043rwa; Tue, 23 Aug 2022 12:48:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR52xoUx2mEBke+2ParZ+USbkWu6OdRNG6RDndpBQJ1kkVfAfl7Oi64Z3IEjYmishi4U383N X-Received: by 2002:a17:902:f0d4:b0:172:aa2a:49ac with SMTP id v20-20020a170902f0d400b00172aa2a49acmr25222015pla.9.1661284089090; Tue, 23 Aug 2022 12:48:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661284089; cv=none; d=google.com; s=arc-20160816; b=VQ68kQ9DKAsm9c2O6hprJq7P8M+f+OxbK93HEZhf3avm2kXMT98H2JiMQrKPqgRIhD 0pRu4bqwC/q2NdPpFvMCnoDcgdwriMDzNsn9C17XDyV87nq3XyoAmG+Sx/znjZiEVbnX pJL6PUEq6+mmKD9FH1MbbQwKiMoLGytPBqp6dJ5Rz2zGdLe4iPp9zmrIy5+/2sYdgm95 pFd8ipUUfgcA0q9NVWhPCZ9vg409P6IXOr1bePqpbwxMBsFhKAR4Q0dUo/3g4Ugf739L PxmRCHfDvZInPgnHhi09/57prqrm/ie8Ozu8wPJOTjeDowGbmCP7vZ9xKF6mTqLw921O OUBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=8prhI6GQnzEv0AB7c0CxDVueI7N8Z4o3xoGWMtMEbMM=; b=zQHlya/bx/IZVrWqprvXQdaO6Hdc0O3h01abOdyrnto9/CTQR2pW6Ud6cS3K6xkxrp 2OUWGcQIuVyk0C6GpuD4X6MSDoHF0bAVSA9loYWzYfrfHHKCbOlfjK/eBm1Qz+y1itz8 eq/NiTaCpMZ+C9UcQwlVtylqO0IIgaRG7uhHE5EMRqsEMH1D7hyrse1EKzvrHTeC3UGu hQ2cbI2/YHkE07u7cpsoacZSzNwjUGYr2/SUk/3c91XsxepBcndjalJ6M2hWBzWdXHWR ++Jw5+duHtH40Z3fg3+x185gH6/gnRF1HVJSt3c/XNvu8TzHJFt4D7RCADSQz9PVe7Zf Rx7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=j9qgr9DW; 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 g3-20020a635643000000b0041ddd5b89ecsi16379974pgm.419.2022.08.23.12.47.58; Tue, 23 Aug 2022 12:48:09 -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=j9qgr9DW; 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 S232027AbiHWT02 (ORCPT + 99 others); Tue, 23 Aug 2022 15:26:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231877AbiHWT0G (ORCPT ); Tue, 23 Aug 2022 15:26:06 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B683D326CE for ; Tue, 23 Aug 2022 11:12:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661278345; x=1692814345; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=EBJWFGhgoiE90SZv+jqTQEYMjFebOZvYueUFtmHFLJI=; b=j9qgr9DW9FcHZyJvDawhoGtRrqiDJfPTjI80ZZCK5HpoesQuvN58q2CK auHflwStrN3sEvgPm1Z1w7eP4lePHVXQ+qtEh+fOVabncqDiYCQQpHgeY /Catt/+GIbYbJ5r5x6s4R4ieA/O/rCcNGIOF8XxQV/vpX31IjKFJ28as9 kEm103yON/5cxZACFjxury5gtU6pqpvv5SvKgndVPKGo5cLxg9Cr71F0m 9EZgtPTucYRI1+Gg1pbvPtslcXKjH6Qk+LGXQOPoXwd/dcPttPTwmMCHR tpSI9RIVkZpb6jT1TbpatMDernrwCtrALx56EfxRVKJJZHY0wfz9SlgY4 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10448"; a="280734199" X-IronPort-AV: E=Sophos;i="5.93,258,1654585200"; d="scan'208";a="280734199" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Aug 2022 11:12:24 -0700 X-IronPort-AV: E=Sophos;i="5.93,258,1654585200"; d="scan'208";a="677721791" Received: from tmnguye8-mobl1.amr.corp.intel.com (HELO [10.212.174.243]) ([10.212.174.243]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Aug 2022 11:12:24 -0700 Message-ID: Date: Tue, 23 Aug 2022 11:12:24 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: PKU usage improvements for threads Content-Language: en-US To: =?UTF-8?Q?Stephen_R=c3=b6ttger?= Cc: Kees Cook , Dave Hansen , x86@kernel.org, linux-kernel@vger.kernel.org, Andy Lutomirski References: <202208221331.71C50A6F@keescook> From: Dave Hansen In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 On 8/23/22 04:08, Stephen Röttger wrote: > On Mon, Aug 22, 2022 at 11:11 PM Dave Hansen wrote: >> On 8/22/22 13:40, Kees Cook wrote: >>> 1) It appears to be a bug that a thread without the correct PK can make >>> VMAs covered by a separate PK, out from under other threads. (e.g. mmap >>> a new mapping to wipe out the defined PK for it.) It seems that PK checks >>> should be made when modifying VMAs. >> >> Could you give an example of this? Is this something along the lines of >> a mmap(MAP_FIXED) wiping out an earlier mapping? Or, is it more subtle >> than that? > > Yes, that's one example. And the same applies to other operations on the > VMA. E.g. another case we'd like to prevent would be munmap(addr) where > addr is covered by a pkey to which the calling thread doesn't have access > permissions to. Yeah, that's something for which our defenses are quite weak. But, it also calls for a very generic mm/ solution and not something specific at all to pkeys. I assume that you wouldn't want to turn off *all* mmap(), MAP_FIXED or munmap() in the process. You just want to make one or more VMAs more or less immutable. That _sounds_ like a topic that would have broached at some point in the past, although it doesn't ring any bells. The concept would make a good lightning talk at Plumbers of LSF/MM. >>> 2) It would be very helpful to have a mechanism for the signal stack to >>> be PK aware, in the sense that the kernel would switch to a predefined >>> PK. i.e. having a new interface to sigaltstack() which includes a PK. >> >> Are you thinking that when switching to the sigaltstack that it would >> also pick up a specific PKRU value? Or, that it would ensure that PKRU >> allows access to the sigaltstack's pkey? > > Either of those would work for us. > >> Logically something like this: >> >> stack_t sas = { >> ss_sp = stack_ptr; >> ss_flags = ... flags; >> ss_size = ...; >> ss_pkey = 12; >> }; >> >> Then the kernel would set up RSP to point to ss_sp, and do (logically): >> >> pkkru &= ~(3<<(12*2)); // clear Write and Access-disable for pkey-12 >> >> before building the signal frame running the signal handler? > > Yeah, that would work for our use case. > We also have a doc discussing this in more detail :) : That doesn't seem like it would be too much of a stretch. There's a delicate point when building the stack frame that the kernel would need to move over to the new PKRU value to build the frame before it writes the *OLD* value to the frame. But, it's far from impossible. I also bet we could do this with minimal new ABI. There's already a ->ss_flags field. We could assign a flag to mean that stack_t doesn't end at '->ss_size' and that there's a pkey value *after* ss_size. I do think having a single pkey that is made accessible before signal entry is a more flexible ABI than taking an explicit PKRU value. I think that would allow just reusing sys_sigaltstack().