stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ext4: don't allow journal inode to have encrypt flag
@ 2022-11-02  5:33 Eric Biggers
  2022-11-29  0:23 ` Eric Biggers
  2022-12-06 21:01 ` Theodore Ts'o
  0 siblings, 2 replies; 4+ messages in thread
From: Eric Biggers @ 2022-11-02  5:33 UTC (permalink / raw)
  To: linux-ext4; +Cc: linux-fscrypt, stable, syzbot+ba9dac45bc76c490b7c3

From: Eric Biggers <ebiggers@google.com>

Mounting a filesystem whose journal inode has the encrypt flag causes a
NULL dereference in fscrypt_limit_io_blocks() when the 'inlinecrypt'
mount option is used.

The problem is that when jbd2_journal_init_inode() calls bmap(), it
eventually finds its way into ext4_iomap_begin(), which calls
fscrypt_limit_io_blocks().  fscrypt_limit_io_blocks() requires that if
the inode is encrypted, then its encryption key must already be set up.
That's not the case here, since the journal inode is never "opened" like
a normal file would be.  Hence the crash.

A reproducer is:

    mkfs.ext4 -F /dev/vdb
    debugfs -w /dev/vdb -R "set_inode_field <8> flags 0x80808"
    mount /dev/vdb /mnt -o inlinecrypt

To fix this, make ext4 consider journal inodes with the encrypt flag to
be invalid.  (Note, maybe other flags should be rejected on the journal
inode too.  For now, this is just the minimal fix for the above issue.)

I've marked this as fixing the commit that introduced the call to
fscrypt_limit_io_blocks(), since that's what made an actual crash start
being possible.  But this fix could be applied to any version of ext4
that supports the encrypt feature.

Reported-by: syzbot+ba9dac45bc76c490b7c3@syzkaller.appspotmail.com
Fixes: 38ea50daa7a4 ("ext4: support direct I/O with fscrypt using blk-crypto")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 7950904fbf04f..2274f730b87e5 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -5723,7 +5723,7 @@ static struct inode *ext4_get_journal_inode(struct super_block *sb,
 
 	ext4_debug("Journal inode found at %p: %lld bytes\n",
 		  journal_inode, journal_inode->i_size);
-	if (!S_ISREG(journal_inode->i_mode)) {
+	if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
 		iput(journal_inode);
 		return NULL;

base-commit: 8f71a2b3f435f29b787537d1abedaa7d8ebe6647
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] ext4: don't allow journal inode to have encrypt flag
  2022-11-02  5:33 [PATCH] ext4: don't allow journal inode to have encrypt flag Eric Biggers
@ 2022-11-29  0:23 ` Eric Biggers
  2022-12-06 21:01 ` Theodore Ts'o
  1 sibling, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2022-11-29  0:23 UTC (permalink / raw)
  To: linux-ext4; +Cc: linux-fscrypt, stable, syzbot+ba9dac45bc76c490b7c3

On Tue, Nov 01, 2022 at 10:33:12PM -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> Mounting a filesystem whose journal inode has the encrypt flag causes a
> NULL dereference in fscrypt_limit_io_blocks() when the 'inlinecrypt'
> mount option is used.
> 
> The problem is that when jbd2_journal_init_inode() calls bmap(), it
> eventually finds its way into ext4_iomap_begin(), which calls
> fscrypt_limit_io_blocks().  fscrypt_limit_io_blocks() requires that if
> the inode is encrypted, then its encryption key must already be set up.
> That's not the case here, since the journal inode is never "opened" like
> a normal file would be.  Hence the crash.
> 
> A reproducer is:
> 
>     mkfs.ext4 -F /dev/vdb
>     debugfs -w /dev/vdb -R "set_inode_field <8> flags 0x80808"
>     mount /dev/vdb /mnt -o inlinecrypt
> 
> To fix this, make ext4 consider journal inodes with the encrypt flag to
> be invalid.  (Note, maybe other flags should be rejected on the journal
> inode too.  For now, this is just the minimal fix for the above issue.)
> 
> I've marked this as fixing the commit that introduced the call to
> fscrypt_limit_io_blocks(), since that's what made an actual crash start
> being possible.  But this fix could be applied to any version of ext4
> that supports the encrypt feature.
> 
> Reported-by: syzbot+ba9dac45bc76c490b7c3@syzkaller.appspotmail.com
> Fixes: 38ea50daa7a4 ("ext4: support direct I/O with fscrypt using blk-crypto")
> Cc: stable@vger.kernel.org
> Signed-off-by: Eric Biggers <ebiggers@google.com>
> ---
>  fs/ext4/super.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index 7950904fbf04f..2274f730b87e5 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -5723,7 +5723,7 @@ static struct inode *ext4_get_journal_inode(struct super_block *sb,
>  
>  	ext4_debug("Journal inode found at %p: %lld bytes\n",
>  		  journal_inode, journal_inode->i_size);
> -	if (!S_ISREG(journal_inode->i_mode)) {
> +	if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
>  		ext4_msg(sb, KERN_ERR, "invalid journal inode");
>  		iput(journal_inode);
>  		return NULL;
> 
> base-commit: 8f71a2b3f435f29b787537d1abedaa7d8ebe6647
> -- 

Ping.

- Eric

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ext4: don't allow journal inode to have encrypt flag
  2022-11-02  5:33 [PATCH] ext4: don't allow journal inode to have encrypt flag Eric Biggers
  2022-11-29  0:23 ` Eric Biggers
@ 2022-12-06 21:01 ` Theodore Ts'o
  2022-12-08  3:41   ` Eric Biggers
  1 sibling, 1 reply; 4+ messages in thread
From: Theodore Ts'o @ 2022-12-06 21:01 UTC (permalink / raw)
  To: Eric Biggers, linux-ext4
  Cc: Theodore Ts'o, syzbot+ba9dac45bc76c490b7c3, linux-fscrypt, stable

On Tue, 1 Nov 2022 22:33:12 -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> Mounting a filesystem whose journal inode has the encrypt flag causes a
> NULL dereference in fscrypt_limit_io_blocks() when the 'inlinecrypt'
> mount option is used.
> 
> The problem is that when jbd2_journal_init_inode() calls bmap(), it
> eventually finds its way into ext4_iomap_begin(), which calls
> fscrypt_limit_io_blocks().  fscrypt_limit_io_blocks() requires that if
> the inode is encrypted, then its encryption key must already be set up.
> That's not the case here, since the journal inode is never "opened" like
> a normal file would be.  Hence the crash.
> 
> [...]

Applied, thanks!

[1/1] ext4: don't allow journal inode to have encrypt flag
      commit: 29cef51d8522c4d8953856afaffcaf1b754e4f6c

Best regards,
-- 
Theodore Ts'o <tytso@mit.edu>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ext4: don't allow journal inode to have encrypt flag
  2022-12-06 21:01 ` Theodore Ts'o
@ 2022-12-08  3:41   ` Eric Biggers
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2022-12-08  3:41 UTC (permalink / raw)
  To: Theodore Ts'o
  Cc: linux-ext4, syzbot+ba9dac45bc76c490b7c3, linux-fscrypt, stable

On Tue, Dec 06, 2022 at 04:01:48PM -0500, Theodore Ts'o wrote:
> On Tue, 1 Nov 2022 22:33:12 -0700, Eric Biggers wrote:
> > From: Eric Biggers <ebiggers@google.com>
> > 
> > Mounting a filesystem whose journal inode has the encrypt flag causes a
> > NULL dereference in fscrypt_limit_io_blocks() when the 'inlinecrypt'
> > mount option is used.
> > 
> > The problem is that when jbd2_journal_init_inode() calls bmap(), it
> > eventually finds its way into ext4_iomap_begin(), which calls
> > fscrypt_limit_io_blocks().  fscrypt_limit_io_blocks() requires that if
> > the inode is encrypted, then its encryption key must already be set up.
> > That's not the case here, since the journal inode is never "opened" like
> > a normal file would be.  Hence the crash.
> > 
> > [...]
> 
> Applied, thanks!
> 
> [1/1] ext4: don't allow journal inode to have encrypt flag
>       commit: 29cef51d8522c4d8953856afaffcaf1b754e4f6c
> 
> Best regards,
> -- 
> Theodore Ts'o <tytso@mit.edu>

Thanks Ted.  Note that I also sent an e2fsprogs patch to make e2fsck fix this
situation: https://lore.kernel.org/r/20221102220551.3940-1-ebiggers@kernel.org

- Eric

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-12-08  3:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-02  5:33 [PATCH] ext4: don't allow journal inode to have encrypt flag Eric Biggers
2022-11-29  0:23 ` Eric Biggers
2022-12-06 21:01 ` Theodore Ts'o
2022-12-08  3:41   ` Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).