Blame sysdeps/unix/sysv/linux/readonly-area.c

Packit 6c4009
/* Copyright (C) 2004-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdio_ext.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include "libio/libioP.h"
Packit 6c4009
Packit 6c4009
/* Return 1 if the whole area PTR .. PTR+SIZE is not writable.
Packit 6c4009
   Return -1 if it is writable.  */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__readonly_area (const char *ptr, size_t size)
Packit 6c4009
{
Packit 6c4009
  const void *ptr_end = ptr + size;
Packit 6c4009
Packit 6c4009
  FILE *fp = fopen ("/proc/self/maps", "rce");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* It is the system administrator's choice to not have /proc
Packit 6c4009
	 available to this process (e.g., because it runs in a chroot
Packit 6c4009
	 environment.  Don't fail in this case.  */
Packit 6c4009
      if (errno == ENOENT
Packit 6c4009
	  /* The kernel has a bug in that a process is denied access
Packit 6c4009
	     to the /proc filesystem if it is set[ug]id.  There has
Packit 6c4009
	     been no willingness to change this in the kernel so
Packit 6c4009
	     far.  */
Packit 6c4009
	  || errno == EACCES)
Packit 6c4009
	return 1;
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We need no locking.  */
Packit 6c4009
  __fsetlocking (fp, FSETLOCKING_BYCALLER);
Packit 6c4009
Packit 6c4009
  char *line = NULL;
Packit 6c4009
  size_t linelen = 0;
Packit 6c4009
Packit 6c4009
  while (! __feof_unlocked (fp))
Packit 6c4009
    {
Packit 6c4009
      if (_IO_getdelim (&line, &linelen, '\n', fp) <= 0)
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      char *p;
Packit 6c4009
      uintptr_t from = strtoul (line, &p, 16);
Packit 6c4009
Packit 6c4009
      if (p == line || *p++ != '-')
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      char *q;
Packit 6c4009
      uintptr_t to = strtoul (p, &q, 16);
Packit 6c4009
Packit 6c4009
      if (q == p || *q++ != ' ')
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      if (from < (uintptr_t) ptr_end && to > (uintptr_t) ptr)
Packit 6c4009
	{
Packit 6c4009
	  /* Found an entry that at least partially covers the area.  */
Packit 6c4009
	  if (*q++ != 'r' || *q++ != '-')
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  if (from <= (uintptr_t) ptr && to >= (uintptr_t) ptr_end)
Packit 6c4009
	    {
Packit 6c4009
	      size = 0;
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
	  else if (from <= (uintptr_t) ptr)
Packit 6c4009
	    size -= to - (uintptr_t) ptr;
Packit 6c4009
	  else if (to >= (uintptr_t) ptr_end)
Packit 6c4009
	    size -= (uintptr_t) ptr_end - from;
Packit 6c4009
	  else
Packit 6c4009
	    size -= to - from;
Packit 6c4009
Packit 6c4009
	  if (!size)
Packit 6c4009
	    break;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fclose (fp);
Packit 6c4009
  free (line);
Packit 6c4009
Packit 6c4009
  /* If the whole area between ptr and ptr_end is covered by read-only
Packit 6c4009
     VMAs, return 1.  Otherwise return -1.  */
Packit 6c4009
  return size == 0 ? 1 : -1;
Packit 6c4009
}