Skip to contentSkip to conversation

vfs: don't check may_create_in_sticky if the file is already open/created

RFC PATCH In review Jeff Layton 2 participants 8 messages Jul 26, 2022 – Jul 27, 2022

Subsystems (3)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

RFC PATCH 1/1 vfs: don't check may_create_in_sticky if the file is already open/created

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. Only call may_create_in_sticky if the file hasn't already been opened or created.

---
 fs/namei.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/namei.c +9 -4
index 1f28d3f463c3..7480b6dc8d27 100644
@@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
 			return -EEXIST;
 		if (d_is_dir(nd->path.dentry))
 			return -EISDIR;
-		error = may_create_in_sticky(mnt_userns, nd,
-					     d_backing_inode(nd->path.dentry));
-		if (unlikely(error))
-			return error;
+		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
+			error = may_create_in_sticky(mnt_userns, nd,
+						d_backing_inode(nd->path.dentry));
+			if (unlikely(error)) {
+				printk("%s: f_mode=0x%x oflag=0x%x\n",
+					__func__, file->f_mode, open_flag);
+				return error;
+			}
+		}
 	}
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;
Signature · 1 line
2.37.1
author maintainer
#1 lore raw
Quoted 31 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. Only call may_create_in_sticky if the file hasn't already been opened or created.

---
 fs/namei.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/namei.c +2 -4
index 1f28d3f463c3..7480b6dc8d27 100644
@@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
 			return -EEXIST;
 		if (d_is_dir(nd->path.dentry))
 			return -EISDIR;
-		error = may_create_in_sticky(mnt_userns, nd,
-					     d_backing_inode(nd->path.dentry));
-		if (unlikely(error))
-			return error;
+		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
+			error = may_create_in_sticky(mnt_userns, nd,

fs/namei.c static int do_open(struct nameidata *nd, · line 3509

Quoted 10 lines
fs/namei.c +7 -0
+						d_backing_inode(nd->path.dentry));
+			if (unlikely(error)) {
+				printk("%s: f_mode=0x%x oflag=0x%x\n",
+					__func__, file->f_mode, open_flag);
+				return error;
+			}
+		}
 	}
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;

I'm pretty sure this patch is the wrong approach, actually, since it doesn't fix the regular (non-atomic) open codepath. Any thoughts on what the right fix might be?

Signature · 1 line
Jeff Layton <[email protected]>
#2 lore raw
Quoted 37 lines · Jeff Layton
Quoted 36 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. Only call may_create_in_sticky if the file hasn't already been opened or created.

---
 fs/namei.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/namei.c +7 -4
index 1f28d3f463c3..7480b6dc8d27 100644
@@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
 			return -EEXIST;
 		if (d_is_dir(nd->path.dentry))
 			return -EISDIR;
-		error = may_create_in_sticky(mnt_userns, nd,
-					     d_backing_inode(nd->path.dentry));
-		if (unlikely(error))
-			return error;
+		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
+			error = may_create_in_sticky(mnt_userns, nd,
+						d_backing_inode(nd->path.dentry));
+			if (unlikely(error)) {
+				printk("%s: f_mode=0x%x oflag=0x%x\n",
+					__func__, file->f_mode, open_flag);
+				return error;
Quoted 8 lines

fs/namei.c static int do_open(struct nameidata *nd, · line 3509

Quoted 5 lines
fs/namei.c +2 -0
+			}
+		}
 	}
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;

I'm pretty sure this patch is the wrong approach, actually, since it doesn't fix the regular (non-atomic) open codepath. Any thoughts on what

Hey Jeff,

I haven't quite understood why that won't work for the regular open codepaths. I'm probably missing something obvious.

Quoted 1 line

the right fix might be?

When an actual creation has taken place - and not just a lookup - then may_create_in_sticky() assumes that the owner of the inode matches current_fsuid(). That'd would also be problematic on fat or in fact on any fs where the actual inode->i_{g,u}id are based on e.g. uid/gid mount options and not on current_fsuid(), I think?

So in order to improve this we would need to work around that assumption in some way. Either by skipping may_create_in_sticky() if the file got created or by adapting may_create_in_sticky().

I only wonder whether skipping may_create_in_sticky() altogether might be a bit too lax. One possibility that came to my mind might be to relax this assumption when the file has been created and the creator has CAP_FOWNER.

So (not compile tested or in any way) sm like:

fs/namei.c +7 -3
index 1f28d3f463c3..239e9f423346 100644
@@ -1221,7 +1221,8 @@ int may_linkat(struct user_namespace *mnt_userns, struct path *link)
  * Returns 0 if the open is allowed, -ve on error.
  */
 static int may_create_in_sticky(struct user_namespace *mnt_userns,
-                               struct nameidata *nd, struct inode *const inode)
+                               struct nameidata *nd, struct inode *const inode,
+                               bool created)
 {
        umode_t dir_mode = nd->dir_mode;
        kuid_t dir_uid = nd->dir_uid;
@@ -1230,7 +1231,9 @@ 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)) ||
+           (created &&
+            capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER)))
                return 0;
 
        if (likely(dir_mode & 0002) ||
@@ -3496,7 +3499,8 @@ static int do_open(struct nameidata *nd,
                if (d_is_dir(nd->path.dentry))
                        return -EISDIR;
                error = may_create_in_sticky(mnt_userns, nd,
-                                            d_backing_inode(nd->path.dentry));
+                                            d_backing_inode(nd->path.dentry),
+                                            (file->f_mode & FMODE_CREATED));
                if (unlikely(error))
                        return error;
        }
#3 lore raw
Quoted 86 lines · Christian Brauner
Quoted 37 lines · Jeff Layton
Quoted 36 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. Only call may_create_in_sticky if the file hasn't already been opened or created.

---
 fs/namei.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/namei.c +7 -4
index 1f28d3f463c3..7480b6dc8d27 100644
@@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
 			return -EEXIST;
 		if (d_is_dir(nd->path.dentry))
 			return -EISDIR;
-		error = may_create_in_sticky(mnt_userns, nd,
-					     d_backing_inode(nd->path.dentry));
-		if (unlikely(error))
-			return error;
+		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
+			error = may_create_in_sticky(mnt_userns, nd,
+						d_backing_inode(nd->path.dentry));
+			if (unlikely(error)) {
+				printk("%s: f_mode=0x%x oflag=0x%x\n",
+					__func__, file->f_mode, open_flag);
+				return error;
Quoted 8 lines

fs/namei.c static int do_open(struct nameidata *nd, · line 3509

Quoted 5 lines
fs/namei.c +2 -0
+			}
+		}
 	}
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;

I'm pretty sure this patch is the wrong approach, actually, since it doesn't fix the regular (non-atomic) open codepath. Any thoughts on what

Hey Jeff,

I haven't quite understood why that won't work for the regular open codepaths. I'm probably missing something obvious.

Quoted 1 line

the right fix might be?

When an actual creation has taken place - and not just a lookup - then may_create_in_sticky() assumes that the owner of the inode matches current_fsuid(). That'd would also be problematic on fat or in fact on any fs where the actual inode->i_{g,u}id are based on e.g. uid/gid mount options and not on current_fsuid(), I think?

So in order to improve this we would need to work around that assumption in some way. Either by skipping may_create_in_sticky() if the file got created or by adapting may_create_in_sticky().

I only wonder whether skipping may_create_in_sticky() altogether might be a bit too lax. One possibility that came to my mind might be to relax this assumption when the file has been created and the creator has CAP_FOWNER.

So (not compile tested or in any way) sm like:

fs/namei.c +2 -1
index 1f28d3f463c3..239e9f423346 100644
@@ -1221,7 +1221,8 @@ int may_linkat(struct user_namespace *mnt_userns, struct path *link)
  * Returns 0 if the open is allowed, -ve on error.
  */
 static int may_create_in_sticky(struct user_namespace *mnt_userns,
-                               struct nameidata *nd, struct inode *const inode)
+                               struct nameidata *nd, struct inode *const inode,
+                               bool created)
 {
        umode_t dir_mode = nd->dir_mode;
        kuid_t dir_uid = nd->dir_uid;

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

Quoted 7 lines
fs/namei.c +3 -1
@@ -1230,7 +1231,9 @@ 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)) ||
+           (created &&
+            capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER)))

Sorry, this should be inode_owner_or_capable(mnt_userns, inode)

Quoted 13 lines
fs/namei.c +2 -1
                return 0;
 
        if (likely(dir_mode & 0002) ||
@@ -3496,7 +3499,8 @@ static int do_open(struct nameidata *nd,
                if (d_is_dir(nd->path.dentry))
                        return -EISDIR;
                error = may_create_in_sticky(mnt_userns, nd,
-                                            d_backing_inode(nd->path.dentry));
+                                            d_backing_inode(nd->path.dentry),
+                                            (file->f_mode & FMODE_CREATED));
                if (unlikely(error))
                        return error;
        }
author maintainer
#4 lore raw
In reply to Christian Brauner
Quoted 47 lines · Christian Brauner
Quoted 37 lines · Jeff Layton
Quoted 36 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. Only call may_create_in_sticky if the file hasn't already been opened or created.

---
 fs/namei.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/namei.c +7 -4
index 1f28d3f463c3..7480b6dc8d27 100644
@@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
 			return -EEXIST;
 		if (d_is_dir(nd->path.dentry))
 			return -EISDIR;
-		error = may_create_in_sticky(mnt_userns, nd,
-					     d_backing_inode(nd->path.dentry));
-		if (unlikely(error))
-			return error;
+		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
+			error = may_create_in_sticky(mnt_userns, nd,
+						d_backing_inode(nd->path.dentry));
+			if (unlikely(error)) {
+				printk("%s: f_mode=0x%x oflag=0x%x\n",
+					__func__, file->f_mode, open_flag);
+				return error;
Quoted 8 lines

fs/namei.c static int do_open(struct nameidata *nd, · line 3509

Quoted 5 lines
fs/namei.c +2 -0
+			}
+		}
 	}
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;

I'm pretty sure this patch is the wrong approach, actually, since it doesn't fix the regular (non-atomic) open codepath. Any thoughts on what

Quoted 5 lines

Hey Jeff,

I haven't quite understood why that won't work for the regular open codepaths. I'm probably missing something obvious.

In the normal open codepaths, FMODE_OPENED | FMODE_CREATED are still clear. If we're not doing an atomic_open (i.e. the dentry doesn't exist yet or is negative), then nothing really happens until you get to the vfs_open call.

Quoted 8 lines
Quoted 1 line

the right fix might be?

When an actual creation has taken place - and not just a lookup - then may_create_in_sticky() assumes that the owner of the inode matches current_fsuid(). That'd would also be problematic on fat or in fact on any fs where the actual inode->i_{g,u}id are based on e.g. uid/gid mount options and not on current_fsuid(), I think?

Quoted 9 lines

So in order to improve this we would need to work around that assumption in some way. Either by skipping may_create_in_sticky() if the file got created or by adapting may_create_in_sticky().

I only wonder whether skipping may_create_in_sticky() altogether might be a bit too lax. One possibility that came to my mind might be to relax this assumption when the file has been created and the creator has CAP_FOWNER.

That may be the best option. I'll tinker around with that and see if I can get it to work. Thanks for the suggestion.

Quoted 28 lines

So (not compile tested or in any way) sm like:

fs/namei.c +5 -2
index 1f28d3f463c3..239e9f423346 100644
@@ -1221,7 +1221,8 @@ int may_linkat(struct user_namespace *mnt_userns, struct path *link)
  * Returns 0 if the open is allowed, -ve on error.
  */
 static int may_create_in_sticky(struct user_namespace *mnt_userns,
-                               struct nameidata *nd, struct inode *const inode)
+                               struct nameidata *nd, struct inode *const inode,
+                               bool created)
 {
        umode_t dir_mode = nd->dir_mode;
        kuid_t dir_uid = nd->dir_uid;
@@ -1230,7 +1231,9 @@ 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)) ||
+           (created &&
+            capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER)))
                return 0;
 
        if (likely(dir_mode & 0002) ||

fs/namei.c static int do_open(struct nameidata *nd, · line 3506

Quoted 9 lines
fs/namei.c +2 -1
@@ -3496,7 +3499,8 @@ static int do_open(struct nameidata *nd,
                if (d_is_dir(nd->path.dentry))
                        return -EISDIR;
                error = may_create_in_sticky(mnt_userns, nd,
-                                            d_backing_inode(nd->path.dentry));
+                                            d_backing_inode(nd->path.dentry),
+                                            (file->f_mode & FMODE_CREATED));
                if (unlikely(error))
                        return error;
        }

I think that still won't fix it in the normal open codepath. FMODE_CREATED won't be set, so this will just end up failing again.

Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw
Quoted 50 lines · Jeff Layton
Quoted 49 lines · Christian Brauner
Quoted 37 lines · Jeff Layton
Quoted 36 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. Only call may_create_in_sticky if the file hasn't already been opened or created.

---
 fs/namei.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/namei.c +7 -4
index 1f28d3f463c3..7480b6dc8d27 100644
@@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
 			return -EEXIST;
 		if (d_is_dir(nd->path.dentry))
 			return -EISDIR;
-		error = may_create_in_sticky(mnt_userns, nd,
-					     d_backing_inode(nd->path.dentry));
-		if (unlikely(error))
-			return error;
+		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
+			error = may_create_in_sticky(mnt_userns, nd,
+						d_backing_inode(nd->path.dentry));
+			if (unlikely(error)) {
+				printk("%s: f_mode=0x%x oflag=0x%x\n",
+					__func__, file->f_mode, open_flag);
+				return error;
Quoted 8 lines

fs/namei.c static int do_open(struct nameidata *nd, · line 3509

Quoted 5 lines
fs/namei.c +2 -0
+			}
+		}
 	}
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;

I'm pretty sure this patch is the wrong approach, actually, since it doesn't fix the regular (non-atomic) open codepath. Any thoughts on what

Hey Jeff,

Quoted 8 lines
Quoted 3 lines

I haven't quite understood why that won't work for the regular open codepaths. I'm probably missing something obvious.

In the normal open codepaths, FMODE_OPENED | FMODE_CREATED are still clear. If we're not doing an atomic_open (i.e. the dentry doesn't exist yet or is negative), then nothing really happens until you get to the vfs_open call.

Hm, so for atomic open with O_CREAT it's:

path_openat()
-> open_last_lookups()
-> lookup_open()
/*
* This is ->atomic_open() and FMODE_CREATED is set in the fs so
* for NFS it's done in:
* fs/nfs/dir.c: file->f_mode |= FMODE_CREATED;
*/
-> atomic_open()

and for regular O_CREAT open it's:

path_openat()
-> open_last_lookups()
-> lookup_open()

      {
        if (!dentry->d_inode && (open_flag & O_CREAT)) {
                file->f_mode |= FMODE_CREATED;
      }

and that should all get surfaced to:

path_openat()
-> do_open()

      -> may_create_in_sticky()

?

author maintainer
#6 lore raw
Quoted 83 lines · Christian Brauner
Quoted 50 lines · Jeff Layton
Quoted 49 lines · Christian Brauner
Quoted 37 lines · Jeff Layton
Quoted 36 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. Only call may_create_in_sticky if the file hasn't already been opened or created.

---
 fs/namei.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/namei.c +7 -4
index 1f28d3f463c3..7480b6dc8d27 100644
@@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
 			return -EEXIST;
 		if (d_is_dir(nd->path.dentry))
 			return -EISDIR;
-		error = may_create_in_sticky(mnt_userns, nd,
-					     d_backing_inode(nd->path.dentry));
-		if (unlikely(error))
-			return error;
+		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
+			error = may_create_in_sticky(mnt_userns, nd,
+						d_backing_inode(nd->path.dentry));
+			if (unlikely(error)) {
+				printk("%s: f_mode=0x%x oflag=0x%x\n",
+					__func__, file->f_mode, open_flag);
+				return error;
Quoted 8 lines

fs/namei.c static int do_open(struct nameidata *nd, · line 3509

Quoted 5 lines
fs/namei.c +2 -0
+			}
+		}
 	}
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;

I'm pretty sure this patch is the wrong approach, actually, since it doesn't fix the regular (non-atomic) open codepath. Any thoughts on what

Hey Jeff,

Quoted 8 lines
Quoted 3 lines

I haven't quite understood why that won't work for the regular open codepaths. I'm probably missing something obvious.

In the normal open codepaths, FMODE_OPENED | FMODE_CREATED are still clear. If we're not doing an atomic_open (i.e. the dentry doesn't exist yet or is negative), then nothing really happens until you get to the vfs_open call.

Hm, so for atomic open with O_CREAT it's:

path_openat()
-> open_last_lookups()
-> lookup_open()
/*
* This is ->atomic_open() and FMODE_CREATED is set in the fs so
* for NFS it's done in:
* fs/nfs/dir.c: file->f_mode |= FMODE_CREATED;
*/
-> atomic_open()

and for regular O_CREAT open it's:

path_openat()
-> open_last_lookups()
-> lookup_open()

      {
        if (!dentry->d_inode && (open_flag & O_CREAT)) {
                file->f_mode |= FMODE_CREATED;
      }
Quoted 7 lines

and that should all get surfaced to:

path_openat()
-> do_open()

      -> may_create_in_sticky()

?

Basically, yes, but we also need to deal with the case where the file already exists. That's being denied currently too and I don't think it should be.

Signature · 1 line
Jeff Layton <[email protected]>
#7 lore raw
Quoted 90 lines · Jeff Layton
Quoted 89 lines · Christian Brauner
Quoted 50 lines · Jeff Layton
Quoted 49 lines · Christian Brauner
Quoted 37 lines · Jeff Layton
Quoted 36 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. Only call may_create_in_sticky if the file hasn't already been opened or created.

---
 fs/namei.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/namei.c +7 -4
index 1f28d3f463c3..7480b6dc8d27 100644
@@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
 			return -EEXIST;
 		if (d_is_dir(nd->path.dentry))
 			return -EISDIR;
-		error = may_create_in_sticky(mnt_userns, nd,
-					     d_backing_inode(nd->path.dentry));
-		if (unlikely(error))
-			return error;
+		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
+			error = may_create_in_sticky(mnt_userns, nd,
+						d_backing_inode(nd->path.dentry));
+			if (unlikely(error)) {
+				printk("%s: f_mode=0x%x oflag=0x%x\n",
+					__func__, file->f_mode, open_flag);
+				return error;
Quoted 8 lines

fs/namei.c static int do_open(struct nameidata *nd, · line 3509

Quoted 5 lines
fs/namei.c +2 -0
+			}
+		}
 	}
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;

I'm pretty sure this patch is the wrong approach, actually, since it doesn't fix the regular (non-atomic) open codepath. Any thoughts on what

Hey Jeff,

Quoted 8 lines
Quoted 3 lines

I haven't quite understood why that won't work for the regular open codepaths. I'm probably missing something obvious.

In the normal open codepaths, FMODE_OPENED | FMODE_CREATED are still clear. If we're not doing an atomic_open (i.e. the dentry doesn't exist yet or is negative), then nothing really happens until you get to the vfs_open call.

Hm, so for atomic open with O_CREAT it's:

path_openat()
-> open_last_lookups()
-> lookup_open()
/*
* This is ->atomic_open() and FMODE_CREATED is set in the fs so
* for NFS it's done in:
* fs/nfs/dir.c: file->f_mode |= FMODE_CREATED;
*/
-> atomic_open()

and for regular O_CREAT open it's:

path_openat()
-> open_last_lookups()
-> lookup_open()

      {
        if (!dentry->d_inode && (open_flag & O_CREAT)) {
                file->f_mode |= FMODE_CREATED;
      }

and that should all get surfaced to:

path_openat()
-> do_open()

      -> may_create_in_sticky()
Quoted 5 lines
Quoted 1 line

?

Basically, yes, but we also need to deal with the case where the file already exists. That's being denied currently too and I don't think it should be.

Yeah, that's the part I'm not sure I agree with (see other thread).