]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/fileXX.c
malloc: Use overflow checking primitives where we do complex allocations
[grub2.git] / grub-core / commands / fileXX.c
CommitLineData
ec824e0f
VS
1/*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <grub/fileid.h>
20#include <grub/elfload.h>
f371dd5d 21#include <grub/misc.h>
ec824e0f 22
9246d5c8
VS
23#pragma GCC diagnostic ignored "-Wcast-align"
24
ec824e0f
VS
25int
26grub_file_check_netbsdXX (grub_elf_t elf)
27{
28 Elf_Shdr *s, *s0;
29
30 grub_size_t shnum = elf->ehdr.ehdrXX.e_shnum;
31 grub_size_t shentsize = elf->ehdr.ehdrXX.e_shentsize;
32 grub_size_t shsize = shnum * shentsize;
33 grub_off_t stroff;
34
35 if (!shnum || !shentsize)
36 return 0;
37
38 s0 = grub_malloc (shsize);
39 if (!s0)
40 return 0;
41
42 if (grub_file_seek (elf->file, elf->ehdr.ehdrXX.e_shoff) == (grub_off_t) -1)
95a8c918 43 goto fail;
ec824e0f
VS
44
45 if (grub_file_read (elf->file, s0, shsize) != (grub_ssize_t) shsize)
95a8c918 46 goto fail;
ec824e0f
VS
47
48 s = (Elf_Shdr *) ((char *) s0 + elf->ehdr.ehdrXX.e_shstrndx * shentsize);
49 stroff = s->sh_offset;
50
51 for (s = s0; s < (Elf_Shdr *) ((char *) s0 + shnum * shentsize);
52 s = (Elf_Shdr *) ((char *) s + shentsize))
53 {
54 char name[sizeof(".note.netbsd.ident")];
55 grub_memset (name, 0, sizeof (name));
56 if (grub_file_seek (elf->file, stroff + s->sh_name) == (grub_off_t) -1)
fb66b512 57 goto fail;
ec824e0f
VS
58
59 if (grub_file_read (elf->file, name, sizeof (name)) != (grub_ssize_t) sizeof (name))
60 {
61 if (grub_errno)
95a8c918 62 goto fail;
ec824e0f
VS
63 continue;
64 }
65 if (grub_memcmp (name, ".note.netbsd.ident",
66 sizeof(".note.netbsd.ident")) != 0)
67 continue;
95a8c918 68 grub_free (s0);
ec824e0f
VS
69 return 1;
70 }
95a8c918
VS
71 fail:
72 grub_free (s0);
ec824e0f
VS
73 return 0;
74}