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