Skip to contentSkip to conversation

reconnect_one(): fix a missing error code

PATCH In review Dan Carpenter 3 participants 7 messages Jun 14, 2017 – Jun 16, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 reconnect_one(): fix a missing error code

I found this bug by reviewing places where we do ERR_PTR(0) (which is NULL).

We used to return an error pointer if lookup_one_len() failed but we moved this code into a helper function and accidentally removed that. NULL is a valid return for this function but it's not what we intended.

Fixes bbf7a8a3562f ("exportfs: move most of reconnect_path to helper function")
fs/exportfs/expfs.c +1 -0
index 329a5d103846..451237745689 100644
@@ -147,6 +147,7 @@ static struct dentry *reconnect_one(struct vfsmount *mnt,
 	tmp = lookup_one_len_unlocked(nbuf, parent, strlen(nbuf));
 	if (IS_ERR(tmp)) {
 		dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
+		err = PTR_ERR(tmp);
 		goto out_err;
 	}
 	if (tmp != dentry) {
#1 lore raw
Quoted 9 lines · Dan Carpenter

I found this bug by reviewing places where we do ERR_PTR(0) (which is NULL).

We used to return an error pointer if lookup_one_len() failed but we moved this code into a helper function and accidentally removed that. NULL is a valid return for this function but it's not what we intended.

Fixes bbf7a8a3562f ("exportfs: move most of reconnect_path to helper function")

ACK. Agreed that the current code is wrong, and that this is the correct fix.

What I don't quite understand yet is what the impact of the bug would be.

--b.

Quoted 13 lines
fs/exportfs/expfs.c +1 -0
index 329a5d103846..451237745689 100644
@@ -147,6 +147,7 @@ static struct dentry *reconnect_one(struct vfsmount *mnt,
 	tmp = lookup_one_len_unlocked(nbuf, parent, strlen(nbuf));
 	if (IS_ERR(tmp)) {
 		dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
+		err = PTR_ERR(tmp);
 		goto out_err;
 	}
 	if (tmp != dentry) {
#2 lore raw
Quoted 8 lines · J. Bruce Fields
Quoted 7 lines · Dan Carpenter

I found this bug by reviewing places where we do ERR_PTR(0) (which is NULL).

We used to return an error pointer if lookup_one_len() failed but we moved this code into a helper function and accidentally removed that. NULL is a valid return for this function but it's not what we intended.

Quoted 9 lines
Quoted 2 lines
Fixes bbf7a8a3562f ("exportfs: move most of reconnect_path to helper function")

ACK. Agreed that the current code is wrong, and that this is the correct fix.

What I don't quite understand yet is what the impact of the bug would be.

It is interesting that reconnect_path() handles the possibility of reconnect_one() returning NULL, even though it will only do that if this "bug" is triggered.
When that happens, the target_dir (a descendent of dentry) gets its DCACHE_DISCONNECTED flag cleared.

The bug can presumably only be triggered by a race.
We look through a directory to find the name for an inode (exportfs_get_name), then try to look up that name and it doesn't exist.

So presumably if you lose the race, some dentry will get DCACHE_DISCONNECTED cleared, even though it is still disconnected. This breaks a contract and can cause weirdness in dcache operations.

If the lookup_one_len_unlocked() fails, we should probably retry, at least once. But if we do decide to give up, we shouldn't assume it all worked.

So I suggest:
- the fix as provided by Dan, plus
- remove "if (!parent) break;" from reconnect_path(), plus
- maybe retry the get_name/lookup_one operation once if the first attempt fails.

NeilBrown

Quoted 15 lines

--b.

Quoted 13 lines
fs/exportfs/expfs.c +1 -0
index 329a5d103846..451237745689 100644
@@ -147,6 +147,7 @@ static struct dentry *reconnect_one(struct vfsmount *mnt,
 	tmp = lookup_one_len_unlocked(nbuf, parent, strlen(nbuf));
 	if (IS_ERR(tmp)) {
 		dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
+		err = PTR_ERR(tmp);
 		goto out_err;
 	}
 	if (tmp != dentry) {
#3 lore raw
Quoted 16 lines · NeilBrown
Quoted 14 lines · J. Bruce Fields
Quoted 9 lines · Dan Carpenter

I found this bug by reviewing places where we do ERR_PTR(0) (which is NULL).

We used to return an error pointer if lookup_one_len() failed but we moved this code into a helper function and accidentally removed that. NULL is a valid return for this function but it's not what we intended.

Fixes bbf7a8a3562f ("exportfs: move most of reconnect_path to helper function")

ACK. Agreed that the current code is wrong, and that this is the correct fix.

Quoted 7 lines
Quoted 3 lines

What I don't quite understand yet is what the impact of the bug would be.

It is interesting that reconnect_path() handles the possibility of reconnect_one() returning NULL, even though it will only do that if this "bug" is triggered.

No, we return NULL for the goto out_reconnected case.

regards,
dan carpenter

#4 lore raw
In reply to NeilBrown
Quoted 16 lines · NeilBrown
Quoted 14 lines · J. Bruce Fields
Quoted 9 lines · Dan Carpenter

I found this bug by reviewing places where we do ERR_PTR(0) (which is NULL).

We used to return an error pointer if lookup_one_len() failed but we moved this code into a helper function and accidentally removed that. NULL is a valid return for this function but it's not what we intended.

Fixes bbf7a8a3562f ("exportfs: move most of reconnect_path to helper function")

ACK. Agreed that the current code is wrong, and that this is the correct fix.

Quoted 7 lines
Quoted 3 lines

What I don't quite understand yet is what the impact of the bug would be.

It is interesting that reconnect_path() handles the possibility of reconnect_one() returning NULL, even though it will only do that if this "bug" is triggered.

As Dan says, you're missing a case.

Quoted 6 lines

When that happens, the target_dir (a descendent of dentry) gets its DCACHE_DISCONNECTED flag cleared.

The bug can presumably only be triggered by a race.
We look through a directory to find the name for an inode (exportfs_get_name), then try to look up that name and it doesn't exist.

Wouldn't lookup_one_len succesfully return a negative dentry in that case?

I think the error cases here are more likely due to permissions or IO errors.

So, I wonder if you can get some kind of dcache corruption with an uncached lookup of a directory with an ancestor that we lack permission to.

Quoted 8 lines

So presumably if you lose the race, some dentry will get DCACHE_DISCONNECTED cleared, even though it is still disconnected. This breaks a contract and can cause weirdness in dcache operations.

If the lookup_one_len_unlocked() fails, we should probably retry, at least once. But if we do decide to give up, we shouldn't assume it all worked.

Quoted 5 lines

So I suggest:
- the fix as provided by Dan, plus
- remove "if (!parent) break;" from reconnect_path(), plus
- maybe retry the get_name/lookup_one operation once if the first attempt fails.

See the comments in the code--if we lose the race, then it's because of a concurrent operation which should have done the reconnection for us.

--b.

#5 lore raw
Quoted 21 lines · J. Bruce Fields
Quoted 20 lines · NeilBrown
Quoted 17 lines · J. Bruce Fields
Quoted 9 lines · Dan Carpenter

I found this bug by reviewing places where we do ERR_PTR(0) (which is NULL).

We used to return an error pointer if lookup_one_len() failed but we moved this code into a helper function and accidentally removed that. NULL is a valid return for this function but it's not what we intended.

Fixes bbf7a8a3562f ("exportfs: move most of reconnect_path to helper function")

ACK. Agreed that the current code is wrong, and that this is the correct fix.

What I don't quite understand yet is what the impact of the bug would be.

Quoted 5 lines
Quoted 3 lines

It is interesting that reconnect_path() handles the possibility of reconnect_one() returning NULL, even though it will only do that if this "bug" is triggered.

As Dan says, you're missing a case.

:-(

Quoted 10 lines
Quoted 7 lines

When that happens, the target_dir (a descendent of dentry) gets its DCACHE_DISCONNECTED flag cleared.

The bug can presumably only be triggered by a race.
We look through a directory to find the name for an inode (exportfs_get_name), then try to look up that name and it doesn't exist.

Wouldn't lookup_one_len succesfully return a negative dentry in that case?

Uhm, yes. That case has a nice big comment in the "tmp != dentry" case too.

Quoted 7 lines

I think the error cases here are more likely due to permissions or IO errors.

So, I wonder if you can get some kind of dcache corruption with an uncached lookup of a directory with an ancestor that we lack permission to.

It wouldn't be too hard to create that scenario. It might be harder to find a way to expose the corruption.

I think you should trigger the WARN_ON_ONCE() in clear_disconnected() if you manage to trigger the bug.

The main reason that it is dangerous to have disconnected dentries around is for the loop detection on directory renames. You might be able to confuse the locking logic in there if one of the directories isn't connected to the root.

Thanks,
NeilBrown

Quoted 19 lines
Quoted 9 lines

So presumably if you lose the race, some dentry will get DCACHE_DISCONNECTED cleared, even though it is still disconnected. This breaks a contract and can cause weirdness in dcache operations.

If the lookup_one_len_unlocked() fails, we should probably retry, at least once. But if we do decide to give up, we shouldn't assume it all worked.

Quoted 5 lines

So I suggest:
- the fix as provided by Dan, plus
- remove "if (!parent) break;" from reconnect_path(), plus
- maybe retry the get_name/lookup_one operation once if the first attempt fails.

See the comments in the code--if we lose the race, then it's because of a concurrent operation which should have done the reconnection for us.

--b.

#6 lore raw
Quoted 2 lines · NeilBrown

It wouldn't be too hard to create that scenario. It might be harder to find a way to expose the corruption.

Yes. Well, in any case, I assume this Al's to take. ACK to the patch, and it should go to stable as well.

--b.