diff mbox

[1/3] ext4: require key for truncate(2) of encrypted file

Message ID 20170613234755.111167-2-ebiggers3@gmail.com (mailing list archive)
State Not Applicable
Headers show

Commit Message

Eric Biggers June 13, 2017, 11:47 p.m. UTC
From: Eric Biggers <ebiggers@google.com>

Currently, filesystems allow truncate(2) on an encrypted file without
the encryption key.  However, it's impossible to correctly handle the
case where the size being truncated to is not a multiple of the
filesystem block size, because that would require decrypting the final
block, zeroing the part beyond i_size, then encrypting the block.

As other modifications to encrypted file contents are prohibited without
the key, just prohibit truncate(2) as well, making it fail with ENOKEY.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/inode.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Andreas Dilger June 14, 2017, 12:14 a.m. UTC | #1
On Jun 13, 2017, at 5:47 PM, Eric Biggers <ebiggers3@gmail.com> wrote:
> 
> From: Eric Biggers <ebiggers@google.com>
> 
> Currently, filesystems allow truncate(2) on an encrypted file without
> the encryption key.  However, it's impossible to correctly handle the
> case where the size being truncated to is not a multiple of the
> filesystem block size, because that would require decrypting the final
> block, zeroing the part beyond i_size, then encrypting the block.
> 
> As other modifications to encrypted file contents are prohibited without
> the key, just prohibit truncate(2) as well, making it fail with ENOKEY.

Out of curiosity, if an encrypted block is zero-filled at the end when
the key is unavailable, what would happen?  At worst this would result
in garbage at the end of the file when the zeroes are "decrypted"?

Is it possible to unlink files if they are encrypted without the key (if
the user has permission to write to the directory)?

Does file unlink result in the per-file crypto key being erased in the
inode, or is the inode just marked unused but not overwritten?  One of
the desirable features of per-file crypto is "secure erase" so that once
the inode is unlinked the crypto key is gone and there is no way to decrypt
the data after the fact, even if the filesystem/user key is later recovered.
If the per-file key is still sitting in the inode, then it may be possible
to decrypt the data long after the file was deleted if the key is later
compromised and the inode+data blocks were not re-used.

Cheers, Andreas

> Signed-off-by: Eric Biggers <ebiggers@google.com>
> ---
> fs/ext4/inode.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
> 
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 5cf82d03968c..baf8630de6a5 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -5307,6 +5307,14 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
> 		loff_t oldsize = inode->i_size;
> 		int shrink = (attr->ia_size <= inode->i_size);
> 
> +		if (ext4_encrypted_inode(inode)) {
> +			error = fscrypt_get_encryption_info(inode);
> +			if (error)
> +				return error;
> +			if (!fscrypt_has_encryption_key(inode))
> +				return -ENOKEY;
> +		}
> +
> 		if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
> 			struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
> 
> --
> 2.13.1.508.gb3defc5cc-goog
> 


Cheers, Andreas
Eric Biggers June 14, 2017, 3:12 a.m. UTC | #2
Hi Andreas,

On Tue, Jun 13, 2017 at 06:14:53PM -0600, Andreas Dilger wrote:
> On Jun 13, 2017, at 5:47 PM, Eric Biggers <ebiggers3@gmail.com> wrote:
> > 
> > From: Eric Biggers <ebiggers@google.com>
> > 
> > Currently, filesystems allow truncate(2) on an encrypted file without
> > the encryption key.  However, it's impossible to correctly handle the
> > case where the size being truncated to is not a multiple of the
> > filesystem block size, because that would require decrypting the final
> > block, zeroing the part beyond i_size, then encrypting the block.
> > 
> > As other modifications to encrypted file contents are prohibited without
> > the key, just prohibit truncate(2) as well, making it fail with ENOKEY.
> 
> Out of curiosity, if an encrypted block is zero-filled at the end when
> the key is unavailable, what would happen?  At worst this would result
> in garbage at the end of the file when the zeroes are "decrypted"?

Currently ext4 just skips zeroing the final block entirely, which is perhaps the
most reasonable behavior, but it's wrong because it breaks truncate() semantics.
If it did zero the final block on-disk, yes it wouldn't actually be zeroes after
being decrypted, so if the file were to be extended later the extended portion
wouldn't be filled with zeroes, and more importantly even without extending, up
to the last 15 bytes of the file would be corrupted since AES-XTS encryption
operates on 16 byte blocks.  Also, the ciphertext page would likely end up in
the pagecache, which is supposed to contain the plaintext.  So if the file were
read later (which requires the key), up to the last PAGE_SIZE bytes of the file
would appear as garbage.

> 
> Is it possible to unlink files if they are encrypted without the key (if
> the user has permission to write to the directory)?
> 

Yes, that's supported.

> Does file unlink result in the per-file crypto key being erased in the
> inode, or is the inode just marked unused but not overwritten?  One of
> the desirable features of per-file crypto is "secure erase" so that once
> the inode is unlinked the crypto key is gone and there is no way to decrypt
> the data after the fact, even if the filesystem/user key is later recovered.
> If the per-file key is still sitting in the inode, then it may be possible
> to decrypt the data long after the file was deleted if the key is later
> compromised and the inode+data blocks were not re-used.

I assume you're talking about the key derivation nonce in the encryption xattr
(which is stored on-disk and is not really a "key"; although it's used as an
AES-ECB key in derive_key_aes(), that's really an implementation detail, and the
key derivation algorithm could/should be replaced with something less
idiosyncratic like an HKDF).  This is a completely separate issue, but no, as
far as I know we aren't securely erasing the nonce following a file deletion so
that someone with access to both the raw disk and the master key can no longer
derive the per-file key.  It's a good idea, though, and I think it should be
done if practical.  Note, though, that the xattr may be stored in an external
xattr block, and it also may have moved around over time --- so it may be
necessary to zero the whole inode and xattr block.

Eric
--
To unsubscribe from this list: send the line "unsubscribe linux-fscrypt" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Andreas Dilger June 14, 2017, 4:02 a.m. UTC | #3
On Jun 13, 2017, at 9:12 PM, Eric Biggers <ebiggers3@gmail.com> wrote:
> 
> Hi Andreas,
> 
> On Tue, Jun 13, 2017 at 06:14:53PM -0600, Andreas Dilger wrote:
>> On Jun 13, 2017, at 5:47 PM, Eric Biggers <ebiggers3@gmail.com> wrote:
>>> 
>>> From: Eric Biggers <ebiggers@google.com>
>>> 
>>> Currently, filesystems allow truncate(2) on an encrypted file without
>>> the encryption key.  However, it's impossible to correctly handle the
>>> case where the size being truncated to is not a multiple of the
>>> filesystem block size, because that would require decrypting the final
>>> block, zeroing the part beyond i_size, then encrypting the block.
>>> 
>>> As other modifications to encrypted file contents are prohibited without
>>> the key, just prohibit truncate(2) as well, making it fail with ENOKEY.
>> 
>> Out of curiosity, if an encrypted block is zero-filled at the end when
>> the key is unavailable, what would happen?  At worst this would result
>> in garbage at the end of the file when the zeroes are "decrypted"?
> 
> Currently ext4 just skips zeroing the final block entirely, which is perhaps the
> most reasonable behavior, but it's wrong because it breaks truncate() semantics.
> If it did zero the final block on-disk, yes it wouldn't actually be zeroes after
> being decrypted, so if the file were to be extended later the extended portion
> wouldn't be filled with zeroes, and more importantly even without extending, up
> to the last 15 bytes of the file would be corrupted since AES-XTS encryption
> operates on 16 byte blocks.  Also, the ciphertext page would likely end up in
> the pagecache, which is supposed to contain the plaintext.  So if the file were
> read later (which requires the key), up to the last PAGE_SIZE bytes of the file
> would appear as garbage.

It wouldn't be bad to add this kind of explanation to the commit message, so
that it is clear why simple zeroing of the partial block doesn't work.

>> Is it possible to unlink files if they are encrypted without the key (if
>> the user has permission to write to the directory)?
> 
> Yes, that's supported.
> 
>> Does file unlink result in the per-file crypto key being erased in the
>> inode, or is the inode just marked unused but not overwritten?  One of
>> the desirable features of per-file crypto is "secure erase" so that once
>> the inode is unlinked the crypto key is gone and there is no way to decrypt
>> the data after the fact, even if the filesystem/user key is later recovered.
>> If the per-file key is still sitting in the inode, then it may be possible
>> to decrypt the data long after the file was deleted if the key is later
>> compromised and the inode+data blocks were not re-used.
> 
> I assume you're talking about the key derivation nonce in the encryption xattr
> (which is stored on-disk and is not really a "key"; although it's used as an
> AES-ECB key in derive_key_aes(), that's really an implementation detail, and the
> key derivation algorithm could/should be replaced with something less
> idiosyncratic like an HKDF).  This is a completely separate issue, but no, as
> far as I know we aren't securely erasing the nonce following a file deletion so
> that someone with access to both the raw disk and the master key can no longer
> derive the per-file key.  It's a good idea, though, and I think it should be
> done if practical.  Note, though, that the xattr may be stored in an external
> xattr block, and it also may have moved around over time --- so it may be
> necessary to zero the whole inode and xattr block.

While it is true that the crypto xattr _could_ exist in the external xattr block,
this is fairly unlikely since this xattr is created first, so it is most likely
to be in the inode xattr space.  Also, it is common that there is no external
xattr block.  In most cases, setting the xattr with the same size will overwrite
it in-place rather than try to allocate new space for it, so this should be
relatively easy to implement.

In the end, trying to zero this xattr and failing is no worse than not doing it
at all, so I think it reasonable to try.  It shouldn't result in any extra I/O,
since the inode always needs to be written out anyway to store the dtime and
nlink = 0.

Cheers, Andreas
Christoph Hellwig June 14, 2017, 6:52 a.m. UTC | #4
On Tue, Jun 13, 2017 at 04:47:53PM -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> Currently, filesystems allow truncate(2) on an encrypted file without
> the encryption key.  However, it's impossible to correctly handle the
> case where the size being truncated to is not a multiple of the
> filesystem block size, because that would require decrypting the final
> block, zeroing the part beyond i_size, then encrypting the block.
> 
> As other modifications to encrypted file contents are prohibited without
> the key, just prohibit truncate(2) as well, making it fail with ENOKEY.

What about hole punches?  What about fallocate which just adds zeroes
but still changes the content.  What about insert or collapse range?
--
To unsubscribe from this list: send the line "unsubscribe linux-fscrypt" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Eric Biggers June 14, 2017, 7:03 a.m. UTC | #5
Hi Christoph,

On Tue, Jun 13, 2017 at 11:52:10PM -0700, Christoph Hellwig wrote:
> On Tue, Jun 13, 2017 at 04:47:53PM -0700, Eric Biggers wrote:
> > From: Eric Biggers <ebiggers@google.com>
> > 
> > Currently, filesystems allow truncate(2) on an encrypted file without
> > the encryption key.  However, it's impossible to correctly handle the
> > case where the size being truncated to is not a multiple of the
> > filesystem block size, because that would require decrypting the final
> > block, zeroing the part beyond i_size, then encrypting the block.
> > 
> > As other modifications to encrypted file contents are prohibited without
> > the key, just prohibit truncate(2) as well, making it fail with ENOKEY.
> 
> What about hole punches?  What about fallocate which just adds zeroes
> but still changes the content.  What about insert or collapse range?

None of those are allowed because fallocate() requires a file descriptor, and
open() fails with ENOKEY if the encryption key is not available.  truncate() is
different because it takes a path, not a file descriptor.

Eric
--
To unsubscribe from this list: send the line "unsubscribe linux-fscrypt" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Christoph Hellwig June 14, 2017, 7:06 a.m. UTC | #6
On Wed, Jun 14, 2017 at 12:03:57AM -0700, Eric Biggers wrote:
> None of those are allowed because fallocate() requires a file descriptor, and
> open() fails with ENOKEY if the encryption key is not available.  truncate() is
> different because it takes a path, not a file descriptor.

Ok, that makes sense.
--
To unsubscribe from this list: send the line "unsubscribe linux-fscrypt" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Theodore Ts'o June 23, 2017, 11:52 p.m. UTC | #7
On Tue, Jun 13, 2017 at 04:47:53PM -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> Currently, filesystems allow truncate(2) on an encrypted file without
> the encryption key.  However, it's impossible to correctly handle the
> case where the size being truncated to is not a multiple of the
> filesystem block size, because that would require decrypting the final
> block, zeroing the part beyond i_size, then encrypting the block.
> 
> As other modifications to encrypted file contents are prohibited without
> the key, just prohibit truncate(2) as well, making it fail with ENOKEY.
> 
> Signed-off-by: Eric Biggers <ebiggers@google.com>

Thanks, applied.

					- Ted
--
To unsubscribe from this list: send the line "unsubscribe linux-fscrypt" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 5cf82d03968c..baf8630de6a5 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5307,6 +5307,14 @@  int ext4_setattr(struct dentry *dentry, struct iattr *attr)
 		loff_t oldsize = inode->i_size;
 		int shrink = (attr->ia_size <= inode->i_size);
 
+		if (ext4_encrypted_inode(inode)) {
+			error = fscrypt_get_encryption_info(inode);
+			if (error)
+				return error;
+			if (!fscrypt_has_encryption_key(inode))
+				return -ENOKEY;
+		}
+
 		if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
 			struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);