8ae002
Adjusted for the lack of the ports move.
8ae002
8ae002
commit 65f6f938cd562a614a68e15d0581a34b177ec29d
8ae002
Author: Eric Rannaud <e@nanocritical.com>
8ae002
Date:   Tue Feb 24 13:12:26 2015 +0530
8ae002
8ae002
    linux: open and openat ignore 'mode' with O_TMPFILE in flags
8ae002
    
8ae002
    Both open and openat load their last argument 'mode' lazily, using
8ae002
    va_arg() only if O_CREAT is found in oflag. This is wrong, mode is also
8ae002
    necessary if O_TMPFILE is in oflag.
8ae002
    
8ae002
    By chance on x86_64, the problem wasn't evident when using O_TMPFILE
8ae002
    with open, as the 3rd argument of open, even when not loaded with
8ae002
    va_arg, is left untouched in RDX, where the syscall expects it.
8ae002
    
8ae002
    However, openat was not so lucky, and O_TMPFILE couldn't be used: mode
8ae002
    is the 4th argument, in RCX, but the syscall expects its 4th argument in
8ae002
    a different register than the glibc wrapper, in R10.
8ae002
    
8ae002
    Introduce a macro __OPEN_NEEDS_MODE (oflag) to test if either O_CREAT or
8ae002
    O_TMPFILE is set in oflag.
8ae002
    
8ae002
Index: b/io/bits/fcntl2.h
8ae002
===================================================================
8ae002
--- a/io/bits/fcntl2.h
8ae002
+++ b/io/bits/fcntl2.h
8ae002
@@ -20,7 +20,7 @@
8ae002
 # error "Never include <bits/fcntl2.h> directly; use <fcntl.h> instead."
8ae002
 #endif
8ae002
 
8ae002
-/* Check that calls to open and openat with O_CREAT set have an
8ae002
+/* Check that calls to open and openat with O_CREAT or O_TMPFILE set have an
8ae002
    appropriate third/fourth parameter.  */
8ae002
 #ifndef __USE_FILE_OFFSET64
8ae002
 extern int __open_2 (const char *__path, int __oflag) __nonnull ((1));
8ae002
@@ -35,7 +35,7 @@ extern int __REDIRECT (__open_alias, (co
8ae002
 __errordecl (__open_too_many_args,
8ae002
 	     "open can be called either with 2 or 3 arguments, not more");
8ae002
 __errordecl (__open_missing_mode,
8ae002
-	     "open with O_CREAT in second argument needs 3 arguments");
8ae002
+	     "open with O_CREAT or O_TMPFILE in second argument needs 3 arguments");
8ae002
 
8ae002
 __fortify_function int
8ae002
 open (const char *__path, int __oflag, ...)
8ae002
@@ -45,7 +45,7 @@ open (const char *__path, int __oflag, .
8ae002
 
8ae002
   if (__builtin_constant_p (__oflag))
8ae002
     {
8ae002
-      if ((__oflag & O_CREAT) != 0 && __va_arg_pack_len () < 1)
8ae002
+      if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
8ae002
 	{
8ae002
 	  __open_missing_mode ();
8ae002
 	  return __open_2 (__path, __oflag);
8ae002
@@ -67,7 +67,7 @@ extern int __REDIRECT (__open64_alias, (
8ae002
 __errordecl (__open64_too_many_args,
8ae002
 	     "open64 can be called either with 2 or 3 arguments, not more");
8ae002
 __errordecl (__open64_missing_mode,
8ae002
-	     "open64 with O_CREAT in second argument needs 3 arguments");
8ae002
+	     "open64 with O_CREAT or O_TMPFILE in second argument needs 3 arguments");
8ae002
 
8ae002
 __fortify_function int
8ae002
 open64 (const char *__path, int __oflag, ...)
8ae002
@@ -77,7 +77,7 @@ open64 (const char *__path, int __oflag,
8ae002
 
8ae002
   if (__builtin_constant_p (__oflag))
8ae002
     {
8ae002
-      if ((__oflag & O_CREAT) != 0 && __va_arg_pack_len () < 1)
8ae002
+      if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
8ae002
 	{
8ae002
 	  __open64_missing_mode ();
8ae002
 	  return __open64_2 (__path, __oflag);
8ae002
@@ -111,7 +111,7 @@ extern int __REDIRECT (__openat_alias, (
8ae002
 __errordecl (__openat_too_many_args,
8ae002
 	     "openat can be called either with 3 or 4 arguments, not more");
8ae002
 __errordecl (__openat_missing_mode,
8ae002
-	     "openat with O_CREAT in third argument needs 4 arguments");
8ae002
+	     "openat with O_CREAT or O_TMPFILE in third argument needs 4 arguments");
8ae002
 
8ae002
 __fortify_function int
8ae002
 openat (int __fd, const char *__path, int __oflag, ...)
8ae002
@@ -121,7 +121,7 @@ openat (int __fd, const char *__path, in
8ae002
 
8ae002
   if (__builtin_constant_p (__oflag))
8ae002
     {
8ae002
-      if ((__oflag & O_CREAT) != 0 && __va_arg_pack_len () < 1)
8ae002
+      if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
8ae002
 	{
8ae002
 	  __openat_missing_mode ();
8ae002
 	  return __openat_2 (__fd, __path, __oflag);
8ae002
@@ -145,7 +145,7 @@ extern int __REDIRECT (__openat64_alias,
8ae002
 __errordecl (__openat64_too_many_args,
8ae002
 	     "openat64 can be called either with 3 or 4 arguments, not more");
8ae002
 __errordecl (__openat64_missing_mode,
8ae002
-	     "openat64 with O_CREAT in third argument needs 4 arguments");
8ae002
+	     "openat64 with O_CREAT or O_TMPFILE in third argument needs 4 arguments");
8ae002
 
8ae002
 __fortify_function int
8ae002
 openat64 (int __fd, const char *__path, int __oflag, ...)
8ae002
@@ -155,7 +155,7 @@ openat64 (int __fd, const char *__path,
8ae002
 
8ae002
   if (__builtin_constant_p (__oflag))
8ae002
     {
8ae002
-      if ((__oflag & O_CREAT) != 0 && __va_arg_pack_len () < 1)
8ae002
+      if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
8ae002
 	{
8ae002
 	  __openat64_missing_mode ();
8ae002
 	  return __openat64_2 (__fd, __path, __oflag);
8ae002
Index: b/io/fcntl.h
8ae002
===================================================================
8ae002
--- a/io/fcntl.h
8ae002
+++ b/io/fcntl.h
8ae002
@@ -34,6 +34,15 @@ __BEGIN_DECLS
8ae002
    numbers and flag bits for `open', `fcntl', et al.  */
8ae002
 #include <bits/fcntl.h>
8ae002
 
8ae002
+/* Detect if open needs mode as a third argument (or for openat as a fourth
8ae002
+   argument).  */
8ae002
+#ifdef __O_TMPFILE
8ae002
+# define __OPEN_NEEDS_MODE(oflag) \
8ae002
+  (((oflag) & O_CREAT) != 0 || ((oflag) & __O_TMPFILE) == __O_TMPFILE)
8ae002
+#else
8ae002
+# define __OPEN_NEEDS_MODE(oflag) (((oflag) & O_CREAT) != 0)
8ae002
+#endif
8ae002
+
8ae002
 /* POSIX.1-2001 specifies that these types are defined by <fcntl.h>.
8ae002
    Earlier POSIX standards permitted any type ending in `_t' to be defined
8ae002
    by any POSIX header, so we don't conditionalize the definitions here.  */
8ae002
@@ -154,8 +163,9 @@ typedef __pid_t pid_t;
8ae002
 extern int fcntl (int __fd, int __cmd, ...);
8ae002
 
8ae002
 /* Open FILE and return a new file descriptor for it, or -1 on error.
8ae002
-   OFLAG determines the type of access used.  If O_CREAT is on OFLAG,
8ae002
-   the third argument is taken as a `mode_t', the mode of the created file.
8ae002
+   OFLAG determines the type of access used.  If O_CREAT or O_TMPFILE is set
8ae002
+   in OFLAG, the third argument is taken as a `mode_t', the mode of the
8ae002
+   created file.
8ae002
 
8ae002
    This function is a cancellation point and therefore not marked with
8ae002
    __THROW.  */
8ae002
Index: b/io/open.c
8ae002
===================================================================
8ae002
--- a/io/open.c
8ae002
+++ b/io/open.c
8ae002
@@ -23,7 +23,7 @@
8ae002
 #include <stdio.h>
8ae002
 
8ae002
 
8ae002
-/* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
8ae002
+/* Open FILE with access OFLAG.  If O_CREAT or O_TMPFILE is in OFLAG,
8ae002
    a third argument is the file protection.  */
8ae002
 int
8ae002
 __libc_open (file, oflag)
8ae002
@@ -38,7 +38,7 @@ __libc_open (file, oflag)
8ae002
       return -1;
8ae002
     }
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start(arg, oflag);
8ae002
Index: b/io/open64.c
8ae002
===================================================================
8ae002
--- a/io/open64.c
8ae002
+++ b/io/open64.c
8ae002
@@ -22,7 +22,7 @@
8ae002
 #include <stddef.h>
8ae002
 #include <stdio.h>
8ae002
 
8ae002
-/* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
8ae002
+/* Open FILE with access OFLAG.  If O_CREAT or O_TMPFILE is in OFLAG,
8ae002
    a third argument is the file protection.  */
8ae002
 int
8ae002
 __libc_open64 (file, oflag)
8ae002
@@ -37,7 +37,7 @@ __libc_open64 (file, oflag)
8ae002
       return -1;
8ae002
     }
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/io/open64_2.c
8ae002
===================================================================
8ae002
--- a/io/open64_2.c
8ae002
+++ b/io/open64_2.c
8ae002
@@ -22,8 +22,8 @@
8ae002
 int
8ae002
 __open64_2 (const char *file, int oflag)
8ae002
 {
8ae002
-  if (oflag & O_CREAT)
8ae002
-    __fortify_fail ("invalid open64 call: O_CREAT without mode");
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
+    __fortify_fail ("invalid open64 call: O_CREAT or O_TMPFILE without mode");
8ae002
 
8ae002
   return __open64 (file, oflag);
8ae002
 }
8ae002
Index: b/io/open_2.c
8ae002
===================================================================
8ae002
--- a/io/open_2.c
8ae002
+++ b/io/open_2.c
8ae002
@@ -22,8 +22,8 @@
8ae002
 int
8ae002
 __open_2 (const char *file, int oflag)
8ae002
 {
8ae002
-  if (oflag & O_CREAT)
8ae002
-    __fortify_fail ("invalid open call: O_CREAT without mode");
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
+    __fortify_fail ("invalid open call: O_CREAT or O_TMPFILE without mode");
8ae002
 
8ae002
   return __open (file, oflag);
8ae002
 }
8ae002
Index: b/io/openat.c
8ae002
===================================================================
8ae002
--- a/io/openat.c
8ae002
+++ b/io/openat.c
8ae002
@@ -30,7 +30,7 @@ int __have_atfcts;
8ae002
 #endif
8ae002
 
8ae002
 /* Open FILE with access OFLAG.  Interpret relative paths relative to
8ae002
-   the directory associated with FD.  If OFLAG includes O_CREAT, a
8ae002
+   the directory associated with FD.  If O_CREAT or O_TMPFILE is in OFLAG, a
8ae002
    third argument is the file protection.  */
8ae002
 int
8ae002
 __openat (fd, file, oflag)
8ae002
@@ -60,7 +60,7 @@ __openat (fd, file, oflag)
8ae002
 	}
8ae002
     }
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/io/openat64.c
8ae002
===================================================================
8ae002
--- a/io/openat64.c
8ae002
+++ b/io/openat64.c
8ae002
@@ -23,7 +23,7 @@
8ae002
 #include <sys/stat.h>
8ae002
 
8ae002
 /* Open FILE with access OFLAG.  Interpret relative paths relative to
8ae002
-   the directory associated with FD.  If OFLAG includes O_CREAT, a
8ae002
+   the directory associated with FD.  If O_CREAT or O_TMPFILE is in OFLAG, a
8ae002
    third argument is the file protection.  */
8ae002
 int
8ae002
 __openat64 (fd, file, oflag)
8ae002
@@ -53,7 +53,7 @@ __openat64 (fd, file, oflag)
8ae002
 	}
8ae002
     }
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/io/openat64_2.c
8ae002
===================================================================
8ae002
--- a/io/openat64_2.c
8ae002
+++ b/io/openat64_2.c
8ae002
@@ -22,8 +22,8 @@
8ae002
 int
8ae002
 __openat64_2 (int fd, const char *file, int oflag)
8ae002
 {
8ae002
-  if (oflag & O_CREAT)
8ae002
-    __fortify_fail ("invalid openat64 call: O_CREAT without mode");
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
+    __fortify_fail ("invalid openat64 call: O_CREAT or O_TMPFILE without mode");
8ae002
 
8ae002
   return __openat64 (fd, file, oflag);
8ae002
 }
8ae002
Index: b/io/openat_2.c
8ae002
===================================================================
8ae002
--- a/io/openat_2.c
8ae002
+++ b/io/openat_2.c
8ae002
@@ -22,8 +22,8 @@
8ae002
 int
8ae002
 __openat_2 (int fd, const char *file, int oflag)
8ae002
 {
8ae002
-  if (oflag & O_CREAT)
8ae002
-    __fortify_fail ("invalid openat call: O_CREAT without mode");
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
+    __fortify_fail ("invalid openat call: O_CREAT or O_TMPFILE without mode");
8ae002
 
8ae002
   return __openat (fd, file, oflag);
8ae002
 }
8ae002
Index: b/sysdeps/mach/hurd/open.c
8ae002
===================================================================
8ae002
--- a/sysdeps/mach/hurd/open.c
8ae002
+++ b/sysdeps/mach/hurd/open.c
8ae002
@@ -22,7 +22,7 @@
8ae002
 #include <hurd.h>
8ae002
 #include <hurd/fd.h>
8ae002
 
8ae002
-/* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
8ae002
+/* Open FILE with access OFLAG.  If O_CREAT or O_TMPFILE is in OFLAG,
8ae002
    a third argument is the file protection.  */
8ae002
 int
8ae002
 __libc_open (const char *file, int oflag, ...)
8ae002
@@ -30,7 +30,7 @@ __libc_open (const char *file, int oflag
8ae002
   mode_t mode;
8ae002
   io_t port;
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/sysdeps/mach/hurd/openat.c
8ae002
===================================================================
8ae002
--- a/sysdeps/mach/hurd/openat.c
8ae002
+++ b/sysdeps/mach/hurd/openat.c
8ae002
@@ -26,7 +26,7 @@
8ae002
 #include <hurd/fd.h>
8ae002
 
8ae002
 /* Open FILE with access OFLAG.  Interpret relative paths relative to
8ae002
-   the directory associated with FD.  If OFLAG includes O_CREAT, a
8ae002
+   the directory associated with FD.  If O_CREAT or O_TMPFILE is in OFLAG, a
8ae002
    third argument is the file protection.  */
8ae002
 int
8ae002
 __openat (fd, file, oflag)
8ae002
@@ -37,7 +37,7 @@ __openat (fd, file, oflag)
8ae002
   mode_t mode;
8ae002
   io_t port;
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/sysdeps/posix/open64.c
8ae002
===================================================================
8ae002
--- a/sysdeps/posix/open64.c
8ae002
+++ b/sysdeps/posix/open64.c
8ae002
@@ -20,14 +20,14 @@
8ae002
 #include <bp-sym.h>
8ae002
 #include <sysdep-cancel.h>
8ae002
 
8ae002
-/* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
8ae002
+/* Open FILE with access OFLAG.  If O_CREAT or O_TMPFILE is in OFLAG,
8ae002
    a third argument is the file protection.  */
8ae002
 int
8ae002
 __libc_open64 (const char *file, int oflag, ...)
8ae002
 {
8ae002
   int mode = 0;
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/sysdeps/unix/sysv/linux/dl-openat64.c
8ae002
===================================================================
8ae002
--- a/sysdeps/unix/sysv/linux/dl-openat64.c
8ae002
+++ b/sysdeps/unix/sysv/linux/dl-openat64.c
8ae002
@@ -28,7 +28,7 @@ openat64 (dfd, file, oflag)
8ae002
      const char *file;
8ae002
      int oflag;
8ae002
 {
8ae002
-  assert ((oflag & O_CREAT) == 0);
8ae002
+  assert (!__OPEN_NEEDS_MODE (oflag));
8ae002
 
8ae002
 #ifdef __NR_openat
8ae002
   return INLINE_SYSCALL (openat, 3, dfd, file, oflag | O_LARGEFILE);
8ae002
Index: b/ports/sysdeps/unix/sysv/linux/generic/open.c
8ae002
===================================================================
8ae002
--- a/ports/sysdeps/unix/sysv/linux/generic/open.c
8ae002
+++ b/ports/sysdeps/unix/sysv/linux/generic/open.c
8ae002
@@ -22,14 +22,14 @@
8ae002
 #include <stdio.h>
8ae002
 #include <sysdep-cancel.h>
8ae002
 
8ae002
-/* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
8ae002
+/* Open FILE with access OFLAG.  If O_CREAT or O_TMPFILE is in OFLAG,
8ae002
    a third argument is the file protection.  */
8ae002
 int
8ae002
 __libc_open (const char *file, int oflag, ...)
8ae002
 {
8ae002
   int mode = 0;
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
@@ -59,7 +59,7 @@ __open_nocancel (const char *file, int o
8ae002
 {
8ae002
   int mode = 0;
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/ports/sysdeps/unix/sysv/linux/generic/open64.c
8ae002
===================================================================
8ae002
--- a/ports/sysdeps/unix/sysv/linux/generic/open64.c
8ae002
+++ b/ports/sysdeps/unix/sysv/linux/generic/open64.c
8ae002
@@ -22,14 +22,14 @@
8ae002
 #include <stdio.h>
8ae002
 #include <sysdep-cancel.h>
8ae002
 
8ae002
-/* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
8ae002
+/* Open FILE with access OFLAG.  If O_CREAT or O_TMPFILE is in OFLAG,
8ae002
    a third argument is the file protection.  */
8ae002
 int
8ae002
 __libc_open64 (const char *file, int oflag, ...)
8ae002
 {
8ae002
   int mode = 0;
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/sysdeps/unix/sysv/linux/open64.c
8ae002
===================================================================
8ae002
--- a/sysdeps/unix/sysv/linux/open64.c
8ae002
+++ b/sysdeps/unix/sysv/linux/open64.c
8ae002
@@ -22,14 +22,14 @@
8ae002
 #include <stdio.h>
8ae002
 #include <sysdep-cancel.h>
8ae002
 
8ae002
-/* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
8ae002
+/* Open FILE with access OFLAG.  If O_CREAT or O_TMPFILE is in OFLAG,
8ae002
    a third argument is the file protection.  */
8ae002
 int
8ae002
 __libc_open64 (const char *file, int oflag, ...)
8ae002
 {
8ae002
   int mode = 0;
8ae002
 
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);
8ae002
Index: b/sysdeps/unix/sysv/linux/openat.c
8ae002
===================================================================
8ae002
--- a/sysdeps/unix/sysv/linux/openat.c
8ae002
+++ b/sysdeps/unix/sysv/linux/openat.c
8ae002
@@ -148,8 +148,8 @@ OPENAT_NOT_CANCEL (fd, file, oflag, mode
8ae002
 
8ae002
 
8ae002
 /* Open FILE with access OFLAG.  Interpret relative paths relative to
8ae002
-   the directory associated with FD.  If OFLAG includes O_CREAT, a
8ae002
-   third argument is the file protection.  */
8ae002
+   the directory associated with FD.  If OFLAG includes O_CREAT or
8ae002
+   O_TMPFILE, a fourth argument is the file protection.  */
8ae002
 int
8ae002
 __OPENAT (fd, file, oflag)
8ae002
      int fd;
8ae002
@@ -157,7 +157,7 @@ __OPENAT (fd, file, oflag)
8ae002
      int oflag;
8ae002
 {
8ae002
   mode_t mode = 0;
8ae002
-  if (oflag & O_CREAT)
8ae002
+  if (__OPEN_NEEDS_MODE (oflag))
8ae002
     {
8ae002
       va_list arg;
8ae002
       va_start (arg, oflag);