fail dentry revalidation after namespace change
Subsystems (3)
PATCH v2 1/1 fail dentry revalidation after namespace change
When we change the namespace tag of a sysfs entry, the associated dentry is still kept around. readdir() will work correctly and not display the old entries, but open() will still succeed, so will reads and writes.
This will no longer happen if sysfs is remounted, hinting that this is a cache-related problem.
I am using the following sequence to demonstrate that:
shell1:
ip link add type veth
unshare -nm
shell2:
ip link set veth1 <pid_of_shell_1>
cat /sys/devices/virtual/net/veth1/ifindex
Before that patch, this will succeed (fail to fail). After it, it will correctly return an error. Differently from a normal rename, which we handle fine, changing the object namespace will keep it's path intact. So this check seems necessary as well.
[ v2: get type from parent, as suggested by Eric Biederman ]
--- fs/sysfs/dir.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/sysfs/dir.c +8 -0
@@ -307,6 +307,7 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) {struct sysfs_dirent *sd;int is_dir; +int type;if (nd->flags &LOOKUP_RCU )return -ECHILD ;
@@ -326,6 +327,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)if (strcmp (dentry->d_name.name, sd->s_name) !=0 ) goto out_bad; +/* The sysfs dirent has been moved to a different namespace */ + type =KOBJ_NS_TYPE_NONE ; +if (sd->s_parent) + type =sysfs_ns_type (sd->s_parent); +if (type && (sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns)) + goto out_bad; +mutex_unlock (&sysfs_mutex); out_valid:return 1 ;
Signature · 1 line
1.7.10.4
Quoted 3 lines · Glauber Costa
When we change the namespace tag of a sysfs entry, the associated dentry is still kept around.
readdir()will work correctly and not display the old entries, butopen()will still succeed, so will reads and writes.
Note reads and writes of file handles open before the move should continue to work.
Quoted 13 lines
This will no longer happen if sysfs is remounted, hinting that this is a cache-related problem.
I am using the following sequence to demonstrate that:
shell1:
ip link add type veth
unshare -nmshell2:
ip link set veth1 <pid_of_shell_1>
cat/sys/devices/virtual/net/veth1/ifindex
Quoted 6 lines
Before that patch, this will succeed (fail to fail). After it, it will correctly return an error. Differently from a normal rename, which we handle fine, changing the object namespace will keep it's path intact. So this check seems necessary as well.
[ v2: get type from parent, as suggested by Eric Biederman ]
Quoted 34 lines
Signed-off-by Glauber Costa <[email protected]>CC Tejun Heo <[email protected]>CC Eric W. Biederman <[email protected]>CC Greg Kroah-Hartman <[email protected]>--- fs/sysfs/dir.c | 8 ++++++++ 1 file changed, 8 insertions(+)fs/sysfs/dir.c +8
-0@@ -307,6 +307,7 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) {struct sysfs_dirent *sd;int is_dir; +int type;if (nd->flags &LOOKUP_RCU )return -ECHILD ;@@ -326,6 +327,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)if (strcmp (dentry->d_name.name, sd->s_name) !=0 ) goto out_bad; +/* The sysfs dirent has been moved to a different namespace */ + type =KOBJ_NS_TYPE_NONE ; +if (sd->s_parent) + type =sysfs_ns_type (sd->s_parent); +if (type && (sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns)) + goto out_bad; +mutex_unlock (&sysfs_mutex); out_valid:return 1 ;
Quoted 8 lines · Eric W. Biederman
Quoted 3 lines · Glauber Costa
When we change the namespace tag of a sysfs entry, the associated dentry is still kept around.
readdir()will work correctly and not display the old entries, butopen()will still succeed, so will reads and writes.Note reads and writes of file handles open before the move should continue to work.
Well, yes. But do you see it as a big problem?
This can probably be fixed as well, but I foresee a big hackishness in the way =p
Quoted 7 lines · Glauber Costa
Quoted 6 lines · Eric W. Biederman
Quoted 3 lines · Glauber Costa
When we change the namespace tag of a sysfs entry, the associated dentry is still kept around.
readdir()will work correctly and not display the old entries, butopen()will still succeed, so will reads and writes.
Quoted 7 lines
Quoted 2 lines
Note reads and writes of file handles open before the move should continue to work.
Well, yes. But do you see it as a big problem?
This can probably be fixed as well, but I foresee a big hackishness in the way =p
At the moment it looks like a feature.
The only reason we bounce between different instances of sysfs is because of the unfortunate sysfs directory layout that we need to remain compatible with.
But I don't see it making much of a difference either way.
Eric
Quoted 35 lines · On Fri, 6 Jul 2012 13:09:07 +0400 Glauber Costa <[email protected]> wrote:
When we change the namespace tag of a sysfs entry, the associated dentry is still kept around.
readdir()will work correctly and not display the old entries, butopen()will still succeed, so will reads and writes.This will no longer happen if sysfs is remounted, hinting that this is a cache-related problem.
I am using the following sequence to demonstrate that:
shell1:
ip link add type veth
unshare -nmshell2:
ip link set veth1 <pid_of_shell_1>
cat/sys/devices/virtual/net/veth1/ifindexBefore that patch, this will succeed (fail to fail). After it, it will correctly return an error. Differently from a normal rename, which we handle fine, changing the object namespace will keep it's path intact. So this check seems necessary as well.
...
fs/sysfs/dir.c +1
-0@@ -307,6 +307,7 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) {struct sysfs_dirent *sd;int is_dir; +int type;if (nd->flags &LOOKUP_RCU )return -ECHILD ;
fs/sysfs/dir.c static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) · line 334
Quoted 8 lines
fs/sysfs/dir.c +5
-0@@ -326,6 +327,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)if (strcmp (dentry->d_name.name, sd->s_name) !=0 ) goto out_bad; +/* The sysfs dirent has been moved to a different namespace */ + type =KOBJ_NS_TYPE_NONE ; +if (sd->s_parent) + type =sysfs_ns_type (sd->s_parent); +if (type && (sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns))
eww, the code is assuming that KOBJ_NS_TYPE_NONE has a value of zero. Don't do that; it smells bad.
I renamed my version of this patch to "sysfs: fail dentry revalidation after namespace change", as carefully explained in section 15 of the excellent Documentation/SubmittingPatches, then queued this:
From: Andrew Morton <[email protected]>
Subject: sysfs-fail-dentry-revalidation-after-namespace-change-fix
don't assume that KOBJ_NS_TYPE_NONE==0. Also save a test-n-branch.
---
fs/sysfs/dir.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~sysfs-fail-dentry-revalidation-after-namespace-change-fix +5 -3
@@ -329,10 +329,12 @@ static int sysfs_dentry_revalidate(struc/* The sysfs dirent has been moved to a different namespace */ type =KOBJ_NS_TYPE_NONE ; -if (sd->s_parent) +if (sd->s_parent) { type =sysfs_ns_type (sd->s_parent); -if (type && (sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns)) - goto out_bad; +if (type !=KOBJ_NS_TYPE_NONE && +sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns) + goto out_bad; + }mutex_unlock (&sysfs_mutex); out_valid:
_
Serge Hallyn
Acked-by Serge E. HallynQuoted 58 lines · Andrew Morton
Quoted 35 lines · On Fri, 6 Jul 2012 13:09:07 +0400 Glauber Costa <[email protected]> wrote:
When we change the namespace tag of a sysfs entry, the associated dentry is still kept around.
readdir()will work correctly and not display the old entries, butopen()will still succeed, so will reads and writes.This will no longer happen if sysfs is remounted, hinting that this is a cache-related problem.
I am using the following sequence to demonstrate that:
shell1:
ip link add type veth
unshare -nmshell2:
ip link set veth1 <pid_of_shell_1>
cat/sys/devices/virtual/net/veth1/ifindexBefore that patch, this will succeed (fail to fail). After it, it will correctly return an error. Differently from a normal rename, which we handle fine, changing the object namespace will keep it's path intact. So this check seems necessary as well.
...
fs/sysfs/dir.c +1
-0@@ -307,6 +307,7 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) {struct sysfs_dirent *sd;int is_dir; +int type;if (nd->flags &LOOKUP_RCU )return -ECHILD ;fs/sysfs/dir.c static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) · line 334
Quoted 8 lines
fs/sysfs/dir.c +5
-0@@ -326,6 +327,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)if (strcmp (dentry->d_name.name, sd->s_name) !=0 ) goto out_bad; +/* The sysfs dirent has been moved to a different namespace */ + type =KOBJ_NS_TYPE_NONE ; +if (sd->s_parent) + type =sysfs_ns_type (sd->s_parent); +if (type && (sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns))eww, the code is assuming that
KOBJ_NS_TYPE_NONEhas a value of zero. Don't do that; it smells bad.I renamed my version of this patch to "sysfs: fail dentry revalidation after namespace change", as carefully explained in section 15 of the excellent
Documentation/SubmittingPatches, then queued this:From: Andrew Morton <[email protected]>
Subject: sysfs-fail-dentry-revalidation-after-namespace-change-fix
Quoted 6 lines
don't assume that KOBJ_NS_TYPE_NONE==0. Also save a test-n-branch.
Cc Eric W. Biederman <[email protected]>Cc Glauber Costa <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Tejun Heo <[email protected]>
Quoted 28 lines
Signed-off-by Andrew Morton <[email protected]>---
fs/sysfs/dir.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)diff -puN
fs/sysfs/dir.c~sysfs-fail-dentry-revalidation-after-namespace-change-fixfs/sysfs/dir.cfs/sysfs/dir.c renamed from fs/sysfs/dir.c~sysfs-fail-dentry-revalidation-after-namespace-change-fix +5
-3@@ -329,10 +329,12 @@ static int sysfs_dentry_revalidate(struc/* The sysfs dirent has been moved to a different namespace */ type =KOBJ_NS_TYPE_NONE ; -if (sd->s_parent) +if (sd->s_parent) { type =sysfs_ns_type (sd->s_parent); -if (type && (sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns)) - goto out_bad; +if (type !=KOBJ_NS_TYPE_NONE && +sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns) + goto out_bad; + }mutex_unlock (&sysfs_mutex); out_valid:_
Quoted 11 lines · Andrew Morton
Quoted 11 lines
{
(quoted diff) +1
-0struct sysfs_dirent *sd; int is_dir; + int type; if (nd->flags & LOOKUP_RCU) return -ECHILD;@@ -326,6 +327,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) if (strcmp(dentry->d_name.name, sd->s_name) != 0) goto out_bad;
Quoted 8 lines
Review static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) · line 334
Quoted 5 lines
(quoted diff) +5
-0+ /* The sysfs dirent has been moved to a different namespace */ + type = KOBJ_NS_TYPE_NONE; + if (sd->s_parent) + type = sysfs_ns_type(sd->s_parent); + if (type && (sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns))eww, the code is assuming that
KOBJ_NS_TYPE_NONEhas a value of zero. Don't do that; it smells bad.
Gag. An incomplete change in idiom.
KOBJ_NS_TYPE_NONE is explicitly defined as 0 so that it can be used this way, and every where else in fs/sysfs/dir.c uses this idiom.
Furthermore your change below takes one line of readable code and turns it into something inappropriate to talk about in polite company.
If you want the code to be perfect type should be defined as "enum kobj_ns_type type" instead of "int kobj_ns_type".
Of course the truly perfect solution is to rework the sysfs code in a manner similar to proc, with magic internal symlinks and multiple parallel tress for the different namespaces. For the users of sysfs semantically there would be no changes but in the implementation there would many fewer special cases
for namespaces. The only special case would be reduced to
the internal sysfs symlink that lookup would have to know about.
fs/sysfs/dir.c static int sysfs_dentry_revalidate(struc · line 337
Quoted 13 lines
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~sysfs-fail-dentry-revalidation-after-namespace-change-fix +5
-3@@ -329,10 +329,12 @@ static int sysfs_dentry_revalidate(struc/* The sysfs dirent has been moved to a different namespace */ type =KOBJ_NS_TYPE_NONE ; -if (sd->s_parent) +if (sd->s_parent) { type =sysfs_ns_type (sd->s_parent); -if (type && (sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns)) - goto out_bad; +if (type !=KOBJ_NS_TYPE_NONE && +sysfs_info (dentry->d_sb)->ns[type] != sd->s_ns) + goto out_bad; + }
Pray tell in what parallel universe is that monstrosity above more readable than the line it replaces?
Eric
Quoted 22 lines · On Mon, 09 Jul 2012 17:30:48 -0700 [email protected] (Eric W. Biederman)…
Quoted 11 lines · Andrew Morton
Quoted 11 lines
{
(quoted diff) +1
-0struct sysfs_dirent *sd; int is_dir; + int type; if (nd->flags & LOOKUP_RCU) return -ECHILD;@@ -326,6 +327,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) if (strcmp(dentry->d_name.name, sd->s_name) != 0) goto out_bad;Quoted 8 lines
Review static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) · line 334
Quoted 5 lines
(quoted diff) +5
-0+ /* The sysfs dirent has been moved to a different namespace */ + type = KOBJ_NS_TYPE_NONE; + if (sd->s_parent) + type = sysfs_ns_type(sd->s_parent); + if (type && (sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns))eww, the code is assuming that
KOBJ_NS_TYPE_NONEhas a value of zero. Don't do that; it smells bad.
Quoted 4 lines
Gag. An incomplete change in idiom.
KOBJ_NS_TYPE_NONEis explicitly defined as 0 so that it can be used this way, and every where else infs/sysfs/dir.cuses this idiom.
One man's idiom is another man's idiocy.
Seriously. What sort of idea is that? Create an enumerated type and then just ignore it?
Quoted 2 lines
Pray tell in what parallel universe is that monstrosity above more readable than the line it replaces?
Don't be silly, it is not a "monstrosity". The code it is modifying contains an unneeded test-and-branch. It's a test and branch which the compiler might be able to avoid. If we can demonstrate that the compiler does indeed optimise it, or if we can find a less monstrous way of implementing it then fine. Otherwise, efficiency wins.
Quoted 24 lines · Andrew Morton
Quoted 22 lines · On Mon, 09 Jul 2012 17:30:48 -0700 [email protected] (Eric W. Biederman)…
Quoted 19 lines · Andrew Morton
Quoted 8 lines
{
(quoted diff) +1
-0struct sysfs_dirent *sd; int is_dir; + int type; if (nd->flags & LOOKUP_RCU) return -ECHILD;Review static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) · line 334
Quoted 8 lines
(quoted diff) +5
-0@@ -326,6 +327,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) if (strcmp(dentry->d_name.name, sd->s_name) != 0) goto out_bad; + /* The sysfs dirent has been moved to a different namespace */ + type = KOBJ_NS_TYPE_NONE; + if (sd->s_parent) + type = sysfs_ns_type(sd->s_parent); + if (type && (sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns))eww, the code is assuming that
KOBJ_NS_TYPE_NONEhas a value of zero. Don't do that; it smells bad.
Quoted 6 lines
Quoted 4 lines
Gag. An incomplete change in idiom.
KOBJ_NS_TYPE_NONEis explicitly defined as 0 so that it can be used this way, and every where else infs/sysfs/dir.cuses this idiom.One man's idiom is another man's idiocy.
And code that uses inconsistent idioms is even harder to read.
A half assed cleanup is worse than no cleanup.
Quoted 2 lines
Seriously. What sort of idea is that? Create an enumerated type and then just ignore it?
It isn't ignored. It just has a well defined NULL value. That is hardly controversial.
Quoted 8 lines
Quoted 2 lines
Pray tell in what parallel universe is that monstrosity above more readable than the line it replaces?
Don't be silly, it is not a "monstrosity". The code it is modifying contains an unneeded test-and-branch. It's a test and branch which the compiler might be able to avoid. If we can demonstrate that the compiler does indeed optimise it, or if we can find a less monstrous way of implementing it then fine. Otherwise, efficiency wins.
Efficiency wins? In a rarely used function? Which kernel are you working on?
Readable maintainable code wins. Unreadable code causes regressions.
Your addition while it may not be monstrous is most definitely less readable.
Eric
Quoted 26 lines · On Mon, 09 Jul 2012 18:51:37 -0700 [email protected] (Eric W. Biederman)…
Quoted 24 lines · Andrew Morton
Quoted 22 lines · On Mon, 09 Jul 2012 17:30:48 -0700 [email protected] (Eric W. Biederman)…
Quoted 19 lines · Andrew Morton
Quoted 8 lines
{
(quoted diff) +1
-0struct sysfs_dirent *sd; int is_dir; + int type; if (nd->flags & LOOKUP_RCU) return -ECHILD;Review static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) · line 334
Quoted 8 lines
(quoted diff) +5
-0@@ -326,6 +327,13 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) if (strcmp(dentry->d_name.name, sd->s_name) != 0) goto out_bad; + /* The sysfs dirent has been moved to a different namespace */ + type = KOBJ_NS_TYPE_NONE; + if (sd->s_parent) + type = sysfs_ns_type(sd->s_parent); + if (type && (sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns))eww, the code is assuming that
KOBJ_NS_TYPE_NONEhas a value of zero. Don't do that; it smells bad.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Gag. An incomplete change in idiom.
KOBJ_NS_TYPE_NONEis explicitly defined as 0 so that it can be used this way, and every where else infs/sysfs/dir.cuses this idiom.One man's idiom is another man's idiocy.
And code that uses inconsistent idioms is even harder to read.
Not true. That patch is more readable when it is changed to use correct types. If only because readers don't need to go in and check that KOBJ_NS_TYPE_NONE has value zero.
Quoted 5 lines
Quoted 2 lines
Seriously. What sort of idea is that? Create an enumerated type and then just ignore it?
It isn't ignored. It just has a well defined NULL value. That is hardly controversial.
If it's uncontroversial, why are we talking about it? Why did I, an experienced C and kernel developer, think that it looked stupid and possibly buggy?
I'm uncomfortable with propagating this idiotic and unnecessary trick any further. It's better to fix it.
Quoted 11 lines
Quoted 8 lines
Quoted 2 lines
Pray tell in what parallel universe is that monstrosity above more readable than the line it replaces?
Don't be silly, it is not a "monstrosity". The code it is modifying contains an unneeded test-and-branch. It's a test and branch which the compiler might be able to avoid. If we can demonstrate that the compiler does indeed optimise it, or if we can find a less monstrous way of implementing it then fine. Otherwise, efficiency wins.
Efficiency wins? In a rarely used function? Which kernel are you working on?
One in which we frequently optimise uncommon code paths.
Quoted 1 line
Readable maintainable code wins. Unreadable code causes regressions.
Dude, the whole reason for having enums and enumerated types is for readability and maintainability. If we didn't care about that, we'd use literal constants everywhere. And here you are arguing against that readability and maintainability.
If you want to say "yes, the sysfs code is bad but I can't be bothered fixing it all" then grumble, but OK. But for heavens sake, don't go and *defend* what that code is doing.