Commit d2fa4a84 authored by Mikhail Efremov's avatar Mikhail Efremov Committed by Al Viro

vfs: Don't exchange "short" filenames unconditionally.

Only exchange source and destination filenames
if flags contain RENAME_EXCHANGE.
In case if executable file was running and replaced by
other file /proc/PID/exe should still show correct file name,
not the old name of the file by which it was replaced.

The scenario when this bug manifests itself was like this:
* ALT Linux uses rpm and start-stop-daemon;
* during a package upgrade rpm creates a temporary file
  for an executable to rename it upon successful unpacking;
* start-stop-daemon is run subsequently and it obtains
  the (nonexistant) temporary filename via /proc/PID/exe
  thus failing to identify the running process.

Note that "long" filenames (> DNAiME_INLINE_LEN) are still
exchanged without RENAME_EXCHANGE and this behaviour exists
long enough (should be fixed too apparently).
So this patch is just an interim workaround that restores
behavior for "short" names as it was before changes
introduced by commit da1ce067 ("vfs: add cross-rename").

See https://lkml.org/lkml/2014/9/7/6 for details.

AV: the comments about being more careful with ->d_name.hash
than with ->d_name.name are from back in 2.3.40s; they
became obsolete by 2.3.60s, when we started to unhash the
target instead of swapping hash chain positions followed
by d_delete() as we used to do when dcache was first
introduced.
Acked-by: default avatarMiklos Szeredi <mszeredi@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: linux-fsdevel@vger.kernel.org
Cc: stable@vger.kernel.org
Fixes: da1ce067 "vfs: add cross-rename"
Signed-off-by: default avatarMikhail Efremov <sem@altlinux.org>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent a28ddb87
...@@ -2372,7 +2372,8 @@ void dentry_update_name_case(struct dentry *dentry, struct qstr *name) ...@@ -2372,7 +2372,8 @@ void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
} }
EXPORT_SYMBOL(dentry_update_name_case); EXPORT_SYMBOL(dentry_update_name_case);
static void switch_names(struct dentry *dentry, struct dentry *target) static void switch_names(struct dentry *dentry, struct dentry *target,
bool exchange)
{ {
if (dname_external(target)) { if (dname_external(target)) {
if (dname_external(dentry)) { if (dname_external(dentry)) {
...@@ -2406,6 +2407,12 @@ static void switch_names(struct dentry *dentry, struct dentry *target) ...@@ -2406,6 +2407,12 @@ static void switch_names(struct dentry *dentry, struct dentry *target)
*/ */
unsigned int i; unsigned int i;
BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long))); BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
if (!exchange) {
memcpy(dentry->d_iname, target->d_name.name,
target->d_name.len + 1);
dentry->d_name.hash_len = target->d_name.hash_len;
return;
}
for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) { for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
swap(((long *) &dentry->d_iname)[i], swap(((long *) &dentry->d_iname)[i],
((long *) &target->d_iname)[i]); ((long *) &target->d_iname)[i]);
...@@ -2456,12 +2463,15 @@ static void dentry_unlock_for_move(struct dentry *dentry, struct dentry *target) ...@@ -2456,12 +2463,15 @@ static void dentry_unlock_for_move(struct dentry *dentry, struct dentry *target)
* When switching names, the actual string doesn't strictly have to * When switching names, the actual string doesn't strictly have to
* be preserved in the target - because we're dropping the target * be preserved in the target - because we're dropping the target
* anyway. As such, we can just do a simple memcpy() to copy over * anyway. As such, we can just do a simple memcpy() to copy over
* the new name before we switch. * the new name before we switch, unless we are going to rehash
* * it. Note that if we *do* unhash the target, we are not allowed
* Note that we have to be a lot more careful about getting the hash * to rehash it without giving it a new name/hash key - whether
* switched - we have to switch the hash value properly even if it * we swap or overwrite the names here, resulting name won't match
* then no longer matches the actual (corrupted) string of the target. * the reality in filesystem; it's only there for d_path() purposes.
* The hash value has to match the hash queue that the dentry is on.. * Note that all of this is happening under rename_lock, so the
* any hash lookup seeing it in the middle of manipulations will
* be discarded anyway. So we do not care what happens to the hash
* key in that case.
*/ */
/* /*
* __d_move - move a dentry * __d_move - move a dentry
...@@ -2507,9 +2517,8 @@ static void __d_move(struct dentry *dentry, struct dentry *target, ...@@ -2507,9 +2517,8 @@ static void __d_move(struct dentry *dentry, struct dentry *target,
d_hash(dentry->d_parent, dentry->d_name.hash)); d_hash(dentry->d_parent, dentry->d_name.hash));
} }
/* Switch the names.. */ /* Switch the names.. */
switch_names(dentry, target); switch_names(dentry, target, exchange);
/* ... and switch them in the tree */ /* ... and switch them in the tree */
if (IS_ROOT(dentry)) { if (IS_ROOT(dentry)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment