Back to index

glibc  2.9
readonly-area.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stdint.h>
00021 #include <stdio.h>
00022 #include <stdio_ext.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include "libio/libioP.h"
00026 
00027 /* Return 1 if the whole area PTR .. PTR+SIZE is not writable.
00028    Return -1 if it is writable.  */
00029 
00030 int
00031 __readonly_area (const char *ptr, size_t size)
00032 {
00033   const void *ptr_end = ptr + size;
00034 
00035   FILE *fp = fopen ("/proc/self/maps", "rc");
00036   if (fp == NULL)
00037     {
00038       /* It is the system administrator's choice to not have /proc
00039         available to this process (e.g., because it runs in a chroot
00040         environment.  Don't fail in this case.  */
00041       if (errno == ENOENT
00042          /* The kernel has a bug in that a process is denied access
00043             to the /proc filesystem if it is set[ug]id.  There has
00044             been no willingness to change this in the kernel so
00045             far.  */
00046          || errno == EACCES)
00047        return 1;
00048       return -1;
00049     }
00050 
00051   /* We need no locking.  */
00052   __fsetlocking (fp, FSETLOCKING_BYCALLER);
00053 
00054   char *line = NULL;
00055   size_t linelen = 0;
00056 
00057   while (! feof_unlocked (fp))
00058     {
00059       if (_IO_getdelim (&line, &linelen, '\n', fp) <= 0)
00060        break;
00061 
00062       char *p;
00063       uintptr_t from = strtoul (line, &p, 16);
00064 
00065       if (p == line || *p++ != '-')
00066        break;
00067 
00068       char *q;
00069       uintptr_t to = strtoul (p, &q, 16);
00070 
00071       if (q == p || *q++ != ' ')
00072        break;
00073 
00074       if (from < (uintptr_t) ptr_end && to > (uintptr_t) ptr)
00075        {
00076          /* Found an entry that at least partially covers the area.  */
00077          if (*q++ != 'r' || *q++ != '-')
00078            break;
00079 
00080          if (from <= (uintptr_t) ptr && to >= (uintptr_t) ptr_end)
00081            {
00082              size = 0;
00083              break;
00084            }
00085          else if (from <= (uintptr_t) ptr)
00086            size -= to - (uintptr_t) ptr;
00087          else if (to >= (uintptr_t) ptr_end)
00088            size -= (uintptr_t) ptr_end - from;
00089          else
00090            size -= to - from;
00091 
00092          if (!size)
00093            break;
00094        }
00095     }
00096 
00097   fclose (fp);
00098   free (line);
00099 
00100   /* If the whole area between ptr and ptr_end is covered by read-only
00101      VMAs, return 1.  Otherwise return -1.  */
00102   return size == 0 ? 1 : -1;
00103 }