Revision 8e7f82deb0c0386a03b62e30082574347f8b57d5 authored by Filipe Manana on 12 September 2023, 10:45:39 UTC, committed by David Sterba on 14 September 2023, 21:24:42 UTC
When opening a directory (opendir(3)) or rewinding it (rewinddir(3)), we
are not holding the directory's inode locked, and this can result in later
attempting to add two entries to the directory with the same index number,
resulting in a transaction abort, with -EEXIST (-17), when inserting the
second delayed dir index. This results in a trace like the following:

  Sep 11 22:34:59 myhostname kernel: BTRFS error (device dm-3): err add delayed dir index item(name: cockroach-stderr.log) into the insertion tree of the delayed node(root id: 5, inode id: 4539217, errno: -17)
  Sep 11 22:34:59 myhostname kernel: ------------[ cut here ]------------
  Sep 11 22:34:59 myhostname kernel: kernel BUG at fs/btrfs/delayed-inode.c:1504!
  Sep 11 22:34:59 myhostname kernel: invalid opcode: 0000 [#1] PREEMPT SMP NOPTI
  Sep 11 22:34:59 myhostname kernel: CPU: 0 PID: 7159 Comm: cockroach Not tainted 6.4.15-200.fc38.x86_64 #1
  Sep 11 22:34:59 myhostname kernel: Hardware name: ASUS ESC500 G3/P9D WS, BIOS 2402 06/27/2018
  Sep 11 22:34:59 myhostname kernel: RIP: 0010:btrfs_insert_delayed_dir_index+0x1da/0x260
  Sep 11 22:34:59 myhostname kernel: Code: eb dd 48 (...)
  Sep 11 22:34:59 myhostname kernel: RSP: 0000:ffffa9980e0fbb28 EFLAGS: 00010282
  Sep 11 22:34:59 myhostname kernel: RAX: 0000000000000000 RBX: ffff8b10b8f4a3c0 RCX: 0000000000000000
  Sep 11 22:34:59 myhostname kernel: RDX: 0000000000000000 RSI: ffff8b177ec21540 RDI: ffff8b177ec21540
  Sep 11 22:34:59 myhostname kernel: RBP: ffff8b110cf80888 R08: 0000000000000000 R09: ffffa9980e0fb938
  Sep 11 22:34:59 myhostname kernel: R10: 0000000000000003 R11: ffffffff86146508 R12: 0000000000000014
  Sep 11 22:34:59 myhostname kernel: R13: ffff8b1131ae5b40 R14: ffff8b10b8f4a418 R15: 00000000ffffffef
  Sep 11 22:34:59 myhostname kernel: FS:  00007fb14a7fe6c0(0000) GS:ffff8b177ec00000(0000) knlGS:0000000000000000
  Sep 11 22:34:59 myhostname kernel: CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  Sep 11 22:34:59 myhostname kernel: CR2: 000000c00143d000 CR3: 00000001b3b4e002 CR4: 00000000001706f0
  Sep 11 22:34:59 myhostname kernel: Call Trace:
  Sep 11 22:34:59 myhostname kernel:  <TASK>
  Sep 11 22:34:59 myhostname kernel:  ? die+0x36/0x90
  Sep 11 22:34:59 myhostname kernel:  ? do_trap+0xda/0x100
  Sep 11 22:34:59 myhostname kernel:  ? btrfs_insert_delayed_dir_index+0x1da/0x260
  Sep 11 22:34:59 myhostname kernel:  ? do_error_trap+0x6a/0x90
  Sep 11 22:34:59 myhostname kernel:  ? btrfs_insert_delayed_dir_index+0x1da/0x260
  Sep 11 22:34:59 myhostname kernel:  ? exc_invalid_op+0x50/0x70
  Sep 11 22:34:59 myhostname kernel:  ? btrfs_insert_delayed_dir_index+0x1da/0x260
  Sep 11 22:34:59 myhostname kernel:  ? asm_exc_invalid_op+0x1a/0x20
  Sep 11 22:34:59 myhostname kernel:  ? btrfs_insert_delayed_dir_index+0x1da/0x260
  Sep 11 22:34:59 myhostname kernel:  ? btrfs_insert_delayed_dir_index+0x1da/0x260
  Sep 11 22:34:59 myhostname kernel:  btrfs_insert_dir_item+0x200/0x280
  Sep 11 22:34:59 myhostname kernel:  btrfs_add_link+0xab/0x4f0
  Sep 11 22:34:59 myhostname kernel:  ? ktime_get_real_ts64+0x47/0xe0
  Sep 11 22:34:59 myhostname kernel:  btrfs_create_new_inode+0x7cd/0xa80
  Sep 11 22:34:59 myhostname kernel:  btrfs_symlink+0x190/0x4d0
  Sep 11 22:34:59 myhostname kernel:  ? schedule+0x5e/0xd0
  Sep 11 22:34:59 myhostname kernel:  ? __d_lookup+0x7e/0xc0
  Sep 11 22:34:59 myhostname kernel:  vfs_symlink+0x148/0x1e0
  Sep 11 22:34:59 myhostname kernel:  do_symlinkat+0x130/0x140
  Sep 11 22:34:59 myhostname kernel:  __x64_sys_symlinkat+0x3d/0x50
  Sep 11 22:34:59 myhostname kernel:  do_syscall_64+0x5d/0x90
  Sep 11 22:34:59 myhostname kernel:  ? syscall_exit_to_user_mode+0x2b/0x40
  Sep 11 22:34:59 myhostname kernel:  ? do_syscall_64+0x6c/0x90
  Sep 11 22:34:59 myhostname kernel:  entry_SYSCALL_64_after_hwframe+0x72/0xdc

The race leading to the problem happens like this:

1) Directory inode X is loaded into memory, its ->index_cnt field is
   initialized to (u64)-1 (at btrfs_alloc_inode());

2) Task A is adding a new file to directory X, holding its vfs inode lock,
   and calls btrfs_set_inode_index() to get an index number for the entry.

   Because the inode's index_cnt field is set to (u64)-1 it calls
   btrfs_inode_delayed_dir_index_count() which fails because no dir index
   entries were added yet to the delayed inode and then it calls
   btrfs_set_inode_index_count(). This functions finds the last dir index
   key and then sets index_cnt to that index value + 1. It found that the
   last index key has an offset of 100. However before it assigns a value
   of 101 to index_cnt...

3) Task B calls opendir(3), ending up at btrfs_opendir(), where the VFS
   lock for inode X is not taken, so it calls btrfs_get_dir_last_index()
   and sees index_cnt still with a value of (u64)-1. Because of that it
   calls btrfs_inode_delayed_dir_index_count() which fails since no dir
   index entries were added to the delayed inode yet, and then it also
   calls btrfs_set_inode_index_count(). This also finds that the last
   index key has an offset of 100, and before it assigns the value 101
   to the index_cnt field of inode X...

4) Task A assigns a value of 101 to index_cnt. And then the code flow
   goes to btrfs_set_inode_index() where it increments index_cnt from
   101 to 102. Task A then creates a delayed dir index entry with a
   sequence number of 101 and adds it to the delayed inode;

5) Task B assigns 101 to the index_cnt field of inode X;

6) At some later point when someone tries to add a new entry to the
   directory, btrfs_set_inode_index() will return 101 again and shortly
   after an attempt to add another delayed dir index key with index
   number 101 will fail with -EEXIST resulting in a transaction abort.

Fix this by locking the inode at btrfs_get_dir_last_index(), which is only
only used when opening a directory or attempting to lseek on it.

Reported-by: ken <ken@bllue.org>
Link: https://lore.kernel.org/linux-btrfs/CAE6xmH+Lp=Q=E61bU+v9eWX8gYfLvu6jLYxjxjFpo3zHVPR0EQ@mail.gmail.com/
Reported-by: syzbot+d13490c82ad5353c779d@syzkaller.appspotmail.com
Link: https://lore.kernel.org/linux-btrfs/00000000000036e1290603e097e0@google.com/
Fixes: 9b378f6ad48c ("btrfs: fix infinite directory reads")
CC: stable@vger.kernel.org # 6.5+
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
1 parent e60aa5d
History
File Mode Size
842
crypto
dim
fonts
kunit
livepatch
lz4
lzo
math
mpi
pldmfw
raid6
reed_solomon
test_fortify
vdso
xz
zlib_deflate
zlib_dfltcc
zlib_inflate
zstd
.gitignore -rw-r--r-- 174 bytes
Kconfig -rw-r--r-- 17.1 KB
Kconfig.debug -rw-r--r-- 98.1 KB
Kconfig.kasan -rw-r--r-- 6.9 KB
Kconfig.kcsan -rw-r--r-- 10.0 KB
Kconfig.kfence -rw-r--r-- 3.6 KB
Kconfig.kgdb -rw-r--r-- 5.6 KB
Kconfig.kmsan -rw-r--r-- 2.3 KB
Kconfig.ubsan -rw-r--r-- 5.8 KB
Makefile -rw-r--r-- 15.0 KB
argv_split.c -rw-r--r-- 2.1 KB
ashldi3.c -rw-r--r-- 541 bytes
ashrdi3.c -rw-r--r-- 565 bytes
asn1_decoder.c -rw-r--r-- 13.2 KB
asn1_encoder.c -rw-r--r-- 10.3 KB
assoc_array.c -rw-r--r-- 51.9 KB
atomic64.c -rw-r--r-- 4.5 KB
atomic64_test.c -rw-r--r-- 6.4 KB
audit.c -rw-r--r-- 1.9 KB
base64.c -rw-r--r-- 2.4 KB
bcd.c -rw-r--r-- 297 bytes
bch.c -rw-r--r-- 38.3 KB
bitfield_kunit.c -rw-r--r-- 4.4 KB
bitmap.c -rw-r--r-- 46.3 KB
bitrev.c -rw-r--r-- 1.9 KB
bootconfig-data.S -rw-r--r-- 275 bytes
bootconfig.c -rw-r--r-- 21.5 KB
bsearch.c -rw-r--r-- 1.2 KB
btree.c -rw-r--r-- 19.2 KB
bucket_locks.c -rw-r--r-- 1.4 KB
bug.c -rw-r--r-- 6.1 KB
build_OID_registry -rwxr-xr-x 4.5 KB
buildid.c -rw-r--r-- 5.0 KB
bust_spinlocks.c -rw-r--r-- 632 bytes
check_signature.c -rw-r--r-- 635 bytes
checksum.c -rw-r--r-- 4.1 KB
checksum_kunit.c -rw-r--r-- 16.7 KB
clz_ctz.c -rw-r--r-- 1.2 KB
clz_tab.c -rw-r--r-- 891 bytes
cmdline.c -rw-r--r-- 5.8 KB
cmdline_kunit.c -rw-r--r-- 4.2 KB
cmpdi2.c -rw-r--r-- 501 bytes
compat_audit.c -rw-r--r-- 1002 bytes
cpu_rmap.c -rw-r--r-- 8.2 KB
cpumask.c -rw-r--r-- 5.4 KB
cpumask_kunit.c -rw-r--r-- 4.9 KB
crc-ccitt.c -rw-r--r-- 5.6 KB
crc-itu-t.c -rw-r--r-- 2.7 KB
crc-t10dif.c -rw-r--r-- 3.2 KB
crc16.c -rw-r--r-- 2.7 KB
crc32.c -rw-r--r-- 9.3 KB
crc32defs.h -rw-r--r-- 1.6 KB
crc32test.c -rw-r--r-- 37.4 KB
crc4.c -rw-r--r-- 1003 bytes
crc64-rocksoft.c -rw-r--r-- 3.2 KB
crc64.c -rw-r--r-- 2.6 KB
crc7.c -rw-r--r-- 2.5 KB
crc8.c -rw-r--r-- 2.4 KB
ctype.c -rw-r--r-- 1.4 KB
debug_info.c -rw-r--r-- 782 bytes
debug_locks.c -rw-r--r-- 1.2 KB
debugobjects.c -rw-r--r-- 36.9 KB
dec_and_lock.c -rw-r--r-- 2.0 KB
decompress.c -rw-r--r-- 1.8 KB
decompress_bunzip2.c -rw-r--r-- 23.5 KB
decompress_inflate.c -rw-r--r-- 4.8 KB
decompress_unlz4.c -rw-r--r-- 4.2 KB
decompress_unlzma.c -rw-r--r-- 15.8 KB
decompress_unlzo.c -rw-r--r-- 6.4 KB
decompress_unxz.c -rw-r--r-- 11.0 KB
decompress_unzstd.c -rw-r--r-- 10.3 KB
devmem_is_allowed.c -rw-r--r-- 705 bytes
devres.c -rw-r--r-- 14.6 KB
dhry.h -rw-r--r-- 14.3 KB
dhry_1.c -rw-r--r-- 7.9 KB
dhry_2.c -rw-r--r-- 4.4 KB
dhry_run.c -rw-r--r-- 1.7 KB
digsig.c -rw-r--r-- 5.5 KB
dump_stack.c -rw-r--r-- 3.0 KB
dynamic_debug.c -rw-r--r-- 37.4 KB
dynamic_queue_limits.c -rw-r--r-- 4.3 KB
earlycpio.c -rw-r--r-- 3.6 KB
errname.c -rw-r--r-- 3.9 KB
error-inject.c -rw-r--r-- 5.3 KB
errseq.c -rw-r--r-- 6.6 KB
extable.c -rw-r--r-- 2.9 KB
fault-inject-usercopy.c -rw-r--r-- 823 bytes
fault-inject.c -rw-r--r-- 11.3 KB
fdt.c -rw-r--r-- 69 bytes
fdt_addresses.c -rw-r--r-- 79 bytes
fdt_empty_tree.c -rw-r--r-- 80 bytes
fdt_ro.c -rw-r--r-- 72 bytes
fdt_rw.c -rw-r--r-- 72 bytes
fdt_strerror.c -rw-r--r-- 78 bytes
fdt_sw.c -rw-r--r-- 72 bytes
fdt_wip.c -rw-r--r-- 73 bytes
find_bit.c -rw-r--r-- 7.2 KB
find_bit_benchmark.c -rw-r--r-- 4.9 KB
flex_proportions.c -rw-r--r-- 7.1 KB
fortify_kunit.c -rw-r--r-- 11.1 KB
gen_crc32table.c -rw-r--r-- 3.3 KB
gen_crc64table.c -rw-r--r-- 2.1 KB
genalloc.c -rw-r--r-- 26.4 KB
generic-radix-tree.c -rw-r--r-- 5.3 KB
glob.c -rw-r--r-- 3.5 KB
globtest.c -rw-r--r-- 4.2 KB
group_cpus.c -rw-r--r-- 10.5 KB
hashtable_test.c -rw-r--r-- 8.0 KB
hexdump.c -rw-r--r-- 8.5 KB
hweight.c -rw-r--r-- 1.9 KB
idr.c -rw-r--r-- 17.5 KB
inflate.c -rw-r--r-- 38.7 KB
interval_tree.c -rw-r--r-- 4.2 KB
interval_tree_test.c -rw-r--r-- 3.4 KB
iomap.c -rw-r--r-- 11.0 KB
iomap_copy.c -rw-r--r-- 2.2 KB
iommu-helper.c -rw-r--r-- 755 bytes
iov_iter.c -rw-r--r-- 46.6 KB
irq_poll.c -rw-r--r-- 5.6 KB
irq_regs.c -rw-r--r-- 394 bytes
is_signed_type_kunit.c -rw-r--r-- 1.5 KB
is_single_threaded.c -rw-r--r-- 1.2 KB
kasprintf.c -rw-r--r-- 1.4 KB
kfifo.c -rw-r--r-- 12.1 KB
klist.c -rw-r--r-- 10.4 KB
kobject.c -rw-r--r-- 27.8 KB
kobject_uevent.c -rw-r--r-- 18.9 KB
kstrtox.c -rw-r--r-- 10.8 KB
kstrtox.h -rw-r--r-- 411 bytes
libcrc32c.c -rw-r--r-- 1.9 KB
linear_ranges.c -rw-r--r-- 8.1 KB
list-test.c -rw-r--r-- 35.1 KB
list_debug.c -rw-r--r-- 2.2 KB
list_sort.c -rw-r--r-- 8.2 KB
llist.c -rw-r--r-- 2.5 KB
locking-selftest-hardirq.h -rw-r--r-- 246 bytes
locking-selftest-mutex.h -rw-r--r-- 159 bytes
locking-selftest-rlock-hardirq.h -rw-r--r-- 74 bytes
locking-selftest-rlock-softirq.h -rw-r--r-- 74 bytes
locking-selftest-rlock.h -rw-r--r-- 197 bytes
locking-selftest-rsem.h -rw-r--r-- 202 bytes
locking-selftest-rtmutex.h -rw-r--r-- 162 bytes
locking-selftest-softirq.h -rw-r--r-- 246 bytes
locking-selftest-spin-hardirq.h -rw-r--r-- 73 bytes
locking-selftest-spin-softirq.h -rw-r--r-- 73 bytes
locking-selftest-spin.h -rw-r--r-- 157 bytes
locking-selftest-wlock-hardirq.h -rw-r--r-- 74 bytes
locking-selftest-wlock-softirq.h -rw-r--r-- 74 bytes
locking-selftest-wlock.h -rw-r--r-- 197 bytes
locking-selftest-wsem.h -rw-r--r-- 202 bytes
locking-selftest.c -rw-r--r-- 65.6 KB
lockref.c -rw-r--r-- 3.9 KB
logic_iomem.c -rw-r--r-- 7.2 KB
logic_pio.c -rw-r--r-- 8.5 KB
lru_cache.c -rw-r--r-- 17.5 KB
lshrdi3.c -rw-r--r-- 559 bytes
maple_tree.c -rw-r--r-- 180.7 KB
memcat_p.c -rw-r--r-- 753 bytes
memcpy_kunit.c -rw-r--r-- 16.9 KB
memory-notifier-error-inject.c -rw-r--r-- 1.1 KB
memregion.c -rw-r--r-- 429 bytes
memweight.c -rw-r--r-- 1.0 KB
muldi3.c -rw-r--r-- 1.7 KB
net_utils.c -rw-r--r-- 697 bytes
netdev-notifier-error-inject.c -rw-r--r-- 1.5 KB
nlattr.c -rw-r--r-- 27.2 KB
nmi_backtrace.c -rw-r--r-- 3.4 KB
notifier-error-inject.c -rw-r--r-- 2.5 KB
notifier-error-inject.h -rw-r--r-- 653 bytes
objagg.c -rw-r--r-- 28.2 KB
of-reconfig-notifier-error-inject.c -rw-r--r-- 1.3 KB
oid_registry.c -rw-r--r-- 4.4 KB
once.c -rw-r--r-- 2.2 KB
overflow_kunit.c -rw-r--r-- 43.9 KB
packing.c -rw-r--r-- 6.3 KB
parman.c -rw-r--r-- 10.6 KB
parser.c -rw-r--r-- 9.1 KB
pci_iomap.c -rw-r--r-- 5.7 KB
percpu-refcount.c -rw-r--r-- 15.3 KB
percpu_counter.c -rw-r--r-- 7.2 KB
percpu_test.c -rw-r--r-- 3.2 KB
plist.c -rw-r--r-- 5.9 KB
pm-notifier-error-inject.c -rw-r--r-- 1.2 KB
polynomial.c -rw-r--r-- 3.6 KB
radix-tree.c -rw-r--r-- 43.1 KB
radix-tree.h -rw-r--r-- 244 bytes
random32.c -rw-r--r-- 9.1 KB
ratelimit.c -rw-r--r-- 1.8 KB
rbtree.c -rw-r--r-- 17.1 KB
rbtree_test.c -rw-r--r-- 9.4 KB
rcuref.c -rw-r--r-- 9.5 KB
ref_tracker.c -rw-r--r-- 6.7 KB
refcount.c -rw-r--r-- 4.8 KB
rhashtable.c -rw-r--r-- 29.6 KB
sbitmap.c -rw-r--r-- 18.3 KB
scatterlist.c -rw-r--r-- 35.1 KB
seq_buf.c -rw-r--r-- 10.7 KB
sg_pool.c -rw-r--r-- 4.0 KB
sg_split.c -rw-r--r-- 5.0 KB
siphash.c -rw-r--r-- 11.6 KB
siphash_kunit.c -rw-r--r-- 7.2 KB
slub_kunit.c -rw-r--r-- 4.3 KB
smp_processor_id.c -rw-r--r-- 1.4 KB
sort.c -rw-r--r-- 8.9 KB
stackdepot.c -rw-r--r-- 15.6 KB
stackinit_kunit.c -rw-r--r-- 13.9 KB
stmp_device.c -rw-r--r-- 1.9 KB
strcat_kunit.c -rw-r--r-- 3.3 KB
string.c -rw-r--r-- 18.2 KB
string_helpers.c -rw-r--r-- 24.1 KB
strncpy_from_user.c -rw-r--r-- 3.9 KB
strnlen_user.c -rw-r--r-- 3.4 KB
strscpy_kunit.c -rw-r--r-- 4.5 KB
syscall.c -rw-r--r-- 2.7 KB
test-kstrtox.c -rw-r--r-- 17.3 KB
test-string_helpers.c -rw-r--r-- 14.9 KB
test_bitmap.c -rw-r--r-- 37.8 KB
test_bitops.c -rw-r--r-- 2.5 KB
test_bits.c -rw-r--r-- 1.8 KB
test_blackhole_dev.c -rw-r--r-- 2.5 KB
test_bpf.c -rw-r--r-- 356.2 KB
test_debug_virtual.c -rw-r--r-- 981 bytes
test_dynamic_debug.c -rw-r--r-- 3.7 KB
test_firmware.c -rw-r--r-- 37.4 KB
test_fprobe.c -rw-r--r-- 6.9 KB
test_fpu.c -rw-r--r-- 2.0 KB
test_free_pages.c -rw-r--r-- 1003 bytes
test_hash.c -rw-r--r-- 6.5 KB
test_hexdump.c -rw-r--r-- 6.3 KB
test_hmm.c -rw-r--r-- 38.0 KB
test_hmm_uapi.h -rw-r--r-- 2.9 KB
test_ida.c -rw-r--r-- 4.3 KB
test_kmod.c -rw-r--r-- 29.4 KB
test_kprobes.c -rw-r--r-- 10.1 KB
test_linear_ranges.c -rw-r--r-- 7.4 KB
test_list_sort.c -rw-r--r-- 3.3 KB
test_lockup.c -rw-r--r-- 16.5 KB
test_maple_tree.c -rw-r--r-- 96.4 KB
test_memcat_p.c -rw-r--r-- 2.2 KB
test_meminit.c -rw-r--r-- 10.2 KB
test_min_heap.c -rw-r--r-- 4.3 KB
test_module.c -rw-r--r-- 794 bytes
test_objagg.c -rw-r--r-- 24.6 KB
test_parman.c -rw-r--r-- 11.2 KB
test_printf.c -rw-r--r-- 19.5 KB
test_ref_tracker.c -rw-r--r-- 3.1 KB
test_rhashtable.c -rw-r--r-- 19.8 KB
test_scanf.c -rw-r--r-- 28.9 KB
test_sort.c -rw-r--r-- 907 bytes
test_static_key_base.c -rw-r--r-- 1.6 KB
test_static_keys.c -rw-r--r-- 5.6 KB
test_string.c -rw-r--r-- 4.6 KB
test_sysctl.c -rw-r--r-- 5.9 KB
test_ubsan.c -rw-r--r-- 2.4 KB
test_user_copy.c -rw-r--r-- 9.1 KB
test_uuid.c -rw-r--r-- 3.4 KB
test_vmalloc.c -rw-r--r-- 11.8 KB
test_xarray.c -rw-r--r-- 47.2 KB
textsearch.c -rw-r--r-- 9.3 KB
timerqueue.c -rw-r--r-- 2.3 KB
trace_readwrite.c -rw-r--r-- 1.5 KB
ts_bm.c -rw-r--r-- 5.1 KB
ts_fsm.c -rw-r--r-- 10.4 KB
ts_kmp.c -rw-r--r-- 4.1 KB
ubsan.c -rw-r--r-- 11.5 KB
ubsan.h -rw-r--r-- 3.2 KB
ucmpdi2.c -rw-r--r-- 568 bytes
ucs2_string.c -rw-r--r-- 2.5 KB
usercopy.c -rw-r--r-- 2.4 KB
uuid.c -rw-r--r-- 2.9 KB
vsprintf.c -rw-r--r-- 87.7 KB
win_minmax.c -rw-r--r-- 3.4 KB
xarray.c -rw-r--r-- 58.7 KB
xxhash.c -rw-r--r-- 12.7 KB

back to top