1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
/*
* Copyright (C) 2016 Citrix Systems R&D Ltd.
*/
#include <xen/errno.h>
#include <xen/lib.h>
#include <xen/symbols.h>
#include <xen/livepatch_elf.h>
#include <xen/livepatch.h>
const struct livepatch_elf_sec *
livepatch_elf_sec_by_name(const struct livepatch_elf *elf,
const char *name)
{
unsigned int i;
for ( i = 1; i < elf->hdr->e_shnum; i++ )
{
if ( !strcmp(name, elf->sec[i].name) )
return &elf->sec[i];
}
return NULL;
}
static int elf_verify_strtab(const struct livepatch_elf_sec *sec)
{
const Elf_Shdr *s;
const char *contents;
s = sec->sec;
if ( s->sh_type != SHT_STRTAB )
return -EINVAL;
if ( !s->sh_size )
return -EINVAL;
contents = sec->data;
if ( contents[0] || contents[s->sh_size - 1] )
return -EINVAL;
return 0;
}
static int elf_resolve_sections(struct livepatch_elf *elf, const void *data)
{
struct livepatch_elf_sec *sec;
unsigned int i;
Elf_Off delta;
int rc;
/* livepatch_elf_load sanity checked e_shnum. */
sec = xzalloc_array(struct livepatch_elf_sec, elf->hdr->e_shnum);
if ( !sec )
{
printk(XENLOG_ERR LIVEPATCH"%s: Could not allocate memory for section table\n",
elf->name);
return -ENOMEM;
}
elf->sec = sec;
/* e_shoff and e_shnum overflow checks are done in livepatch_header_check. */
delta = elf->hdr->e_shoff + elf->hdr->e_shnum * elf->hdr->e_shentsize;
ASSERT(delta <= elf->len);
for ( i = 1; i < elf->hdr->e_shnum; i++ )
{
delta = elf->hdr->e_shoff + i * elf->hdr->e_shentsize;
sec[i].sec = data + delta;
delta = sec[i].sec->sh_offset;
/*
* N.B. elf_resolve_section_names, elf_get_sym skip this check as
* we do it here.
*/
if ( delta < sizeof(Elf_Ehdr) ||
(sec[i].sec->sh_type != SHT_NOBITS && /* Skip SHT_NOBITS */
(delta > elf->len || (delta + sec[i].sec->sh_size > elf->len))) )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section [%u] data %s of payload\n",
elf->name, i,
delta < sizeof(Elf_Ehdr) ? "at ELF header" : "is past end");
return -EINVAL;
}
else if ( sec[i].sec->sh_addralign & (sec[i].sec->sh_addralign - 1) )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section [%u] alignment (%#"PRIxElfAddr") is not supported\n",
elf->name, i, sec[i].sec->sh_addralign);
return -EOPNOTSUPP;
}
else if ( sec[i].sec->sh_addralign &&
sec[i].sec->sh_addr % sec[i].sec->sh_addralign )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section [%u] addr (%#"PRIxElfAddr") is not aligned properly (%#"PRIxElfAddr")\n",
elf->name, i, sec[i].sec->sh_addr, sec[i].sec->sh_addralign);
return -EINVAL;
}
else if ( (sec[i].sec->sh_flags & (SHF_WRITE | SHF_ALLOC)) &&
sec[i].sec->sh_type == SHT_NOBITS &&
sec[i].sec->sh_size > LIVEPATCH_MAX_SIZE )
return -EINVAL;
sec[i].data = data + delta;
/* Name is populated in elf_resolve_section_names. */
sec[i].name = NULL;
if ( sec[i].sec->sh_type == SHT_SYMTAB )
{
if ( elf->symtab )
{
printk(XENLOG_ERR LIVEPATCH "%s: Unsupported multiple symbol tables\n",
elf->name);
return -EOPNOTSUPP;
}
elf->symtab = &sec[i];
elf->symtab_idx = i;
/*
* elf->symtab->sec->sh_link would point to the right section
* but we hadn't finished parsing all the sections.
*/
if ( elf->symtab->sec->sh_link >= elf->hdr->e_shnum )
{
printk(XENLOG_ERR LIVEPATCH
"%s: Symbol table idx (%u) to strtab past end (%u)\n",
elf->name, elf->symtab->sec->sh_link,
elf->hdr->e_shnum);
return -EINVAL;
}
}
}
if ( !elf->symtab )
{
printk(XENLOG_ERR LIVEPATCH "%s: No symbol table found\n",
elf->name);
return -EINVAL;
}
if ( !elf->symtab->sec->sh_size ||
elf->symtab->sec->sh_entsize < sizeof(Elf_Sym) ||
elf->symtab->sec->sh_size % elf->symtab->sec->sh_entsize )
{
printk(XENLOG_ERR LIVEPATCH "%s: Symbol table header is corrupted\n",
elf->name);
return -EINVAL;
}
/*
* There can be multiple SHT_STRTAB (.shstrtab, .strtab) so pick the one
* associated with the symbol table.
*/
elf->strtab = &sec[elf->symtab->sec->sh_link];
rc = elf_verify_strtab(elf->strtab);
if ( rc )
{
printk(XENLOG_ERR LIVEPATCH "%s: String table section is corrupted\n",
elf->name);
}
return rc;
}
static int elf_resolve_section_names(struct livepatch_elf *elf, const void *data)
{
const char *shstrtab;
unsigned int i;
Elf_Off offset, delta;
struct livepatch_elf_sec *sec;
int rc;
/*
* The elf->sec[0 -> e_shnum] structures have been verified by
* elf_resolve_sections. Find file offset for section string table
* (normally called .shstrtab)
*/
sec = &elf->sec[elf->hdr->e_shstrndx];
rc = elf_verify_strtab(sec);
if ( rc )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section string table is corrupted\n",
elf->name);
return rc;
}
/* Verified in elf_resolve_sections but just in case. */
offset = sec->sec->sh_offset;
ASSERT(offset < elf->len && (offset + sec->sec->sh_size <= elf->len));
shstrtab = data + offset;
for ( i = 1; i < elf->hdr->e_shnum; i++ )
{
delta = elf->sec[i].sec->sh_name;
/* Boundary check on offset of name within the .shstrtab. */
if ( delta >= sec->sec->sh_size )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section %u name is not within .shstrtab\n",
elf->name, i);
return -EINVAL;
}
elf->sec[i].name = shstrtab + delta;
}
return 0;
}
static int elf_get_sym(struct livepatch_elf *elf, const void *data)
{
const struct livepatch_elf_sec *symtab_sec, *strtab_sec;
struct livepatch_elf_sym *sym;
unsigned int i, nsym;
Elf_Off offset;
Elf_Word delta;
symtab_sec = elf->symtab;
strtab_sec = elf->strtab;
/* Pointers arithmetic to get file offset. */
offset = strtab_sec->data - data;
/* Checked already in elf_resolve_sections, but just in case. */
ASSERT(offset == strtab_sec->sec->sh_offset);
ASSERT(offset < elf->len && (offset + strtab_sec->sec->sh_size <= elf->len));
/* symtab_sec->data was computed in elf_resolve_sections. */
ASSERT((symtab_sec->sec->sh_offset + data) == symtab_sec->data);
/* No need to check values as elf_resolve_sections did it. */
nsym = symtab_sec->sec->sh_size / symtab_sec->sec->sh_entsize;
sym = xzalloc_array(struct livepatch_elf_sym, nsym);
if ( !sym )
{
printk(XENLOG_ERR LIVEPATCH "%s: Could not allocate memory for symbols\n",
elf->name);
return -ENOMEM;
}
/* So we don't leak memory. */
elf->sym = sym;
for ( i = 1; i < nsym; i++ )
{
const Elf_Sym *s = symtab_sec->data + symtab_sec->sec->sh_entsize * i;
delta = s->st_name;
/* Boundary check within the .strtab. */
if ( delta >= strtab_sec->sec->sh_size )
{
printk(XENLOG_ERR LIVEPATCH "%s: Symbol [%u] name is not within .strtab\n",
elf->name, i);
return -EINVAL;
}
sym[i].sym = s;
sym[i].name = strtab_sec->data + delta;
if ( arch_livepatch_symbol_deny(elf, &sym[i]) )
{
printk(XENLOG_ERR LIVEPATCH "%s: Symbol '%s' should not be in payload\n",
elf->name, sym[i].name);
return -EINVAL;
}
}
elf->nsym = nsym;
return 0;
}
int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
{
unsigned int i;
int rc = 0;
ASSERT(elf->sym);
for ( i = 1; i < elf->nsym; i++ )
{
unsigned int idx = elf->sym[i].sym->st_shndx;
const Elf_Sym *sym = elf->sym[i].sym;
Elf_Addr st_value = sym->st_value;
switch ( idx )
{
case SHN_COMMON:
printk(XENLOG_ERR LIVEPATCH "%s: Unexpected common symbol: %s\n",
elf->name, elf->sym[i].name);
rc = -EINVAL;
break;
case SHN_UNDEF:
st_value = symbols_lookup_by_name(elf->sym[i].name);
if ( !st_value )
{
st_value = livepatch_symbols_lookup_by_name(elf->sym[i].name);
if ( !st_value )
{
printk(XENLOG_ERR LIVEPATCH "%s: Unknown symbol: %s\n",
elf->name, elf->sym[i].name);
rc = -ENOENT;
break;
}
}
dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => %#"PRIxElfAddr"\n",
elf->name, elf->sym[i].name, st_value);
break;
case SHN_ABS:
dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Absolute symbol: %s => %#"PRIxElfAddr"\n",
elf->name, elf->sym[i].name, sym->st_value);
break;
default:
/* SHN_COMMON and SHN_ABS are above. */
if ( idx >= SHN_LORESERVE )
rc = -EOPNOTSUPP;
else if ( idx >= elf->hdr->e_shnum )
rc = -EINVAL;
if ( rc )
{
printk(XENLOG_ERR LIVEPATCH "%s: Out of bounds symbol section %#x\n",
elf->name, idx);
break;
}
if ( livepatch_elf_ignore_section(elf->sec[idx].sec) )
{
dprintk(XENLOG_DEBUG, LIVEPATCH
"%s: Symbol %s from section %s ignored\n",
elf->name, elf->sym[i].name, elf->sec[idx].name);
elf->sym[i].ignored = true;
break;
}
st_value += (unsigned long)elf->sec[idx].load_addr;
if ( elf->sym[i].name )
dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Symbol resolved: %s => %#"PRIxElfAddr" (%s)\n",
elf->name, elf->sym[i].name,
st_value, elf->sec[idx].name);
}
if ( rc )
break;
((Elf_Sym *)sym)->st_value = st_value;
}
return rc;
}
int livepatch_elf_perform_relocs(struct livepatch_elf *elf)
{
struct livepatch_elf_sec *r, *base;
unsigned int i;
int rc = 0;
size_t sz;
ASSERT(elf->sym);
for ( i = 1; i < elf->hdr->e_shnum; i++ )
{
r = &elf->sec[i];
if ( (r->sec->sh_type != SHT_RELA) &&
(r->sec->sh_type != SHT_REL) )
continue;
/* Is it a valid relocation section? */
if ( r->sec->sh_info >= elf->hdr->e_shnum )
continue;
base = &elf->sec[r->sec->sh_info];
/* Don't relocate non-allocated sections. */
if ( !(base->sec->sh_flags & SHF_ALLOC) )
continue;
if ( r->sec->sh_link != elf->symtab_idx )
{
printk(XENLOG_ERR LIVEPATCH "%s: Relative link of %s is incorrect (%d, expected=%d)\n",
elf->name, r->name, r->sec->sh_link, elf->symtab_idx);
rc = -EINVAL;
break;
}
if ( r->sec->sh_type == SHT_RELA )
sz = sizeof(Elf_RelA);
else
sz = sizeof(Elf_Rel);
if ( !r->sec->sh_size )
continue;
if ( r->sec->sh_entsize < sz || r->sec->sh_size % r->sec->sh_entsize )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section relative header is corrupted\n",
elf->name);
rc = -EINVAL;
break;
}
if ( r->sec->sh_type == SHT_RELA )
rc = arch_livepatch_perform_rela(elf, base, r);
else /* SHT_REL */
rc = arch_livepatch_perform_rel(elf, base, r);
if ( rc )
break;
}
return rc;
}
static int livepatch_header_check(const struct livepatch_elf *elf)
{
const Elf_Ehdr *hdr = elf->hdr;
int rc;
if ( sizeof(*elf->hdr) > elf->len )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section header is bigger than payload\n",
elf->name);
return -EINVAL;
}
if ( !IS_ELF(*hdr) )
{
printk(XENLOG_ERR LIVEPATCH "%s: Not an ELF payload\n", elf->name);
return -EINVAL;
}
/* EI_CLASS, EI_DATA, and e_flags are platform specific. */
if ( hdr->e_version != EV_CURRENT ||
hdr->e_ident[EI_VERSION] != EV_CURRENT ||
hdr->e_ident[EI_ABIVERSION] != 0 ||
(hdr->e_ident[EI_OSABI] != ELFOSABI_NONE &&
hdr->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) ||
hdr->e_type != ET_REL ||
hdr->e_phnum != 0 )
{
printk(XENLOG_ERR LIVEPATCH "%s: Invalid ELF payload\n", elf->name);
return -EOPNOTSUPP;
}
rc = arch_livepatch_verify_elf(elf);
if ( rc )
return rc;
if ( elf->hdr->e_shstrndx == SHN_UNDEF )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section name idx is undefined\n",
elf->name);
return -EINVAL;
}
/* Arbitrary boundary limit. */
if ( elf->hdr->e_shnum >= 1024 )
{
printk(XENLOG_ERR LIVEPATCH "%s: Too many (%u) sections\n",
elf->name, elf->hdr->e_shnum);
return -EOPNOTSUPP;
}
/* Check that section name index is within the sections. */
if ( elf->hdr->e_shstrndx >= elf->hdr->e_shnum )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section name idx (%u) is past end of sections (%u)\n",
elf->name, elf->hdr->e_shstrndx, elf->hdr->e_shnum);
return -EINVAL;
}
if ( elf->hdr->e_shoff >= elf->len )
{
printk(XENLOG_ERR LIVEPATCH "%s: Bogus e_shoff\n", elf->name);
return -EINVAL;
}
if ( elf->hdr->e_shentsize < sizeof(Elf_Shdr) )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section header size is %u! Expected %zu.\n",
elf->name, elf->hdr->e_shentsize, sizeof(Elf_Shdr));
return -EINVAL;
}
if ( ((elf->len - elf->hdr->e_shoff) / elf->hdr->e_shentsize) <
elf->hdr->e_shnum )
{
printk(XENLOG_ERR LIVEPATCH "%s: Section header size is corrupted\n",
elf->name);
return -EINVAL;
}
return 0;
}
int livepatch_elf_load(struct livepatch_elf *elf, const void *data)
{
int rc;
elf->hdr = data;
rc = livepatch_header_check(elf);
if ( rc )
return rc;
rc = elf_resolve_sections(elf, data);
if ( rc )
return rc;
rc = elf_resolve_section_names(elf, data);
if ( rc )
return rc;
rc = elf_get_sym(elf, data);
if ( rc )
return rc;
return 0;
}
void livepatch_elf_free(struct livepatch_elf *elf)
{
xfree(elf->sec);
elf->sec = NULL;
xfree(elf->sym);
elf->sym = NULL;
elf->nsym = 0;
elf->name = NULL;
elf->len = 0;
}
/*
* Local variables:
* mode: C
* c-file-style: "BSD"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|