Skip to contentSkip to conversation

vfs: bypass may_create_in_sticky check if task has CAP_FOWNER

PATCH Superseded Jeff Layton 2 participants 6 messages Jul 27, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

PATCH 1/1 vfs: bypass may_create_in_sticky check if task has CAP_FOWNER

NFS server is exporting a sticky directory (mode 01777) with root squashing enabled. Client has protect_regular enabled and then tries to open a file as root in that directory. File is created (with ownership set to nobody:nobody) but the open syscall returns an error.

The problem is may_create_in_sticky, which rejects the open even though the file has already been created/opened. Bypass the checks in may_create_in_sticky if the task has CAP_FOWNER in the given namespace.

---
 fs/namei.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/namei.c +2 -1
index 1f28d3f463c3..170c2396ba29 100644
@@ -1230,7 +1230,8 @@ static int may_create_in_sticky(struct user_namespace *mnt_userns,
 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
 	    likely(!(dir_mode & S_ISVTX)) ||
 	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
-	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
+	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) ||
+	    ns_capable(mnt_userns, CAP_FOWNER))
 		return 0;
 
 	if (likely(dir_mode & 0002) ||
Signature · 1 line
2.37.1
#1 lore raw
Quoted 22 lines · Jeff Layton

NFS server is exporting a sticky directory (mode 01777) with root squashing enabled. Client has protect_regular enabled and then tries to open a file as root in that directory. File is created (with ownership set to nobody:nobody) but the open syscall returns an error.

The problem is may_create_in_sticky, which rejects the open even though the file has already been created/opened. Bypass the checks in may_create_in_sticky if the task has CAP_FOWNER in the given namespace.

---
 fs/namei.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/namei.c +0 -0
index 1f28d3f463c3..170c2396ba29 100644

fs/namei.c static int may_create_in_sticky(struct user_namespace *mnt_userns, · line 1235

Quoted 7 lines
fs/namei.c +2 -1
@@ -1230,7 +1230,8 @@ static int may_create_in_sticky(struct user_namespace *mnt_userns,
 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
 	    likely(!(dir_mode & S_ISVTX)) ||
 	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
-	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
+	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) ||
+	    ns_capable(mnt_userns, CAP_FOWNER))
 		return 0;

Hm, no. You really want inode_owner_or_capable() here..
You need to verify that you have a mapping for the inode->i_{g,u}id in question and that you're having CAP_FOWNER in the caller's userns.

I'm pretty sure we should also restrict this to the case were the caller actually created the file otherwise we introduce a potential issue where the caller is susceptible to data spoofing. For example, the file was created by another user racing the caller's O_CREAT.

author maintainer
#2 lore raw
Quoted 18 lines · Christian Brauner
Quoted 17 lines · Jeff Layton

NFS server is exporting a sticky directory (mode 01777) with root squashing enabled. Client has protect_regular enabled and then tries to open a file as root in that directory. File is created (with ownership set to nobody:nobody) but the open syscall returns an error.

The problem is may_create_in_sticky, which rejects the open even though the file has already been created/opened. Bypass the checks in may_create_in_sticky if the task has CAP_FOWNER in the given namespace.

---
 fs/namei.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 17 lines

fs/namei.c static int may_create_in_sticky(struct user_namespace *mnt_userns, · line 1235

Quoted 12 lines
fs/namei.c +2 -1
index 1f28d3f463c3..170c2396ba29 100644
@@ -1230,7 +1230,8 @@ static int may_create_in_sticky(struct user_namespace *mnt_userns,
 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
 	    likely(!(dir_mode & S_ISVTX)) ||
 	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
-	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
+	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) ||
+	    ns_capable(mnt_userns, CAP_FOWNER))
 		return 0;

Hm, no. You really want inode_owner_or_capable() here..
You need to verify that you have a mapping for the inode->i_{g,u}id in question and that you're having CAP_FOWNER in the caller's userns.

Ok, I should be able to make that change and test it out.

Quoted 4 lines

I'm pretty sure we should also restrict this to the case were the caller actually created the file otherwise we introduce a potential issue where the caller is susceptible to data spoofing. For example, the file was created by another user racing the caller's O_CREAT.

That won't be sufficient to fix the testcase, I think. If a file already exists in the sticky dir and is owned by nobody:nobody, do we really want to prevent root from opening it? I wouldn't think so.

Signature · 1 line
Jeff Layton <[email protected]>
#3 lore raw
Quoted 39 lines · Jeff Layton
Quoted 18 lines · Christian Brauner
Quoted 17 lines · Jeff Layton

NFS server is exporting a sticky directory (mode 01777) with root squashing enabled. Client has protect_regular enabled and then tries to open a file as root in that directory. File is created (with ownership set to nobody:nobody) but the open syscall returns an error.

The problem is may_create_in_sticky, which rejects the open even though the file has already been created/opened. Bypass the checks in may_create_in_sticky if the task has CAP_FOWNER in the given namespace.

---
 fs/namei.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 17 lines

fs/namei.c static int may_create_in_sticky(struct user_namespace *mnt_userns, · line 1235

Quoted 12 lines
fs/namei.c +2 -1
index 1f28d3f463c3..170c2396ba29 100644
@@ -1230,7 +1230,8 @@ static int may_create_in_sticky(struct user_namespace *mnt_userns,
 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
 	    likely(!(dir_mode & S_ISVTX)) ||
 	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
-	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
+	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) ||
+	    ns_capable(mnt_userns, CAP_FOWNER))
 		return 0;

Hm, no. You really want inode_owner_or_capable() here..
You need to verify that you have a mapping for the inode->i_{g,u}id in question and that you're having CAP_FOWNER in the caller's userns.

Ok, I should be able to make that change and test it out.

Quoted 8 lines
Quoted 4 lines

I'm pretty sure we should also restrict this to the case were the caller actually created the file otherwise we introduce a potential issue where the caller is susceptible to data spoofing. For example, the file was created by another user racing the caller's O_CREAT.

That won't be sufficient to fix the testcase, I think. If a file already exists in the sticky dir and is owned by nobody:nobody, do we really want to prevent root from opening it? I wouldn't think so.

Afaict, the whole stick behind the protected_regular thing in may_create_in_sticky() thing is that you prevent scenarios where you can be tricked into opening a file that you didn't intend to with O_CREAT.

That's specifically also a protection for root. So say root specifies O_CREAT but someone beats root to it and creates the file dumping malicious data in there. The uid_eq() requirement is supposed to prevent such attacks and it's a sysctl that userspace opted into.

We'd be relaxing that restriction quite a bit if we not just allow newly created but also pre-existing file to be opened even with the CAP_FOWNER requirement.

So the dd call should really fail if O_CREAT is passed but the file is pre-existing, imho. It's a different story if dd created that file and has CAP_FOWNER imho.

author maintainer
#4 lore raw
Quoted 45 lines · Christian Brauner
Quoted 44 lines · Jeff Layton
Quoted 18 lines · Christian Brauner
Quoted 17 lines · Jeff Layton

NFS server is exporting a sticky directory (mode 01777) with root squashing enabled. Client has protect_regular enabled and then tries to open a file as root in that directory. File is created (with ownership set to nobody:nobody) but the open syscall returns an error.

The problem is may_create_in_sticky, which rejects the open even though the file has already been created/opened. Bypass the checks in may_create_in_sticky if the task has CAP_FOWNER in the given namespace.

---
 fs/namei.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 17 lines

fs/namei.c static int may_create_in_sticky(struct user_namespace *mnt_userns, · line 1235

Quoted 12 lines
fs/namei.c +2 -1
index 1f28d3f463c3..170c2396ba29 100644
@@ -1230,7 +1230,8 @@ static int may_create_in_sticky(struct user_namespace *mnt_userns,
 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
 	    likely(!(dir_mode & S_ISVTX)) ||
 	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
-	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
+	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) ||
+	    ns_capable(mnt_userns, CAP_FOWNER))
 		return 0;

Hm, no. You really want inode_owner_or_capable() here..
You need to verify that you have a mapping for the inode->i_{g,u}id in question and that you're having CAP_FOWNER in the caller's userns.

Ok, I should be able to make that change and test it out.

Quoted 4 lines

I'm pretty sure we should also restrict this to the case were the caller actually created the file otherwise we introduce a potential issue where the caller is susceptible to data spoofing. For example, the file was created by another user racing the caller's O_CREAT.

Quoted 8 lines
Quoted 3 lines

That won't be sufficient to fix the testcase, I think. If a file already exists in the sticky dir and is owned by nobody:nobody, do we really want to prevent root from opening it? I wouldn't think so.

Afaict, the whole stick behind the protected_regular thing in may_create_in_sticky() thing is that you prevent scenarios where you can be tricked into opening a file that you didn't intend to with O_CREAT.

Yuck. The proper way to get that protection is to use O_EXCL...

Quoted 5 lines

That's specifically also a protection for root. So say root specifies O_CREAT but someone beats root to it and creates the file dumping malicious data in there. The uid_eq() requirement is supposed to prevent such attacks and it's a sysctl that userspace opted into.

Quoted 7 lines

We'd be relaxing that restriction quite a bit if we not just allow newly created but also pre-existing file to be opened even with the CAP_FOWNER requirement.

So the dd call should really fail if O_CREAT is passed but the file is pre-existing, imho. It's a different story if dd created that file and has CAP_FOWNER imho.

That's pretty nasty. So if I create a file as root in a sticky dir that doesn't exist, and then close it and try to open it again it'll fail with -EACCES? That's terribly confusing.

Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw
Quoted 50 lines · Jeff Layton
Quoted 49 lines · Christian Brauner
Quoted 47 lines · Jeff Layton
Quoted 18 lines · Christian Brauner
Quoted 17 lines · Jeff Layton

NFS server is exporting a sticky directory (mode 01777) with root squashing enabled. Client has protect_regular enabled and then tries to open a file as root in that directory. File is created (with ownership set to nobody:nobody) but the open syscall returns an error.

The problem is may_create_in_sticky, which rejects the open even though the file has already been created/opened. Bypass the checks in may_create_in_sticky if the task has CAP_FOWNER in the given namespace.

---
 fs/namei.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 17 lines

fs/namei.c static int may_create_in_sticky(struct user_namespace *mnt_userns, · line 1235

Quoted 12 lines
fs/namei.c +2 -1
index 1f28d3f463c3..170c2396ba29 100644
@@ -1230,7 +1230,8 @@ static int may_create_in_sticky(struct user_namespace *mnt_userns,
 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
 	    likely(!(dir_mode & S_ISVTX)) ||
 	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
-	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
+	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) ||
+	    ns_capable(mnt_userns, CAP_FOWNER))
 		return 0;

Hm, no. You really want inode_owner_or_capable() here..
You need to verify that you have a mapping for the inode->i_{g,u}id in question and that you're having CAP_FOWNER in the caller's userns.

Ok, I should be able to make that change and test it out.

Quoted 4 lines

I'm pretty sure we should also restrict this to the case were the caller actually created the file otherwise we introduce a potential issue where the caller is susceptible to data spoofing. For example, the file was created by another user racing the caller's O_CREAT.

That won't be sufficient to fix the testcase, I think. If a file already exists in the sticky dir and is owned by nobody:nobody, do we really want to prevent root from opening it? I wouldn't think so.

Quoted 6 lines
Quoted 4 lines

Afaict, the whole stick behind the protected_regular thing in may_create_in_sticky() thing is that you prevent scenarios where you can be tricked into opening a file that you didn't intend to with O_CREAT.

Yuck. The proper way to get that protection is to use O_EXCL...

I'm not saying the interface was a particularly great idea. But it's at least a sysctl...

Quoted 10 lines
Quoted 10 lines

That's specifically also a protection for root. So say root specifies O_CREAT but someone beats root to it and creates the file dumping malicious data in there. The uid_eq() requirement is supposed to prevent such attacks and it's a sysctl that userspace opted into.

We'd be relaxing that restriction quite a bit if we not just allow newly created but also pre-existing file to be opened even with the CAP_FOWNER requirement.

Quoted 7 lines
Quoted 3 lines

So the dd call should really fail if O_CREAT is passed but the file is pre-existing, imho. It's a different story if dd created that file and has CAP_FOWNER imho.

That's pretty nasty. So if I create a file as root in a sticky dir that doesn't exist, and then close it and try to open it again it'll fail with -EACCES? That's terribly confusing.

At least only if you try to re-open with O_CREAT and have this protected_regular sysctl thingy turned on...