]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/vismain.c
elf: Refuse to dlopen PIE objects [BZ #24323]
[thirdparty/glibc.git] / elf / vismain.c
CommitLineData
04277e02 1/* Copyright (C) 2000-2019 Free Software Foundation, Inc.
eec8ef5a
UD
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
eec8ef5a
UD
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
eec8ef5a 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
eec8ef5a 17
ef4ad06f
L
18/* This file must be compiled as PIE to avoid copy relocation when
19 accessing protected symbols defined in shared libaries since copy
20 relocation doesn't work with protected symbols and linker in
21 binutils 2.26 enforces this rule. */
22
eec8ef5a
UD
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
715ed1e9
AJ
27#include "vismod.h"
28
741845cb
AJ
29/* Prototype for our test function. */
30extern int do_test (void);
31
741845cb
AJ
32
33/* This defines the `main' function and some more. */
36fe25fd 34#include <support/test-driver.c>
741845cb 35
eec8ef5a
UD
36
37/* Prototypes for local functions. */
38extern int protlocal (void);
39
40const char *protvarlocal = __FILE__;
41extern const char *protvarinmod;
42extern const char *protvaritcpt;
43
44int
741845cb 45do_test (void)
eec8ef5a
UD
46{
47 int res = 0;
48 int val;
49
50 /* First test: check whether .protected is handled correctly by the
51 assembler/linker. The uses of `protlocal' in the DSOs and in the
52 main program should all be resolved with the local definitions. */
53 val = protlocal () + calllocal1 () + calllocal2 ();
54 if (val != 0x155)
55 {
56 puts ("\
57The handling of `.protected' seems to be implemented incorrectly: giving up");
58 abort ();
59 }
60 puts ("`.protected' seems to be handled correctly, good!");
61
62 /* Function pointers: for functions which are marked local and for
63 which definitions are available all function pointers must be
64 distinct. */
65 if (protlocal == getlocal1 ())
66 {
67 puts ("`protlocal' in main and mod1 have same address");
68 res = 1;
69 }
70 if (protlocal == getlocal2 ())
71 {
72 puts ("`protlocal' in main and mod2 have same address");
73 res = 1;
74 }
75 if (getlocal1 () == getlocal2 ())
76 {
77 puts ("`protlocal' in mod1 and mod2 have same address");
78 res = 1;
79 }
80 if (getlocal1 () () + getlocal2 () () != 0x44)
81 {
82 puts ("pointers to `protlocal' in mod1 or mod2 incorrect");
83 res = 1;
84 }
85
86 /* Next test. This is similar to the last one but the function we
87 are calling is not defined in the main object. This means that
88 the invocation in the main object uses the definition in the
89 first DSO. */
90 if (protinmod != getinmod1 ())
91 {
5972c58b
RM
92 printf ("&protinmod in main (%p) != &protinmod in mod1 (%p)\n",
93 protinmod, getinmod1 ());
eec8ef5a
UD
94 res = 1;
95 }
96 if (protinmod == getinmod2 ())
97 {
98 puts ("`protinmod' in main and mod2 have same address");
99 res = 1;
100 }
101 if (getinmod1 () == getinmod2 ())
102 {
103 puts ("`protinmod' in mod1 and mod2 have same address");
104 res = 1;
105 }
106 if (protinmod () + getinmod1 () () + getinmod2 () () != 0x4800)
107 {
108 puts ("pointers to `protinmod' in mod1 or mod2 incorrect");
109 res = 1;
110 }
111 val = protinmod () + callinmod1 () + callinmod2 ();
112 if (val != 0x15800)
113 {
114 printf ("calling of `protinmod' leads to wrong result (%#x)\n", val);
115 res = 1;
116 }
117
118 /* A very similar text. Same setup for the main object and the modules
119 but this time we have another definition in a preloaded module. This
120 one intercepts the references from the main object. */
121 if (protitcpt != getitcpt3 ())
122 {
5972c58b
RM
123 printf ("&protitcpt in main (%p) != &protitcpt in mod3 (%p)\n",
124 &protitcpt, getitcpt3 ());
eec8ef5a
UD
125 res = 1;
126 }
127 if (protitcpt == getitcpt1 ())
128 {
129 puts ("`protitcpt' in main and mod1 have same address");
130 res = 1;
131 }
132 if (protitcpt == getitcpt2 ())
133 {
134 puts ("`protitcpt' in main and mod2 have same address");
135 res = 1;
136 }
137 if (getitcpt1 () == getitcpt2 ())
138 {
139 puts ("`protitcpt' in mod1 and mod2 have same address");
140 res = 1;
141 }
142 val = protitcpt () + getitcpt1 () () + getitcpt2 () () + getitcpt3 () ();
143 if (val != 0x8440000)
144 {
145 printf ("\
146pointers to `protitcpt' in mod1 or mod2 or mod3 incorrect (%#x)\n", val);
147 res = 1;
148 }
149 val = protitcpt () + callitcpt1 () + callitcpt2 () + callitcpt3 ();
150 if (val != 0x19540000)
151 {
152 printf ("calling of `protitcpt' leads to wrong result (%#x)\n", val);
153 res = 1;
154 }
155
156 /* Now look at variables. First a variable which is available
157 everywhere. We must have three different addresses. */
5972c58b 158 if (&protvarlocal == getvarlocal1 ())
eec8ef5a
UD
159 {
160 puts ("`protvarlocal' in main and mod1 have same address");
161 res = 1;
162 }
5972c58b 163 if (&protvarlocal == getvarlocal2 ())
eec8ef5a
UD
164 {
165 puts ("`protvarlocal' in main and mod2 have same address");
166 res = 1;
167 }
168 if (getvarlocal1 () == getvarlocal2 ())
169 {
170 puts ("`protvarlocal' in mod1 and mod2 have same address");
171 res = 1;
172 }
173 if (strcmp (protvarlocal, __FILE__) != 0)
174 {
175 puts ("`protvarlocal in main has wrong value");
176 res = 1;
177 }
5972c58b 178 if (strcmp (*getvarlocal1 (), "vismod1.c") != 0)
eec8ef5a
UD
179 {
180 puts ("`getvarlocal1' returns wrong value");
181 res = 1;
182 }
5972c58b 183 if (strcmp (*getvarlocal2 (), "vismod2.c") != 0)
eec8ef5a
UD
184 {
185 puts ("`getvarlocal2' returns wrong value");
186 res = 1;
187 }
188
189 /* Now the case where there is no local definition. */
5972c58b 190 if (&protvarinmod != getvarinmod1 ())
eec8ef5a 191 {
5972c58b
RM
192 printf ("&protvarinmod in main (%p) != &protitcpt in mod1 (%p)\n",
193 &protvarinmod, getvarinmod1 ());
d403820a
UD
194 // XXX Possibly enable once fixed.
195 // res = 1;
eec8ef5a 196 }
5972c58b 197 if (&protvarinmod == getvarinmod2 ())
eec8ef5a
UD
198 {
199 puts ("`protvarinmod' in main and mod2 have same address");
200 res = 1;
201 }
5972c58b 202 if (strcmp (*getvarinmod1 (), "vismod1.c") != 0)
eec8ef5a
UD
203 {
204 puts ("`getvarinmod1' returns wrong value");
205 res = 1;
206 }
5972c58b 207 if (strcmp (*getvarinmod2 (), "vismod2.c") != 0)
eec8ef5a
UD
208 {
209 puts ("`getvarinmod2' returns wrong value");
210 res = 1;
211 }
212
213 /* And a test where a variable definition is intercepted. */
5972c58b 214 if (&protvaritcpt == getvaritcpt1 ())
eec8ef5a
UD
215 {
216 puts ("`protvaritcpt' in main and mod1 have same address");
217 res = 1;
218 }
5972c58b 219 if (&protvaritcpt == getvaritcpt2 ())
eec8ef5a
UD
220 {
221 puts ("`protvaritcpt' in main and mod2 have same address");
222 res = 1;
223 }
5972c58b 224 if (&protvaritcpt != getvaritcpt3 ())
eec8ef5a 225 {
5972c58b
RM
226 printf ("&protvaritcpt in main (%p) != &protvaritcpt in mod3 (%p)\n",
227 &protvaritcpt, getvaritcpt3 ());
d403820a
UD
228 // XXX Possibly enable once fixed.
229 // res = 1;
eec8ef5a
UD
230 }
231 if (getvaritcpt1 () == getvaritcpt2 ())
232 {
233 puts ("`protvaritcpt' in mod1 and mod2 have same address");
234 res = 1;
235 }
236 if (strcmp (protvaritcpt, "vismod3.c") != 0)
237 {
238 puts ("`protvaritcpt in main has wrong value");
239 res = 1;
240 }
5972c58b 241 if (strcmp (*getvaritcpt1 (), "vismod1.c") != 0)
eec8ef5a
UD
242 {
243 puts ("`getvaritcpt1' returns wrong value");
244 res = 1;
245 }
5972c58b 246 if (strcmp (*getvaritcpt2 (), "vismod2.c") != 0)
eec8ef5a
UD
247 {
248 puts ("`getvaritcpt2' returns wrong value");
249 res = 1;
250 }
251
252 return res;
253}
254
255
256int
257protlocal (void)
258{
259 return 0x1;
260}