Skip to content

Commit 083e14c

Browse files
author
Martin Schwidefsky
committed
s390/modules: add relocation overflow checking
Given enough debug options some modules can grow large enough that the GOT table gets bigger than 4K. On s390 the modules are compiled with -fpic which limits the GOT to 4K. The end result is a module that is loaded but won't work. Add a sanity check to apply_rela and return with an error if a relocation error is detected for a module. Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent 4d334fd commit 083e14c

1 file changed

Lines changed: 89 additions & 51 deletions

File tree

‎arch/s390/kernel/module.c‎

Lines changed: 89 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ void module_free(struct module *mod, void *module_region)
6565
vfree(module_region);
6666
}
6767

68-
static void
69-
check_rela(Elf_Rela *rela, struct module *me)
68+
static void check_rela(Elf_Rela *rela, struct module *me)
7069
{
7170
struct mod_arch_syminfo *info;
7271

@@ -115,9 +114,8 @@ check_rela(Elf_Rela *rela, struct module *me)
115114
* Account for GOT and PLT relocations. We can't add sections for
116115
* got and plt but we can increase the core module size.
117116
*/
118-
int
119-
module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
120-
char *secstrings, struct module *me)
117+
int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
118+
char *secstrings, struct module *me)
121119
{
122120
Elf_Shdr *symtab;
123121
Elf_Sym *symbols;
@@ -179,13 +177,52 @@ module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
179177
return 0;
180178
}
181179

182-
static int
183-
apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
184-
struct module *me)
180+
static int apply_rela_bits(Elf_Addr loc, Elf_Addr val,
181+
int sign, int bits, int shift)
182+
{
183+
unsigned long umax;
184+
long min, max;
185+
186+
if (val & ((1UL << shift) - 1))
187+
return -ENOEXEC;
188+
if (sign) {
189+
val = (Elf_Addr)(((long) val) >> shift);
190+
min = -(1L << (bits - 1));
191+
max = (1L << (bits - 1)) - 1;
192+
if ((long) val < min || (long) val > max)
193+
return -ENOEXEC;
194+
} else {
195+
val >>= shift;
196+
umax = ((1UL << (bits - 1)) << 1) - 1;
197+
if ((unsigned long) val > umax)
198+
return -ENOEXEC;
199+
}
200+
201+
if (bits == 8)
202+
*(unsigned char *) loc = val;
203+
else if (bits == 12)
204+
*(unsigned short *) loc = (val & 0xfff) |
205+
(*(unsigned short *) loc & 0xf000);
206+
else if (bits == 16)
207+
*(unsigned short *) loc = val;
208+
else if (bits == 20)
209+
*(unsigned int *) loc = (val & 0xfff) << 16 |
210+
(val & 0xff000) >> 4 |
211+
(*(unsigned int *) loc & 0xf00000ff);
212+
else if (bits == 32)
213+
*(unsigned int *) loc = val;
214+
else if (bits == 64)
215+
*(unsigned long *) loc = val;
216+
return 0;
217+
}
218+
219+
static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
220+
const char *strtab, struct module *me)
185221
{
186222
struct mod_arch_syminfo *info;
187223
Elf_Addr loc, val;
188224
int r_type, r_sym;
225+
int rc;
189226

190227
/* This is where to make the change */
191228
loc = base + rela->r_offset;
@@ -205,20 +242,17 @@ apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
205242
case R_390_64: /* Direct 64 bit. */
206243
val += rela->r_addend;
207244
if (r_type == R_390_8)
208-
*(unsigned char *) loc = val;
245+
rc = apply_rela_bits(loc, val, 0, 8, 0);
209246
else if (r_type == R_390_12)
210-
*(unsigned short *) loc = (val & 0xfff) |
211-
(*(unsigned short *) loc & 0xf000);
247+
rc = apply_rela_bits(loc, val, 0, 12, 0);
212248
else if (r_type == R_390_16)
213-
*(unsigned short *) loc = val;
249+
rc = apply_rela_bits(loc, val, 0, 16, 0);
214250
else if (r_type == R_390_20)
215-
*(unsigned int *) loc =
216-
(*(unsigned int *) loc & 0xf00000ff) |
217-
(val & 0xfff) << 16 | (val & 0xff000) >> 4;
251+
rc = apply_rela_bits(loc, val, 1, 20, 0);
218252
else if (r_type == R_390_32)
219-
*(unsigned int *) loc = val;
253+
rc = apply_rela_bits(loc, val, 0, 32, 0);
220254
else if (r_type == R_390_64)
221-
*(unsigned long *) loc = val;
255+
rc = apply_rela_bits(loc, val, 0, 64, 0);
222256
break;
223257
case R_390_PC16: /* PC relative 16 bit. */
224258
case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
@@ -227,15 +261,15 @@ apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
227261
case R_390_PC64: /* PC relative 64 bit. */
228262
val += rela->r_addend - loc;
229263
if (r_type == R_390_PC16)
230-
*(unsigned short *) loc = val;
264+
rc = apply_rela_bits(loc, val, 1, 16, 0);
231265
else if (r_type == R_390_PC16DBL)
232-
*(unsigned short *) loc = val >> 1;
266+
rc = apply_rela_bits(loc, val, 1, 16, 1);
233267
else if (r_type == R_390_PC32DBL)
234-
*(unsigned int *) loc = val >> 1;
268+
rc = apply_rela_bits(loc, val, 1, 32, 1);
235269
else if (r_type == R_390_PC32)
236-
*(unsigned int *) loc = val;
270+
rc = apply_rela_bits(loc, val, 1, 32, 0);
237271
else if (r_type == R_390_PC64)
238-
*(unsigned long *) loc = val;
272+
rc = apply_rela_bits(loc, val, 1, 64, 0);
239273
break;
240274
case R_390_GOT12: /* 12 bit GOT offset. */
241275
case R_390_GOT16: /* 16 bit GOT offset. */
@@ -260,26 +294,24 @@ apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
260294
val = info->got_offset + rela->r_addend;
261295
if (r_type == R_390_GOT12 ||
262296
r_type == R_390_GOTPLT12)
263-
*(unsigned short *) loc = (val & 0xfff) |
264-
(*(unsigned short *) loc & 0xf000);
297+
rc = apply_rela_bits(loc, val, 0, 12, 0);
265298
else if (r_type == R_390_GOT16 ||
266299
r_type == R_390_GOTPLT16)
267-
*(unsigned short *) loc = val;
300+
rc = apply_rela_bits(loc, val, 0, 16, 0);
268301
else if (r_type == R_390_GOT20 ||
269302
r_type == R_390_GOTPLT20)
270-
*(unsigned int *) loc =
271-
(*(unsigned int *) loc & 0xf00000ff) |
272-
(val & 0xfff) << 16 | (val & 0xff000) >> 4;
303+
rc = apply_rela_bits(loc, val, 1, 20, 0);
273304
else if (r_type == R_390_GOT32 ||
274305
r_type == R_390_GOTPLT32)
275-
*(unsigned int *) loc = val;
276-
else if (r_type == R_390_GOTENT ||
277-
r_type == R_390_GOTPLTENT)
278-
*(unsigned int *) loc =
279-
(val + (Elf_Addr) me->module_core - loc) >> 1;
306+
rc = apply_rela_bits(loc, val, 0, 32, 0);
280307
else if (r_type == R_390_GOT64 ||
281308
r_type == R_390_GOTPLT64)
282-
*(unsigned long *) loc = val;
309+
rc = apply_rela_bits(loc, val, 0, 64, 0);
310+
else if (r_type == R_390_GOTENT ||
311+
r_type == R_390_GOTPLTENT) {
312+
val += (Elf_Addr) me->module_core - loc;
313+
rc = apply_rela_bits(loc, val, 1, 32, 1);
314+
}
283315
break;
284316
case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
285317
case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
@@ -321,58 +353,64 @@ apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
321353
val += rela->r_addend - loc;
322354
}
323355
if (r_type == R_390_PLT16DBL)
324-
*(unsigned short *) loc = val >> 1;
356+
rc = apply_rela_bits(loc, val, 1, 16, 1);
325357
else if (r_type == R_390_PLTOFF16)
326-
*(unsigned short *) loc = val;
358+
rc = apply_rela_bits(loc, val, 0, 16, 0);
327359
else if (r_type == R_390_PLT32DBL)
328-
*(unsigned int *) loc = val >> 1;
360+
rc = apply_rela_bits(loc, val, 1, 32, 1);
329361
else if (r_type == R_390_PLT32 ||
330362
r_type == R_390_PLTOFF32)
331-
*(unsigned int *) loc = val;
363+
rc = apply_rela_bits(loc, val, 0, 32, 0);
332364
else if (r_type == R_390_PLT64 ||
333365
r_type == R_390_PLTOFF64)
334-
*(unsigned long *) loc = val;
366+
rc = apply_rela_bits(loc, val, 0, 64, 0);
335367
break;
336368
case R_390_GOTOFF16: /* 16 bit offset to GOT. */
337369
case R_390_GOTOFF32: /* 32 bit offset to GOT. */
338370
case R_390_GOTOFF64: /* 64 bit offset to GOT. */
339371
val = val + rela->r_addend -
340372
((Elf_Addr) me->module_core + me->arch.got_offset);
341373
if (r_type == R_390_GOTOFF16)
342-
*(unsigned short *) loc = val;
374+
rc = apply_rela_bits(loc, val, 0, 16, 0);
343375
else if (r_type == R_390_GOTOFF32)
344-
*(unsigned int *) loc = val;
376+
rc = apply_rela_bits(loc, val, 0, 32, 0);
345377
else if (r_type == R_390_GOTOFF64)
346-
*(unsigned long *) loc = val;
378+
rc = apply_rela_bits(loc, val, 0, 64, 0);
347379
break;
348380
case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
349381
case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
350382
val = (Elf_Addr) me->module_core + me->arch.got_offset +
351383
rela->r_addend - loc;
352384
if (r_type == R_390_GOTPC)
353-
*(unsigned int *) loc = val;
385+
rc = apply_rela_bits(loc, val, 1, 32, 0);
354386
else if (r_type == R_390_GOTPCDBL)
355-
*(unsigned int *) loc = val >> 1;
387+
rc = apply_rela_bits(loc, val, 1, 32, 1);
356388
break;
357389
case R_390_COPY:
358390
case R_390_GLOB_DAT: /* Create GOT entry. */
359391
case R_390_JMP_SLOT: /* Create PLT entry. */
360392
case R_390_RELATIVE: /* Adjust by program base. */
361393
/* Only needed if we want to support loading of
362394
modules linked with -shared. */
363-
break;
395+
return -ENOEXEC;
364396
default:
365-
printk(KERN_ERR "module %s: Unknown relocation: %u\n",
397+
printk(KERN_ERR "module %s: unknown relocation: %u\n",
366398
me->name, r_type);
367399
return -ENOEXEC;
368400
}
401+
if (rc) {
402+
printk(KERN_ERR "module %s: relocation error for symbol %s "
403+
"(r_type %i, value 0x%lx)\n",
404+
me->name, strtab + symtab[r_sym].st_name,
405+
r_type, (unsigned long) val);
406+
return rc;
407+
}
369408
return 0;
370409
}
371410

372-
int
373-
apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
374-
unsigned int symindex, unsigned int relsec,
375-
struct module *me)
411+
int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
412+
unsigned int symindex, unsigned int relsec,
413+
struct module *me)
376414
{
377415
Elf_Addr base;
378416
Elf_Sym *symtab;
@@ -388,7 +426,7 @@ apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
388426
n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
389427

390428
for (i = 0; i < n; i++, rela++) {
391-
rc = apply_rela(rela, base, symtab, me);
429+
rc = apply_rela(rela, base, symtab, strtab, me);
392430
if (rc)
393431
return rc;
394432
}

0 commit comments

Comments
 (0)