From patchwork Wed Sep 20 22:45:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9962855 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 CC2906056A for ; Wed, 20 Sep 2017 22:49:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BE4D528925 for ; Wed, 20 Sep 2017 22:49:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B32F629270; Wed, 20 Sep 2017 22:49:32 +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 625BB28A28 for ; Wed, 20 Sep 2017 22:49:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752231AbdITWt1 (ORCPT ); Wed, 20 Sep 2017 18:49:27 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:34714 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751931AbdITWrD (ORCPT ); Wed, 20 Sep 2017 18:47:03 -0400 Received: by mail-pg0-f66.google.com with SMTP id u18so2378867pgo.1; Wed, 20 Sep 2017 15:47:03 -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=CXOIAKtWON/V/TY53npXTAfo3/xiUczbtICqD1BkKUE=; b=GLyIydD1lNvj2+I4MU5s2IxOd+DPck3Z3BMuZ1tctVMoIFjOJeb3ZOn9YedrRFNA/D CwJoc4xiiRhoPdLX7TmZ9JLRPwDGzJg7xHhwJ96j34jchFA0JCmv8jRYSKq0YlmA0a0P piQMi/2p+l59t/K38IFB/Yfn8Pqi43DY45cTnnZBuRfbJT15c7XYoN+xkT5UPPBD4tCt bDVBoqzZFdEBhZ3FK/koHWHpJjGMAjNtZqVbl3CTmXYHzZ+GxOf3dW5HKAb0bOrhSnu2 6ascTFV7m3A/J3qusNd2sTmI8GUJk3v0EGRXB4CIePRaMAkJasFjsZ3qwonXa7YP9p3J QLTQ== 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=CXOIAKtWON/V/TY53npXTAfo3/xiUczbtICqD1BkKUE=; b=hlAkalL0ONHlrDqVm7EPHlA+usmgbRbzz1BwEDozitvDf7KnBu2Bq+520XrRInNrp9 YPuUSZI+KrHq0RtbGdfaCgMMtQ1hGf6pyNJ5m6U78e8U21d6f7KP/nByb8hZm4l5HaNb wSFbH5AcLbm1QZyaaXi3A8wg/yKBJrtIKAs2OiUYDgj6X3+vLOmQPXSHyHlm2mPBfZdF uf1gpVcdW4mpbjodX0oS2uBXg3jKnwGx4g2Dpk2YYO8kvw7bYMzzUyPD4XjD7r6PgS/Y 2KR0SR7rt1E4q8h1nnpB5P4KA12vwTUsI50IOZw5iWEYuLbCaZOj5AmRzuqVxiXOQ9ee PXNg== X-Gm-Message-State: AHPjjUjz6p8xQClkVOKg6eZg0sgPm04sOar5SItcUk3rvEl+w9Kfrc95 P/Boxe1FjNmkAg+zEUmZAi12RRSEUOQ= X-Google-Smtp-Source: AOwi7QB31rzc3THPqVCbTPvbx8PuoattTFuqT5wSpHHJSIzN6IiXU/Gmde6i6mqJHP5qBcsH6CgMDw== X-Received: by 10.99.53.206 with SMTP id c197mr3677053pga.440.1505947622479; Wed, 20 Sep 2017 15:47:02 -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.47.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 20 Sep 2017 15:47:01 -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 10/25] fscrypt: new helper function - fscrypt_prepare_setattr() Date: Wed, 20 Sep 2017 15:45:50 -0700 Message-Id: <20170920224605.22030-11-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 for filesystems to call when processing ->setattr() on a possibly-encrypted inode. It handles enforcing that an encrypted file can only be truncated if its encryption key is available. Signed-off-by: Eric Biggers --- include/linux/fscrypt_notsupp.h | 8 ++++++++ include/linux/fscrypt_supp.h | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/linux/fscrypt_notsupp.h b/include/linux/fscrypt_notsupp.h index e9b437605f9c..b0dd2d1756b2 100644 --- a/include/linux/fscrypt_notsupp.h +++ b/include/linux/fscrypt_notsupp.h @@ -218,4 +218,12 @@ static inline int fscrypt_prepare_lookup(struct inode *dir, return 0; } +static inline int fscrypt_prepare_setattr(struct dentry *dentry, + struct iattr *attr) +{ + if (IS_ENCRYPTED(d_inode(dentry)) && (attr->ia_valid & ATTR_SIZE)) + return -EOPNOTSUPP; + return 0; +} + #endif /* _LINUX_FSCRYPT_NOTSUPP_H */ diff --git a/include/linux/fscrypt_supp.h b/include/linux/fscrypt_supp.h index 2b99bc01c59b..bdd01e02435b 100644 --- a/include/linux/fscrypt_supp.h +++ b/include/linux/fscrypt_supp.h @@ -270,4 +270,29 @@ static inline int fscrypt_prepare_lookup(struct inode *dir, return 0; } +/** + * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes + * @dentry: dentry through which the inode is being changed + * @attr: attributes to change + * + * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file, + * most attribute changes are allowed even without the encryption key. However, + * without the encryption key we do have to forbid truncates. This is needed + * because the size being truncated to may not be a multiple of the filesystem + * block size, and in that case we'd have to decrypt the final block, zero the + * portion past i_size, and re-encrypt it. (We *could* allow truncating to a + * filesystem block boundary, but it's simpler to just forbid all truncates --- + * and we already forbid all other contents modifications without the key.) + * + * 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_prepare_setattr(struct dentry *dentry, + struct iattr *attr) +{ + if (IS_ENCRYPTED(d_inode(dentry)) && (attr->ia_valid & ATTR_SIZE)) + return fscrypt_require_key(d_inode(dentry)); + return 0; +} + #endif /* _LINUX_FSCRYPT_SUPP_H */