Skip to contentSkip to conversation

NFS: check FMODE_EXEC from open context mode

PATCH In review ChenXiaoSong 1 participant 5 messages Sep 23, 2022 – Nov 16, 2022

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

v3 cover NFS: check FMODE_EXEC from open context mode

Currently, we check FMODE_EXEC from file f_flags, and check FMODE_READ or FMODE_WRITE from open context mode.

After converting file f_flags to open context mode by flags_to_mode(), we can check all mode from open context mode.

ChenXiaoSong (2):
NFS: make sure open context mode have FMODE_EXEC when file open for
exec
NFSv4: check FMODE_EXEC from open context mode in
nfs4_opendata_access()

 fs/nfs/inode.c    |  3 ++-
 fs/nfs/nfs4file.c | 12 ++++--------
 fs/nfs/nfs4proc.c | 16 +++++-----------
 3 files changed, 11 insertions(+), 20 deletions(-)
Signature · 3 lines
v1->v2: use FMODE_EXEC instead of __FMODE_EXEC in nfs4_opendata_access to
	fix compiler warning
v2->v3: update cover letter message
#1 lore raw

PATCH v3 1/2 NFS: make sure open context mode have FMODE_EXEC when file open for exec

Because file f_mode never have FMODE_EXEC, open context mode won't get FMODE_EXEC from file f_mode. Open context mode only care about FMODE_READ/ FMODE_WRITE/FMODE_EXEC, and all info about open context mode can be convert from file f_flags, so convert file f_flags to open context mode by flags_to_mode().

---
 fs/nfs/inode.c    |  3 ++-
 fs/nfs/nfs4file.c | 12 ++++--------
 2 files changed, 6 insertions(+), 9 deletions(-)
fs/nfs/inode.c +2 -1
index bea7c005119c..bafa808823db 100644
@@ -1173,7 +1173,8 @@ int nfs_open(struct inode *inode, struct file *filp)
 {
 	struct nfs_open_context *ctx;
 
-	ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
+	ctx = alloc_nfs_open_context(file_dentry(filp),
+				     flags_to_mode(filp->f_flags), filp);
 	if (IS_ERR(ctx))
 		return PTR_ERR(ctx);
 	nfs_file_set_open_context(filp, ctx);
fs/nfs/nfs4file.c +4 -8
index 9eb181287879..2563ed8580f3 100644
@@ -32,7 +32,6 @@ nfs4_file_open(struct inode *inode, struct file *filp)
 	struct dentry *parent = NULL;
 	struct inode *dir;
 	unsigned openflags = filp->f_flags;
-	fmode_t f_mode;
 	struct iattr attr;
 	int err;
 
@@ -51,17 +50,14 @@ nfs4_file_open(struct inode *inode, struct file *filp)
 	if (err)
 		return err;
 
-	f_mode = filp->f_mode;
-	if ((openflags & O_ACCMODE) == 3)
-		f_mode |= flags_to_mode(openflags);
-
 	/* We can't create new files here */
 	openflags &= ~(O_CREAT|O_EXCL);
 
 	parent = dget_parent(dentry);
 	dir = d_inode(parent);
 
-	ctx = alloc_nfs_open_context(file_dentry(filp), f_mode, filp);
+	ctx = alloc_nfs_open_context(file_dentry(filp),
+				     flags_to_mode(openflags), filp);
 	err = PTR_ERR(ctx);
 	if (IS_ERR(ctx))
 		goto out;
@@ -366,8 +362,8 @@ static struct file *__nfs42_ssc_open(struct vfsmount *ss_mnt,
 		goto out_free_name;
 	}
 
-	ctx = alloc_nfs_open_context(filep->f_path.dentry, filep->f_mode,
-					filep);
+	ctx = alloc_nfs_open_context(filep->f_path.dentry,
+				     flags_to_mode(filep->f_flags), filep);
 	if (IS_ERR(ctx)) {
 		res = ERR_CAST(ctx);
 		goto out_filep;
Signature · 1 line
2.31.1
#2 lore raw

PATCH v3 2/2 NFSv4: check FMODE_EXEC from open context mode in nfs4_opendata_access()

In reply to ChenXiaoSong

After converting file f_flags to open context mode by flags_to_mode(), open context mode will have FMODE_EXEC when file open for exec, so we check FMODE_EXEC from open context mode.

No functional change, just simplify the code.

---
 fs/nfs/nfs4proc.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
fs/nfs/nfs4proc.c +5 -11
index 3ed14a2a84a4..806d243f66e8 100644
@@ -2624,8 +2624,7 @@ static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  */
 static int nfs4_opendata_access(const struct cred *cred,
 				struct nfs4_opendata *opendata,
-				struct nfs4_state *state, fmode_t fmode,
-				int openflags)
+				struct nfs4_state *state, fmode_t fmode)
 {
 	struct nfs_access_entry cache;
 	u32 mask, flags;
@@ -2636,11 +2635,7 @@ static int nfs4_opendata_access(const struct cred *cred,
 		return 0;
 
 	mask = 0;
-	/*
-	 * Use openflags to check for exec, because fmode won't
-	 * always have FMODE_EXEC set when file open for exec.
-	 */
-	if (openflags & __FMODE_EXEC) {
+	if (fmode & FMODE_EXEC) {
 		/* ONLY check for exec rights */
 		if (S_ISDIR(state->inode->i_mode))
 			mask = NFS4_ACCESS_LOOKUP;
@@ -3023,7 +3018,7 @@ static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
 }
 
 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
-		int flags, struct nfs_open_context *ctx)
+		struct nfs_open_context *ctx)
 {
 	struct nfs4_state_owner *sp = opendata->owner;
 	struct nfs_server *server = sp->so_server;
@@ -3084,8 +3079,7 @@ static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
 	/* Parse layoutget results before we check for access */
 	pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
 
-	ret = nfs4_opendata_access(sp->so_cred, opendata, state,
-			acc_mode, flags);
+	ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
 	if (ret != 0)
 		goto out;
 
@@ -3159,7 +3153,7 @@ static int _nfs4_do_open(struct inode *dir,
 	if (d_really_is_positive(dentry))
 		opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
 
-	status = _nfs4_open_and_get_state(opendata, flags, ctx);
+	status = _nfs4_open_and_get_state(opendata, ctx);
 	if (status != 0)
 		goto err_opendata_put;
 	state = ctx->state;
Signature · 1 line
2.31.1
#3 lore raw
In reply to ChenXiaoSong

Friendly ping...

Quoted 17 lines · ChenXiaoSong

Currently, we check FMODE_EXEC from file f_flags, and check FMODE_READ or FMODE_WRITE from open context mode.

After converting file f_flags to open context mode by flags_to_mode(), we can check all mode from open context mode.

ChenXiaoSong (2):
NFS: make sure open context mode have FMODE_EXEC when file open for
exec
NFSv4: check FMODE_EXEC from open context mode in
nfs4_opendata_access()

  fs/nfs/inode.c    |  3 ++-
  fs/nfs/nfs4file.c | 12 ++++--------
  fs/nfs/nfs4proc.c | 16 +++++-----------
  3 files changed, 11 insertions(+), 20 deletions(-)
#4 lore raw
In reply to ChenXiaoSong

Hi Trond:

Do you have any suggestions for this patchset ?

Quoted 17 lines · ChenXiaoSong

Currently, we check FMODE_EXEC from file f_flags, and check FMODE_READ or FMODE_WRITE from open context mode.

After converting file f_flags to open context mode by flags_to_mode(), we can check all mode from open context mode.

ChenXiaoSong (2):
NFS: make sure open context mode have FMODE_EXEC when file open for
exec
NFSv4: check FMODE_EXEC from open context mode in
nfs4_opendata_access()

  fs/nfs/inode.c    |  3 ++-
  fs/nfs/nfs4file.c | 12 ++++--------
  fs/nfs/nfs4proc.c | 16 +++++-----------
  3 files changed, 11 insertions(+), 20 deletions(-)