mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* ima/evm: security.ima and security.evm get written to frozen filesystems
@ 2026-11-13 23:55 Christian Brauner
  2026-11-23 21:10 ` Mimi Zohar
  0 siblings, 1 reply; 2+ messages in thread
From: Christian Brauner @ 2026-11-13 23:55 UTC (permalink / raw)
  To: Mimi Zohar, Roberto Sassu, Dmitry Kasatkin, Eric Snowberg
  Cc: Paul Moore, James Morris, Serge E. Hallyn, Jan Kara,
	linux-integrity, linux-security-module, linux-fsdevel,
	linux-kernel

Hey,

I'm working on some freeze protection fixes that plague the tree.
IMA and EVM update security.ima and security.evm without freeze
protection. IOW, they modify filesystems that are frozen. On ext4 that
trips the WARN_ON() in ext4_journal_check_start().

The update on the last close:

__fput()
-> security_file_release()
   -> ima_file_free()
      -> ima_check_last_writer()                  # iint->mutex
         -> ima_update_xattr()                    # inode lock
            -> ima_fix_xattr()
               -> __vfs_setxattr_noperm(security.ima)
                  -> evm_inode_post_setxattr()
                     -> evm_update_evmxattr()

Nothing on that path holds sb_writers. Write to a file covered by an
appraise policy, freeze the filesystem, close the file.

The same happens when files are appraised:

- fix mode: ima_appraise_measurement() -> ima_fix_xattr() and
  evm_fix_hmac()
- evm_verify_hmac() converting a digsig into an HMAC

for open, exec, mmap and kernel_read_file(). Only an O_TRUNC open of an
existing file holds sb_writers there.

Reproducer on v7.4-rc1, booted with ima_appraise=fix, ext4 on /dev/vdb:

    mount /dev/vdb /mnt
    echo "appraise fsuuid=$(blkid -s UUID -o value /dev/vdb)" > /sys/kernel/security/ima/policy
    echo a > /mnt/f; sync
    exec 3>>/mnt/f; echo b >&3
    fsfreeze -f /mnt
    exec 3>&-

The close rewrites security.ima on the frozen filesystem:

  WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#0: bash/280
  Call Trace:
   __ext4_journal_start_sb+0x7a/0x470
   ext4_xattr_set+0x20d/0x380
   __vfs_setxattr+0x10f/0x170
   __vfs_setxattr_noperm+0xf4/0x5e0
   ima_update_xattr+0x1bb/0x2a0
   ima_file_free+0x2a7/0x3c0
   security_file_release+0x55/0x60
   __fput+0x2d6/0xab0
   fput_close_sync+0xf2/0x1c0
   filp_close_sync+0x5c/0x90
   __x64_sys_close+0x71/0xe0
   do_syscall_64+0x10e/0x5e0

Removing security.ima from a file and opening it while frozen does the
same from the appraisal:

   __vfs_setxattr_noperm+0xf4/0x5e0
   ima_appraise_measurement+0x2d2/0x36a0
   process_measurement+0x15b8/0x2120
   ima_file_check+0xbc/0x110
   security_file_post_open+0x3f/0x90
   path_openat+0x4ab/0x2300

I don't think this has a trivial fix. iint->mutex nests inside
sb_writers: do_open() holds mnt_want_write() for O_TRUNC across
security_file_post_open() -> ima_file_check() -> process_measurement().

So taking sb_start_write() under iint->mutex or under the inode lock
inverts the order and deadlocks against a pending freeze:

- close holds iint->mutex and waits in sb_start_write()
- an O_TRUNC open holds sb_writers and waits for iint->mutex
- the freezer waits for the open

Skipping the update while frozen isn't great either.
ima_check_last_writer() has already cleared IMA_UPDATE_XATTR by then and
nothing redoes the update, so the next open in enforce mode fails with
-EACCES on the stale hash.

For close the only thing I see is taking sb_start_write() before
iint->mutex when an update may be needed. That makes close() block
while the filesystem is frozen, which xfs_file_release() goes out of
its way to avoid.

The appraisal-time fixups can use sb_start_write_trylock() and skip when
frozen since they're best effort anyway.

This goes back to 2fe5d6def167 ("ima: integrity appraisal extension").

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

* Re: ima/evm: security.ima and security.evm get written to frozen filesystems
  2026-11-13 23:55 ima/evm: security.ima and security.evm get written to frozen filesystems Christian Brauner
@ 2026-11-23 21:10 ` Mimi Zohar
  0 siblings, 0 replies; 2+ messages in thread
From: Mimi Zohar @ 2026-11-23 21:10 UTC (permalink / raw)
  To: Christian Brauner, Roberto Sassu, Dmitry Kasatkin, Eric Snowberg
  Cc: Paul Moore, James Morris, Serge E. Hallyn, Jan Kara,
	linux-integrity, linux-security-module, linux-fsdevel,
	linux-kernel

On Sat, 2026-11-14 at 00:55 +0100, Christian Brauner wrote:
> Hey,
> 
> I'm working on some freeze protection fixes that plague the tree.
> IMA and EVM update security.ima and security.evm without freeze
> protection. IOW, they modify filesystems that are frozen. On ext4 that
> trips the WARN_ON() in ext4_journal_check_start().
> 
> The update on the last close:
> 
> __fput()
> -> security_file_release()
>    -> ima_file_free()
>       -> ima_check_last_writer()                  # iint->mutex
>          -> ima_update_xattr()                    # inode lock
>             -> ima_fix_xattr()
>                -> __vfs_setxattr_noperm(security.ima)
>                   -> evm_inode_post_setxattr()
>                      -> evm_update_evmxattr()
> 
> Nothing on that path holds sb_writers. Write to a file covered by an
> appraise policy, freeze the filesystem, close the file.
> 
> The same happens when files are appraised:
> 
> - fix mode: ima_appraise_measurement() -> ima_fix_xattr() and
>   evm_fix_hmac()
> - evm_verify_hmac() converting a digsig into an HMAC
> 
> for open, exec, mmap and kernel_read_file(). Only an O_TRUNC open of an
> existing file holds sb_writers there.
> 
> Reproducer on v7.4-rc1, booted with ima_appraise=fix, ext4 on /dev/vdb:
> 
>     mount /dev/vdb /mnt
>     echo "appraise fsuuid=$(blkid -s UUID -o value /dev/vdb)" > /sys/kernel/security/ima/policy
>     echo a > /mnt/f; sync
>     exec 3>>/mnt/f; echo b >&3
>     fsfreeze -f /mnt
>     exec 3>&-
> 
> The close rewrites security.ima on the frozen filesystem:
> 
>   WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#0: bash/280
>   Call Trace:
>    __ext4_journal_start_sb+0x7a/0x470
>    ext4_xattr_set+0x20d/0x380
>    __vfs_setxattr+0x10f/0x170
>    __vfs_setxattr_noperm+0xf4/0x5e0
>    ima_update_xattr+0x1bb/0x2a0
>    ima_file_free+0x2a7/0x3c0
>    security_file_release+0x55/0x60
>    __fput+0x2d6/0xab0
>    fput_close_sync+0xf2/0x1c0
>    filp_close_sync+0x5c/0x90
>    __x64_sys_close+0x71/0xe0
>    do_syscall_64+0x10e/0x5e0
> 
> Removing security.ima from a file and opening it while frozen does the
> same from the appraisal:
> 
>    __vfs_setxattr_noperm+0xf4/0x5e0
>    ima_appraise_measurement+0x2d2/0x36a0
>    process_measurement+0x15b8/0x2120
>    ima_file_check+0xbc/0x110
>    security_file_post_open+0x3f/0x90
>    path_openat+0x4ab/0x2300
> 
> I don't think this has a trivial fix. iint->mutex nests inside
> sb_writers: do_open() holds mnt_want_write() for O_TRUNC across
> security_file_post_open() -> ima_file_check() -> process_measurement().
> 
> So taking sb_start_write() under iint->mutex or under the inode lock
> inverts the order and deadlocks against a pending freeze:
> 
> - close holds iint->mutex and waits in sb_start_write()
> - an O_TRUNC open holds sb_writers and waits for iint->mutex
> - the freezer waits for the open
> 
> Skipping the update while frozen isn't great either.
> ima_check_last_writer() has already cleared IMA_UPDATE_XATTR by then and
> nothing redoes the update, so the next open in enforce mode fails with
> -EACCES on the stale hash.
> 
> For close the only thing I see is taking sb_start_write() before
> iint->mutex when an update may be needed. That makes close() block
> while the filesystem is frozen, which xfs_file_release() goes out of
> its way to avoid.
> 
> The appraisal-time fixups can use sb_start_write_trylock() and skip when
> frozen since they're best effort anyway.
> 
> This goes back to 2fe5d6def167 ("ima: integrity appraisal extension").

Hi Christian,

Thanks for the detailed report and reproducer. I only looked at the
ima_file_free() side, since that is where neither blocking in close() nor
dropping the update looks good.

The only option I came up with is to try sb_start_write_trylock() in
ima_update_xattr(), after the hashing and just before the xattr write, and if it
fails, defer only that write to a workqueue. Everything that needs the struct
file, including the hashing, stays synchronous. The worker would take
sb_start_write(), then iint->mutex and inode_lock() (the same order as the open
path), and write only if no new writer has appeared and the change cookie still
matches. The EVM update is triggered by the xattr write, so it is deferred with
it.

Where this does not work is an open while the filesystem is still frozen. Freeze
blocks writers, not read opens, and close has already cleared IMA_DONE_MASK, so
I believe the open re-appraises and compares the new hash with the stale on-disk
xattr. In enforce mode that is -EACCES. After thaw, an open could flush the
pending update inline before verifying, so only the window during the freeze is
affected.

Options for that window:

  1. Accept -EACCES for files closed during a freeze until thaw.

  2. Make the open wait for thaw. That changes behavior for read opens, and
     the wait would have to happen before taking iint->mutex.

  3. Appraise against the digest pending in the iint instead of the
     stale on-disk xattr. This seems workable for IMA alone, but not
     for EVM: security.evm is only recomputed as a side effect of
     writing security.ima, so nothing covers the pending digest.

This is from reading the code and discussing with AI, not from testing.

(For background on fput-time locking in IMA, see LWN, "Toward a safer
fput()", April 2012. [1]).

Thanks,

Mimi

[1] https://lwn.net/Articles/494158/

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

end of thread, other threads:[~2026-11-23 21:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-13 23:55 ima/evm: security.ima and security.evm get written to frozen filesystems Christian Brauner
2026-11-23 21:10 ` Mimi Zohar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®