You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

image.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stddef.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <libgen.h>
  27. #include <syslog.h>
  28. #include <ipxe/list.h>
  29. #include <ipxe/umalloc.h>
  30. #include <ipxe/uri.h>
  31. #include <ipxe/image.h>
  32. /** @file
  33. *
  34. * Executable images
  35. *
  36. */
  37. /* Disambiguate the various error causes */
  38. #define EACCES_UNTRUSTED \
  39. __einfo_error ( EINFO_EACCES_UNTRUSTED )
  40. #define EINFO_EACCES_UNTRUSTED \
  41. __einfo_uniqify ( EINFO_EACCES, 0x01, "Untrusted image" )
  42. #define EACCES_PERMANENT \
  43. __einfo_error ( EINFO_EACCES_PERMANENT )
  44. #define EINFO_EACCES_PERMANENT \
  45. __einfo_uniqify ( EINFO_EACCES, 0x02, "Trust requirement is permanent" )
  46. /** List of registered images */
  47. struct list_head images = LIST_HEAD_INIT ( images );
  48. /** Currently-executing image */
  49. struct image *current_image;
  50. /** Current image trust requirement */
  51. static int require_trusted_images = 0;
  52. /** Prevent changes to image trust requirement */
  53. static int require_trusted_images_permanent = 0;
  54. /**
  55. * Free executable image
  56. *
  57. * @v refcnt Reference counter
  58. */
  59. static void free_image ( struct refcnt *refcnt ) {
  60. struct image *image = container_of ( refcnt, struct image, refcnt );
  61. DBGC ( image, "IMAGE %s freed\n", image->name );
  62. free ( image->name );
  63. free ( image->cmdline );
  64. uri_put ( image->uri );
  65. ufree ( image->data );
  66. image_put ( image->replacement );
  67. free ( image );
  68. }
  69. /**
  70. * Allocate executable image
  71. *
  72. * @v uri URI, or NULL
  73. * @ret image Executable image
  74. */
  75. struct image * alloc_image ( struct uri *uri ) {
  76. const char *name;
  77. struct image *image;
  78. int rc;
  79. /* Allocate image */
  80. image = zalloc ( sizeof ( *image ) );
  81. if ( ! image )
  82. goto err_alloc;
  83. /* Initialise image */
  84. ref_init ( &image->refcnt, free_image );
  85. if ( uri ) {
  86. image->uri = uri_get ( uri );
  87. if ( uri->path ) {
  88. name = basename ( ( char * ) uri->path );
  89. if ( ( rc = image_set_name ( image, name ) ) != 0 )
  90. goto err_set_name;
  91. }
  92. }
  93. return image;
  94. err_set_name:
  95. image_put ( image );
  96. err_alloc:
  97. return NULL;
  98. }
  99. /**
  100. * Set image name
  101. *
  102. * @v image Image
  103. * @v name New image name
  104. * @ret rc Return status code
  105. */
  106. int image_set_name ( struct image *image, const char *name ) {
  107. char *name_copy;
  108. /* Duplicate name */
  109. name_copy = strdup ( name );
  110. if ( ! name_copy )
  111. return -ENOMEM;
  112. /* Replace existing name */
  113. free ( image->name );
  114. image->name = name_copy;
  115. return 0;
  116. }
  117. /**
  118. * Set image command line
  119. *
  120. * @v image Image
  121. * @v cmdline New image command line, or NULL
  122. * @ret rc Return status code
  123. */
  124. int image_set_cmdline ( struct image *image, const char *cmdline ) {
  125. free ( image->cmdline );
  126. image->cmdline = NULL;
  127. if ( cmdline ) {
  128. image->cmdline = strdup ( cmdline );
  129. if ( ! image->cmdline )
  130. return -ENOMEM;
  131. }
  132. return 0;
  133. }
  134. /**
  135. * Register executable image
  136. *
  137. * @v image Executable image
  138. * @ret rc Return status code
  139. */
  140. int register_image ( struct image *image ) {
  141. static unsigned int imgindex = 0;
  142. char name[8]; /* "imgXXXX" */
  143. int rc;
  144. /* Create image name if it doesn't already have one */
  145. if ( ! image->name ) {
  146. snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
  147. if ( ( rc = image_set_name ( image, name ) ) != 0 )
  148. return rc;
  149. }
  150. /* Avoid ending up with multiple "selected" images on
  151. * re-registration
  152. */
  153. if ( image_find_selected() )
  154. image->flags &= ~IMAGE_SELECTED;
  155. /* Add to image list */
  156. image_get ( image );
  157. image->flags |= IMAGE_REGISTERED;
  158. list_add_tail ( &image->list, &images );
  159. DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
  160. image->name, user_to_phys ( image->data, 0 ),
  161. user_to_phys ( image->data, image->len ) );
  162. return 0;
  163. }
  164. /**
  165. * Unregister executable image
  166. *
  167. * @v image Executable image
  168. */
  169. void unregister_image ( struct image *image ) {
  170. /* Do nothing unless image is registered */
  171. if ( ! ( image->flags & IMAGE_REGISTERED ) )
  172. return;
  173. DBGC ( image, "IMAGE %s unregistered\n", image->name );
  174. list_del ( &image->list );
  175. image->flags &= ~IMAGE_REGISTERED;
  176. image_put ( image );
  177. }
  178. /**
  179. * Find image by name
  180. *
  181. * @v name Image name
  182. * @ret image Executable image, or NULL
  183. */
  184. struct image * find_image ( const char *name ) {
  185. struct image *image;
  186. list_for_each_entry ( image, &images, list ) {
  187. if ( strcmp ( image->name, name ) == 0 )
  188. return image;
  189. }
  190. return NULL;
  191. }
  192. /**
  193. * Determine image type
  194. *
  195. * @v image Executable image
  196. * @ret rc Return status code
  197. */
  198. int image_probe ( struct image *image ) {
  199. struct image_type *type;
  200. int rc;
  201. /* Succeed if we already have a type */
  202. if ( image->type )
  203. return 0;
  204. /* Try each type in turn */
  205. for_each_table_entry ( type, IMAGE_TYPES ) {
  206. if ( ( rc = type->probe ( image ) ) == 0 ) {
  207. image->type = type;
  208. DBGC ( image, "IMAGE %s is %s\n",
  209. image->name, type->name );
  210. return 0;
  211. }
  212. DBGC ( image, "IMAGE %s is not %s: %s\n", image->name,
  213. type->name, strerror ( rc ) );
  214. }
  215. DBGC ( image, "IMAGE %s format not recognised\n", image->name );
  216. return -ENOEXEC;
  217. }
  218. /**
  219. * Execute image
  220. *
  221. * @v image Executable image
  222. * @ret rc Return status code
  223. *
  224. * The image must already be registered. Note that executing an image
  225. * may cause it to unregister itself. The caller must therefore
  226. * assume that the image pointer becomes invalid.
  227. */
  228. int image_exec ( struct image *image ) {
  229. struct image *saved_current_image;
  230. struct image *replacement = NULL;
  231. struct uri *old_cwuri;
  232. int rc;
  233. /* Sanity check */
  234. assert ( image->flags & IMAGE_REGISTERED );
  235. /* Switch current working directory to be that of the image itself */
  236. old_cwuri = uri_get ( cwuri );
  237. churi ( image->uri );
  238. /* Preserve record of any currently-running image */
  239. saved_current_image = current_image;
  240. /* Take out a temporary reference to the image. This allows
  241. * the image to unregister itself if necessary, without
  242. * automatically freeing itself.
  243. */
  244. current_image = image_get ( image );
  245. /* Check that this image can be selected for execution */
  246. if ( ( rc = image_select ( image ) ) != 0 )
  247. goto err;
  248. /* Check that image is trusted (if applicable) */
  249. if ( require_trusted_images && ! ( image->flags & IMAGE_TRUSTED ) ) {
  250. DBGC ( image, "IMAGE %s is not trusted\n", image->name );
  251. rc = -EACCES_UNTRUSTED;
  252. goto err;
  253. }
  254. /* Record boot attempt */
  255. syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
  256. /* Try executing the image */
  257. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  258. DBGC ( image, "IMAGE %s could not execute: %s\n",
  259. image->name, strerror ( rc ) );
  260. /* Do not return yet; we still have clean-up to do */
  261. }
  262. /* Record result of boot attempt */
  263. if ( rc == 0 ) {
  264. syslog ( LOG_NOTICE, "Execution of \"%s\" completed\n",
  265. image->name );
  266. } else {
  267. syslog ( LOG_ERR, "Execution of \"%s\" failed: %s\n",
  268. image->name, strerror ( rc ) );
  269. }
  270. /* Pick up replacement image before we drop the original
  271. * image's temporary reference. The replacement image must
  272. * already be registered, so we don't need to hold a temporary
  273. * reference (which would complicate the tail-recursion).
  274. */
  275. replacement = image->replacement;
  276. if ( replacement )
  277. assert ( replacement->flags & IMAGE_REGISTERED );
  278. err:
  279. /* Unregister image if applicable */
  280. if ( image->flags & IMAGE_AUTO_UNREGISTER )
  281. unregister_image ( image );
  282. /* Debug message for tail-recursion. Placed here because the
  283. * image_put() may end up freeing the image.
  284. */
  285. if ( replacement ) {
  286. DBGC ( image, "IMAGE %s replacing self with IMAGE %s\n",
  287. image->name, replacement->name );
  288. }
  289. /* Drop temporary reference to the original image */
  290. image_put ( image );
  291. /* Restore previous currently-running image */
  292. current_image = saved_current_image;
  293. /* Reset current working directory */
  294. churi ( old_cwuri );
  295. uri_put ( old_cwuri );
  296. /* Tail-recurse into replacement image, if one exists */
  297. if ( replacement )
  298. return image_exec ( replacement );
  299. return rc;
  300. }
  301. /**
  302. * Set replacement image
  303. *
  304. * @v replacement Replacement image
  305. * @ret rc Return status code
  306. *
  307. * The replacement image must already be registered, and must remain
  308. * registered until the currently-executing image returns.
  309. */
  310. int image_replace ( struct image *replacement ) {
  311. struct image *image = current_image;
  312. int rc;
  313. /* Sanity check */
  314. assert ( replacement->flags & IMAGE_REGISTERED );
  315. /* Fail unless there is a currently-executing image */
  316. if ( ! image ) {
  317. rc = -ENOTTY;
  318. DBGC ( replacement, "IMAGE %s cannot replace non-existent "
  319. "image: %s\n", replacement->name, strerror ( rc ) );
  320. return rc;
  321. }
  322. /* Check that the replacement image can be executed */
  323. if ( ( rc = image_probe ( replacement ) ) != 0 )
  324. return rc;
  325. /* Clear any existing replacement */
  326. image_put ( image->replacement );
  327. /* Set replacement */
  328. image->replacement = image_get ( replacement );
  329. DBGC ( image, "IMAGE %s will replace self with IMAGE %s\n",
  330. image->name, replacement->name );
  331. return 0;
  332. }
  333. /**
  334. * Select image for execution
  335. *
  336. * @v image Executable image
  337. * @ret rc Return status code
  338. */
  339. int image_select ( struct image *image ) {
  340. struct image *tmp;
  341. int rc;
  342. /* Unselect all other images */
  343. for_each_image ( tmp )
  344. tmp->flags &= ~IMAGE_SELECTED;
  345. /* Check that this image can be executed */
  346. if ( ( rc = image_probe ( image ) ) != 0 )
  347. return rc;
  348. /* Mark image as selected */
  349. image->flags |= IMAGE_SELECTED;
  350. return 0;
  351. }
  352. /**
  353. * Find selected image
  354. *
  355. * @ret image Executable image, or NULL
  356. */
  357. struct image * image_find_selected ( void ) {
  358. struct image *image;
  359. for_each_image ( image ) {
  360. if ( image->flags & IMAGE_SELECTED )
  361. return image;
  362. }
  363. return NULL;
  364. }
  365. /**
  366. * Change image trust requirement
  367. *
  368. * @v require_trusted Require trusted images
  369. * @v permanent Make trust requirement permanent
  370. * @ret rc Return status code
  371. */
  372. int image_set_trust ( int require_trusted, int permanent ) {
  373. /* Update trust requirement, if permitted to do so */
  374. if ( ! require_trusted_images_permanent ) {
  375. require_trusted_images = require_trusted;
  376. require_trusted_images_permanent = permanent;
  377. }
  378. /* Fail if we attempted to change the trust requirement but
  379. * were not permitted to do so.
  380. */
  381. if ( require_trusted_images != require_trusted )
  382. return -EACCES_PERMANENT;
  383. return 0;
  384. }