linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs/fcntl: return -ESRCH in f_setown when pid/pgid can't be found
@ 2017-06-14 14:52 Jeff Layton
  2017-06-15  1:54 ` zhong jiang
  0 siblings, 1 reply; 2+ messages in thread
From: Jeff Layton @ 2017-06-14 14:52 UTC (permalink / raw)
  To: linux-fsdevel, linux-kernel; +Cc: jslaby, zhongjiang, bfields, viro, dhowells

The current implementation of F_SETOWN doesn't properly vet the argument
passed in. It never returns an error. If the argument doesn't specify a
valid pid/pgid, then we just end up cleaning out the file->f_owner
structure.

What we really want is to only clean that out only in the case where
userland passed in an argument of 0. For anything else, we want to
return ESRCH if it doesn't refer to a valid pid.

The relevant POSIX spec page is here:

    http://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html

Cc: Jiri Slaby <jslaby@suse.cz>
Cc: zhong jiang <zhongjiang@huawei.com>
Signed-off-by: Jeff Layton <jlayton@redhat.com>
---
 fs/fcntl.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/fs/fcntl.c b/fs/fcntl.c
index 693322e28751..afed3b364979 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -112,8 +112,9 @@ EXPORT_SYMBOL(__f_setown);
 int f_setown(struct file *filp, unsigned long arg, int force)
 {
 	enum pid_type type;
-	struct pid *pid;
-	int who = arg;
+	struct pid *pid = NULL;
+	int who = arg, ret = 0;
+
 	type = PIDTYPE_PID;
 	if (who < 0) {
 		/* avoid overflow below */
@@ -123,12 +124,19 @@ int f_setown(struct file *filp, unsigned long arg, int force)
 		type = PIDTYPE_PGID;
 		who = -who;
 	}
+
 	rcu_read_lock();
-	pid = find_vpid(who);
-	__f_setown(filp, pid, type, force);
+	if (who) {
+		pid = find_vpid(who);
+		if (!pid)
+			ret = -ESRCH;
+	}
+
+	if (!ret)
+		__f_setown(filp, pid, type, force);
 	rcu_read_unlock();
 
-	return 0;
+	return ret;
 }
 EXPORT_SYMBOL(f_setown);
 
-- 
2.13.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] fs/fcntl: return -ESRCH in f_setown when pid/pgid can't be found
  2017-06-14 14:52 [PATCH] fs/fcntl: return -ESRCH in f_setown when pid/pgid can't be found Jeff Layton
@ 2017-06-15  1:54 ` zhong jiang
  0 siblings, 0 replies; 2+ messages in thread
From: zhong jiang @ 2017-06-15  1:54 UTC (permalink / raw)
  To: Jeff Layton; +Cc: linux-fsdevel, linux-kernel, jslaby, bfields, viro, dhowells

yes,  look good to me.

but I found the another issue.  if the pass argument  is -1.  by the spec describe,
type should be assigned to PIDTYPE_MAX,  Do you think that it deserve another patch ?

Thanks
zhongjiang

On 2017/6/14 22:52, Jeff Layton wrote:
> The current implementation of F_SETOWN doesn't properly vet the argument
> passed in. It never returns an error. If the argument doesn't specify a
> valid pid/pgid, then we just end up cleaning out the file->f_owner
> structure.
>
> What we really want is to only clean that out only in the case where
> userland passed in an argument of 0. For anything else, we want to
> return ESRCH if it doesn't refer to a valid pid.
>
> The relevant POSIX spec page is here:
>
>     http://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
>
> Cc: Jiri Slaby <jslaby@suse.cz>
> Cc: zhong jiang <zhongjiang@huawei.com>
> Signed-off-by: Jeff Layton <jlayton@redhat.com>
> ---
>  fs/fcntl.c | 18 +++++++++++++-----
>  1 file changed, 13 insertions(+), 5 deletions(-)
>
> diff --git a/fs/fcntl.c b/fs/fcntl.c
> index 693322e28751..afed3b364979 100644
> --- a/fs/fcntl.c
> +++ b/fs/fcntl.c
> @@ -112,8 +112,9 @@ EXPORT_SYMBOL(__f_setown);
>  int f_setown(struct file *filp, unsigned long arg, int force)
>  {
>  	enum pid_type type;
> -	struct pid *pid;
> -	int who = arg;
> +	struct pid *pid = NULL;
> +	int who = arg, ret = 0;
> +
>  	type = PIDTYPE_PID;
>  	if (who < 0) {
>  		/* avoid overflow below */
> @@ -123,12 +124,19 @@ int f_setown(struct file *filp, unsigned long arg, int force)
>  		type = PIDTYPE_PGID;
>  		who = -who;
>  	}
> +
>  	rcu_read_lock();
> -	pid = find_vpid(who);
> -	__f_setown(filp, pid, type, force);
> +	if (who) {
> +		pid = find_vpid(who);
> +		if (!pid)
> +			ret = -ESRCH;
> +	}
> +
> +	if (!ret)
> +		__f_setown(filp, pid, type, force);
>  	rcu_read_unlock();
>  
> -	return 0;
> +	return ret;
>  }
>  EXPORT_SYMBOL(f_setown);
>  

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-06-15  1:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-14 14:52 [PATCH] fs/fcntl: return -ESRCH in f_setown when pid/pgid can't be found Jeff Layton
2017-06-15  1:54 ` zhong jiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).