Commit Graph

3 Commits

Author SHA1 Message Date
Pavel Reichl 28cbd3bc92 cachefiles: fix slab-use-after-free in cachefiles_withdraw_cookie()
JIRA: https://issues.redhat.com/browse/RHEL-51157
CVE: CVE-2024-41057

We got the following issue in our fault injection stress test:

==================================================================
BUG: KASAN: slab-use-after-free in cachefiles_withdraw_cookie+0x4d9/0x600
Read of size 8 at addr ffff888118efc000 by task kworker/u78:0/109

CPU: 13 PID: 109 Comm: kworker/u78:0 Not tainted 6.8.0-dirty #566
Call Trace:
 <TASK>
 kasan_report+0x93/0xc0
 cachefiles_withdraw_cookie+0x4d9/0x600
 fscache_cookie_state_machine+0x5c8/0x1230
 fscache_cookie_worker+0x91/0x1c0
 process_one_work+0x7fa/0x1800
 [...]

Allocated by task 117:
 kmalloc_trace+0x1b3/0x3c0
 cachefiles_acquire_volume+0xf3/0x9c0
 fscache_create_volume_work+0x97/0x150
 process_one_work+0x7fa/0x1800
 [...]

Freed by task 120301:
 kfree+0xf1/0x2c0
 cachefiles_withdraw_cache+0x3fa/0x920
 cachefiles_put_unbind_pincount+0x1f6/0x250
 cachefiles_daemon_release+0x13b/0x290
 __fput+0x204/0xa00
 task_work_run+0x139/0x230
 do_exit+0x87a/0x29b0
 [...]
==================================================================

Following is the process that triggers the issue:

           p1                |             p2
------------------------------------------------------------
                              fscache_begin_lookup
                               fscache_begin_volume_access
                                fscache_cache_is_live(fscache_cache)
cachefiles_daemon_release
 cachefiles_put_unbind_pincount
  cachefiles_daemon_unbind
   cachefiles_withdraw_cache
    fscache_withdraw_cache
     fscache_set_cache_state(cache, FSCACHE_CACHE_IS_WITHDRAWN);
    cachefiles_withdraw_objects(cache)
    fscache_wait_for_objects(fscache)
      atomic_read(&fscache_cache->object_count) == 0
                              fscache_perform_lookup
                               cachefiles_lookup_cookie
                                cachefiles_alloc_object
                                 refcount_set(&object->ref, 1);
                                 object->volume = volume
                                 fscache_count_object(vcookie->cache);
                                  atomic_inc(&fscache_cache->object_count)
    cachefiles_withdraw_volumes
     cachefiles_withdraw_volume
      fscache_withdraw_volume
      __cachefiles_free_volume
       kfree(cachefiles_volume)
                              fscache_cookie_state_machine
                               cachefiles_withdraw_cookie
                                cache = object->volume->cache;
                                // cachefiles_volume UAF !!!

After setting FSCACHE_CACHE_IS_WITHDRAWN, wait for all the cookie lookups
to complete first, and then wait for fscache_cache->object_count == 0 to
avoid the cookie exiting after the volume has been freed and triggering
the above issue. Therefore call fscache_withdraw_volume() before calling
cachefiles_withdraw_objects().

This way, after setting FSCACHE_CACHE_IS_WITHDRAWN, only the following two
cases will occur:
1) fscache_begin_lookup fails in fscache_begin_volume_access().
2) fscache_withdraw_volume() will ensure that fscache_count_object() has
   been executed before calling fscache_wait_for_objects().

Fixes: fe2140e2f57f ("cachefiles: Implement volume support")
Suggested-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Baokun Li <libaokun1@huawei.com>
Link: https://lore.kernel.org/r/20240628062930.2467993-4-libaokun@huaweicloud.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
(cherry picked from commit 5d8f805789072ea7fd39504694b7bd17e5f751c4)
Signed-off-by: Pavel Reichl <preichl@redhat.com>
2024-08-01 17:42:30 +02:00
Jeffrey Layton 2e0e76f653 fscache, cachefiles: Store the volume coherency data
Bugzilla: http://bugzilla.redhat.com/1229736

commit 32e150037dce368d129996ffe5f98217b1974d9e
Author: David Howells <dhowells@redhat.com>
Date:   Tue Dec 14 09:51:43 2021 +0000

    fscache, cachefiles: Store the volume coherency data

    Store the volume coherency data in an xattr and check it when we rebind the
    volume.  If it doesn't match the cache volume is moved to the graveyard and
    rebuilt anew.

    Changes
    =======
    ver #4:
     - Remove a couple of debugging prints.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    Link: https://lore.kernel.org/r/163967164397.1823006.2950539849831291830.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021563138.640689.15851092065380543119.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:36 -04:00
Jeffrey Layton 09af55a58e cachefiles: Implement volume support
Bugzilla: http://bugzilla.redhat.com/1229736

commit fe2140e2f57fef8562e0f9b7cd447d2b08dc2f35
Author: David Howells <dhowells@redhat.com>
Date:   Thu Oct 21 09:55:21 2021 +0100

    cachefiles: Implement volume support

    Implement support for creating the directory layout for a volume on disk
    and setting up and withdrawing volume caching.

    Each volume has a directory named for the volume key under the root of the
    cache (prefixed with an 'I' to indicate to cachefilesd that it's an index)
    and then creates a bunch of hash bucket subdirectories under that (named as
    '@' plus a hex number) in which cookie files will be created.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819635314.215744.13081522301564537723.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906936397.143852.17788457778396467161.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967143860.1823006.7185205806080225038.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021545212.640689.5064821392307582927.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:35 -04:00