dl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /* dl.c - loadable module support */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* Force native word size */
  20. #define GRUB_TARGET_WORDSIZE (8 * GRUB_CPU_SIZEOF_VOID_P)
  21. #include <config.h>
  22. #include <grub/elf.h>
  23. #include <grub/dl.h>
  24. #include <grub/misc.h>
  25. #include <grub/mm.h>
  26. #include <grub/err.h>
  27. #include <grub/types.h>
  28. #include <grub/symbol.h>
  29. #include <grub/file.h>
  30. #include <grub/env.h>
  31. #include <grub/cache.h>
  32. #include <grub/i18n.h>
  33. #ifdef GRUB_MACHINE_EFI
  34. #include <grub/efi/memory.h>
  35. #endif
  36. /* Platforms where modules are in a readonly area of memory. */
  37. #if defined(GRUB_MACHINE_QEMU)
  38. #define GRUB_MODULES_MACHINE_READONLY
  39. #endif
  40. #ifdef GRUB_MACHINE_EFI
  41. #define DL_ALIGN GRUB_EFI_PAGE_SIZE
  42. #else
  43. #define DL_ALIGN 1
  44. #endif
  45. #pragma GCC diagnostic ignored "-Wcast-align"
  46. grub_dl_t grub_dl_head = 0;
  47. grub_err_t
  48. grub_dl_add (grub_dl_t mod);
  49. /* Keep global so that GDB scripts work. */
  50. grub_err_t
  51. grub_dl_add (grub_dl_t mod)
  52. {
  53. if (grub_dl_get (mod->name))
  54. return grub_error (GRUB_ERR_BAD_MODULE,
  55. "`%s' is already loaded", mod->name);
  56. return GRUB_ERR_NONE;
  57. }
  58. static void
  59. grub_dl_remove (grub_dl_t mod)
  60. {
  61. grub_dl_t *p, q;
  62. for (p = &grub_dl_head, q = *p; q; p = &q->next, q = *p)
  63. if (q == mod)
  64. {
  65. *p = q->next;
  66. return;
  67. }
  68. }
  69. struct grub_symbol
  70. {
  71. struct grub_symbol *next;
  72. const char *name;
  73. void *addr;
  74. int isfunc;
  75. grub_dl_t mod; /* The module to which this symbol belongs. */
  76. };
  77. typedef struct grub_symbol *grub_symbol_t;
  78. /* The size of the symbol table. */
  79. #define GRUB_SYMTAB_SIZE 509
  80. /* The symbol table (using an open-hash). */
  81. static struct grub_symbol *grub_symtab[GRUB_SYMTAB_SIZE];
  82. /* Simple hash function. */
  83. static unsigned
  84. grub_symbol_hash (const char *s)
  85. {
  86. unsigned key = 0;
  87. while (*s)
  88. key = key * 65599 + *s++;
  89. return (key + (key >> 5)) % GRUB_SYMTAB_SIZE;
  90. }
  91. /* Resolve the symbol name NAME and return the address.
  92. Return NULL, if not found. */
  93. static grub_symbol_t
  94. grub_dl_resolve_symbol (const char *name)
  95. {
  96. grub_symbol_t sym;
  97. for (sym = grub_symtab[grub_symbol_hash (name)]; sym; sym = sym->next)
  98. if (grub_strcmp (sym->name, name) == 0)
  99. return sym;
  100. return 0;
  101. }
  102. /* Register a symbol with the name NAME and the address ADDR. */
  103. grub_err_t
  104. grub_dl_register_symbol (const char *name, void *addr, int isfunc,
  105. grub_dl_t mod)
  106. {
  107. grub_symbol_t sym;
  108. unsigned k;
  109. sym = (grub_symbol_t) grub_malloc (sizeof (*sym));
  110. if (! sym)
  111. return grub_errno;
  112. if (mod)
  113. {
  114. sym->name = grub_strdup (name);
  115. if (! sym->name)
  116. {
  117. grub_free (sym);
  118. return grub_errno;
  119. }
  120. }
  121. else
  122. sym->name = name;
  123. sym->addr = addr;
  124. sym->mod = mod;
  125. sym->isfunc = isfunc;
  126. k = grub_symbol_hash (name);
  127. sym->next = grub_symtab[k];
  128. grub_symtab[k] = sym;
  129. return GRUB_ERR_NONE;
  130. }
  131. /* Unregister all the symbols defined in the module MOD. */
  132. static void
  133. grub_dl_unregister_symbols (grub_dl_t mod)
  134. {
  135. unsigned i;
  136. if (! mod)
  137. grub_fatal ("core symbols cannot be unregistered");
  138. for (i = 0; i < GRUB_SYMTAB_SIZE; i++)
  139. {
  140. grub_symbol_t sym, *p, q;
  141. for (p = &grub_symtab[i], sym = *p; sym; sym = q)
  142. {
  143. q = sym->next;
  144. if (sym->mod == mod)
  145. {
  146. *p = q;
  147. grub_free ((void *) sym->name);
  148. grub_free (sym);
  149. }
  150. else
  151. p = &sym->next;
  152. }
  153. }
  154. }
  155. /* Return the address of a section whose index is N. */
  156. static void *
  157. grub_dl_get_section_addr (grub_dl_t mod, unsigned n)
  158. {
  159. grub_dl_segment_t seg;
  160. for (seg = mod->segment; seg; seg = seg->next)
  161. if (seg->section == n)
  162. return seg->addr;
  163. return 0;
  164. }
  165. /* Check if EHDR is a valid ELF header. */
  166. static grub_err_t
  167. grub_dl_check_header (void *ehdr, grub_size_t size)
  168. {
  169. Elf_Ehdr *e = ehdr;
  170. grub_err_t err;
  171. /* Check the header size. */
  172. if (size < sizeof (Elf_Ehdr))
  173. return grub_error (GRUB_ERR_BAD_OS, "ELF header smaller than expected");
  174. /* Check the magic numbers. */
  175. if (e->e_ident[EI_MAG0] != ELFMAG0
  176. || e->e_ident[EI_MAG1] != ELFMAG1
  177. || e->e_ident[EI_MAG2] != ELFMAG2
  178. || e->e_ident[EI_MAG3] != ELFMAG3
  179. || e->e_ident[EI_VERSION] != EV_CURRENT
  180. || e->e_version != EV_CURRENT)
  181. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
  182. err = grub_arch_dl_check_header (ehdr);
  183. if (err)
  184. return err;
  185. return GRUB_ERR_NONE;
  186. }
  187. /* Load all segments from memory specified by E. */
  188. static grub_err_t
  189. grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  190. {
  191. unsigned i;
  192. const Elf_Shdr *s;
  193. grub_size_t tsize = 0, talign = 1, arch_addralign = 1;
  194. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
  195. !defined (__loongarch__)
  196. grub_size_t tramp;
  197. grub_size_t tramp_align;
  198. grub_size_t got;
  199. grub_size_t got_align;
  200. grub_err_t err;
  201. #endif
  202. char *ptr;
  203. arch_addralign = DL_ALIGN;
  204. for (i = 0, s = (const Elf_Shdr *)((const char *) e + e->e_shoff);
  205. i < e->e_shnum;
  206. i++, s = (const Elf_Shdr *)((const char *) s + e->e_shentsize))
  207. {
  208. grub_size_t sh_addralign;
  209. grub_size_t sh_size;
  210. if (s->sh_size == 0 || !(s->sh_flags & SHF_ALLOC))
  211. continue;
  212. sh_addralign = ALIGN_UP (s->sh_addralign, arch_addralign);
  213. sh_size = ALIGN_UP (s->sh_size, sh_addralign);
  214. tsize = ALIGN_UP (tsize, sh_addralign) + sh_size;
  215. talign = grub_max (talign, sh_addralign);
  216. }
  217. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
  218. !defined (__loongarch__)
  219. err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
  220. if (err)
  221. return err;
  222. tramp_align = grub_max (GRUB_ARCH_DL_TRAMP_ALIGN, arch_addralign);
  223. tsize += ALIGN_UP (tramp, tramp_align);
  224. talign = grub_max (talign, tramp_align);
  225. got_align = grub_max (GRUB_ARCH_DL_GOT_ALIGN, arch_addralign);
  226. tsize += ALIGN_UP (got, got_align);
  227. talign = grub_max (talign, got_align);
  228. #endif
  229. #ifdef GRUB_MACHINE_EMU
  230. mod->base = grub_osdep_dl_memalign (talign, tsize);
  231. #else
  232. mod->base = grub_memalign (talign, tsize);
  233. #endif
  234. if (!mod->base)
  235. return grub_errno;
  236. mod->sz = tsize;
  237. ptr = mod->base;
  238. for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
  239. i < e->e_shnum;
  240. i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
  241. {
  242. grub_size_t sh_addralign = ALIGN_UP (s->sh_addralign, arch_addralign);
  243. grub_size_t sh_size = ALIGN_UP (s->sh_size, sh_addralign);
  244. if (s->sh_flags & SHF_ALLOC)
  245. {
  246. grub_dl_segment_t seg;
  247. seg = (grub_dl_segment_t) grub_malloc (sizeof (*seg));
  248. if (! seg)
  249. return grub_errno;
  250. if (s->sh_size)
  251. {
  252. void *addr;
  253. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, sh_addralign);
  254. addr = ptr;
  255. ptr += sh_size;
  256. switch (s->sh_type)
  257. {
  258. case SHT_PROGBITS:
  259. grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
  260. grub_memset ((char *) addr + s->sh_size, 0, sh_size - s->sh_size);
  261. break;
  262. case SHT_NOBITS:
  263. grub_memset (addr, 0, sh_size);
  264. break;
  265. }
  266. seg->addr = addr;
  267. }
  268. else
  269. seg->addr = 0;
  270. seg->size = sh_size;
  271. seg->section = i;
  272. seg->next = mod->segment;
  273. mod->segment = seg;
  274. }
  275. }
  276. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
  277. !defined (__loongarch__)
  278. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, tramp_align);
  279. mod->tramp = ptr;
  280. mod->trampptr = ptr;
  281. ptr += tramp;
  282. ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, got_align);
  283. mod->got = ptr;
  284. mod->gotptr = ptr;
  285. ptr += got;
  286. #endif
  287. return GRUB_ERR_NONE;
  288. }
  289. static grub_err_t
  290. grub_dl_resolve_symbols (grub_dl_t mod, Elf_Ehdr *e)
  291. {
  292. unsigned i;
  293. Elf_Shdr *s;
  294. Elf_Sym *sym;
  295. const char *str;
  296. Elf_Word size, entsize;
  297. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  298. i < e->e_shnum;
  299. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  300. if (s->sh_type == SHT_SYMTAB)
  301. break;
  302. /* Module without symbol table may still be used to pull in dependencies.
  303. We verify at build time that such modules do not contain any relocations
  304. that may reference symbol table. */
  305. if (i == e->e_shnum)
  306. return GRUB_ERR_NONE;
  307. #ifdef GRUB_MODULES_MACHINE_READONLY
  308. mod->symtab = grub_malloc (s->sh_size);
  309. if (!mod->symtab)
  310. return grub_errno;
  311. grub_memcpy (mod->symtab, (char *) e + s->sh_offset, s->sh_size);
  312. #else
  313. mod->symtab = (Elf_Sym *) ((char *) e + s->sh_offset);
  314. #endif
  315. mod->symsize = s->sh_entsize;
  316. sym = mod->symtab;
  317. size = s->sh_size;
  318. entsize = s->sh_entsize;
  319. s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
  320. str = (char *) e + s->sh_offset;
  321. for (i = 0;
  322. i < size / entsize;
  323. i++, sym = (Elf_Sym *) ((char *) sym + entsize))
  324. {
  325. unsigned char type = ELF_ST_TYPE (sym->st_info);
  326. unsigned char bind = ELF_ST_BIND (sym->st_info);
  327. const char *name = str + sym->st_name;
  328. switch (type)
  329. {
  330. case STT_NOTYPE:
  331. case STT_OBJECT:
  332. /* Resolve a global symbol. */
  333. if (sym->st_name != 0 && sym->st_shndx == 0)
  334. {
  335. grub_symbol_t nsym = grub_dl_resolve_symbol (name);
  336. if (! nsym)
  337. return grub_error (GRUB_ERR_BAD_MODULE,
  338. N_("symbol `%s' not found"), name);
  339. sym->st_value = (Elf_Addr) nsym->addr;
  340. if (nsym->isfunc)
  341. sym->st_info = ELF_ST_INFO (bind, STT_FUNC);
  342. }
  343. else
  344. {
  345. sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
  346. sym->st_shndx);
  347. if (bind != STB_LOCAL)
  348. if (grub_dl_register_symbol (name, (void *) sym->st_value, 0, mod))
  349. return grub_errno;
  350. }
  351. break;
  352. case STT_FUNC:
  353. sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
  354. sym->st_shndx);
  355. #ifdef __ia64__
  356. {
  357. /* FIXME: free descriptor once it's not used anymore. */
  358. char **desc;
  359. desc = grub_malloc (2 * sizeof (char *));
  360. if (!desc)
  361. return grub_errno;
  362. desc[0] = (void *) sym->st_value;
  363. desc[1] = mod->base;
  364. sym->st_value = (grub_addr_t) desc;
  365. }
  366. #endif
  367. if (bind != STB_LOCAL)
  368. if (grub_dl_register_symbol (name, (void *) sym->st_value, 1, mod))
  369. return grub_errno;
  370. if (grub_strcmp (name, "grub_mod_init") == 0)
  371. mod->init = (void (*) (grub_dl_t)) sym->st_value;
  372. else if (grub_strcmp (name, "grub_mod_fini") == 0)
  373. mod->fini = (void (*) (void)) sym->st_value;
  374. break;
  375. case STT_SECTION:
  376. sym->st_value = (Elf_Addr) grub_dl_get_section_addr (mod,
  377. sym->st_shndx);
  378. break;
  379. case STT_FILE:
  380. sym->st_value = 0;
  381. break;
  382. default:
  383. return grub_error (GRUB_ERR_BAD_MODULE,
  384. "unknown symbol type `%d'", (int) type);
  385. }
  386. }
  387. return GRUB_ERR_NONE;
  388. }
  389. static Elf_Shdr *
  390. grub_dl_find_section (Elf_Ehdr *e, const char *name)
  391. {
  392. Elf_Shdr *s;
  393. const char *str;
  394. unsigned i;
  395. s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize);
  396. str = (char *) e + s->sh_offset;
  397. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  398. i < e->e_shnum;
  399. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  400. if (grub_strcmp (str + s->sh_name, name) == 0)
  401. return s;
  402. return NULL;
  403. }
  404. /* Me, Vladimir Serbinenko, hereby I add this module check as per new
  405. GNU module policy. Note that this license check is informative only.
  406. Modules have to be licensed under GPLv3 or GPLv3+ (optionally
  407. multi-licensed under other licences as well) independently of the
  408. presence of this check and solely by linking (module loading in GRUB
  409. constitutes linking) and GRUB core being licensed under GPLv3+.
  410. Be sure to understand your license obligations.
  411. */
  412. static grub_err_t
  413. grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e)
  414. {
  415. Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
  416. if (s == NULL)
  417. return grub_error (GRUB_ERR_BAD_MODULE,
  418. "no license section in module %.63s", mod->name);
  419. if (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
  420. || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
  421. || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0)
  422. return GRUB_ERR_NONE;
  423. return grub_error (GRUB_ERR_BAD_MODULE,
  424. "incompatible license in module %.63s: %.63s", mod->name,
  425. (char *) e + s->sh_offset);
  426. }
  427. static grub_err_t
  428. grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e)
  429. {
  430. Elf_Shdr *s;
  431. s = grub_dl_find_section (e, ".modname");
  432. if (!s)
  433. return grub_error (GRUB_ERR_BAD_MODULE, "no module name found");
  434. mod->name = grub_strdup ((char *) e + s->sh_offset);
  435. if (! mod->name)
  436. return grub_errno;
  437. return GRUB_ERR_NONE;
  438. }
  439. static grub_err_t
  440. grub_dl_resolve_dependencies (grub_dl_t mod, Elf_Ehdr *e)
  441. {
  442. Elf_Shdr *s;
  443. s = grub_dl_find_section (e, ".moddeps");
  444. if (!s)
  445. return GRUB_ERR_NONE;
  446. const char *name = (char *) e + s->sh_offset;
  447. const char *max = name + s->sh_size;
  448. while ((name < max) && (*name))
  449. {
  450. grub_dl_t m;
  451. grub_dl_dep_t dep;
  452. m = grub_dl_load (name);
  453. if (! m)
  454. return grub_errno;
  455. grub_dl_ref (m);
  456. dep = (grub_dl_dep_t) grub_malloc (sizeof (*dep));
  457. if (! dep)
  458. return grub_errno;
  459. dep->mod = m;
  460. dep->next = mod->dep;
  461. mod->dep = dep;
  462. name += grub_strlen (name) + 1;
  463. }
  464. return GRUB_ERR_NONE;
  465. }
  466. int
  467. grub_dl_ref (grub_dl_t mod)
  468. {
  469. grub_dl_dep_t dep;
  470. if (!mod)
  471. return 0;
  472. for (dep = mod->dep; dep; dep = dep->next)
  473. grub_dl_ref (dep->mod);
  474. return ++mod->ref_count;
  475. }
  476. int
  477. grub_dl_unref (grub_dl_t mod)
  478. {
  479. grub_dl_dep_t dep;
  480. if (!mod)
  481. return 0;
  482. for (dep = mod->dep; dep; dep = dep->next)
  483. grub_dl_unref (dep->mod);
  484. return --mod->ref_count;
  485. }
  486. int
  487. grub_dl_ref_count (grub_dl_t mod)
  488. {
  489. if (mod == NULL)
  490. return 0;
  491. return mod->ref_count;
  492. }
  493. static void
  494. grub_dl_flush_cache (grub_dl_t mod)
  495. {
  496. grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
  497. (unsigned long) mod->sz, mod->base);
  498. grub_arch_sync_caches (mod->base, mod->sz);
  499. }
  500. static grub_err_t
  501. grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
  502. {
  503. Elf_Ehdr *e = ehdr;
  504. Elf_Shdr *s;
  505. unsigned i;
  506. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  507. i < e->e_shnum;
  508. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  509. if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
  510. {
  511. grub_dl_segment_t seg;
  512. grub_err_t err;
  513. /* Find the target segment. */
  514. for (seg = mod->segment; seg; seg = seg->next)
  515. if (seg->section == s->sh_info)
  516. break;
  517. if (seg)
  518. {
  519. if (!mod->symtab)
  520. return grub_error (GRUB_ERR_BAD_MODULE, "relocation without symbol table");
  521. err = grub_arch_dl_relocate_symbols (mod, ehdr, s, seg);
  522. if (err)
  523. return err;
  524. }
  525. }
  526. return GRUB_ERR_NONE;
  527. }
  528. /* Only define this on EFI to save space in core. */
  529. #ifdef GRUB_MACHINE_EFI
  530. static grub_err_t
  531. grub_dl_set_mem_attrs (grub_dl_t mod, void *ehdr)
  532. {
  533. unsigned i;
  534. const Elf_Shdr *s;
  535. const Elf_Ehdr *e = ehdr;
  536. grub_err_t err;
  537. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
  538. !defined (__loongarch__)
  539. grub_size_t arch_addralign = DL_ALIGN;
  540. grub_addr_t tgaddr;
  541. grub_size_t tgsz;
  542. #endif
  543. for (i = 0, s = (const Elf_Shdr *) ((const char *) e + e->e_shoff);
  544. i < e->e_shnum;
  545. i++, s = (const Elf_Shdr *) ((const char *) s + e->e_shentsize))
  546. {
  547. grub_dl_segment_t seg;
  548. grub_uint64_t set_attrs = GRUB_MEM_ATTR_R;
  549. grub_uint64_t clear_attrs = GRUB_MEM_ATTR_W | GRUB_MEM_ATTR_X;
  550. for (seg = mod->segment; seg; seg = seg->next)
  551. /* Does this ELF section's index match GRUB DL segment? */
  552. if (seg->section == s->sh_info)
  553. break;
  554. /* No GRUB DL segment found for this ELF section, skip it. */
  555. if (!seg)
  556. continue;
  557. if (seg->size == 0 || !(s->sh_flags & SHF_ALLOC))
  558. continue;
  559. if (s->sh_flags & SHF_WRITE)
  560. {
  561. set_attrs |= GRUB_MEM_ATTR_W;
  562. clear_attrs &= ~GRUB_MEM_ATTR_W;
  563. }
  564. if (s->sh_flags & SHF_EXECINSTR)
  565. {
  566. set_attrs |= GRUB_MEM_ATTR_X;
  567. clear_attrs &= ~GRUB_MEM_ATTR_X;
  568. }
  569. err = grub_update_mem_attrs ((grub_addr_t) seg->addr, seg->size,
  570. set_attrs, clear_attrs);
  571. if (err != GRUB_ERR_NONE)
  572. return err;
  573. }
  574. #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
  575. !defined (__loongarch__)
  576. tgaddr = grub_min ((grub_addr_t) mod->tramp, (grub_addr_t) mod->got);
  577. tgsz = grub_max ((grub_addr_t) mod->trampptr, (grub_addr_t) mod->gotptr) - tgaddr;
  578. if (tgsz)
  579. {
  580. tgsz = ALIGN_UP (tgsz, arch_addralign);
  581. if (tgaddr < (grub_addr_t) mod->base ||
  582. tgsz > (grub_addr_t) -1 - tgaddr ||
  583. tgaddr + tgsz > (grub_addr_t) mod->base + mod->sz)
  584. return grub_error (GRUB_ERR_BUG,
  585. "BUG: trying to protect pages outside of module "
  586. "allocation (\"%s\"): module base %p, size 0x%"
  587. PRIxGRUB_SIZE "; tramp/GOT base 0x%" PRIxGRUB_ADDR
  588. ", size 0x%" PRIxGRUB_SIZE,
  589. mod->name, mod->base, mod->sz, tgaddr, tgsz);
  590. err = grub_update_mem_attrs (tgaddr, tgsz, GRUB_MEM_ATTR_R | GRUB_MEM_ATTR_X, GRUB_MEM_ATTR_W);
  591. if (err != GRUB_ERR_NONE)
  592. return err;
  593. }
  594. #endif
  595. return GRUB_ERR_NONE;
  596. }
  597. #else
  598. static grub_err_t
  599. grub_dl_set_mem_attrs (grub_dl_t mod __attribute__ ((unused)), void *ehdr __attribute__ ((unused)))
  600. {
  601. return GRUB_ERR_NONE;
  602. }
  603. #endif
  604. /* Load a module from core memory. */
  605. grub_dl_t
  606. grub_dl_load_core_noinit (void *addr, grub_size_t size)
  607. {
  608. Elf_Ehdr *e;
  609. grub_dl_t mod;
  610. grub_dprintf ("modules", "module at %p, size 0x%lx\n", addr,
  611. (unsigned long) size);
  612. e = addr;
  613. if (grub_dl_check_header (e, size))
  614. return 0;
  615. if (e->e_type != ET_REL)
  616. {
  617. grub_error (GRUB_ERR_BAD_MODULE, N_("this ELF file is not of the right type"));
  618. return 0;
  619. }
  620. /* Make sure that every section is within the core. */
  621. if (size < e->e_shoff + (grub_uint32_t) e->e_shentsize * e->e_shnum)
  622. {
  623. grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core");
  624. return 0;
  625. }
  626. mod = (grub_dl_t) grub_zalloc (sizeof (*mod));
  627. if (! mod)
  628. return 0;
  629. mod->ref_count = 1;
  630. grub_dprintf ("modules", "relocating to %p\n", mod);
  631. /* Me, Vladimir Serbinenko, hereby I add this module check as per new
  632. GNU module policy. Note that this license check is informative only.
  633. Modules have to be licensed under GPLv3 or GPLv3+ (optionally
  634. multi-licensed under other licences as well) independently of the
  635. presence of this check and solely by linking (module loading in GRUB
  636. constitutes linking) and GRUB core being licensed under GPLv3+.
  637. Be sure to understand your license obligations.
  638. */
  639. if (grub_dl_resolve_name (mod, e)
  640. || grub_dl_check_license (mod, e)
  641. || grub_dl_resolve_dependencies (mod, e)
  642. || grub_dl_load_segments (mod, e)
  643. || grub_dl_resolve_symbols (mod, e)
  644. || grub_dl_relocate_symbols (mod, e)
  645. || grub_dl_set_mem_attrs (mod, e))
  646. {
  647. mod->fini = 0;
  648. grub_dl_unload (mod);
  649. return 0;
  650. }
  651. grub_dl_flush_cache (mod);
  652. grub_dprintf ("modules", "module name: %s\n", mod->name);
  653. grub_dprintf ("modules", "init function: %p\n", mod->init);
  654. if (grub_dl_add (mod))
  655. {
  656. grub_dl_unload (mod);
  657. return 0;
  658. }
  659. return mod;
  660. }
  661. grub_dl_t
  662. grub_dl_load_core (void *addr, grub_size_t size)
  663. {
  664. grub_dl_t mod;
  665. grub_boot_time ("Parsing module");
  666. mod = grub_dl_load_core_noinit (addr, size);
  667. if (!mod)
  668. return NULL;
  669. grub_boot_time ("Initing module %s", mod->name);
  670. grub_dl_init (mod);
  671. grub_boot_time ("Module %s inited", mod->name);
  672. return mod;
  673. }
  674. /* Load a module from the file FILENAME. */
  675. grub_dl_t
  676. grub_dl_load_file (const char *filename)
  677. {
  678. grub_file_t file = NULL;
  679. grub_ssize_t size;
  680. void *core = 0;
  681. grub_dl_t mod = 0;
  682. grub_boot_time ("Loading module %s", filename);
  683. file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE);
  684. if (! file)
  685. return 0;
  686. size = grub_file_size (file);
  687. core = grub_malloc (size);
  688. if (! core)
  689. {
  690. grub_file_close (file);
  691. return 0;
  692. }
  693. if (grub_file_read (file, core, size) != (int) size)
  694. {
  695. grub_file_close (file);
  696. grub_free (core);
  697. return 0;
  698. }
  699. /* We must close this before we try to process dependencies.
  700. Some disk backends do not handle gracefully multiple concurrent
  701. opens of the same device. */
  702. grub_file_close (file);
  703. mod = grub_dl_load_core (core, size);
  704. grub_free (core);
  705. if (! mod)
  706. return 0;
  707. mod->ref_count--;
  708. return mod;
  709. }
  710. /* Load a module using a symbolic name. */
  711. grub_dl_t
  712. grub_dl_load (const char *name)
  713. {
  714. char *filename;
  715. grub_dl_t mod;
  716. const char *grub_dl_dir = grub_env_get ("prefix");
  717. mod = grub_dl_get (name);
  718. if (mod)
  719. return mod;
  720. if (grub_no_modules)
  721. return 0;
  722. if (! grub_dl_dir) {
  723. grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
  724. return 0;
  725. }
  726. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
  727. grub_dl_dir, name);
  728. if (! filename)
  729. return 0;
  730. mod = grub_dl_load_file (filename);
  731. grub_free (filename);
  732. if (! mod)
  733. return 0;
  734. if (grub_strcmp (mod->name, name) != 0)
  735. grub_error (GRUB_ERR_BAD_MODULE, "mismatched names");
  736. return mod;
  737. }
  738. /* Unload the module MOD. */
  739. int
  740. grub_dl_unload (grub_dl_t mod)
  741. {
  742. grub_dl_dep_t dep, depn;
  743. if (mod->ref_count > 0)
  744. return 0;
  745. if (mod->fini)
  746. (mod->fini) ();
  747. grub_dl_remove (mod);
  748. grub_dl_unregister_symbols (mod);
  749. for (dep = mod->dep; dep; dep = depn)
  750. {
  751. depn = dep->next;
  752. grub_dl_unload (dep->mod);
  753. grub_free (dep);
  754. }
  755. #ifdef GRUB_MACHINE_EMU
  756. grub_dl_osdep_dl_free (mod->base);
  757. #else
  758. grub_free (mod->base);
  759. #endif
  760. grub_free (mod->name);
  761. #ifdef GRUB_MODULES_MACHINE_READONLY
  762. grub_free (mod->symtab);
  763. #endif
  764. grub_free (mod);
  765. return 1;
  766. }