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

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