Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6363415iob; Tue, 10 May 2022 17:10:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhwmJl3noQKEfudIsGQXG1SagnSDmisEQhHVKTqmeubCYDBpZZ0UZpqOdROtKlA0QWfAnp X-Received: by 2002:a05:6402:2692:b0:427:ddba:d811 with SMTP id w18-20020a056402269200b00427ddbad811mr25475027edd.343.1652227801639; Tue, 10 May 2022 17:10:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652227801; cv=none; d=google.com; s=arc-20160816; b=i0/BFBArNe0WnKjHt24Fm9QYXXKpQ1K4xkQVyhr7eS1tpLbPOiXw3mFMH1x2aBE6z3 0+brNHG0IgNVGmBb5irQcvMdQKpLXSQHItnfhdDkOr5oFWFUPgV6mbvxykf1vhjyVYj9 i0ji3XTUnnQUMxkSocy020qxROmHqK0VixmNxev842UBfUOtGoxVZsrKAutLFbFkkptV zjCOUEkzov+axLqaMKHSTEqjsbBAqOx0L73pe4VX9VdnSDHZmP2f9tsyzLr13HVoxjaA +k95oJT/n6uCQvf3x7qmSTUJt/zwXwe/EWkXRuQkqvTHyPrzlmun6FT9BrMhbBhSAG0C n13w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=9wKOeHUc2xnhBn2gmB2Ek1IaCVg0XLzxAc3hNyfWilQ=; b=DBvKFnLUVS3Uf/lfwSpBAuC7G8PZS/dyAWrH7M/uOpwvmzhkTFR48a9OfrBfhiVcTF u1wyUpvNbr9uYciKvJYZRR4GrZYBcssnf9qXshrfyee467QIpsgjZdpJhIEz7kQjIu7N Rxx8Tn9+qxN68RMzggfKlf2rUaaeAOz3m6Xcb+SfpjJgaubMdZbYo1TOA1Azxlw68Sul VuarAEChWz9Z3qbb6lwxnYKPZWa1a5abQ3yToqrwnZFKhJBIbn8XZNGZJS7NHCOToNIe wwi9hwBRpDXOLIix7o4o2pFl99XC/qxumaIVRvpvpIqjQhmF09q787E+KzLoYmJOYzNU unpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=K2ASJuEd; 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=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c8-20020a05640227c800b00427ba3579f5si883861ede.26.2022.05.10.17.09.34; Tue, 10 May 2022 17:10:01 -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=@chromium.org header.s=google header.b=K2ASJuEd; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232511AbiEJWJB (ORCPT + 99 others); Tue, 10 May 2022 18:09:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231561AbiEJWJA (ORCPT ); Tue, 10 May 2022 18:09:00 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F4E9289BD9 for ; Tue, 10 May 2022 15:08:58 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id n18so78700plg.5 for ; Tue, 10 May 2022 15:08:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=9wKOeHUc2xnhBn2gmB2Ek1IaCVg0XLzxAc3hNyfWilQ=; b=K2ASJuEd+iD69DLoR1SkVzlMzX/UTfBdn/8tDBQ/EVWLM4dHjtlT+uiOV6MEp7j4mW UGPc+jw7BrCbQvRcsUTyE/u8BV+0iBPiMJkLu3GLKxLsY7ULcriSxDZXYD/iH17Ua4SD 1Uh00kSRYnnOqL7wyAOlfDKfayhL+wp642usM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=9wKOeHUc2xnhBn2gmB2Ek1IaCVg0XLzxAc3hNyfWilQ=; b=nHmd5rT7PUI3m/d/+z5jaRKSgy6oidbMnsFY3LtpJ4jIqK/bZOqk/jEn+zlgK+gy0u VtysWmzsibca4/848nkezvD2TCiB9vkEjqdNfmKdGBB2jeCnlAIv/9vKGoHUHwoSpCRd qeM0rFUkb/PHjwmLEMzBLTG95H8Hla4AZsgj7nnduZydfrEw9nt3HDyeeHjm787w8jpC MLIIgdagA2rCqk/CxpQt183cx7nXOOAOe3xozvrG4kC3qr4ZNq5E918lhVAcCUV6h2S6 3Qa9wk2q87F5rlnG1KA1bttkhkZvf4PqBhbSDcv7Pxbuc5kZWY5AhGgadv90Xd6mmQTm OvRg== X-Gm-Message-State: AOAM530VXBGaAbRBy4ekERoZbL51lWrmOY91NoIeP0NqHPKq4L9YpBGp R/fOOVHLSmiWHEd0z6be6uljbg== X-Received: by 2002:a17:90b:1894:b0:1dc:1028:aecb with SMTP id mn20-20020a17090b189400b001dc1028aecbmr1957870pjb.106.1652220538079; Tue, 10 May 2022 15:08:58 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id k6-20020a63d846000000b003c14af505f9sm221971pgj.17.2022.05.10.15.08.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 May 2022 15:08:57 -0700 (PDT) Date: Tue, 10 May 2022 15:08:56 -0700 From: Kees Cook To: Ira Weiny Cc: Dave Hansen , "H. Peter Anvin" , Dan Williams , Fenghua Yu , Rick Edgecombe , "Shankar, Ravi V" , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH V10 14/44] mm/pkeys: Introduce pks_set_readwrite() Message-ID: <202205101504.5B5C693F7C@keescook> References: <20220419170649.1022246-1-ira.weiny@intel.com> <20220419170649.1022246-15-ira.weiny@intel.com> <202205091304.434A9B45@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable 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 Tue, May 10, 2022 at 02:33:03PM -0700, Ira Weiny wrote: > On Mon, May 09, 2022 at 02:38:38PM -0700, Kees Cook wrote: > > [...] > > Better yet would be: > > > > preempt_disable(); > > rdmsrl(MSR_IA32_PKRS, pkrs); > > pkrs = pkey_update_pkval(pkrs, pkey, protection); > > pks_write_pkrs(pkrs); > > current->thread.pkrs = pkrs; > > preempt_enable(); > > > > Then cross-thread attacks cannot corrupt the _other_ PKS keys (i.e. > > write the desired changes to target's current->thread.kprs and trigger > > an update to a different pkey, resulting in flushing the attacker's > > changes to that CPU's pkey state. > > Unfortunately I don't think this entirely prevents an attack through the > thread.pkrs value. thread.pkrs has to be used to set the MSR when a thread is > scheduled. Therefore the rdmsrl above will by definition pick up the > thread.pkrs but from an earlier time. Ooh, good point, yeah. > I'm not opposed to doing this as I think it does reduce the time window of such > an attack but I wanted to mention it. Especially since I specifically avoided > ever reading the MSR to improve performance. > > I'm going to run some tests. Perhaps the MSR read is not that big of a deal > and I can convince myself that the performance diff is negligible. Yeah, given "loaded at scheduling" point, there's not much benefit in read/write pair. I think my first suggestion about only writing to thread.pkrs after the write, etc, still stands. I'll ponder this a bit more. > > While adding these, can you please also add pks_set_nowrite()? This > > will be needed for protecting writes to memory that should be otherwise > > readable. > > I have a patch to add pks_set_readonly() but I was advised to not send it > because this series does not include a use case for it. (PMEM does not need > it.) > > Dave, Dan? Are you ok adding that back? > > Kees would you prefer pks_set_nowrite() as a name? I think nowrite is the better name (in the sense that "read-only" can sometimes imply non-executable). > > > > With these changes it should be possible to protect the kernel's page > > table entries from "stray" writes. :) > > Yes, Rick has done some great work in that area. Oh! I would _love_ to see this series. I was trying to scope the work yesterday but gave up after I couldn't figure out the qemu PKS trick. :) -- Kees Cook