linux-stable/Documentation
Al Viro 362be9ec32 rename(): fix the locking of subdirectories
commit 22e111ed6c upstream.

	We should never lock two subdirectories without having taken
->s_vfs_rename_mutex; inode pointer order or not, the "order" proposed
in 28eceeda13 "fs: Lock moved directories" is not transitive, with
the usual consequences.

	The rationale for locking renamed subdirectory in all cases was
the possibility of race between rename modifying .. in a subdirectory to
reflect the new parent and another thread modifying the same subdirectory.
For a lot of filesystems that's not a problem, but for some it can lead
to trouble (e.g. the case when short directory contents is kept in the
inode, but creating a file in it might push it across the size limit
and copy its contents into separate data block(s)).

	However, we need that only in case when the parent does change -
otherwise ->rename() doesn't need to do anything with .. entry in the
first place.  Some instances are lazy and do a tautological update anyway,
but it's really not hard to avoid.

Amended locking rules for rename():
	find the parent(s) of source and target
	if source and target have the same parent
		lock the common parent
	else
		lock ->s_vfs_rename_mutex
		lock both parents, in ancestor-first order; if neither
		is an ancestor of another, lock the parent of source
		first.
	find the source and target.
	if source and target have the same parent
		if operation is an overwriting rename of a subdirectory
			lock the target subdirectory
	else
		if source is a subdirectory
			lock the source
		if target is a subdirectory
			lock the target
	lock non-directories involved, in inode pointer order if both
	source and target are such.

That way we are guaranteed that parents are locked (for obvious reasons),
that any renamed non-directory is locked (nfsd relies upon that),
that any victim is locked (emptiness check needs that, among other things)
and subdirectory that changes parent is locked (needed to protect the update
of .. entries).  We are also guaranteed that any operation locking more
than one directory either takes ->s_vfs_rename_mutex or locks a parent
followed by its child.

Cc: stable@vger.kernel.org
Fixes: 28eceeda13 "fs: Lock moved directories"
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-01-31 16:17:02 -08:00
..
ABI PM / devfreq: Fix buffer overflow in trans_stat_show 2024-01-31 16:16:58 -08:00
accounting
admin-guide docs: kernel_abi.py: fix command injection 2024-01-31 16:17:00 -08:00
arc
arm
arm64 arm64: errata: Add Cortex-A520 speculative unprivileged load workaround 2023-10-10 22:00:39 +02:00
block
bpf
cdrom
core-api
cpu-freq
crypto
dev-tools
devicetree dt-bindings: gpio: xilinx: Fix node address in gpio 2024-01-25 15:27:50 -08:00
doc-guide
driver-api PCI/P2PDMA: Remove reference to pci_p2pdma_map_sg() 2024-01-25 15:27:44 -08:00
fault-injection
fb
features
filesystems rename(): fix the locking of subdirectories 2024-01-31 16:17:02 -08:00
firmware-guide
firmware_class
fpga
gpu
hid
hwmon
i2c
ia64
iio
images
infiniband
input
isdn
kbuild
kernel-hacking
leds
litmus-tests
livepatch
locking
loongarch
m68k
maintainer
mhi
mips
misc-devices
mm
netlabel
networking tcp: enforce receive buffer memory limits by allowing the tcp window to shrink 2023-10-19 23:08:54 +02:00
nios2
nvdimm
openrisc
parisc
PCI
pcmcia
peci
power
powerpc
process
RCU
riscv
rust
s390
scheduler
scsi
security
sh
sound
sparc
sphinx docs: kernel_abi.py: fix command injection 2024-01-31 16:17:00 -08:00
sphinx-static
spi
staging
target
timers
tools
trace
translations
usb
userspace-api
virt x86/sev: Change snp_guest_issue_request()'s fw_err argument 2023-11-20 11:52:13 +01:00
w1
watchdog
x86
xtensa
.gitignore
arch.rst
atomic_bitops.txt
atomic_t.txt
Changes
CodingStyle
conf.py
docutils.conf
dontdiff
index.rst
Kconfig
Makefile
memory-barriers.txt
SubmittingPatches
subsystem-apis.rst