From patchwork Mon Oct 9 19:15:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9994113 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 5DE11603B5 for ; Mon, 9 Oct 2017 19:19:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 52210284AA for ; Mon, 9 Oct 2017 19:19:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4726A286BF; Mon, 9 Oct 2017 19:19:34 +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 666BB286BD for ; Mon, 9 Oct 2017 19:19:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755264AbdJITTa (ORCPT ); Mon, 9 Oct 2017 15:19:30 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:32908 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754830AbdJITTI (ORCPT ); Mon, 9 Oct 2017 15:19:08 -0400 Received: by mail-pf0-f193.google.com with SMTP id m28so29746388pfi.0; Mon, 09 Oct 2017 12:19:08 -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=Vn5RVAZadwxXyOOngRLJeTeT7TsGPBcXkeCXRxLx8Rs=; b=sdxmU7EVxngr9gOR3kPoQS+3fx/9qb+cnMOqYC/nKS7l6jH5CH1rug87QIPQbiJRgn JaAUByHZNl77k3HrBaH0Q3cA6EG3HPERgGmHI787qeXNC103cgL2ooJL0XGnhS8pXnfY AaXyGP3oLdDiF0VnVkFzN+zj97jLPMzZvWf2XT5ajWv4pwSpmDi6xUFcsEEuJ3dCxvKt 1H5h0WLGpNIuNSitt12zNLjJELg3976uZYUvDGG9v6Otued8fbqSf86ncE7s+z1PHoD6 FUOLOF7Q3lEh+dmr20A0DS5LpthO8M/SSSB4LJPCcArBNHctU+dnQ6QSNFJ9h/V6NY9P Kwaw== 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=Vn5RVAZadwxXyOOngRLJeTeT7TsGPBcXkeCXRxLx8Rs=; b=iDV2E65+UdxDxSVB/mkKULxm43z/7x2SCv/xcd+6ibeR1I1nhWu5PQnkP2fZsV3Q1m iCLd5vK3CJbhc1vvaRC8qF5Cs5dsRm7MzqQcsQSMpIXMNFaliPt0Wd5jUGcxtLyzbN6K 0IhYWcnBZ/QIeHAhyFrmQ7d30zMaXo2Oy0zS2Ff5BnyvppY0CcdVK69/fklRSFDzRGdk TAPUG3woQ666L3iF4hwk3UkXz19A6osXcoWMyuwgx+Q9mawKLcEKxL36Wfig2zldrjik yniLVJ6RpxJgsMqZwQqn2lP08LodUXnt9BPz20cylw0256bvflrZktMZqWsEbIYHQhMU o39A== X-Gm-Message-State: AMCzsaU04Ya9XskMBIYc7/0Egm0W3m/tZXrdWEQnPZhbYbYcLn89gjV0 LjKyONmfLG9c8A6zoZaTAqDiS2YB X-Google-Smtp-Source: AOwi7QCss3GfOzOaIbTeiWn7MqfuppaAu3V1vfc+DAzSWrsVujvGqYV3BK2pkTrAX2SsqU+amt1wYA== X-Received: by 10.84.252.152 with SMTP id y24mr10138353pll.392.1507576747571; Mon, 09 Oct 2017 12:19:07 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.174.81]) by smtp.gmail.com with ESMTPSA id n29sm17039819pgf.44.2017.10.09.12.19.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 09 Oct 2017 12:19:07 -0700 (PDT) From: Eric Biggers To: linux-fscrypt@vger.kernel.org, "Theodore Y . Ts'o" Cc: Jaegeuk Kim , linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, Eric Biggers Subject: [PATCH v2 08/11] fscrypt: new helper function - fscrypt_prepare_link() Date: Mon, 9 Oct 2017 12:15:41 -0700 Message-Id: <20171009191544.43656-9-ebiggers3@gmail.com> X-Mailer: git-send-email 2.14.2.920.gcf0c67979c-goog In-Reply-To: <20171009191544.43656-1-ebiggers3@gmail.com> References: <20171009191544.43656-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 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. Acked-by: Dave Chinner Signed-off-by: Eric Biggers --- fs/crypto/hooks.c | 15 +++++++++++++++ include/linux/fscrypt.h | 27 +++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 6 ++++++ include/linux/fscrypt_supp.h | 1 + 4 files changed, 49 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.h b/include/linux/fscrypt.h index b3e2a5f93415..9c9a53f99327 100644 --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -177,4 +177,31 @@ static inline int fscrypt_require_key(struct inode *inode) return 0; } +/** + * 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_H */ diff --git a/include/linux/fscrypt_notsupp.h b/include/linux/fscrypt_notsupp.h index 162da6517ac4..d7d1039eb6b5 100644 --- a/include/linux/fscrypt_notsupp.h +++ b/include/linux/fscrypt_notsupp.h @@ -186,4 +186,10 @@ static inline int fscrypt_file_open(struct inode *inode, struct file *filp) return 0; } +static inline int __fscrypt_prepare_link(struct inode *inode, + struct inode *dir) +{ + return -EOPNOTSUPP; +} + #endif /* _LINUX_FSCRYPT_NOTSUPP_H */ diff --git a/include/linux/fscrypt_supp.h b/include/linux/fscrypt_supp.h index fd2f6decaee4..80706283da75 100644 --- a/include/linux/fscrypt_supp.h +++ b/include/linux/fscrypt_supp.h @@ -145,5 +145,6 @@ extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, /* hooks.c */ extern int fscrypt_file_open(struct inode *inode, struct file *filp); +extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir); #endif /* _LINUX_FSCRYPT_SUPP_H */