Skip to content

Latest commit

 

History

History
481 lines (442 loc) · 15.2 KB

linux-kernel.diff

File metadata and controls

481 lines (442 loc) · 15.2 KB
 
Oct 21, 2009
Oct 21, 2009
1
2
3
4
5
6
Please note that this patch applies cleanly to Ubuntu 9.04's Linux kernel
sources (2.6.28-15-generic, specifically).
Oct 24, 2009
Oct 24, 2009
7
8
Patches being submitted for consideration on the Linux kernel mailing list
are not kept here, and are made against the current mainline (2.6.31-rc5
Oct 21, 2009
Oct 21, 2009
9
10
11
12
13
14
15
16
17
at the time of this writing). If you want those, get them from the mailing
list.
You probably don't want the below patch unless you're experimenting with a
stable (and older) kernel.
Oct 3, 2009
Oct 3, 2009
18
19
20
21
diff -ru linux-2.6.28-orig/arch/ia64/ia32/binfmt_elf32.c linux-2.6.28/arch/ia64/ia32/binfmt_elf32.c
--- linux-2.6.28-orig/arch/ia64/ia32/binfmt_elf32.c 2008-12-24 18:26:37.000000000 -0500
+++ linux-2.6.28/arch/ia64/ia32/binfmt_elf32.c 2009-10-02 00:10:12.000000000 -0400
@@ -223,12 +223,12 @@
22
23
24
25
26
27
28
29
30
31
32
33
34
35
static unsigned long
elf32_map(struct file *filep, unsigned long addr, struct elf_phdr *eppnt,
- int prot, int type, unsigned long unused)
+ int prot, int type, unsigned long unused, unsigned long base)
{
unsigned long pgoff = (eppnt->p_vaddr) & ~IA32_PAGE_MASK;
return ia32_do_mmap(filep, (addr & IA32_PAGE_MASK), eppnt->p_filesz + pgoff, prot, type,
- eppnt->p_offset - pgoff);
+ (eppnt->p_offset + base) - pgoff);
}
#define cpu_uses_ia32el() (local_cpu_data->family > 0x1f)
Oct 3, 2009
Oct 3, 2009
36
37
38
39
diff -ru linux-2.6.28-orig/fs/binfmt_elf.c linux-2.6.28/fs/binfmt_elf.c
--- linux-2.6.28-orig/fs/binfmt_elf.c 2009-10-03 02:11:23.000000000 -0400
+++ linux-2.6.28/fs/binfmt_elf.c 2009-10-02 18:42:52.000000000 -0400
@@ -44,8 +44,9 @@
40
41
42
43
44
45
46
47
48
49
50
static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
static int load_elf_library(struct file *);
-static unsigned long elf_map(struct file *, unsigned long, struct elf_phdr *,
- int, int, unsigned long);
+static unsigned long
+elf_map(struct file *, unsigned long, struct elf_phdr *,
+ int, int, unsigned long, unsigned long);
/*
* If we don't support core dumping, then supply a NULL so we
Oct 3, 2009
Oct 3, 2009
51
@@ -314,7 +315,7 @@
52
53
54
55
56
57
58
59
static unsigned long elf_map(struct file *filep, unsigned long addr,
struct elf_phdr *eppnt, int prot, int type,
- unsigned long total_size)
+ unsigned long total_size, unsigned long base_offset)
{
unsigned long map_addr;
unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr);
Oct 3, 2009
Oct 3, 2009
60
@@ -338,11 +339,14 @@
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
*/
if (total_size) {
total_size = ELF_PAGEALIGN(total_size);
- map_addr = do_mmap(filep, addr, total_size, prot, type, off);
+ map_addr = do_mmap(filep, addr, total_size, prot, type,
+ off + base_offset);
if (!BAD_ADDR(map_addr))
do_munmap(current->mm, map_addr+size, total_size-size);
- } else
- map_addr = do_mmap(filep, addr, size, prot, type, off);
+ } else {
+ map_addr = do_mmap(filep, addr, size, prot, type,
+ off + base_offset);
+ }
up_write(&current->mm->mmap_sem);
return(map_addr);
Oct 3, 2009
Oct 3, 2009
78
@@ -376,7 +380,7 @@
79
80
81
82
83
84
85
86
static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
struct file *interpreter, unsigned long *interp_map_addr,
- unsigned long no_base)
+ unsigned long no_base, unsigned long base_offset)
{
struct elf_phdr *elf_phdata;
struct elf_phdr *eppnt;
Oct 3, 2009
Oct 3, 2009
87
@@ -414,7 +418,7 @@
88
89
90
91
92
93
94
95
if (!elf_phdata)
goto out;
- retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
+ retval = kernel_read(interpreter, interp_elf_ex->e_phoff + base_offset,
(char *)elf_phdata,size);
error = -EIO;
if (retval != size) {
Oct 3, 2009
Oct 3, 2009
96
@@ -450,7 +454,8 @@
97
98
99
100
101
102
103
104
105
load_addr = -vaddr;
map_addr = elf_map(interpreter, load_addr + vaddr,
- eppnt, elf_prot, elf_type, total_size);
+ eppnt, elf_prot, elf_type, total_size,
+ base_offset);
total_size = 0;
if (!*interp_map_addr)
*interp_map_addr = map_addr;
Oct 3, 2009
Oct 3, 2009
106
@@ -555,6 +560,94 @@
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
#endif
}
+/*
+ * See if we're a valid FatELF binary, find the right record, and
+ * load (*elf) with the actual ELF header. Sets (*offset) to the
+ * base offset of the chosen ELF binary. Returns 0 on success or a negative
+ * error code.
+ * If we're not a FatELF binary, (*elf) is loaded with the existing contents
+ * of (buf) and 0 is returned.
+ */
+static int examine_fatelf(struct file *file, const char *filename, char *buf,
+ int buflen, unsigned long *offset, struct elfhdr *elf)
+{
+ int records, i, rc;
+ const fatelf_hdr *fatelf = (fatelf_hdr *) buf;
+
+ if (likely(le32_to_cpu(fatelf->magic) != FATELF_MAGIC)) {
+ *elf = *((struct elfhdr *)buf); /* treat like normal ELF. */
+ return 0; /* not a FatELF binary; not an error. */
+ } else if (unlikely(le16_to_cpu(fatelf->version) != 1)) {
+ return -ENOEXEC; /* Unrecognized format version. */
+ }
+
+ /*
+ * In theory, there could be 255 separate records packed into this
+ * binary, but for now, bprm->buf (128 bytes) holds exactly 5
+ * records with the fatelf header, and that seems reasonable for
+ * most uses. We could add the complexity to read more records later
+ * if there's a serious need.
+ */
+ records = (int) fatelf->num_records; /* uint8, no byteswap needed */
+
+ if (unlikely(records > 5)) {
+ records = 5; /* clamp, in case we find one we can use. */
+ }
+
+ for (i = 0; i < records; i++) {
+ const fatelf_record *record = &fatelf->records[i];
Sep 17, 2009
Sep 17, 2009
146
+ const __u8 osabi = record->osabi;
147
148
149
150
+ const int abiok = likely( likely(osabi == ELFOSABI_NONE) ||
+ unlikely(osabi == ELFOSABI_LINUX) );
+
+ /* Fill in the data elf_check_arch() might care about. */
Sep 17, 2009
Sep 17, 2009
151
+ elf->e_ident[EI_OSABI] = record->osabi;
152
153
154
155
156
157
158
159
+ elf->e_ident[EI_CLASS] = record->word_size;
+ elf->e_ident[EI_DATA] = record->byte_order;
+ elf->e_machine = le16_to_cpu(record->machine);
+
+ if (likely(!elf_check_arch(elf))) {
+ continue; /* Unsupported CPU architecture. */
+ } else if (unlikely(!abiok)) {
+ continue; /* Unsupported OS ABI. */
Sep 17, 2009
Sep 17, 2009
160
+ } else if (unlikely(record->osabi_version != 0)) {
161
162
163
164
165
166
167
168
169
+ continue; /* Unsupported OS ABI version. */
+ } else {
+ /* We can support this ELF arch/abi. */
+ const __u64 rec_offset = le64_to_cpu(record->offset);
+ const __u64 rec_size = le64_to_cpu(record->size);
+ const __u64 end_offset = rec_offset + rec_size;
+ const unsigned long uloff = (unsigned long) rec_offset;
+
+ if (unlikely(end_offset < rec_offset)) {
Oct 3, 2009
Oct 3, 2009
170
171
172
+ continue; /* overflow (corrupt file?) */
+ } else if (unlikely(ELF_PAGEOFFSET(uloff) != 0)) {
+ continue; /* bad alignment. */
173
174
175
+ }
+
+#if BITS_PER_LONG == 32
Oct 3, 2009
Oct 3, 2009
176
+ if (unlikely(end_offset > 0xFFFFFFFF)) {
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
+ continue;
+ }
+#endif
+
+ /* replace the FatELF data with the real ELF header. */
+ rc = kernel_read(file, uloff, (char*) elf, sizeof(*elf));
+ if (unlikely((rc != sizeof(*elf)) && (rc >= 0))) {
+ rc = -EIO;
+ } else if (likely(rc == sizeof(*elf))) {
+ *offset = uloff;
+ rc = 0;
+ }
+
+ return rc;
+ }
+ }
+
+ return -ENOEXEC; /* no binaries we could use. */
+}
+
+
static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
{
struct file *interpreter = NULL; /* to shut gcc up */
Oct 3, 2009
Oct 3, 2009
201
202
@@ -567,6 +660,8 @@
int elf_exec_fileno;
203
204
205
206
207
208
209
int retval, i;
unsigned int size;
+ unsigned long base_offset = 0;
+ unsigned long interp_base_offset = 0;
unsigned long elf_entry;
unsigned long interp_load_addr = 0;
unsigned long start_code, end_code, start_data, end_data;
Oct 3, 2009
Oct 3, 2009
210
@@ -583,9 +678,12 @@
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
retval = -ENOMEM;
goto out_ret;
}
-
- /* Get the exec-header */
- loc->elf_ex = *((struct elfhdr *)bprm->buf);
+
+ retval = examine_fatelf(bprm->file, bprm->filename, bprm->buf,
+ BINPRM_BUF_SIZE, &base_offset, &loc->elf_ex);
+ if (unlikely(retval < 0)) {
+ goto out_ret;
+ }
retval = -ENOEXEC;
/* First of all, some simple consistency checks */
Oct 3, 2009
Oct 3, 2009
226
@@ -611,7 +709,7 @@
227
228
229
230
231
232
233
234
if (!elf_phdata)
goto out;
- retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
+ retval = kernel_read(bprm->file, loc->elf_ex.e_phoff + base_offset,
(char *)elf_phdata, size);
if (retval != size) {
if (retval >= 0)
Oct 3, 2009
Oct 3, 2009
235
@@ -651,7 +749,8 @@
236
if (!elf_interpreter)
Oct 3, 2009
Oct 3, 2009
237
goto out_free_file;
238
239
240
241
242
243
244
- retval = kernel_read(bprm->file, elf_ppnt->p_offset,
+ retval = kernel_read(bprm->file,
+ elf_ppnt->p_offset + base_offset,
elf_interpreter,
elf_ppnt->p_filesz);
if (retval != elf_ppnt->p_filesz) {
Oct 3, 2009
Oct 3, 2009
245
@@ -706,8 +805,13 @@
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
goto out_free_dentry;
}
- /* Get the exec headers */
- loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
+ retval = examine_fatelf(interpreter, elf_interpreter,
+ bprm->buf, BINPRM_BUF_SIZE,
+ &interp_base_offset,
+ &loc->interp_elf_ex);
+ if (unlikely(retval < 0)) {
+ goto out_free_dentry;
+ }
break;
}
elf_ppnt++;
Oct 3, 2009
Oct 3, 2009
261
@@ -832,7 +936,7 @@
262
263
264
265
266
267
268
269
}
error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
- elf_prot, elf_flags, 0);
+ elf_prot, elf_flags, 0, base_offset);
if (BAD_ADDR(error)) {
send_sig(SIGKILL, current, 0);
retval = IS_ERR((void *)error) ?
Oct 3, 2009
Oct 3, 2009
270
@@ -913,7 +1017,7 @@
271
272
273
274
275
276
277
278
elf_entry = load_elf_interp(&loc->interp_elf_ex,
interpreter,
&interp_map_addr,
- load_bias);
+ load_bias, interp_base_offset);
if (!IS_ERR((void *)elf_entry)) {
/*
* load_elf_interp() returns relocation
Oct 3, 2009
Oct 3, 2009
279
@@ -1032,11 +1136,19 @@
280
281
282
283
284
285
286
287
288
289
290
291
unsigned long elf_bss, bss, len;
int retval, error, i, j;
struct elfhdr elf_ex;
+ unsigned long base_offset = 0;
+ char buf[BINPRM_BUF_SIZE];
- error = -ENOEXEC;
- retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
- if (retval != sizeof(elf_ex))
+ retval = kernel_read(file, 0, buf, sizeof(buf));
+ if (unlikely(retval != sizeof(buf))) {
+ error = (retval >= 0) ? -EIO : retval;
Oct 3, 2009
Oct 3, 2009
292
goto out;
293
294
295
+ }
+ error = examine_fatelf(file, 0, buf, sizeof(buf), &base_offset, &elf_ex);
+ if (unlikely(retval < 0)) {
Oct 3, 2009
Oct 3, 2009
296
+ goto out;
297
298
299
300
301
+ }
+ error = -ENOEXEC;
if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
goto out;
Oct 3, 2009
Oct 3, 2009
302
@@ -1058,7 +1170,8 @@
303
304
305
306
307
308
309
310
311
eppnt = elf_phdata;
error = -ENOEXEC;
- retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
+ retval = kernel_read(file, elf_ex.e_phoff + base_offset,
+ (char *)eppnt, j);
if (retval != j)
goto out_free_ph;
Oct 3, 2009
Oct 3, 2009
312
@@ -1080,7 +1193,7 @@
313
314
315
316
317
318
319
320
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
(eppnt->p_offset -
- ELF_PAGEOFFSET(eppnt->p_vaddr)));
+ ELF_PAGEOFFSET(eppnt->p_vaddr)) + base_offset);
up_write(&current->mm->mmap_sem);
if (error != ELF_PAGESTART(eppnt->p_vaddr))
goto out_free_ph;
Oct 3, 2009
Oct 3, 2009
321
322
323
324
diff -ru linux-2.6.28-orig/include/linux/elf.h linux-2.6.28/include/linux/elf.h
--- linux-2.6.28-orig/include/linux/elf.h 2008-12-24 18:26:37.000000000 -0500
+++ linux-2.6.28/include/linux/elf.h 2009-10-02 00:10:12.000000000 -0400
@@ -188,6 +188,30 @@
325
326
327
328
329
330
331
} Elf64_Sym;
+/* FatELF (multiple ELF binaries in one file) support */
+#define FATELF_MAGIC (0x1F0E70FA)
+
+typedef struct fatelf_record {
Sep 17, 2009
Sep 17, 2009
332
333
+ __le16 machine; /* maps to e_machine */
+ __u8 osabi; /* maps to e_ident[EI_OSABI] */
Sep 24, 2009
Sep 24, 2009
334
+ __u8 osabi_version; /* maps to e_ident[EI_ABIVERSION] */
Sep 17, 2009
Sep 17, 2009
335
336
337
338
+ __u8 word_size; /* maps to e_ident[EI_CLASS] */
+ __u8 byte_order; /* maps to e_ident[EI_DATA] */
+ __u8 reserved0;
+ __u8 reserved1;
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
+ __le64 offset;
+ __le64 size;
+} fatelf_record;
+
+typedef struct fatelf_hdr {
+ __le32 magic;
+ __le16 version;
+ __u8 num_records;
+ __u8 reserved0;
+ fatelf_record records[];
+} fatelf_hdr;
+
+
#define EI_NIDENT 16
typedef struct elf32_hdr{
Oct 3, 2009
Oct 3, 2009
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
diff -ru linux-2.6.28-orig/kernel/module.c linux-2.6.28/kernel/module.c
--- linux-2.6.28-orig/kernel/module.c 2009-10-03 02:11:23.000000000 -0400
+++ linux-2.6.28/kernel/module.c 2009-10-03 02:08:53.000000000 -0400
@@ -1833,13 +1833,69 @@
return ret;
}
+/*
+ * See if we're a valid FatELF binary, find the right record, and
+ * return the offset of that record within the binary. Returns NULL if there's
+ * a problem, or a pointer to the real ELF header if we're okay.
+ * If we don't see the FatELF magic number, we assume this is a regular ELF
+ * binary and let the regular ELF checks handle it.
+ *
+ * This is a simplified version of examine_fatelf in fs/binfmt_elf.c
+ */
+static Elf_Ehdr *examine_fatelf_module(const unsigned char *hdr,
+ const unsigned long len)
+{
+ Elf_Ehdr elf;
+ int records, i;
+ const fatelf_hdr *fatelf = (const fatelf_hdr *) hdr;
+
+ if (likely(le32_to_cpu(fatelf->magic) != FATELF_MAGIC)) {
+ return (Elf_Ehdr *) hdr; /* not FatELF; not an error. */
+ } else if (unlikely(le16_to_cpu(fatelf->version) != 1)) {
+ return NULL; /* Unrecognized format version. */
+ }
+
+ memset(&elf, 0, sizeof (elf));
+
+ records = (int) fatelf->num_records; /* uint8, no byteswap needed */
+ for (i = 0; i < records; i++) {
+ const fatelf_record *record = &fatelf->records[i];
+
+ /* Fill in the data elf_check_arch() might care about. */
+ elf.e_ident[EI_OSABI] = record->osabi;
+ elf.e_ident[EI_CLASS] = record->word_size;
+ elf.e_ident[EI_DATA] = record->byte_order;
+ elf.e_machine = le16_to_cpu(record->machine);
+
+ if (likely(!elf_check_arch(&elf))) {
+ continue; /* Unsupported CPU architecture. */
+ } else {
+ const __u64 rec_offset = le64_to_cpu(record->offset);
+ const __u64 rec_size = le64_to_cpu(record->size);
+ const __u64 end_offset = rec_offset + rec_size;
+ const unsigned long uloff = (unsigned long) rec_offset;
+
+ if (unlikely(end_offset < rec_offset)) {
+ continue; /* overflow (corrupt file?)... */
+ } else if (unlikely(end_offset > len)) {
+ continue; /* past EOF. */
+ }
+
+ return (Elf_Ehdr *) (hdr + uloff);
+ }
+ }
+
+ return NULL; /* no binaries we could use. */
+}
+
/* Allocate and load the module: note that size of section 0 is always
zero, and we rely on this for optional sections. */
static noinline struct module *load_module(void __user *umod,
unsigned long len,
const char __user *uargs)
{
- Elf_Ehdr *hdr;
+ Elf_Ehdr *hdr_alloc; /* returned from vmalloc */
+ Elf_Ehdr *hdr; /* adjusted hdr_alloc for FatELF */
Elf_Shdr *sechdrs;
char *secstrings, *args, *modmagic, *strtab = NULL;
char *staging;
@@ -1863,13 +1919,19 @@
/* Suck in entire file: we'll want most of it. */
/* vmalloc barfs on "unusual" numbers. Check here */
- if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
+ if (len > 64 * 1024 * 1024 || (hdr_alloc = vmalloc(len)) == NULL)
return ERR_PTR(-ENOMEM);
- if (copy_from_user(hdr, umod, len) != 0) {
+ if (copy_from_user(hdr_alloc, umod, len) != 0) {
err = -EFAULT;
goto free_hdr;
}
+ hdr = examine_fatelf_module((unsigned char *) hdr_alloc, len);
+ if (hdr == NULL) {
+ err = -ENOEXEC;
+ goto free_hdr;
+ }
+
/* Sanity checks against insmoding binaries or wrong arch,
weird elf version */
if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
@@ -2255,7 +2317,7 @@
sechdrs[unwindex].sh_size);
/* Get rid of temporary copy */
- vfree(hdr);
+ vfree(hdr_alloc);
/* Done! */
return mod;
@@ -2278,7 +2340,7 @@
free_mod:
kfree(args);
free_hdr:
- vfree(hdr);
+ vfree(hdr_alloc);
return ERR_PTR(err);
truncated:
Oct 15, 2009
Oct 15, 2009
469
470
471
472
473
474
475
476
477
478
479
480
481
--- linux-2.6.28-orig/fs/Makefile 2008-12-24 18:26:37.000000000 -0500
+++ linux-2.6.28/fs/Makefile 2009-10-14 23:32:11.000000000 -0400
@@ -41,8 +41,9 @@
# binfmt_script is always there
obj-y += binfmt_script.o
-obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o
+# List compat_* first, so they insert at end of the list, and are tried last.
obj-$(CONFIG_COMPAT_BINFMT_ELF) += compat_binfmt_elf.o
+obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o
obj-$(CONFIG_BINFMT_ELF_FDPIC) += binfmt_elf_fdpic.o
obj-$(CONFIG_BINFMT_SOM) += binfmt_som.o
obj-$(CONFIG_BINFMT_FLAT) += binfmt_flat.o