From patchwork Wed Sep 20 22:45:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9962819 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 93C2160208 for ; Wed, 20 Sep 2017 22:48:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 856F828925 for ; Wed, 20 Sep 2017 22:48:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7A6F3290DB; Wed, 20 Sep 2017 22:48:51 +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.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID 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 2915928925 for ; Wed, 20 Sep 2017 22:48:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751955AbdITWrG (ORCPT ); Wed, 20 Sep 2017 18:47:06 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:38501 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751899AbdITWrA (ORCPT ); Wed, 20 Sep 2017 18:47:00 -0400 Received: by mail-pg0-f65.google.com with SMTP id m30so2366737pgn.5; Wed, 20 Sep 2017 15:46:59 -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=Na+LcD6lqvx6xIGMkP2UyfXEHy8yAmp8/kQocI7xCYc=; b=aa/xwiIY8GXBx8481C4rZLmDlmE0TeccbXTwb6tmfotlLjUHHvyQ5xuKnn8oCXDkwM a+D0Dn/vmn2kRwyM185JCrksx8cN8TXTCpaMZVUquMCtcNkclH3OVilCBF43YW98y5rA fuW0tAckwilTghmbHzeuRhABDmJjKVHtN6GmweUAkDI0gsfNpGNSbchq8Q+u7vpBMRRI sRn4//MU9ENYTfyylhuTyt7KHfSZ1RubhY8Rn6+x8U8Fx74RpW6dOwyKJ4t9OYdnx5ny vcz0Jow0Dv1inbvp/jDYjmjYGk5A9rfNZu3Z/47V0oZ67oeDAGLFBf5ohh4DM5pYv53F hX0g== 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=Na+LcD6lqvx6xIGMkP2UyfXEHy8yAmp8/kQocI7xCYc=; b=nlbUcIx9ZMzichKdNqm02GrTdI6jVkQv/hXP7vI043LHKd2BM1+VPGCM1F6FTlg6H2 u0GHbab9GpDvyFfm2MVINlKkgdc986Yv6m7hNrZ8MjGc/3+UuFlS4YGyqeq8ff7Z8YcB mZXn59fHtXu6dOSFQYasnn+lh+Tkvv62i39YuRi7rMukfxclWO2IramJ151S6KZenh84 m77ZEnvKRDISt/WWuD5tTNiTVTuVfTIHE58R/moER/mStRnFi+VSLu/VeaXf6/N005Ek ehytH5ntbc2SOmhGluSF+rObR7kptLscQ/MAkKA73+3A9rI/HF8BRPaz7pRn1xpYvWE+ qK/A== X-Gm-Message-State: AHPjjUiG8eNWYIbIhdxb4I4CCA4XoawBLNyOo+f/sdsiEfzDos+sal80 CMIUDBK8gnYwJ3BtR9XAiLv4V6F8TdM= X-Google-Smtp-Source: AOwi7QANmLwPyt/7LqNoZvEK8Q+r3p4fDCBZJzr0+cc5omYgnVKLPY9BkIjuQmHk+pfzzACLARwAOg== X-Received: by 10.84.252.144 with SMTP id y16mr3608861pll.113.1505947619063; Wed, 20 Sep 2017 15:46:59 -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.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 20 Sep 2017 15:46:58 -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 07/25] fscrypt: new helper function - fscrypt_prepare_link() Date: Wed, 20 Sep 2017 15:45:47 -0700 Message-Id: <20170920224605.22030-8-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-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers Introduce a helper function which prepares to link an inode into a possibly-encrypted directory. It handles setting up the target directory's encryption key, then verifying that the link won't violate the constraint that all files in an encrypted directory tree use the same encryption policy. Signed-off-by: Eric Biggers --- fs/crypto/hooks.c | 15 +++++++++++++++ include/linux/fscrypt_notsupp.h | 9 +++++++++ include/linux/fscrypt_supp.h | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c index 069088e91ea9..8b90217320dd 100644 --- a/fs/crypto/hooks.c +++ b/fs/crypto/hooks.c @@ -47,3 +47,18 @@ int fscrypt_file_open(struct inode *inode, struct file *filp) return err; } EXPORT_SYMBOL_GPL(fscrypt_file_open); + +int __fscrypt_prepare_link(struct inode *inode, struct inode *dir) +{ + int err; + + err = fscrypt_require_key(dir); + if (err) + return err; + + if (!fscrypt_has_permitted_context(dir, inode)) + return -EPERM; + + return 0; +} +EXPORT_SYMBOL_GPL(__fscrypt_prepare_link); diff --git a/include/linux/fscrypt_notsupp.h b/include/linux/fscrypt_notsupp.h index 99e8ee6f2ce4..2cb400440be3 100644 --- a/include/linux/fscrypt_notsupp.h +++ b/include/linux/fscrypt_notsupp.h @@ -189,4 +189,13 @@ static inline int fscrypt_file_open(struct inode *inode, struct file *filp) return 0; } +static inline int fscrypt_prepare_link(struct dentry *old_dentry, + struct inode *dir, + struct dentry *dentry) +{ + if (IS_ENCRYPTED(dir)) + return -EOPNOTSUPP; + return 0; +} + #endif /* _LINUX_FSCRYPT_NOTSUPP_H */ diff --git a/include/linux/fscrypt_supp.h b/include/linux/fscrypt_supp.h index 521f15adf83c..ebc0cc41aaf9 100644 --- a/include/linux/fscrypt_supp.h +++ b/include/linux/fscrypt_supp.h @@ -172,4 +172,33 @@ static inline int fscrypt_require_key(struct inode *inode) extern int fscrypt_file_open(struct inode *inode, struct file *filp); +extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir); + +/** + * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory + * @old_dentry: an existing dentry for the inode being linked + * @dir: the target directory + * @dentry: negative dentry for the target filename + * + * A new link can only be added to an encrypted directory if the directory's + * encryption key is available --- since otherwise we'd have no way to encrypt + * the filename. Therefore, we first set up the directory's encryption key (if + * not already done) and return an error if it's unavailable. + * + * We also verify that the link will not violate the constraint that all files + * in an encrypted directory tree use the same encryption policy. + * + * Return: 0 on success, -ENOKEY if the directory's encryption key is missing, + * -EPERM if the link would result in an inconsistent encryption policy, or + * another -errno code. + */ +static inline int fscrypt_prepare_link(struct dentry *old_dentry, + struct inode *dir, + struct dentry *dentry) +{ + if (IS_ENCRYPTED(dir)) + return __fscrypt_prepare_link(d_inode(old_dentry), dir); + return 0; +} + #endif /* _LINUX_FSCRYPT_SUPP_H */