1/* Check whether caller comes from the right place.
2 Copyright (C) 2004-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <assert.h>
20#include <ldsodefs.h>
21#include <stddef.h>
22#include <caller.h>
23#include <gnu/lib-names.h>
24
25
26int
27attribute_hidden
28_dl_check_caller (const void *caller, enum allowmask mask)
29{
30 static const char expected1[] = LIBC_SO;
31 static const char expected2[] = LIBDL_SO;
32#ifdef LIBPTHREAD_SO
33 static const char expected3[] = LIBPTHREAD_SO;
34#endif
35 static const char expected4[] = LD_SO;
36
37 for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
38 for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l != NULL;
39 l = l->l_next)
40 if (caller >= (const void *) l->l_map_start
41 && caller < (const void *) l->l_text_end)
42 {
43 /* The address falls into this DSO's address range. Check the
44 name. */
45 if ((mask & allow_libc) && strcmp (expected1, l->l_name) == 0)
46 return 0;
47 if ((mask & allow_libdl) && strcmp (expected2, l->l_name) == 0)
48 return 0;
49#ifdef LIBPTHREAD_SO
50 if ((mask & allow_libpthread) && strcmp (expected3, l->l_name) == 0)
51 return 0;
52#endif
53 if ((mask & allow_ldso) && strcmp (expected4, l->l_name) == 0)
54 return 0;
55
56 struct libname_list *runp = l->l_libname;
57
58 while (runp != NULL)
59 {
60 if ((mask & allow_libc) && strcmp (expected1, runp->name) == 0)
61 return 0;
62 if ((mask & allow_libdl) && strcmp (expected2, runp->name) == 0)
63 return 0;
64#ifdef LIBPTHREAD_SO
65 if ((mask & allow_libpthread)
66 && strcmp (expected3, runp->name) == 0)
67 return 0;
68#endif
69 if ((mask & allow_ldso) && strcmp (expected4, runp->name) == 0)
70 return 0;
71
72 runp = runp->next;
73 }
74
75 break;
76 }
77
78 /* Maybe the dynamic linker is not yet on the list. */
79 if ((mask & allow_ldso) != 0
80 && caller >= (const void *) GL(dl_rtld_map).l_map_start
81 && caller < (const void *) GL(dl_rtld_map).l_text_end)
82 return 0;
83
84 /* No valid caller. */
85 return 1;
86}
87