glibc/elf/dl-caller.c
Ulrich Drepper eec8b6cae5 Update.
2004-03-26  Ulrich Drepper  <drepper@redhat.com>

	* elf/dl-caller.c: New file.
	* include/caller.h: New file.
	* Makefile (distribute): Add include/caller.h.
	* elf/Makefile (dl-routines): Add dl-caller.
	* elf/dl-load.c (_dl_map_object_from_fd): Record l_text_end.
	* elf/dl-open.c (check_libc_caller): Removed.
	(dl_open_worker): Use __check_caller instead.
	* elf/rtld.c (_rtld_global_ro): Initialize _dl_check_caller.
	(_dl_start_final): Record l_text_end for ld.so map.
	(dl_main): Record l_text_end for main object and vdso.
	* include/link.h (struct link_map): Add l_text_end field.
	* sysdeps/generic/ldsodefs.h (struct rtld_global_ro): Add
	_dl_check_caller field.
	Define enum allowmask.  Add declaration of _dl_check_caller.
	* sysdeps/unix/sysv/linux/dl-execstack.c: Also use __check_caller test.
2004-03-27 03:40:33 +00:00

86 lines
2.6 KiB
C

/* Check whether caller comes from the right place.
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <assert.h>
#include <ldsodefs.h>
#include <stddef.h>
#include <caller.h>
#include <gnu/lib-names.h>
int
attribute_hidden
_dl_check_caller (const void *caller, enum allowmask mask)
{
static const char expected1[] = LIBC_SO;
static const char expected2[] = LIBDL_SO;
#ifdef LIBPTHREAD_SO
static const char expected3[] = LIBPTHREAD_SO;
#endif
static const char expected4[] = LD_SO;
for (struct link_map *l = GL(dl_loaded); l != NULL; l = l->l_next)
if (caller >= (const void *) l->l_map_start
&& caller < (const void *) l->l_text_end)
{
/* The address falls into this DSO's address range. Check the
name. */
if ((mask & allow_libc) && strcmp (expected1, l->l_name) == 0)
return 0;
if ((mask & allow_libdl) && strcmp (expected2, l->l_name) == 0)
return 0;
#ifdef LIBPTHREAD_SO
if ((mask & allow_libpthread) && strcmp (expected3, l->l_name) == 0)
return 0;
#endif
if ((mask & allow_ldso) && strcmp (expected4, l->l_name) == 0)
return 0;
struct libname_list *runp = l->l_libname;
while (runp != NULL)
{
if ((mask & allow_libc) && strcmp (expected1, runp->name) == 0)
return 0;
if ((mask & allow_libdl) && strcmp (expected2, runp->name) == 0)
return 0;
#ifdef LIBPTHREAD_SO
if ((mask & allow_libpthread)
&& strcmp (expected3, runp->name) == 0)
return 0;
#endif
if ((mask & allow_ldso) && strcmp (expected4, runp->name) == 0)
return 0;
runp = runp->next;
}
break;
}
/* Maybe the dynamic linker is not yet on the list. */
if ((mask & allow_ldso) != 0
&& caller >= (const void *) GL(dl_rtld_map).l_map_start
&& caller < (const void *) GL(dl_rtld_map).l_text_end)
return 0;
/* No valid caller. */
return 1;
}