Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1589629 > unrolled thread

[PATCH 1/2] vfs: implement fchmodat2() syscall

Started byGreg Kurz <groug@kaod.org>
First post2017-02-28 18:10 +0100
Last post2017-03-01 10:10 +0100
Articles 6 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 1/2] vfs: implement fchmodat2() syscall Greg Kurz <groug@kaod.org> - 2017-02-28 18:10 +0100
    [PATCH 2/2] x86: wire up fchmodat2() syscall Greg Kurz <groug@kaod.org> - 2017-02-28 18:10 +0100
    Re: [PATCH 1/2] vfs: implement fchmodat2() syscall Eric Blake <eblake@redhat.com> - 2017-02-28 19:30 +0100
      Re: [PATCH 1/2] vfs: implement fchmodat2() syscall Eric Blake <eblake@redhat.com> - 2017-03-01 12:00 +0100
      Re: [PATCH 1/2] vfs: implement fchmodat2() syscall Greg Kurz <groug@kaod.org> - 2017-03-01 12:50 +0100
    Re: [PATCH 1/2] vfs: implement fchmodat2() syscall Michael Kerrisk <mtk.manpages@gmail.com> - 2017-03-01 10:10 +0100

#1589629 — [PATCH 1/2] vfs: implement fchmodat2() syscall

FromGreg Kurz <groug@kaod.org>
Date2017-02-28 18:10 +0100
Subject[PATCH 1/2] vfs: implement fchmodat2() syscall
Message-ID<tfTrH-7ry-3@gated-at.bofh.it>
According to the POSIX.1-2008 manual page [1], the fchmodat() function has
a flag argument which may be passed the following value:

AT_SYMLINK_NOFOLLOW
    If path names a symbolic link, then the mode of the symbolic link is
    changed.

and the following error may be returned:

[EOPNOTSUPP]
    The AT_SYMLINK_NOFOLLOW bit is set in the flag argument, path names a
    symbolic link, and the system does not support changing the mode of a
    symbolic link.

The linux kernel doesn't support changing the mode of a symbolic link, but
the current implementation doesn't even have a flag argument. It is then
up to userspace to deal with that. Unfortunately, it is impossible to
implement the POSIX behavior in a race-free manner.

This patch introduces a new fchmodat2() syscall with a flag argument to
address the issue.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html

Signed-off-by: Greg Kurz <groug@kaod.org>
---
 fs/open.c                         |   23 +++++++++++++++++++----
 include/linux/syscalls.h          |    2 ++
 include/uapi/asm-generic/unistd.h |    4 +++-
 scripts/checksyscalls.sh          |    3 ++-
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/fs/open.c b/fs/open.c
index 9921f70bc5ca..66a8c19f72ca 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -558,24 +558,39 @@ SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
 	return err;
 }
 
-SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
+SYSCALL_DEFINE4(fchmodat2, int, dfd, const char __user *, filename, umode_t,
+		mode, int, flag)
 {
 	struct path path;
-	int error;
-	unsigned int lookup_flags = LOOKUP_FOLLOW;
+	int error = -EINVAL;
+	unsigned int lookup_flags;
+
+	if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
+		goto out;
+
+	lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
 retry:
 	error = user_path_at(dfd, filename, lookup_flags, &path);
 	if (!error) {
-		error = chmod_common(&path, mode);
+		error = -EOPNOTSUPP;
+		if (!d_is_symlink(path.dentry))
+			error = chmod_common(&path, mode);
 		path_put(&path);
 		if (retry_estale(error, lookup_flags)) {
 			lookup_flags |= LOOKUP_REVAL;
 			goto retry;
 		}
 	}
+out:
 	return error;
 }
 
+SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t,
+		mode)
+{
+	return sys_fchmodat2(dfd, filename, mode, 0);
+}
+
 SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
 {
 	return sys_fchmodat(AT_FDCWD, filename, mode);
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index 91a740f6b884..982089d55b31 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -775,6 +775,8 @@ asmlinkage long sys_futimesat(int dfd, const char __user *filename,
 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode);
 asmlinkage long sys_fchmodat(int dfd, const char __user * filename,
 			     umode_t mode);
+asmlinkage long sys_fchmodat2(int dfd, const char __user *filename,
+			      umode_t mode, int flag);
 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
 			     gid_t group, int flag);
 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h
index 9b1462e38b82..e8b0a00908b1 100644
--- a/include/uapi/asm-generic/unistd.h
+++ b/include/uapi/asm-generic/unistd.h
@@ -730,9 +730,11 @@ __SYSCALL(__NR_pkey_mprotect, sys_pkey_mprotect)
 __SYSCALL(__NR_pkey_alloc,    sys_pkey_alloc)
 #define __NR_pkey_free 290
 __SYSCALL(__NR_pkey_free,     sys_pkey_free)
+#define __NR_fchmodat2 291
+__SYSCALL(__NR_fchmodat2,     sys_fchmodat2)
 
 #undef __NR_syscalls
-#define __NR_syscalls 291
+#define __NR_syscalls 292
 
 /*
  * All syscalls below here should go away really,
diff --git a/scripts/checksyscalls.sh b/scripts/checksyscalls.sh
index 2c9082ba6137..2e7471a1d308 100755
--- a/scripts/checksyscalls.sh
+++ b/scripts/checksyscalls.sh
@@ -19,7 +19,7 @@ cat << EOF
 #define __IGNORE_link		/* linkat */
 #define __IGNORE_unlink		/* unlinkat */
 #define __IGNORE_mknod		/* mknodat */
-#define __IGNORE_chmod		/* fchmodat */
+#define __IGNORE_chmod		/* fchmodat2 */
 #define __IGNORE_chown		/* fchownat */
 #define __IGNORE_mkdir		/* mkdirat */
 #define __IGNORE_rmdir		/* unlinkat */
@@ -39,6 +39,7 @@ cat << EOF
 
 /* Missing flags argument */
 #define __IGNORE_renameat	/* renameat2 */
+#define __IGNORE_fchmodat	/* fchmodat2 */
 
 /* CLOEXEC flag */
 #define __IGNORE_pipe		/* pipe2 */

[toc] | [next] | [standalone]


#1589630 — [PATCH 2/2] x86: wire up fchmodat2() syscall

FromGreg Kurz <groug@kaod.org>
Date2017-02-28 18:10 +0100
Subject[PATCH 2/2] x86: wire up fchmodat2() syscall
Message-ID<tfTrI-7ry-21@gated-at.bofh.it>
In reply to#1589629
Signed-off-by: Greg Kurz <groug@kaod.org>
---
 arch/x86/entry/syscalls/syscall_32.tbl |    1 +
 arch/x86/entry/syscalls/syscall_64.tbl |    1 +
 2 files changed, 2 insertions(+)

diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscalls/syscall_32.tbl
index 2b3618542544..413529600b8a 100644
--- a/arch/x86/entry/syscalls/syscall_32.tbl
+++ b/arch/x86/entry/syscalls/syscall_32.tbl
@@ -389,3 +389,4 @@
 380	i386	pkey_mprotect		sys_pkey_mprotect
 381	i386	pkey_alloc		sys_pkey_alloc
 382	i386	pkey_free		sys_pkey_free
+383	i386	fchmodat2		sys_fchmodat2
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index e93ef0b38db8..23a7e5d02ae4 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -338,6 +338,7 @@
 329	common	pkey_mprotect		sys_pkey_mprotect
 330	common	pkey_alloc		sys_pkey_alloc
 331	common	pkey_free		sys_pkey_free
+332	common	fchmodat2		sys_fchmodat2
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact

[toc] | [prev] | [next] | [standalone]


#1589680

FromEric Blake <eblake@redhat.com>
Date2017-02-28 19:30 +0100
Message-ID<tfUH8-8cC-35@gated-at.bofh.it>
In reply to#1589629

[Multipart message — attachments visible in raw view] — view raw

On 02/28/2017 11:03 AM, Greg Kurz wrote:
> According to the POSIX.1-2008 manual page [1], the fchmodat() function has
> a flag argument which may be passed the following value:
> 
> AT_SYMLINK_NOFOLLOW
>     If path names a symbolic link, then the mode of the symbolic link is
>     changed.
> 
> and the following error may be returned:
> 
> [EOPNOTSUPP]
>     The AT_SYMLINK_NOFOLLOW bit is set in the flag argument, path names a
>     symbolic link, and the system does not support changing the mode of a
>     symbolic link.
> 
> The linux kernel doesn't support changing the mode of a symbolic link, but
> the current implementation doesn't even have a flag argument. It is then
> up to userspace to deal with that. Unfortunately, it is impossible to
> implement the POSIX behavior in a race-free manner.
> 
> This patch introduces a new fchmodat2() syscall with a flag argument to
> address the issue.
> 
> [1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
> 
> Signed-off-by: Greg Kurz <groug@kaod.org>
> ---

Might also be worth mentioning that this patch is required in order to
solve CVE-2016-9602, per discussion at
https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg06089.html

> +++ b/include/linux/syscalls.h
> @@ -775,6 +775,8 @@ asmlinkage long sys_futimesat(int dfd, const char __user *filename,
>  asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode);
>  asmlinkage long sys_fchmodat(int dfd, const char __user * filename,
>  			     umode_t mode);
> +asmlinkage long sys_fchmodat2(int dfd, const char __user *filename,
> +			      umode_t mode, int flag);
>  asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
>  			     gid_t group, int flag);

Is the indentation off here?

Reviewed-by: Eric Blake <eblake@redhat.com>


-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

[toc] | [prev] | [next] | [standalone]


#1590243

FromEric Blake <eblake@redhat.com>
Date2017-03-01 12:00 +0100
Message-ID<tga9d-1UD-27@gated-at.bofh.it>
In reply to#1589680

[Multipart message — attachments visible in raw view] — view raw

On 02/28/2017 12:41 PM, Greg Kurz wrote:

>>> +++ b/include/linux/syscalls.h
>>> @@ -775,6 +775,8 @@ asmlinkage long sys_futimesat(int dfd, const char __user *filename,
>>>  asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode);
>>>  asmlinkage long sys_fchmodat(int dfd, const char __user * filename,
>>>  			     umode_t mode);
>>> +asmlinkage long sys_fchmodat2(int dfd, const char __user *filename,
>>> +			      umode_t mode, int flag);
>>>  asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
>>>  			     gid_t group, int flag);  
>>
>> Is the indentation off here?
>>
> 
> This is linux style indent with tabs+spaces. FWIW it is displayed correctly
> in vi and emacs (I've simply copied the sys_fchmodat() declaration).

Sorry for the noise; I see that it is correct now, since fchmodat2 is
one bye longer than fchmodat or fchownat.  Sometimes, I wish I could
convince my mailer to display leading tabs as exactly 8 spaces rather
than to the next 8-space tab stop, so that prefixed characters that
aren't tab-stop aligned don't mess with the actual visual output.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

[toc] | [prev] | [next] | [standalone]


#1590271

FromGreg Kurz <groug@kaod.org>
Date2017-03-01 12:50 +0100
Message-ID<tga9d-1UD-29@gated-at.bofh.it>
In reply to#1589680

[Multipart message — attachments visible in raw view] — view raw

On Tue, 28 Feb 2017 12:23:01 -0600
Eric Blake <eblake@redhat.com> wrote:

> On 02/28/2017 11:03 AM, Greg Kurz wrote:
> > According to the POSIX.1-2008 manual page [1], the fchmodat() function has
> > a flag argument which may be passed the following value:
> > 
> > AT_SYMLINK_NOFOLLOW
> >     If path names a symbolic link, then the mode of the symbolic link is
> >     changed.
> > 
> > and the following error may be returned:
> > 
> > [EOPNOTSUPP]
> >     The AT_SYMLINK_NOFOLLOW bit is set in the flag argument, path names a
> >     symbolic link, and the system does not support changing the mode of a
> >     symbolic link.
> > 
> > The linux kernel doesn't support changing the mode of a symbolic link, but
> > the current implementation doesn't even have a flag argument. It is then
> > up to userspace to deal with that. Unfortunately, it is impossible to
> > implement the POSIX behavior in a race-free manner.
> > 
> > This patch introduces a new fchmodat2() syscall with a flag argument to
> > address the issue.
> > 
> > [1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
> > 
> > Signed-off-by: Greg Kurz <groug@kaod.org>
> > ---  
> 
> Might also be worth mentioning that this patch is required in order to
> solve CVE-2016-9602, per discussion at
> https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg06089.html
> 

True. I'll add a reference to it if I have to send a v2.

> > +++ b/include/linux/syscalls.h
> > @@ -775,6 +775,8 @@ asmlinkage long sys_futimesat(int dfd, const char __user *filename,
> >  asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode);
> >  asmlinkage long sys_fchmodat(int dfd, const char __user * filename,
> >  			     umode_t mode);
> > +asmlinkage long sys_fchmodat2(int dfd, const char __user *filename,
> > +			      umode_t mode, int flag);
> >  asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
> >  			     gid_t group, int flag);  
> 
> Is the indentation off here?
> 

This is linux style indent with tabs+spaces. FWIW it is displayed correctly
in vi and emacs (I've simply copied the sys_fchmodat() declaration).

> Reviewed-by: Eric Blake <eblake@redhat.com>
> 
> 

[toc] | [prev] | [next] | [standalone]


#1590115

FromMichael Kerrisk <mtk.manpages@gmail.com>
Date2017-03-01 10:10 +0100
Message-ID<tg8qK-Tq-31@gated-at.bofh.it>
In reply to#1589629
[CC += linux-api@vger.kernel.org]

Hello Greg,

Since this is a kernel-user-space API change, please CC linux-api@.
The kernel source file Documentation/SubmitChecklist notes that all
Linux kernel patches that change userspace interfaces should be CCed
to linux-api@vger.kernel.org, so that the various parties who are
interested in API changes are informed. For further information, see
https://www.kernel.org/doc/man-pages/linux-api-ml.html

Thanks,

Michael


On Tue, Feb 28, 2017 at 6:03 PM, Greg Kurz <groug@kaod.org> wrote:
> According to the POSIX.1-2008 manual page [1], the fchmodat() function has
> a flag argument which may be passed the following value:
>
> AT_SYMLINK_NOFOLLOW
>     If path names a symbolic link, then the mode of the symbolic link is
>     changed.
>
> and the following error may be returned:
>
> [EOPNOTSUPP]
>     The AT_SYMLINK_NOFOLLOW bit is set in the flag argument, path names a
>     symbolic link, and the system does not support changing the mode of a
>     symbolic link.
>
> The linux kernel doesn't support changing the mode of a symbolic link, but
> the current implementation doesn't even have a flag argument. It is then
> up to userspace to deal with that. Unfortunately, it is impossible to
> implement the POSIX behavior in a race-free manner.
>
> This patch introduces a new fchmodat2() syscall with a flag argument to
> address the issue.
>
> [1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
>
> Signed-off-by: Greg Kurz <groug@kaod.org>
> ---
>  fs/open.c                         |   23 +++++++++++++++++++----
>  include/linux/syscalls.h          |    2 ++
>  include/uapi/asm-generic/unistd.h |    4 +++-
>  scripts/checksyscalls.sh          |    3 ++-
>  4 files changed, 26 insertions(+), 6 deletions(-)
>
> diff --git a/fs/open.c b/fs/open.c
> index 9921f70bc5ca..66a8c19f72ca 100644
> --- a/fs/open.c
> +++ b/fs/open.c
> @@ -558,24 +558,39 @@ SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
>         return err;
>  }
>
> -SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
> +SYSCALL_DEFINE4(fchmodat2, int, dfd, const char __user *, filename, umode_t,
> +               mode, int, flag)
>  {
>         struct path path;
> -       int error;
> -       unsigned int lookup_flags = LOOKUP_FOLLOW;
> +       int error = -EINVAL;
> +       unsigned int lookup_flags;
> +
> +       if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
> +               goto out;
> +
> +       lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
>  retry:
>         error = user_path_at(dfd, filename, lookup_flags, &path);
>         if (!error) {
> -               error = chmod_common(&path, mode);
> +               error = -EOPNOTSUPP;
> +               if (!d_is_symlink(path.dentry))
> +                       error = chmod_common(&path, mode);
>                 path_put(&path);
>                 if (retry_estale(error, lookup_flags)) {
>                         lookup_flags |= LOOKUP_REVAL;
>                         goto retry;
>                 }
>         }
> +out:
>         return error;
>  }
>
> +SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t,
> +               mode)
> +{
> +       return sys_fchmodat2(dfd, filename, mode, 0);
> +}
> +
>  SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
>  {
>         return sys_fchmodat(AT_FDCWD, filename, mode);
> diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
> index 91a740f6b884..982089d55b31 100644
> --- a/include/linux/syscalls.h
> +++ b/include/linux/syscalls.h
> @@ -775,6 +775,8 @@ asmlinkage long sys_futimesat(int dfd, const char __user *filename,
>  asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode);
>  asmlinkage long sys_fchmodat(int dfd, const char __user * filename,
>                              umode_t mode);
> +asmlinkage long sys_fchmodat2(int dfd, const char __user *filename,
> +                             umode_t mode, int flag);
>  asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
>                              gid_t group, int flag);
>  asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
> diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h
> index 9b1462e38b82..e8b0a00908b1 100644
> --- a/include/uapi/asm-generic/unistd.h
> +++ b/include/uapi/asm-generic/unistd.h
> @@ -730,9 +730,11 @@ __SYSCALL(__NR_pkey_mprotect, sys_pkey_mprotect)
>  __SYSCALL(__NR_pkey_alloc,    sys_pkey_alloc)
>  #define __NR_pkey_free 290
>  __SYSCALL(__NR_pkey_free,     sys_pkey_free)
> +#define __NR_fchmodat2 291
> +__SYSCALL(__NR_fchmodat2,     sys_fchmodat2)
>
>  #undef __NR_syscalls
> -#define __NR_syscalls 291
> +#define __NR_syscalls 292
>
>  /*
>   * All syscalls below here should go away really,
> diff --git a/scripts/checksyscalls.sh b/scripts/checksyscalls.sh
> index 2c9082ba6137..2e7471a1d308 100755
> --- a/scripts/checksyscalls.sh
> +++ b/scripts/checksyscalls.sh
> @@ -19,7 +19,7 @@ cat << EOF
>  #define __IGNORE_link          /* linkat */
>  #define __IGNORE_unlink                /* unlinkat */
>  #define __IGNORE_mknod         /* mknodat */
> -#define __IGNORE_chmod         /* fchmodat */
> +#define __IGNORE_chmod         /* fchmodat2 */
>  #define __IGNORE_chown         /* fchownat */
>  #define __IGNORE_mkdir         /* mkdirat */
>  #define __IGNORE_rmdir         /* unlinkat */
> @@ -39,6 +39,7 @@ cat << EOF
>
>  /* Missing flags argument */
>  #define __IGNORE_renameat      /* renameat2 */
> +#define __IGNORE_fchmodat      /* fchmodat2 */
>
>  /* CLOEXEC flag */
>  #define __IGNORE_pipe          /* pipe2 */
>



-- 
Michael Kerrisk Linux man-pages maintainer;
http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface", http://blog.man7.org/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web