~ubuntu-branches/ubuntu/raring/xen/raring-security

« back to all changes in this revision

Viewing changes to debian/patches/xsa55-4.2-0015-libelf-use-only-unsigned-integers.patch

  • Committer: Package Import Robot
  • Author(s): Stefan Bader
  • Date: 2013-06-21 14:38:24 UTC
  • Revision ID: package-import@ubuntu.com-20130621143824-re720sb3yiz1fkux
Tags: 4.2.1-0ubuntu3.3
* Applying Xen Security Advisories:
  - CVE-2013-2194, CVE-2013-2195, CVE-2013-2196 / XSA55
    * libelf: abolish libelf-relocate.c
    * libxc: introduce xc_dom_seg_to_ptr_pages
    * libxc: Fix range checking in xc_dom_pfn_to_ptr etc.
    * libelf: add `struct elf_binary*' parameter to elf_load_image
    * libelf: abolish elf_sval and elf_access_signed
    * libelf: move include of <asm/guest_access.h> to top of file
    * libelf/xc_dom_load_elf_symtab: Do not use "syms" uninitialised
    * libelf: introduce macros for memory access and pointer handling
    * tools/xcutils/readnotes: adjust print_l1_mfn_valid_note
    * libelf: check nul-terminated strings properly
    * libelf: check all pointer accesses
    * libelf: Check pointer references in elf_is_elfbinary
    * libelf: Make all callers call elf_check_broken
    * libelf: use C99 bool for booleans
    * libelf: use only unsigned integers
    * libelf: check loops for running away
    * libelf: abolish obsolete macros
    * libxc: Add range checking to xc_dom_binloader
    * libxc: check failure of xc_dom_*_to_ptr, xc_map_foreign_range
    * libxc: check return values from malloc
    * libxc: range checks in xc_dom_p2m_host and _guest
    * libxc: check blob size before proceeding in xc_dom_check_gzip
    * libxc: Better range check in xc_dom_alloc_segment
  - CVE-XXXX-XXXX / XSA57
    * libxl: Restrict permissions on PV console device xenstore nodes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
From e673ca50127b6c1263727aa31de0b8bb966ca7a2 Mon Sep 17 00:00:00 2001
 
2
From: Ian Jackson <ian.jackson@eu.citrix.com>
 
3
Date: Fri, 14 Jun 2013 16:43:18 +0100
 
4
Subject: [PATCH 15/23] libelf: use only unsigned integers
 
5
 
 
6
Signed integers have undesirable undefined behaviours on overflow.
 
7
Malicious compilers can turn apparently-correct code into code with
 
8
security vulnerabilities etc.
 
9
 
 
10
So use only unsigned integers.  Exceptions are booleans (which we have
 
11
already changed) and error codes.
 
12
 
 
13
We _do_ change all the chars which aren't fixed constants from our own
 
14
text segment, but not the char*s.  This is because it is safe to
 
15
access an arbitrary byte through a char*, but not necessarily safe to
 
16
convert an arbitrary value to a char.
 
17
 
 
18
As a consequence we need to compile libelf with -Wno-pointer-sign.
 
19
 
 
20
It is OK to change all the signed integers to unsigned because all the
 
21
inequalities in libelf are in contexts where we don't "expect"
 
22
negative numbers.
 
23
 
 
24
In libelf-dominfo.c:elf_xen_parse we rename a variable "rc" to
 
25
"more_notes" as it actually contains a note count derived from the
 
26
input image.  The "error" return value from elf_xen_parse_notes is
 
27
changed from -1 to ~0U.
 
28
 
 
29
grepping shows only one occurrence of "PRId" or "%d" or "%ld" in
 
30
libelf and xc_dom_elfloader.c (a "%d" which becomes "%u").
 
31
 
 
32
This is part of the fix to a security issue, XSA-55.
 
33
 
 
34
For those concerned about unintentional functional changes, the
 
35
following rune produces a version of the patch which is much smaller
 
36
and eliminates only non-functional changes:
 
37
 
 
38
 GIT_EXTERNAL_DIFF=.../unsigned-differ git-diff <before>..<after>
 
39
 
 
40
where <before> and <after> are git refs for the code before and after
 
41
this patch, and unsigned-differ is this shell script:
 
42
 
 
43
    #!/bin/bash
 
44
    set -e
 
45
 
 
46
    seddery () {
 
47
            perl -pe 's/\b(?:elf_errorstatus|elf_negerrnoval)\b/int/g'
 
48
    }
 
49
 
 
50
    path="$1"
 
51
    in="$2"
 
52
    out="$5"
 
53
 
 
54
    set +e
 
55
    diff -pu --label "$path~" <(seddery <"$in") --label "$path" <(seddery <"$out")
 
56
    rc=$?
 
57
    set -e
 
58
    if [ $rc = 1 ]; then rc=0; fi
 
59
    exit $rc
 
60
 
 
61
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
 
62
---
 
63
 tools/libxc/Makefile               |    9 +++++-
 
64
 tools/libxc/xc_dom.h               |    7 +++--
 
65
 tools/libxc/xc_dom_elfloader.c     |   42 ++++++++++++++++-------------
 
66
 tools/xcutils/readnotes.c          |   15 +++++-----
 
67
 xen/common/libelf/Makefile         |    2 +
 
68
 xen/common/libelf/libelf-dominfo.c |   52 ++++++++++++++++++-----------------
 
69
 xen/common/libelf/libelf-loader.c  |   20 +++++++-------
 
70
 xen/common/libelf/libelf-tools.c   |   24 ++++++++--------
 
71
 xen/include/xen/libelf.h           |   21 ++++++++------
 
72
 9 files changed, 105 insertions(+), 87 deletions(-)
 
73
 
 
74
diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
 
75
index d8c6a60..a3fd90c 100644
 
76
--- a/tools/libxc/Makefile
 
77
+++ b/tools/libxc/Makefile
 
78
@@ -52,8 +52,13 @@ endif
 
79
 vpath %.c ../../xen/common/libelf
 
80
 CFLAGS += -I../../xen/common/libelf
 
81
 
 
82
-GUEST_SRCS-y += libelf-tools.c libelf-loader.c
 
83
-GUEST_SRCS-y += libelf-dominfo.c
 
84
+ELF_SRCS-y += libelf-tools.c libelf-loader.c
 
85
+ELF_SRCS-y += libelf-dominfo.c
 
86
+
 
87
+GUEST_SRCS-y += $(ELF_SRCS-y)
 
88
+
 
89
+$(patsubst %.c,%.o,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign
 
90
+$(patsubst %.c,%.opic,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign
 
91
 
 
92
 # new domain builder
 
93
 GUEST_SRCS-y                 += xc_dom_core.c xc_dom_boot.c
 
94
diff --git a/tools/libxc/xc_dom.h b/tools/libxc/xc_dom.h
 
95
index 9f8037e..0161459 100644
 
96
--- a/tools/libxc/xc_dom.h
 
97
+++ b/tools/libxc/xc_dom.h
 
98
@@ -140,9 +140,10 @@ struct xc_dom_image {
 
99
 
 
100
 struct xc_dom_loader {
 
101
     char *name;
 
102
-    int (*probe) (struct xc_dom_image * dom);
 
103
-    int (*parser) (struct xc_dom_image * dom);
 
104
-    int (*loader) (struct xc_dom_image * dom);
 
105
+    /* Sadly the error returns from these functions are not consistent: */
 
106
+    elf_negerrnoval (*probe) (struct xc_dom_image * dom);
 
107
+    elf_negerrnoval (*parser) (struct xc_dom_image * dom);
 
108
+    elf_errorstatus (*loader) (struct xc_dom_image * dom);
 
109
 
 
110
     struct xc_dom_loader *next;
 
111
 };
 
112
diff --git a/tools/libxc/xc_dom_elfloader.c b/tools/libxc/xc_dom_elfloader.c
 
113
index 9ba64ae..62a0d3b 100644
 
114
--- a/tools/libxc/xc_dom_elfloader.c
 
115
+++ b/tools/libxc/xc_dom_elfloader.c
 
116
@@ -84,7 +84,7 @@ static char *xc_dom_guest_type(struct xc_dom_image *dom,
 
117
 /* ------------------------------------------------------------------------ */
 
118
 /* parse elf binary                                                         */
 
119
 
 
120
-static int check_elf_kernel(struct xc_dom_image *dom, bool verbose)
 
121
+static elf_negerrnoval check_elf_kernel(struct xc_dom_image *dom, bool verbose)
 
122
 {
 
123
     if ( dom->kernel_blob == NULL )
 
124
     {
 
125
@@ -106,12 +106,12 @@ static int check_elf_kernel(struct xc_dom_image *dom, bool verbose)
 
126
     return 0;
 
127
 }
 
128
 
 
129
-static int xc_dom_probe_elf_kernel(struct xc_dom_image *dom)
 
130
+static elf_negerrnoval xc_dom_probe_elf_kernel(struct xc_dom_image *dom)
 
131
 {
 
132
     return check_elf_kernel(dom, 0);
 
133
 }
 
134
 
 
135
-static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
136
+static elf_errorstatus xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
137
                                   struct elf_binary *elf, bool load)
 
138
 {
 
139
     struct elf_binary syms;
 
140
@@ -119,7 +119,7 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
141
     xen_vaddr_t symtab, maxaddr;
 
142
     ELF_PTRVAL_CHAR hdr;
 
143
     size_t size;
 
144
-    int h, count, type, i, tables = 0;
 
145
+    unsigned h, count, type, i, tables = 0;
 
146
 
 
147
     if ( elf_swap(elf) )
 
148
     {
 
149
@@ -140,13 +140,13 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
150
         elf->caller_xdest_base = hdr_ptr;
 
151
         elf->caller_xdest_size = allow_size;
 
152
         hdr = ELF_REALPTR2PTRVAL(hdr_ptr);
 
153
-        elf_store_val(elf, int, hdr, size - sizeof(int));
 
154
+        elf_store_val(elf, unsigned, hdr, size - sizeof(unsigned));
 
155
     }
 
156
     else
 
157
     {
 
158
         char *hdr_ptr;
 
159
 
 
160
-        size = sizeof(int) + elf_size(elf, elf->ehdr) +
 
161
+        size = sizeof(unsigned) + elf_size(elf, elf->ehdr) +
 
162
             elf_shdr_count(elf) * elf_size(elf, shdr);
 
163
         hdr_ptr = xc_dom_malloc(dom, size);
 
164
         if ( hdr_ptr == NULL )
 
165
@@ -157,15 +157,15 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
166
         dom->bsd_symtab_start = elf_round_up(elf, dom->kernel_seg.vend);
 
167
     }
 
168
 
 
169
-    elf_memcpy_safe(elf, hdr + sizeof(int),
 
170
+    elf_memcpy_safe(elf, hdr + sizeof(unsigned),
 
171
            ELF_IMAGE_BASE(elf),
 
172
            elf_size(elf, elf->ehdr));
 
173
-    elf_memcpy_safe(elf, hdr + sizeof(int) + elf_size(elf, elf->ehdr),
 
174
+    elf_memcpy_safe(elf, hdr + sizeof(unsigned) + elf_size(elf, elf->ehdr),
 
175
            ELF_IMAGE_BASE(elf) + elf_uval(elf, elf->ehdr, e_shoff),
 
176
            elf_shdr_count(elf) * elf_size(elf, shdr));
 
177
     if ( elf_64bit(elf) )
 
178
     {
 
179
-        Elf64_Ehdr *ehdr = (Elf64_Ehdr *)(hdr + sizeof(int));
 
180
+        Elf64_Ehdr *ehdr = (Elf64_Ehdr *)(hdr + sizeof(unsigned));
 
181
         ehdr->e_phoff = 0;
 
182
         ehdr->e_phentsize = 0;
 
183
         ehdr->e_phnum = 0;
 
184
@@ -174,22 +174,22 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
185
     }
 
186
     else
 
187
     {
 
188
-        Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(hdr + sizeof(int));
 
189
+        Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(hdr + sizeof(unsigned));
 
190
         ehdr->e_phoff = 0;
 
191
         ehdr->e_phentsize = 0;
 
192
         ehdr->e_phnum = 0;
 
193
         ehdr->e_shoff = elf_size(elf, elf->ehdr);
 
194
         ehdr->e_shstrndx = SHN_UNDEF;
 
195
     }
 
196
-    if ( elf->caller_xdest_size < sizeof(int) )
 
197
+    if ( elf->caller_xdest_size < sizeof(unsigned) )
 
198
     {
 
199
         DOMPRINTF("%s/%s: header size %"PRIx64" too small",
 
200
                   __FUNCTION__, load ? "load" : "parse",
 
201
                   (uint64_t)elf->caller_xdest_size);
 
202
         return -1;
 
203
     }
 
204
-    if ( elf_init(&syms, elf->caller_xdest_base + sizeof(int),
 
205
-                  elf->caller_xdest_size - sizeof(int)) )
 
206
+    if ( elf_init(&syms, elf->caller_xdest_base + sizeof(unsigned),
 
207
+                  elf->caller_xdest_size - sizeof(unsigned)) )
 
208
         return -1;
 
209
 
 
210
     /*
 
211
@@ -209,7 +209,7 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
212
 
 
213
     xc_elf_set_logfile(dom->xch, &syms, 1);
 
214
 
 
215
-    symtab = dom->bsd_symtab_start + sizeof(int);
 
216
+    symtab = dom->bsd_symtab_start + sizeof(unsigned);
 
217
     maxaddr = elf_round_up(&syms, symtab + elf_size(&syms, syms.ehdr) +
 
218
                            elf_shdr_count(&syms) * elf_size(&syms, shdr));
 
219
 
 
220
@@ -255,7 +255,7 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
221
             size = elf_uval(&syms, shdr, sh_size);
 
222
             maxaddr = elf_round_up(&syms, maxaddr + size);
 
223
             tables++;
 
224
-            DOMPRINTF("%s: h=%d %s, size=0x%zx, maxaddr=0x%" PRIx64 "",
 
225
+            DOMPRINTF("%s: h=%u %s, size=0x%zx, maxaddr=0x%" PRIx64 "",
 
226
                       __FUNCTION__, h,
 
227
                       type == SHT_SYMTAB ? "symtab" : "strtab",
 
228
                       size, maxaddr);
 
229
@@ -294,10 +294,14 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
 
230
     return 0;
 
231
 }
 
232
 
 
233
-static int xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
 
234
+static elf_errorstatus xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
 
235
+    /*
 
236
+     * This function sometimes returns -1 for error and sometimes
 
237
+     * an errno value.  ?!?!
 
238
+     */
 
239
 {
 
240
     struct elf_binary *elf;
 
241
-    int rc;
 
242
+    elf_errorstatus rc;
 
243
 
 
244
     rc = check_elf_kernel(dom, 1);
 
245
     if ( rc != 0 )
 
246
@@ -358,10 +362,10 @@ out:
 
247
     return rc;
 
248
 }
 
249
 
 
250
-static int xc_dom_load_elf_kernel(struct xc_dom_image *dom)
 
251
+static elf_errorstatus xc_dom_load_elf_kernel(struct xc_dom_image *dom)
 
252
 {
 
253
     struct elf_binary *elf = dom->private_loader;
 
254
-    int rc;
 
255
+    elf_errorstatus rc;
 
256
     xen_pfn_t pages;
 
257
 
 
258
     elf->dest_base = xc_dom_seg_to_ptr_pages(dom, &dom->kernel_seg, &pages);
 
259
diff --git a/tools/xcutils/readnotes.c b/tools/xcutils/readnotes.c
 
260
index d1f7a30..2ca7732 100644
 
261
--- a/tools/xcutils/readnotes.c
 
262
+++ b/tools/xcutils/readnotes.c
 
263
@@ -70,7 +70,7 @@ static void print_numeric_note(const char *prefix, struct elf_binary *elf,
 
264
                               ELF_HANDLE_DECL(elf_note) note)
 
265
 {
 
266
        uint64_t value = elf_note_numeric(elf, note);
 
267
-       int descsz = elf_uval(elf, note, descsz);
 
268
+       unsigned descsz = elf_uval(elf, note, descsz);
 
269
 
 
270
        printf("%s: %#*" PRIx64 " (%d bytes)\n",
 
271
               prefix, 2+2*descsz, value, descsz);
 
272
@@ -79,7 +79,7 @@ static void print_numeric_note(const char *prefix, struct elf_binary *elf,
 
273
 static void print_l1_mfn_valid_note(const char *prefix, struct elf_binary *elf,
 
274
                                    ELF_HANDLE_DECL(elf_note) note)
 
275
 {
 
276
-       int descsz = elf_uval(elf, note, descsz);
 
277
+       unsigned descsz = elf_uval(elf, note, descsz);
 
278
        ELF_PTRVAL_CONST_VOID desc = elf_note_desc(elf, note);
 
279
 
 
280
        /* XXX should be able to cope with a list of values. */
 
281
@@ -99,10 +99,10 @@ static void print_l1_mfn_valid_note(const char *prefix, struct elf_binary *elf,
 
282
 
 
283
 }
 
284
 
 
285
-static int print_notes(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) start, ELF_HANDLE_DECL(elf_note) end)
 
286
+static unsigned print_notes(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) start, ELF_HANDLE_DECL(elf_note) end)
 
287
 {
 
288
        ELF_HANDLE_DECL(elf_note) note;
 
289
-       int notes_found = 0;
 
290
+       unsigned notes_found = 0;
 
291
        const char *this_note_name;
 
292
 
 
293
        for ( note = start; ELF_HANDLE_PTRVAL(note) < ELF_HANDLE_PTRVAL(end); note = elf_note_next(elf, note) )
 
294
@@ -161,7 +161,7 @@ static int print_notes(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) start,
 
295
                        break;
 
296
                default:
 
297
                        printf("unknown note type %#x\n",
 
298
-                              (int)elf_uval(elf, note, type));
 
299
+                              (unsigned)elf_uval(elf, note, type));
 
300
                        break;
 
301
                }
 
302
        }
 
303
@@ -171,12 +171,13 @@ static int print_notes(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) start,
 
304
 int main(int argc, char **argv)
 
305
 {
 
306
        const char *f;
 
307
-       int fd,h,size,usize,count;
 
308
+       int fd;
 
309
+       unsigned h,size,usize,count;
 
310
        void *image,*tmp;
 
311
        struct stat st;
 
312
        struct elf_binary elf;
 
313
        ELF_HANDLE_DECL(elf_shdr) shdr;
 
314
-       int notes_found = 0;
 
315
+       unsigned notes_found = 0;
 
316
 
 
317
        struct setup_header *hdr;
 
318
        uint64_t payload_offset, payload_length;
 
319
diff --git a/xen/common/libelf/Makefile b/xen/common/libelf/Makefile
 
320
index 18dc8e2..5bf8f76 100644
 
321
--- a/xen/common/libelf/Makefile
 
322
+++ b/xen/common/libelf/Makefile
 
323
@@ -2,6 +2,8 @@ obj-bin-y := libelf.o
 
324
 
 
325
 SECTIONS := text data $(SPECIAL_DATA_SECTIONS)
 
326
 
 
327
+CFLAGS += -Wno-pointer-sign
 
328
+
 
329
 libelf.o: libelf-temp.o Makefile
 
330
        $(OBJCOPY) $(foreach s,$(SECTIONS),--rename-section .$(s)=.init.$(s)) $< $@
 
331
 
 
332
diff --git a/xen/common/libelf/libelf-dominfo.c b/xen/common/libelf/libelf-dominfo.c
 
333
index 12b6c2a..cdd0d31 100644
 
334
--- a/xen/common/libelf/libelf-dominfo.c
 
335
+++ b/xen/common/libelf/libelf-dominfo.c
 
336
@@ -29,15 +29,15 @@ static const char *const elf_xen_feature_names[] = {
 
337
     [XENFEAT_pae_pgdir_above_4gb] = "pae_pgdir_above_4gb",
 
338
     [XENFEAT_dom0] = "dom0"
 
339
 };
 
340
-static const int elf_xen_features =
 
341
+static const unsigned elf_xen_features =
 
342
 sizeof(elf_xen_feature_names) / sizeof(elf_xen_feature_names[0]);
 
343
 
 
344
-int elf_xen_parse_features(const char *features,
 
345
+elf_errorstatus elf_xen_parse_features(const char *features,
 
346
                            uint32_t *supported,
 
347
                            uint32_t *required)
 
348
 {
 
349
-    char feature[64];
 
350
-    int pos, len, i;
 
351
+    unsigned char feature[64];
 
352
+    unsigned pos, len, i;
 
353
 
 
354
     if ( features == NULL )
 
355
         return 0;
 
356
@@ -94,7 +94,7 @@ int elf_xen_parse_features(const char *features,
 
357
 /* ------------------------------------------------------------------------ */
 
358
 /* xen elf notes                                                            */
 
359
 
 
360
-int elf_xen_parse_note(struct elf_binary *elf,
 
361
+elf_errorstatus elf_xen_parse_note(struct elf_binary *elf,
 
362
                        struct elf_dom_parms *parms,
 
363
                        ELF_HANDLE_DECL(elf_note) note)
 
364
 {
 
365
@@ -125,7 +125,7 @@ int elf_xen_parse_note(struct elf_binary *elf,
 
366
     const char *str = NULL;
 
367
     uint64_t val = 0;
 
368
     unsigned int i;
 
369
-    int type = elf_uval(elf, note, type);
 
370
+    unsigned type = elf_uval(elf, note, type);
 
371
 
 
372
     if ( (type >= sizeof(note_desc) / sizeof(note_desc[0])) ||
 
373
          (note_desc[type].name == NULL) )
 
374
@@ -216,12 +216,14 @@ int elf_xen_parse_note(struct elf_binary *elf,
 
375
     return 0;
 
376
 }
 
377
 
 
378
-static int elf_xen_parse_notes(struct elf_binary *elf,
 
379
+#define ELF_NOTE_INVALID (~0U)
 
380
+
 
381
+static unsigned elf_xen_parse_notes(struct elf_binary *elf,
 
382
                                struct elf_dom_parms *parms,
 
383
                                ELF_PTRVAL_CONST_VOID start,
 
384
                                ELF_PTRVAL_CONST_VOID end)
 
385
 {
 
386
-    int xen_elfnotes = 0;
 
387
+    unsigned xen_elfnotes = 0;
 
388
     ELF_HANDLE_DECL(elf_note) note;
 
389
     const char *note_name;
 
390
 
 
391
@@ -237,7 +239,7 @@ static int elf_xen_parse_notes(struct elf_binary *elf,
 
392
         if ( strcmp(note_name, "Xen") )
 
393
             continue;
 
394
         if ( elf_xen_parse_note(elf, parms, note) )
 
395
-            return -1;
 
396
+            return ELF_NOTE_INVALID;
 
397
         xen_elfnotes++;
 
398
     }
 
399
     return xen_elfnotes;
 
400
@@ -246,12 +248,12 @@ static int elf_xen_parse_notes(struct elf_binary *elf,
 
401
 /* ------------------------------------------------------------------------ */
 
402
 /* __xen_guest section                                                      */
 
403
 
 
404
-int elf_xen_parse_guest_info(struct elf_binary *elf,
 
405
+elf_errorstatus elf_xen_parse_guest_info(struct elf_binary *elf,
 
406
                              struct elf_dom_parms *parms)
 
407
 {
 
408
     ELF_PTRVAL_CONST_CHAR h;
 
409
-    char name[32], value[128];
 
410
-    int len;
 
411
+    unsigned char name[32], value[128];
 
412
+    unsigned len;
 
413
 
 
414
     h = parms->guest_info;
 
415
 #define STAR(h) (elf_access_unsigned(elf, (h), 0, 1))
 
416
@@ -334,13 +336,13 @@ int elf_xen_parse_guest_info(struct elf_binary *elf,
 
417
 /* ------------------------------------------------------------------------ */
 
418
 /* sanity checks                                                            */
 
419
 
 
420
-static int elf_xen_note_check(struct elf_binary *elf,
 
421
+static elf_errorstatus elf_xen_note_check(struct elf_binary *elf,
 
422
                               struct elf_dom_parms *parms)
 
423
 {
 
424
     if ( (ELF_PTRVAL_INVALID(parms->elf_note_start)) &&
 
425
          (ELF_PTRVAL_INVALID(parms->guest_info)) )
 
426
     {
 
427
-        int machine = elf_uval(elf, elf->ehdr, e_machine);
 
428
+        unsigned machine = elf_uval(elf, elf->ehdr, e_machine);
 
429
         if ( (machine == EM_386) || (machine == EM_X86_64) )
 
430
         {
 
431
             elf_err(elf, "%s: ERROR: Not a Xen-ELF image: "
 
432
@@ -378,7 +380,7 @@ static int elf_xen_note_check(struct elf_binary *elf,
 
433
     return 0;
 
434
 }
 
435
 
 
436
-static int elf_xen_addr_calc_check(struct elf_binary *elf,
 
437
+static elf_errorstatus elf_xen_addr_calc_check(struct elf_binary *elf,
 
438
                                    struct elf_dom_parms *parms)
 
439
 {
 
440
     if ( (parms->elf_paddr_offset != UNSET_ADDR) &&
 
441
@@ -464,13 +466,13 @@ static int elf_xen_addr_calc_check(struct elf_binary *elf,
 
442
 /* ------------------------------------------------------------------------ */
 
443
 /* glue it all together ...                                                 */
 
444
 
 
445
-int elf_xen_parse(struct elf_binary *elf,
 
446
+elf_errorstatus elf_xen_parse(struct elf_binary *elf,
 
447
                   struct elf_dom_parms *parms)
 
448
 {
 
449
     ELF_HANDLE_DECL(elf_shdr) shdr;
 
450
     ELF_HANDLE_DECL(elf_phdr) phdr;
 
451
-    int xen_elfnotes = 0;
 
452
-    int i, count, rc;
 
453
+    unsigned xen_elfnotes = 0;
 
454
+    unsigned i, count, more_notes;
 
455
 
 
456
     elf_memset_unchecked(parms, 0, sizeof(*parms));
 
457
     parms->virt_base = UNSET_ADDR;
 
458
@@ -495,13 +497,13 @@ int elf_xen_parse(struct elf_binary *elf,
 
459
         if (elf_uval(elf, phdr, p_offset) == 0)
 
460
              continue;
 
461
 
 
462
-        rc = elf_xen_parse_notes(elf, parms,
 
463
+        more_notes = elf_xen_parse_notes(elf, parms,
 
464
                                  elf_segment_start(elf, phdr),
 
465
                                  elf_segment_end(elf, phdr));
 
466
-        if ( rc == -1 )
 
467
+        if ( more_notes == ELF_NOTE_INVALID )
 
468
             return -1;
 
469
 
 
470
-        xen_elfnotes += rc;
 
471
+        xen_elfnotes += more_notes;
 
472
     }
 
473
 
 
474
     /*
 
475
@@ -518,17 +520,17 @@ int elf_xen_parse(struct elf_binary *elf,
 
476
             if ( elf_uval(elf, shdr, sh_type) != SHT_NOTE )
 
477
                 continue;
 
478
 
 
479
-            rc = elf_xen_parse_notes(elf, parms,
 
480
+            more_notes = elf_xen_parse_notes(elf, parms,
 
481
                                      elf_section_start(elf, shdr),
 
482
                                      elf_section_end(elf, shdr));
 
483
 
 
484
-            if ( rc == -1 )
 
485
+            if ( more_notes == ELF_NOTE_INVALID )
 
486
                 return -1;
 
487
 
 
488
-            if ( xen_elfnotes == 0 && rc > 0 )
 
489
+            if ( xen_elfnotes == 0 && more_notes > 0 )
 
490
                 elf_msg(elf, "%s: using notes from SHT_NOTE section\n", __FUNCTION__);
 
491
 
 
492
-            xen_elfnotes += rc;
 
493
+            xen_elfnotes += more_notes;
 
494
         }
 
495
 
 
496
     }
 
497
diff --git a/xen/common/libelf/libelf-loader.c b/xen/common/libelf/libelf-loader.c
 
498
index 0dccd4d..c3a9e51 100644
 
499
--- a/xen/common/libelf/libelf-loader.c
 
500
+++ b/xen/common/libelf/libelf-loader.c
 
501
@@ -24,7 +24,7 @@
 
502
 
 
503
 /* ------------------------------------------------------------------------ */
 
504
 
 
505
-int elf_init(struct elf_binary *elf, const char *image_input, size_t size)
 
506
+elf_errorstatus elf_init(struct elf_binary *elf, const char *image_input, size_t size)
 
507
 {
 
508
     ELF_HANDLE_DECL(elf_shdr) shdr;
 
509
     uint64_t i, count, section, offset;
 
510
@@ -114,7 +114,7 @@ void elf_set_log(struct elf_binary *elf, elf_log_callback *log_callback,
 
511
     elf->verbose = verbose;
 
512
 }
 
513
 
 
514
-static int elf_load_image(struct elf_binary *elf,
 
515
+static elf_errorstatus elf_load_image(struct elf_binary *elf,
 
516
                           ELF_PTRVAL_VOID dst, ELF_PTRVAL_CONST_VOID src,
 
517
                           uint64_t filesz, uint64_t memsz)
 
518
 {
 
519
@@ -129,9 +129,9 @@ void elf_set_verbose(struct elf_binary *elf)
 
520
     elf->verbose = 1;
 
521
 }
 
522
 
 
523
-static int elf_load_image(struct elf_binary *elf, ELF_PTRVAL_VOID dst, ELF_PTRVAL_CONST_VOID src, uint64_t filesz, uint64_t memsz)
 
524
+static elf_errorstatus elf_load_image(struct elf_binary *elf, ELF_PTRVAL_VOID dst, ELF_PTRVAL_CONST_VOID src, uint64_t filesz, uint64_t memsz)
 
525
 {
 
526
-    int rc;
 
527
+    elf_errorstatus rc;
 
528
     if ( filesz > ULONG_MAX || memsz > ULONG_MAX )
 
529
         return -1;
 
530
     /* We trust the dom0 kernel image completely, so we don't care
 
531
@@ -151,7 +151,7 @@ void elf_parse_bsdsyms(struct elf_binary *elf, uint64_t pstart)
 
532
 {
 
533
     uint64_t sz;
 
534
     ELF_HANDLE_DECL(elf_shdr) shdr;
 
535
-    int i, type;
 
536
+    unsigned i, type;
 
537
 
 
538
     if ( !ELF_HANDLE_VALID(elf->sym_tab) )
 
539
         return;
 
540
@@ -187,7 +187,7 @@ static void elf_load_bsdsyms(struct elf_binary *elf)
 
541
     ELF_PTRVAL_VOID symbase;
 
542
     ELF_PTRVAL_VOID symtab_addr;
 
543
     ELF_HANDLE_DECL_NONCONST(elf_shdr) shdr;
 
544
-    int i, type;
 
545
+    unsigned i, type;
 
546
 
 
547
     if ( !elf->bsd_symtab_pstart )
 
548
         return;
 
549
@@ -220,7 +220,7 @@ do {                                            \
 
550
     elf_memcpy_safe(elf, ELF_HANDLE_PTRVAL(shdr),
 
551
                     ELF_IMAGE_BASE(elf) + elf_uval(elf, elf->ehdr, e_shoff),
 
552
                     sz);
 
553
-    maxva = ELF_OBSOLETE_VOIDP_CAST elf_round_up(elf, (long)maxva + sz);
 
554
+    maxva = ELF_OBSOLETE_VOIDP_CAST elf_round_up(elf, (unsigned long)maxva + sz);
 
555
 
 
556
     for ( i = 0; i < elf_shdr_count(elf); i++ )
 
557
     {
 
558
@@ -233,10 +233,10 @@ do {                                            \
 
559
              elf_memcpy_safe(elf, maxva, elf_section_start(elf, shdr), sz);
 
560
              /* Mangled to be based on ELF header location. */
 
561
              elf_hdr_elm(elf, shdr, sh_offset, maxva - symtab_addr);
 
562
-             maxva = ELF_OBSOLETE_VOIDP_CAST elf_round_up(elf, (long)maxva + sz);
 
563
+             maxva = ELF_OBSOLETE_VOIDP_CAST elf_round_up(elf, (unsigned long)maxva + sz);
 
564
         }
 
565
         shdr = ELF_MAKE_HANDLE(elf_shdr, ELF_HANDLE_PTRVAL(shdr) +
 
566
-                            (long)elf_uval(elf, elf->ehdr, e_shentsize));
 
567
+                            (unsigned long)elf_uval(elf, elf->ehdr, e_shentsize));
 
568
     }
 
569
 
 
570
     /* Write down the actual sym size. */
 
571
@@ -273,7 +273,7 @@ void elf_parse_binary(struct elf_binary *elf)
 
572
             __FUNCTION__, elf->pstart, elf->pend);
 
573
 }
 
574
 
 
575
-int elf_load_binary(struct elf_binary *elf)
 
576
+elf_errorstatus elf_load_binary(struct elf_binary *elf)
 
577
 {
 
578
     ELF_HANDLE_DECL(elf_phdr) phdr;
 
579
     uint64_t i, count, paddr, offset, filesz, memsz;
 
580
diff --git a/xen/common/libelf/libelf-tools.c b/xen/common/libelf/libelf-tools.c
 
581
index fa58f76..46d4ab1 100644
 
582
--- a/xen/common/libelf/libelf-tools.c
 
583
+++ b/xen/common/libelf/libelf-tools.c
 
584
@@ -122,19 +122,19 @@ uint64_t elf_access_unsigned(struct elf_binary * elf, elf_ptrval base,
 
585
 
 
586
 uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr)
 
587
 {
 
588
-    int elf_round = (elf_64bit(elf) ? 8 : 4) - 1;
 
589
+    uint64_t elf_round = (elf_64bit(elf) ? 8 : 4) - 1;
 
590
 
 
591
     return (addr + elf_round) & ~elf_round;
 
592
 }
 
593
 
 
594
 /* ------------------------------------------------------------------------ */
 
595
 
 
596
-int elf_shdr_count(struct elf_binary *elf)
 
597
+unsigned elf_shdr_count(struct elf_binary *elf)
 
598
 {
 
599
     return elf_uval(elf, elf->ehdr, e_shnum);
 
600
 }
 
601
 
 
602
-int elf_phdr_count(struct elf_binary *elf)
 
603
+unsigned elf_phdr_count(struct elf_binary *elf)
 
604
 {
 
605
     return elf_uval(elf, elf->ehdr, e_phnum);
 
606
 }
 
607
@@ -144,7 +144,7 @@ ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_name(struct elf_binary *elf, const char *n
 
608
     uint64_t count = elf_shdr_count(elf);
 
609
     ELF_HANDLE_DECL(elf_shdr) shdr;
 
610
     const char *sname;
 
611
-    int i;
 
612
+    unsigned i;
 
613
 
 
614
     for ( i = 0; i < count; i++ )
 
615
     {
 
616
@@ -156,7 +156,7 @@ ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_name(struct elf_binary *elf, const char *n
 
617
     return ELF_INVALID_HANDLE(elf_shdr);
 
618
 }
 
619
 
 
620
-ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, int index)
 
621
+ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, unsigned index)
 
622
 {
 
623
     uint64_t count = elf_shdr_count(elf);
 
624
     ELF_PTRVAL_CONST_VOID ptr;
 
625
@@ -170,7 +170,7 @@ ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, int index)
 
626
     return ELF_MAKE_HANDLE(elf_shdr, ptr);
 
627
 }
 
628
 
 
629
-ELF_HANDLE_DECL(elf_phdr) elf_phdr_by_index(struct elf_binary *elf, int index)
 
630
+ELF_HANDLE_DECL(elf_phdr) elf_phdr_by_index(struct elf_binary *elf, unsigned index)
 
631
 {
 
632
     uint64_t count = elf_uval(elf, elf->ehdr, e_phnum);
 
633
     ELF_PTRVAL_CONST_VOID ptr;
 
634
@@ -264,7 +264,7 @@ ELF_HANDLE_DECL(elf_sym) elf_sym_by_name(struct elf_binary *elf, const char *sym
 
635
     return ELF_INVALID_HANDLE(elf_sym);
 
636
 }
 
637
 
 
638
-ELF_HANDLE_DECL(elf_sym) elf_sym_by_index(struct elf_binary *elf, int index)
 
639
+ELF_HANDLE_DECL(elf_sym) elf_sym_by_index(struct elf_binary *elf, unsigned index)
 
640
 {
 
641
     ELF_PTRVAL_CONST_VOID ptr = elf_section_start(elf, elf->sym_tab);
 
642
     ELF_HANDLE_DECL(elf_sym) sym;
 
643
@@ -280,7 +280,7 @@ const char *elf_note_name(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note
 
644
 
 
645
 ELF_PTRVAL_CONST_VOID elf_note_desc(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note)
 
646
 {
 
647
-    int namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
 
648
+    unsigned namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
 
649
 
 
650
     return ELF_HANDLE_PTRVAL(note) + elf_size(elf, note) + namesz;
 
651
 }
 
652
@@ -288,7 +288,7 @@ ELF_PTRVAL_CONST_VOID elf_note_desc(struct elf_binary *elf, ELF_HANDLE_DECL(elf_
 
653
 uint64_t elf_note_numeric(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note)
 
654
 {
 
655
     ELF_PTRVAL_CONST_VOID desc = elf_note_desc(elf, note);
 
656
-    int descsz = elf_uval(elf, note, descsz);
 
657
+    unsigned descsz = elf_uval(elf, note, descsz);
 
658
 
 
659
     switch (descsz)
 
660
     {
 
661
@@ -306,7 +306,7 @@ uint64_t elf_note_numeric_array(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note
 
662
                                 unsigned int unitsz, unsigned int idx)
 
663
 {
 
664
     ELF_PTRVAL_CONST_VOID desc = elf_note_desc(elf, note);
 
665
-    int descsz = elf_uval(elf, note, descsz);
 
666
+    unsigned descsz = elf_uval(elf, note, descsz);
 
667
 
 
668
     if ( descsz % unitsz || idx >= descsz / unitsz )
 
669
         return 0;
 
670
@@ -324,8 +324,8 @@ uint64_t elf_note_numeric_array(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note
 
671
 
 
672
 ELF_HANDLE_DECL(elf_note) elf_note_next(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note)
 
673
 {
 
674
-    int namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
 
675
-    int descsz = (elf_uval(elf, note, descsz) + 3) & ~3;
 
676
+    unsigned namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
 
677
+    unsigned descsz = (elf_uval(elf, note, descsz) + 3) & ~3;
 
678
 
 
679
     return ELF_MAKE_HANDLE(elf_note, ELF_HANDLE_PTRVAL(note) + elf_size(elf, note) + namesz + descsz);
 
680
 }
 
681
diff --git a/xen/include/xen/libelf.h b/xen/include/xen/libelf.h
 
682
index 951430f..87e126a 100644
 
683
--- a/xen/include/xen/libelf.h
 
684
+++ b/xen/include/xen/libelf.h
 
685
@@ -31,6 +31,9 @@
 
686
 
 
687
 #include <stdbool.h>
 
688
 
 
689
+typedef int elf_errorstatus; /* 0: ok; -ve (normally -1): error */
 
690
+typedef int elf_negerrnoval; /* 0: ok; -EFOO: error */
 
691
+
 
692
 #undef ELFSIZE
 
693
 #include "elfstructs.h"
 
694
 #ifdef __XEN__
 
695
@@ -328,12 +331,12 @@ bool elf_access_ok(struct elf_binary * elf,
 
696
 /* ------------------------------------------------------------------------ */
 
697
 /* xc_libelf_tools.c                                                        */
 
698
 
 
699
-int elf_shdr_count(struct elf_binary *elf);
 
700
-int elf_phdr_count(struct elf_binary *elf);
 
701
+unsigned elf_shdr_count(struct elf_binary *elf);
 
702
+unsigned elf_phdr_count(struct elf_binary *elf);
 
703
 
 
704
 ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_name(struct elf_binary *elf, const char *name);
 
705
-ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, int index);
 
706
-ELF_HANDLE_DECL(elf_phdr) elf_phdr_by_index(struct elf_binary *elf, int index);
 
707
+ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, unsigned index);
 
708
+ELF_HANDLE_DECL(elf_phdr) elf_phdr_by_index(struct elf_binary *elf, unsigned index);
 
709
 
 
710
 const char *elf_section_name(struct elf_binary *elf, ELF_HANDLE_DECL(elf_shdr) shdr); /* might return NULL if inputs are invalid */
 
711
 ELF_PTRVAL_CONST_VOID elf_section_start(struct elf_binary *elf, ELF_HANDLE_DECL(elf_shdr) shdr);
 
712
@@ -343,7 +346,7 @@ ELF_PTRVAL_CONST_VOID elf_segment_start(struct elf_binary *elf, ELF_HANDLE_DECL(
 
713
 ELF_PTRVAL_CONST_VOID elf_segment_end(struct elf_binary *elf, ELF_HANDLE_DECL(elf_phdr) phdr);
 
714
 
 
715
 ELF_HANDLE_DECL(elf_sym) elf_sym_by_name(struct elf_binary *elf, const char *symbol);
 
716
-ELF_HANDLE_DECL(elf_sym) elf_sym_by_index(struct elf_binary *elf, int index);
 
717
+ELF_HANDLE_DECL(elf_sym) elf_sym_by_index(struct elf_binary *elf, unsigned index);
 
718
 
 
719
 const char *elf_note_name(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note); /* may return NULL */
 
720
 ELF_PTRVAL_CONST_VOID elf_note_desc(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note);
 
721
@@ -360,7 +363,7 @@ bool elf_phdr_is_loadable(struct elf_binary *elf, ELF_HANDLE_DECL(elf_phdr) phdr
 
722
 /* ------------------------------------------------------------------------ */
 
723
 /* xc_libelf_loader.c                                                       */
 
724
 
 
725
-int elf_init(struct elf_binary *elf, const char *image, size_t size);
 
726
+elf_errorstatus elf_init(struct elf_binary *elf, const char *image, size_t size);
 
727
   /*
 
728
    * image and size must be correct.  They will be recorded in
 
729
    * *elf, and must remain valid while the elf is in use.
 
730
@@ -373,7 +376,7 @@ void elf_set_log(struct elf_binary *elf, elf_log_callback*,
 
731
 #endif
 
732
 
 
733
 void elf_parse_binary(struct elf_binary *elf);
 
734
-int elf_load_binary(struct elf_binary *elf);
 
735
+elf_errorstatus elf_load_binary(struct elf_binary *elf);
 
736
 
 
737
 ELF_PTRVAL_VOID elf_get_ptr(struct elf_binary *elf, unsigned long addr);
 
738
 uint64_t elf_lookup_addr(struct elf_binary *elf, const char *symbol);
 
739
@@ -386,7 +389,7 @@ const char *elf_check_broken(const struct elf_binary *elf); /* NULL means OK */
 
740
 /* ------------------------------------------------------------------------ */
 
741
 /* xc_libelf_relocate.c                                                     */
 
742
 
 
743
-int elf_reloc(struct elf_binary *elf);
 
744
+elf_errorstatus elf_reloc(struct elf_binary *elf);
 
745
 
 
746
 /* ------------------------------------------------------------------------ */
 
747
 /* xc_libelf_dominfo.c                                                      */
 
748
@@ -420,7 +423,7 @@ struct elf_dom_parms {
 
749
     char guest_ver[16];
 
750
     char xen_ver[16];
 
751
     char loader[16];
 
752
-    int pae;
 
753
+    int pae; /* some kind of enum apparently */
 
754
     bool bsd_symtab;
 
755
     uint64_t virt_base;
 
756
     uint64_t virt_entry;
 
757
-- 
 
758
1.7.2.5
 
759