From patchwork Mon Oct 9 19:15:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 823440 X-Patchwork-Delegate: richard@nod.at 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="VL6KyiUj"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="F2JdxmXe"; 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 3y9qpw5qvHz9t44 for ; Tue, 10 Oct 2017 06:21:12 +1100 (AEDT) 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=BmuUT/zpuplsrdfcSIBFsjPOdH0EZ/SXl8RAp8Vystc=; b=VL6KyiUjpd4yxNM8f97CMr9Ajr nwteSmAyEjJpjb4v6FfcvLplhtlshVH6vybgtcwwrrLNYI75jWaUPsudoqOa4eQdT8xJae4wpKBwr IBb03vIuj41n+kTbReOddUp544ULFCxoZg10OChPPnTJyqIeHogZFoM007HLPMWvacJTpLahcnU6F NObxxzpSV1QdjbspGOlB4Qj7LKSMdg45hRSfDq4Zwn1XzOW3aGyedcA97fjDEM4MZHAIglEKwe+dw bvVhgKHU6cPa/DWxDrgIaw/7yYTx9zQcqKUZZY9W6r7PBTCmR9os5Po/gDX3xQoXZCS5PeUtbLJA7 jRC1kTiA==; 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 1e1dbx-00031L-5q; Mon, 09 Oct 2017 19:21:09 +0000 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e1daH-0000EY-JB for linux-mtd@lists.infradead.org; Mon, 09 Oct 2017 19:19:36 +0000 Received: by mail-pf0-x243.google.com with SMTP id z11so12491317pfk.3 for ; Mon, 09 Oct 2017 12:19:05 -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=lnCFLmTHIE3cYMhlwH5Y7fgroBczDPBRSzqOVWvsk6U=; b=F2JdxmXe8hPEnJVbrLVPaiOwPnaKwyezm/IsROAtvGPPSc+2e/vgvKve53bi3i8l2+ XXD3J4ycMJSzG8nZar+JK8JN/62lMTJ5qK7tMINfelAFddZcaHtTt8vNkUUvVRXemXtJ +NYyYoSjl/SWVe5q8NEReng3o3Dnu3TFgct6xlavZDh5xptlahj8iKbUHIFREHJtK00V FqdZ/Mvy01Q1jaPzd1xYaf7WtEXqx23BV8Bf40cMqgudKHdYcW3vHGjrKGUeA2TpadvD LsEI4cHlhPZLZ2/5yzv38dWKqQybrCbRlXIW44gc9e640cjjRvDPsBJpfvKCBwrFLH1S 2pbg== 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=lnCFLmTHIE3cYMhlwH5Y7fgroBczDPBRSzqOVWvsk6U=; b=WsssfOHHpFqQk4zHYTi6Xzs/diTnuc0wpdlHsrzD5xceYvP3k6sPxvXTB8M8VdWDSr OvawzN1YgKGXel4t5OzSlYfIqIVmFwnuWcB6ZX9m5Lba4aq76lB9KWNT3U+WwdYKWpSv Gogp+gMLv15tfwoN+af0SApBXSCpHeTvjtkPIdiSyO1ovYkw3edpP4O09Fpd3PoBOenT 8jWdOJZA3OQdbQEgNczFIfbTPI2dAZQO7k85c8lH4p0kzsZwLrWnZnU+IDaDXOH5Eiwh VYZW5RGhkSAM+o6R7pO49y1Oh1cYzzi/xpw+24QQc7T1Bh7mwmsL+ucyq6HA0DdnhDa4 diig== X-Gm-Message-State: AMCzsaWABX9KnZ75IC/cvF88SPiRwgZYFKOliaba67BJtUcqtLh3ZFO+ 6v9CFf0reLkhsmAcI2pHEzg= X-Google-Smtp-Source: AOwi7QAEn31UmfIj3gn7fF0+xCR8deYcdbJUvLD6u2/A0J7fDlej09UGpCS9hzOQ15anbzXCR3CzhA== X-Received: by 10.84.235.198 with SMTP id m6mr9749629plt.57.1507576745021; Mon, 09 Oct 2017 12:19:05 -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.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 09 Oct 2017 12:19:04 -0700 (PDT) From: Eric Biggers To: linux-fscrypt@vger.kernel.org, "Theodore Y . Ts'o" Subject: [PATCH v2 06/11] fscrypt: new helper function - fscrypt_require_key() Date: Mon, 9 Oct 2017 12:15:39 -0700 Message-Id: <20171009191544.43656-7-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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171009_121926_259924_4D189338 X-CRM114-Status: GOOD ( 14.97 ) 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:c00:0:0:0:243 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: Eric Biggers , 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 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. Acked-by: Dave Chinner Signed-off-by: Eric Biggers --- include/linux/fscrypt.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h index d58e6a90cfe4..b3e2a5f93415 100644 --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -151,5 +151,30 @@ static inline bool fscrypt_has_encryption_key(const struct inode *inode) #include #endif /* __FS_HAS_ENCRYPTION */ +/** + * 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 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_H */