fa3bfd
commit 983fd5c41ab7e5a5c33922259ca1ac99b3b413f8
fa3bfd
Author: Florian Weimer <fweimer@redhat.com>
fa3bfd
Date:   Sat Jun 11 12:07:14 2016 +0200
fa3bfd
fa3bfd
    fopencookie: Mangle function pointers stored on the heap [BZ #20222]
fa3bfd
fa3bfd
diff --git a/libio/iofopncook.c b/libio/iofopncook.c
fa3bfd
index 5dcbe51f11182b68..b1c0d7f6ccc4db15 100644
fa3bfd
--- a/libio/iofopncook.c
fa3bfd
+++ b/libio/iofopncook.c
fa3bfd
@@ -46,11 +46,13 @@ _IO_cookie_read (fp, buf, size)
fa3bfd
      _IO_ssize_t size;
fa3bfd
 {
fa3bfd
   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
fa3bfd
+  cookie_read_function_t *read_cb = cfile->__io_functions.read;
fa3bfd
+  PTR_DEMANGLE (read_cb);
fa3bfd
 
fa3bfd
-  if (cfile->__io_functions.read == NULL)
fa3bfd
+  if (read_cb == NULL)
fa3bfd
     return -1;
fa3bfd
 
fa3bfd
-  return cfile->__io_functions.read (cfile->__cookie, buf, size);
fa3bfd
+  return read_cb (cfile->__cookie, buf, size);
fa3bfd
 }
fa3bfd
 
fa3bfd
 static _IO_ssize_t
fa3bfd
@@ -60,14 +62,16 @@ _IO_cookie_write (fp, buf, size)
fa3bfd
      _IO_ssize_t size;
fa3bfd
 {
fa3bfd
   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
fa3bfd
+  cookie_write_function_t *write_cb = cfile->__io_functions.write;
fa3bfd
+  PTR_DEMANGLE (write_cb);
fa3bfd
 
fa3bfd
-  if (cfile->__io_functions.write == NULL)
fa3bfd
+  if (write_cb == NULL)
fa3bfd
     {
fa3bfd
       fp->_flags |= _IO_ERR_SEEN;
fa3bfd
       return 0;
fa3bfd
     }
fa3bfd
 
fa3bfd
-  _IO_ssize_t n = cfile->__io_functions.write (cfile->__cookie, buf, size);
fa3bfd
+  _IO_ssize_t n = write_cb (cfile->__cookie, buf, size);
fa3bfd
   if (n < size)
fa3bfd
     fp->_flags |= _IO_ERR_SEEN;
fa3bfd
 
fa3bfd
@@ -81,9 +85,11 @@ _IO_cookie_seek (fp, offset, dir)
fa3bfd
      int dir;
fa3bfd
 {
fa3bfd
   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
fa3bfd
+  cookie_seek_function_t *seek_cb = cfile->__io_functions.seek;
fa3bfd
+  PTR_DEMANGLE (seek_cb);
fa3bfd
 
fa3bfd
-  return ((cfile->__io_functions.seek == NULL
fa3bfd
-	   || (cfile->__io_functions.seek (cfile->__cookie, &offset, dir)
fa3bfd
+  return ((seek_cb == NULL
fa3bfd
+	   || (seek_cb (cfile->__cookie, &offset, dir)
fa3bfd
 	       == -1)
fa3bfd
 	   || offset == (_IO_off64_t) -1)
fa3bfd
 	  ? _IO_pos_BAD : offset);
fa3bfd
@@ -94,11 +100,13 @@ _IO_cookie_close (fp)
fa3bfd
      _IO_FILE *fp;
fa3bfd
 {
fa3bfd
   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
fa3bfd
+  cookie_close_function_t *close_cb = cfile->__io_functions.close;
fa3bfd
+  PTR_DEMANGLE (close_cb);
fa3bfd
 
fa3bfd
-  if (cfile->__io_functions.close == NULL)
fa3bfd
+  if (close_cb == NULL)
fa3bfd
     return 0;
fa3bfd
 
fa3bfd
-  return cfile->__io_functions.close (cfile->__cookie);
fa3bfd
+  return close_cb (cfile->__cookie);
fa3bfd
 }
fa3bfd
 
fa3bfd
 
fa3bfd
@@ -140,6 +148,19 @@ static const struct _IO_jump_t _IO_cookie_jumps libio_vtable = {
fa3bfd
 };
fa3bfd
 
fa3bfd
 
fa3bfd
+/* Copy the callbacks from SOURCE to *TARGET, with pointer
fa3bfd
+   mangling.  */
fa3bfd
+static void
fa3bfd
+set_callbacks (_IO_cookie_io_functions_t *target,
fa3bfd
+	       _IO_cookie_io_functions_t source)
fa3bfd
+{
fa3bfd
+  PTR_MANGLE (source.read);
fa3bfd
+  PTR_MANGLE (source.write);
fa3bfd
+  PTR_MANGLE (source.seek);
fa3bfd
+  PTR_MANGLE (source.close);
fa3bfd
+  *target = source;
fa3bfd
+}
fa3bfd
+
fa3bfd
 void
fa3bfd
 _IO_cookie_init (struct _IO_cookie_file *cfile, int read_write,
fa3bfd
 		 void *cookie, _IO_cookie_io_functions_t io_functions)
fa3bfd
@@ -148,7 +169,7 @@ _IO_cookie_init (struct _IO_cookie_file *cfile, int read_write,
fa3bfd
   _IO_JUMPS (&cfile->__fp) = &_IO_cookie_jumps;
fa3bfd
 
fa3bfd
   cfile->__cookie = cookie;
fa3bfd
-  cfile->__io_functions = io_functions;
fa3bfd
+  set_callbacks (&cfile->__io_functions, io_functions);
fa3bfd
 
fa3bfd
   _IO_new_file_init_internal (&cfile->__fp);
fa3bfd
 
fa3bfd
@@ -223,14 +244,14 @@ _IO_old_cookie_seek (fp, offset, dir)
fa3bfd
      int dir;
fa3bfd
 {
fa3bfd
   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
fa3bfd
-  int (*seek) (_IO_FILE *, _IO_off_t, int);
fa3bfd
-  int ret;
fa3bfd
+  int (*seek_cb) (_IO_FILE *, _IO_off_t, int)
fa3bfd
+    = (int (*) (_IO_FILE *, _IO_off_t, int)) cfile->__io_functions.seek;;
fa3bfd
+  PTR_DEMANGLE (seek_cb);
fa3bfd
 
fa3bfd
-  seek = (int (*)(_IO_FILE *, _IO_off_t, int)) cfile->__io_functions.seek;
fa3bfd
-  if (seek == NULL)
fa3bfd
+  if (seek_cb == NULL)
fa3bfd
     return _IO_pos_BAD;
fa3bfd
 
fa3bfd
-  ret = seek (cfile->__cookie, offset, dir);
fa3bfd
+  int ret = seek_cb (cfile->__cookie, offset, dir);
fa3bfd
 
fa3bfd
   return (ret == -1) ? _IO_pos_BAD : ret;
fa3bfd
 }