[PATCHv2] fcntl: don't define OFD lock constants for 32-bit builds with small file offsets

Message ID 1471543396-23273-1-git-send-email-jlayton@redhat.com
State New, archived
Headers

Commit Message

Jeff Layton Aug. 18, 2016, 6:03 p.m. UTC
  The Linux kernel expects a flock64 structure whenever you use OFD locks
with fcntl64. Unfortunately, you can currently build a 32-bit program
that passes in a struct flock when it calls fcntl64, which will
typically make the program fail (or crash).

Only define the F_OFD_* command constants when large file offsets are
enabled, either by virtue of being on a 64-bit arch, or by defining
_FILE_OFFSET_BITS=64. By doing this, we ensure that such builds will
break instead of generating a binary that doesn't behave as expected.

I think this is the simplest option all around, and shouldn't cause any
builds to break whose applications are not already broken. Note too that
there is a corresponding manpage patch already proposed by Cyril.

Reported-by: Cyril Hrubis <chrubis@suse.cz>
Cc: Michael Kerrisk (man-pages) <mtk.manpages@gmail.com>
Cc: Yuriy Kolerov <Yuriy.Kolerov@synopsys.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Cc: Joseph Myers <joseph@codesourcery.com>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jeff Layton <jlayton@redhat.com>
---
 ChangeLog                                  |  6 ++++++
 manual/examples/ofdlocks.c                 |  4 ++++
 manual/llio.texi                           |  8 +++++---
 sysdeps/unix/sysv/linux/bits/fcntl-linux.h | 14 ++++++++++----
 4 files changed, 25 insertions(+), 7 deletions(-)
  

Patch

diff --git a/ChangeLog b/ChangeLog
index 59c68d8b9d0d..c2f8ef94dcb1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@ 
+2016-08-18  Jeff Layton <jlayton@redhat.com>
+
+	* sysdeps/unix/sysv/linux/bits/fcntl-linux.h: only define F_OFD_*
+	constants when off_t and off64_t are the same size, or when
+	_FILE_OFFSET_BITS=64.
+
 2016-08-18  Stefan Liebler  <stli@linux.vnet.ibm.com>
 
 	* sysdeps/ieee754/dbl-64/k_rem_pio2.c (__kernel_rem_pio2):
diff --git a/manual/examples/ofdlocks.c b/manual/examples/ofdlocks.c
index ba4f0ef4d237..a6971235841b 100644
--- a/manual/examples/ofdlocks.c
+++ b/manual/examples/ofdlocks.c
@@ -15,6 +15,10 @@ 
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifndef _FILE_OFFSET_BITS
+#define _FILE_OFFSET_BITS 64
+#endif
+
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <sys/types.h>
diff --git a/manual/llio.texi b/manual/llio.texi
index 019dea2c3189..5639a88e79a2 100644
--- a/manual/llio.texi
+++ b/manual/llio.texi
@@ -3907,9 +3907,11 @@  descriptor.
 
 Open file description locks use the same @code{struct flock} as
 process-associated locks as an argument (@pxref{File Locks}) and the
-macros for the @code{command} values are also declared in the header file
-@file{fcntl.h}. To use them, the macro @code{_GNU_SOURCE} must be
-defined prior to including any header file.
+macros for the @code{command} values are also declared in the header
+file @file{fcntl.h}.  To use them, the macro @code{_GNU_SOURCE} must be
+defined prior to including any header file.  Additionally, if building on
+a 32-bit architecture, then large file offsets must also be enabled
+by defining @code{_FILE_OFFSET_BITS == 64}.
 
 In contrast to process-associated locks, any @code{struct flock} used as
 an argument to open file description lock commands must have the @code{l_pid}
diff --git a/sysdeps/unix/sysv/linux/bits/fcntl-linux.h b/sysdeps/unix/sysv/linux/bits/fcntl-linux.h
index 7e5b0aecdcb4..5a4aad0698b8 100644
--- a/sysdeps/unix/sysv/linux/bits/fcntl-linux.h
+++ b/sysdeps/unix/sysv/linux/bits/fcntl-linux.h
@@ -127,11 +127,17 @@ 
    This means that they are inherited across fork or clone with CLONE_FILES
    like BSD (flock) locks, and they are only released automatically when the
    last reference to the the file description against which they were acquired
-   is put. */
+   is put.
+
+   Note that Linux does not support legacy (non-LFS) struct flock on 32-bit
+   arches with OFD locks.  On those arches you need define both _GNU_SOURCE
+   and _FILE_OFFSET_BITS=64.  */
 #ifdef __USE_GNU
-# define F_OFD_GETLK	36
-# define F_OFD_SETLK	37
-# define F_OFD_SETLKW	38
+# if __OFF_T_MATCHES_OFF64_T || defined __USE_FILE_OFFSET64
+#  define F_OFD_GETLK	36
+#  define F_OFD_SETLK	37
+#  define F_OFD_SETLKW	38
+# endif
 #endif
 
 #ifdef __USE_LARGEFILE64