Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3615956ybz; Mon, 27 Apr 2020 19:49:50 -0700 (PDT) X-Google-Smtp-Source: APiQypLMyoeWGFGdB5XxXu9okWmPeMpqTGZxyOsUX/hNM+uSpiWRrbCD3hydDRrfXMksjQ21qdPr X-Received: by 2002:a17:906:4bc9:: with SMTP id x9mr22054457ejv.27.1588042189993; Mon, 27 Apr 2020 19:49:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588042189; cv=none; d=google.com; s=arc-20160816; b=FOnzawWegG9hU7yFOaqqHdKigqp7+UM92g8TPrZxWybfZ1PC3594lkb9BnXItbpsBx HNvQCyuVMi+GCmEKS4JZKNifZssOLAyueVtH5PQQ/cCNlc9RUbGnrT0Z5haNL2BC0eEy jtI7jf+DuvUtUPsKsxSRRg1JA8mRbTxOf1IUzFfkmXLpJuYtpkrd57NDZduOzyEZ5Bs9 HbRAahBBxTGOD+0Vb1WSYI7nv5ixoKfdNIGXW3daQKUfZ8U9jCZSqd3eq8UolO83iEjI jIdLnb8wnmwmr6LJZ4u9p31yHx+nd1k6TGOUrVpFC5olSSyl9fzHqHoMefx1wvQNGNxI u2oQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=nFzIOAGm0reWkAuXMNlRwUHueVUa5ycfy2mznkYNS20=; b=lvvWM1WRWjAIhcJ4QJ24Uv5kNqlNeYzmNf9yOlBngA+yLPluwPEPfI1NeDAme3KuX0 /gDEQ6X4Sbg8K4i+slDqBmjBW7UfgTHM7Q1gCNtaGxRjprnUKchTlKJ6xuhiNREOObyI hMLZSlaeXiXifPT1iCqRZRAmjXGbxpGcbP5TeTtCNeaUGKoTwUzO3tIm96teVKnyfe4S BNYTnLHpMqV3t2OQhTp4nZVB2b7yNLhitrA5c8473rIaGkIIO2bri5rpcTTuUYTXJVSx 9+FyUHpPsv/TSuxfeGXJ1DTdWn3ul5mc1N7Nu+QVtX/ysCwh3tN2b19L+W+eytP7naRV AfUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Rcz7obW4; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id nb6si929054ejb.255.2020.04.27.19.49.20; Mon, 27 Apr 2020 19:49:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Rcz7obW4; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726318AbgD1CqR (ORCPT + 99 others); Mon, 27 Apr 2020 22:46:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:53644 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726264AbgD1CqQ (ORCPT ); Mon, 27 Apr 2020 22:46:16 -0400 Received: from gmail.com (unknown [104.132.1.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C0F79206D9; Tue, 28 Apr 2020 02:46:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588041976; bh=5m8r1IGjxlxCRpj6aIK48lhOdzD/MmpN0dyJZAgoIhI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Rcz7obW4+GLyHh9wb59v7RqJvV8A87h5PViMuZbVkoMO8FhrrV+5cv0wZ8c6bDviT EXtDtywwaVt0YUWODJzuRJwJxOAW5n4wDKpzAnjQDoZXKZIvmjU/c1QeN5PuEBnUJN R0ND6s1V2Hd1BJngTdPgQ9c0O/E8jNK0MN0AZJ0I= Date: Mon, 27 Apr 2020 19:46:14 -0700 From: Eric Biggers To: Satya Tangirala Cc: Christoph Hellwig , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, linux-fscrypt@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-ext4@vger.kernel.org, Barani Muthukumaran , Kuohong Wang , Kim Boojin Subject: Re: [PATCH v10 02/12] block: Keyslot Manager for Inline Encryption Message-ID: <20200428024614.GA251491@gmail.com> References: <20200408035654.247908-1-satyat@google.com> <20200408035654.247908-3-satyat@google.com> <20200422092250.GA12290@infradead.org> <20200428021441.GA52406@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200428021441.GA52406@google.com> Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Tue, Apr 28, 2020 at 02:14:41AM +0000, Satya Tangirala wrote: > > > +int blk_ksm_evict_key(struct blk_keyslot_manager *ksm, > > > + const struct blk_crypto_key *key) > > > +{ > > > + struct blk_ksm_keyslot *slot; > > > + int err = 0; > > > + > > > + blk_ksm_hw_enter(ksm); > > > + slot = blk_ksm_find_keyslot(ksm, key); > > > + if (!slot) > > > + goto out_unlock; > > > + > > > + if (atomic_read(&slot->slot_refs) != 0) { > > > + err = -EBUSY; > > > + goto out_unlock; > > > + } > > > > This check looks racy. > Yes, this could in theory race with blk_ksm_put_slot (e.g. if it's > called while there's still IO in flight/IO that just finished) - But > it's currently only called by fscrypt when a key is being destroyed, > which only happens after all the inodes using that key are evicted, and > no data is in flight, so when this function is called, slot->slot_refs > will be 0. In particular, this function should only be called when the > key isn't being used for IO anymore. I'll add a WARN_ON_ONCE and also > make the assumption clearer. We could also instead make this wait for > the slot_refs to become 0 and then evict the key instead of just > returning -EBUSY as it does now, but I'm not sure if it's really what > we want to do/worth doing right now... Note that we're holding down_write(&ksm->lock) here, which synchronizes with someone getting the keyslot (in particular, incrementing its refcount from 0) because that uses down_read(&ksm->lock). So I don't think there's a race. The behavior is just that if someone tries to evict a key that's still in-use, then we'll correctly fail to evict the key. "Evicting a key that's still in-use" isn't supposed to happen, so printing a warning is a good idea. But I think it needs to be pr_warn_once(), not WARN_ON_ONCE(), because WARN_ON_ONCE() is for kernel bugs only, not userspace bugs. It's theoretically possible for userspace to cause the same key to be used multiple times on the same disk but via different blk_crypto_key's. The keyslot manager will put these in the same keyslot, but there will be a separate eviction attempt for each blk_crypto_key. For example, with fscrypt with -o inlinecrypt and blk-crypto-fallback, userspace could create an encrypted file using FSCRYPT_MODE_ADIANTUM and flags == 0, then get its encryption nonce and derive the file's encryption key. Then in another directory, they could set FSCRYPT_MODE_ADIANTUM and flags == FSCRYPT_POLICY_FLAG_DIRECT_KEY, and use the other file's encryption key as the *master* key. That would be totally insane for userspace to do. But it's possible, so we can't use WARN_ON_ONCE(). - Eric