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