From patchwork Wed Sep 20 22:45:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9962873 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5D47460208 for ; Wed, 20 Sep 2017 22:50:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F77428A28 for ; Wed, 20 Sep 2017 22:50:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 442942924A; Wed, 20 Sep 2017 22:50:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 001F028A28 for ; Wed, 20 Sep 2017 22:50:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751821AbdITWuF (ORCPT ); Wed, 20 Sep 2017 18:50:05 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:38488 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751873AbdITWq5 (ORCPT ); Wed, 20 Sep 2017 18:46:57 -0400 Received: by mail-pg0-f65.google.com with SMTP id m30so2366693pgn.5; Wed, 20 Sep 2017 15:46:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zk/GB8MlBbGtgz+89+4L5zxIzbwfvPyYyWzdMzv86nU=; b=psOkbfqtPyoPJbDDX7Xe+cMoRvOejwU/8FnfA95tq4/8otus2j3GKvamK+b4UH6obL phK3XRoDhRASO+XmKxvZDwG26CxMvcAdyzEozyjkslryfBUrVWcKVyPL5a7CDK9iSca4 611FcNnuGA3GMNgr4s31hDZf6xNzdCjw5F3WHZ5jYxMMLCA3VsMAQljAysP56BZQ7+x+ Zs4xuHfYYFWI/uzvlB+UTNG3+Ig5CLBliJk6SEaT9Vvb3xBzK5CFzAPEqwWCiIeBcq7d NEiHmeHk57HOcH3dfKa9ylKrQZ14kd4SYFIkacmkDoCq6YI5nqp2VDzsXaLTuBQ6PRUQ KI2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zk/GB8MlBbGtgz+89+4L5zxIzbwfvPyYyWzdMzv86nU=; b=B8nOEEXUT9yTbNZrRcdnWWn2RdThtfsQ/vcOx7YkZag2Ks6zaxMV/xV4T0PeUW9Yet Hg5s24Mf0P8Lh8utmbms5Bw37tG8gpbrB0GfIJJDTyJEph6rJhz/PwuxUiBT5s1A3XMd DjH5tbGVUkbF3113+tFuXD+dLcNYjC1RM6hW1J0/Y5ffEehwEr1T8weEwGcjJuuVGksA vlz1e0Qcq4CTb/1VuFuklTehwYgjBY51NiRXIzstnJUmVoeVIUFujbrSpnxZjQ1i6MoJ ykoIA8Rw7fU7xXrkuoQk9oz6R5HuKa58BMvmBI4rjUNQbsOniv3kR7Q6e8ZtZXiwXk0K rk9g== X-Gm-Message-State: AHPjjUhM7UhyYracbamUkeUqXuo2NVE5qIVcvsEgPf/AhRUd+WtzAm3t OOUjG0ERP5mSgmFoJJPYipD4xtMFZIw= X-Google-Smtp-Source: AOwi7QC9kOJQAvuSgsJvinxg+ZX2ljYeSWP1Q5Twwbn/UHYrAJYZbBLHs9PtAoxKPW3hEAlamw1q9Q== X-Received: by 10.99.188.25 with SMTP id q25mr1615630pge.54.1505947616663; Wed, 20 Sep 2017 15:46:56 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.174.81]) by smtp.gmail.com with ESMTPSA id j2sm6249pgn.26.2017.09.20.15.46.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 20 Sep 2017 15:46:56 -0700 (PDT) From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, "Theodore Y . Ts'o" , Jaegeuk Kim , Michael Halcrow , Eric Biggers Subject: [PATCH 05/25] fscrypt: new helper function - fscrypt_require_key() Date: Wed, 20 Sep 2017 15:45:45 -0700 Message-Id: <20170920224605.22030-6-ebiggers3@gmail.com> X-Mailer: git-send-email 2.14.1.821.g8fa685d3b7-goog In-Reply-To: <20170920224605.22030-1-ebiggers3@gmail.com> References: <20170920224605.22030-1-ebiggers3@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers Add a helper function which checks if an inode is encrypted, and if so, tries to set up its encryption key. This is a pattern which is duplicated in multiple places in each of ext4, f2fs, and ubifs --- for example, when a regular file is asked to be opened or truncated. Signed-off-by: Eric Biggers --- include/linux/fscrypt_notsupp.h | 8 ++++++++ include/linux/fscrypt_supp.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/fscrypt_notsupp.h b/include/linux/fscrypt_notsupp.h index 3f1325ec4646..3cfc953fef71 100644 --- a/include/linux/fscrypt_notsupp.h +++ b/include/linux/fscrypt_notsupp.h @@ -174,4 +174,12 @@ static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, return -EOPNOTSUPP; } +/* hooks.c */ +static inline int fscrypt_require_key(struct inode *inode) +{ + if (IS_ENCRYPTED(inode)) + return -EOPNOTSUPP; + return 0; +} + #endif /* _LINUX_FSCRYPT_NOTSUPP_H */ diff --git a/include/linux/fscrypt_supp.h b/include/linux/fscrypt_supp.h index 32e2fcf13b01..b6d4b5d303a3 100644 --- a/include/linux/fscrypt_supp.h +++ b/include/linux/fscrypt_supp.h @@ -142,4 +142,32 @@ extern void fscrypt_pullback_bio_page(struct page **, bool); extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, unsigned int); +/* hooks.c */ + +/** + * fscrypt_require_key - require an inode's encryption key + * @inode: the inode we need the key for + * + * If the inode is encrypted, set up its encryption key if not already done. + * Then require that the key be present and return -ENOKEY otherwise. + * + * No locks are needed, and currently the key will live as long as the struct + * inode --- so it won't go away from under you. + * + * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code + * if a problem occurred while setting up the encryption key. + */ +static inline int fscrypt_require_key(struct inode *inode) +{ + if (IS_ENCRYPTED(inode)) { + int err = fscrypt_get_encryption_info(inode); + + if (err) + return err; + if (!fscrypt_has_encryption_key(inode)) + return -ENOKEY; + } + return 0; +} + #endif /* _LINUX_FSCRYPT_SUPP_H */