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: 816580 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=65.50.211.133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="X9hbYHnh"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="aa/xwiIY"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xyFPz4hK0z9sBZ for ; Thu, 21 Sep 2017 08:52:55 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=njq2yD8sNLy2LF3PuPQWW6tkN+PVFo5RewGj0gBFMMk=; b=X9hbYHnh6qLMYnN17qE166IreX mAfZDCmwCBYoPe6XgyYqo18u9iOIPEnRhQqSy3Lpr0TOBEMlHbQxnYzFhO2pYBO0s5NJ3hozURWO1 kQRdpqM7i4+EwAFFJLljc2ZV9VA4+KL4NEJqIvhmJuen2TexHy/LnAxB7xLJZKNDHSYtvqLMDBw11 jzGRTxyY+4DuYGxmqQiCu0ARHmKDrWbwNkbNKfV9Vtl5IvesK33GmJH9ylpmI2KG/HbbbuI3Qlmax AqGLwffv4twr7w8+4OyUQMhnnrqUYdvPZ9x+1n3aRWDKRxiz2guK6w8YzTnELeMUpjnVWjBEY/rFt n+9pR5NA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dunrL-0001tH-Ky; Wed, 20 Sep 2017 22:52:47 +0000 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dunm2-0004xR-PS for linux-mtd@lists.infradead.org; Wed, 20 Sep 2017 22:47:37 +0000 Received: by mail-pg0-x241.google.com with SMTP id j16so2375784pga.2 for ; 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=g4GXSN5Cq7QHizUvHAMKCwXBcF0yGFXnucYI+8kpQNHzPnEwDK0Wo3+oVC0WK1RUcM 2qfioUfDaFajbVBW82WN/nE37dKQDLIfq9eHvw5CkESTRMEmLx1APsGb15S0GQbCVwh2 L86Oe6iP97MFIxKXPtkJGxzpb4456Sxm/yKuEUOhUqws5YXMEBmxtjyBIzmhq0rZduTM UyS9H/yuix58QK/oi2NZm1D3SZe8ES0sm1wwqJG3EkAMBalvSCijQfr54kD+Fwz7ojPy 2wA8jSV7J2IyKc1PXr678lny7q/cpnMNtU9oUe27/hbXKPmR/xDyqXVHZZxyEA3PsOFF Bdlg== X-Gm-Message-State: AHPjjUilQkRNF7km5mk7Di7jn+O6QPBnitz4K8IzdetT+mJlIXZX3lQK a5ZfM7ED1rt3dAxdK93h5rA= 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 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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170920_154719_286357_D229054B X-CRM114-Status: GOOD ( 13.19 ) X-Spam-Score: -1.8 (-) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-1.8 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2607:f8b0:400e:c05:0:0:0:241 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (ebiggers3[at]gmail.com) 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (ebiggers3[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Theodore Y . Ts'o" , Eric Biggers , Michael Halcrow , linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, linux-fsdevel@vger.kernel.org, Jaegeuk Kim , linux-ext4@vger.kernel.org MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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 */