|
18 | 18 | #include <linux/slab.h>
|
19 | 19 | #include <linux/soc/qcom/mdt_loader.h>
|
20 | 20 |
|
| 21 | +static bool mdt_header_valid(const struct firmware *fw) |
| 22 | +{ |
| 23 | + const struct elf32_hdr *ehdr; |
| 24 | + size_t phend; |
| 25 | + size_t shend; |
| 26 | + |
| 27 | + if (fw->size < sizeof(*ehdr)) |
| 28 | + return false; |
| 29 | + |
| 30 | + ehdr = (struct elf32_hdr *)fw->data; |
| 31 | + |
| 32 | + if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) |
| 33 | + return false; |
| 34 | + |
| 35 | + if (ehdr->e_phentsize != sizeof(struct elf32_phdr)) |
| 36 | + return -EINVAL; |
| 37 | + |
| 38 | + phend = size_add(size_mul(sizeof(struct elf32_phdr), ehdr->e_phnum), ehdr->e_phoff); |
| 39 | + if (phend > fw->size) |
| 40 | + return false; |
| 41 | + |
| 42 | + if (ehdr->e_shentsize != sizeof(struct elf32_shdr)) |
| 43 | + return -EINVAL; |
| 44 | + |
| 45 | + shend = size_add(size_mul(sizeof(struct elf32_shdr), ehdr->e_shnum), ehdr->e_shoff); |
| 46 | + if (shend > fw->size) |
| 47 | + return false; |
| 48 | + |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
21 | 52 | static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
|
22 | 53 | {
|
23 | 54 | if (phdr->p_type != PT_LOAD)
|
@@ -82,6 +113,9 @@ ssize_t qcom_mdt_get_size(const struct firmware *fw)
|
82 | 113 | phys_addr_t max_addr = 0;
|
83 | 114 | int i;
|
84 | 115 |
|
| 116 | + if (!mdt_header_valid(fw)) |
| 117 | + return -EINVAL; |
| 118 | + |
85 | 119 | ehdr = (struct elf32_hdr *)fw->data;
|
86 | 120 | phdrs = (struct elf32_phdr *)(ehdr + 1);
|
87 | 121 |
|
@@ -134,6 +168,9 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
|
134 | 168 | ssize_t ret;
|
135 | 169 | void *data;
|
136 | 170 |
|
| 171 | + if (!mdt_header_valid(fw)) |
| 172 | + return ERR_PTR(-EINVAL); |
| 173 | + |
137 | 174 | ehdr = (struct elf32_hdr *)fw->data;
|
138 | 175 | phdrs = (struct elf32_phdr *)(ehdr + 1);
|
139 | 176 |
|
@@ -214,6 +251,9 @@ int qcom_mdt_pas_init(struct device *dev, const struct firmware *fw,
|
214 | 251 | int ret;
|
215 | 252 | int i;
|
216 | 253 |
|
| 254 | + if (!mdt_header_valid(fw)) |
| 255 | + return -EINVAL; |
| 256 | + |
217 | 257 | ehdr = (struct elf32_hdr *)fw->data;
|
218 | 258 | phdrs = (struct elf32_phdr *)(ehdr + 1);
|
219 | 259 |
|
@@ -310,6 +350,9 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
|
310 | 350 | if (!fw || !mem_region || !mem_phys || !mem_size)
|
311 | 351 | return -EINVAL;
|
312 | 352 |
|
| 353 | + if (!mdt_header_valid(fw)) |
| 354 | + return -EINVAL; |
| 355 | + |
313 | 356 | is_split = qcom_mdt_bins_are_split(fw, fw_name);
|
314 | 357 | ehdr = (struct elf32_hdr *)fw->data;
|
315 | 358 | phdrs = (struct elf32_phdr *)(ehdr + 1);
|
|
0 commit comments