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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. name = basename ( ( char * ) uri->path );
  87. if ( ( rc = image_set_name ( image, name ) ) != 0 )
  88. goto err_set_name;
  89. }
  90. return image;
  91. err_set_name:
  92. image_put ( image );
  93. err_alloc:
  94. return NULL;
  95. }
  96. /**
  97. * Set image name
  98. *
  99. * @v image Image
  100. * @v name New image name
  101. * @ret rc Return status code
  102. */
  103. int image_set_name ( struct image *image, const char *name ) {
  104. char *name_copy;
  105. /* Duplicate name */
  106. name_copy = strdup ( name );
  107. if ( ! name_copy )
  108. return -ENOMEM;
  109. /* Replace existing name */
  110. free ( image->name );
  111. image->name = name_copy;
  112. return 0;
  113. }
  114. /**
  115. * Set image command line
  116. *
  117. * @v image Image
  118. * @v cmdline New image command line, or NULL
  119. * @ret rc Return status code
  120. */
  121. int image_set_cmdline ( struct image *image, const char *cmdline ) {
  122. free ( image->cmdline );
  123. image->cmdline = NULL;
  124. if ( cmdline ) {
  125. image->cmdline = strdup ( cmdline );
  126. if ( ! image->cmdline )
  127. return -ENOMEM;
  128. }
  129. return 0;
  130. }
  131. /**
  132. * Register executable image
  133. *
  134. * @v image Executable image
  135. * @ret rc Return status code
  136. */
  137. int register_image ( struct image *image ) {
  138. static unsigned int imgindex = 0;
  139. char name[8]; /* "imgXXXX" */
  140. int rc;
  141. /* Create image name if it doesn't already have one */
  142. if ( ! image->name ) {
  143. snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
  144. if ( ( rc = image_set_name ( image, name ) ) != 0 )
  145. return rc;
  146. }
  147. /* Avoid ending up with multiple "selected" images on
  148. * re-registration
  149. */
  150. if ( image_find_selected() )
  151. image->flags &= ~IMAGE_SELECTED;
  152. /* Add to image list */
  153. image_get ( image );
  154. image->flags |= IMAGE_REGISTERED;
  155. list_add_tail ( &image->list, &images );
  156. DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
  157. image->name, user_to_phys ( image->data, 0 ),
  158. user_to_phys ( image->data, image->len ) );
  159. return 0;
  160. }
  161. /**
  162. * Unregister executable image
  163. *
  164. * @v image Executable image
  165. */
  166. void unregister_image ( struct image *image ) {
  167. DBGC ( image, "IMAGE %s unregistered\n", image->name );
  168. list_del ( &image->list );
  169. image->flags &= ~IMAGE_REGISTERED;
  170. image_put ( image );
  171. }
  172. /**
  173. * Find image by name
  174. *
  175. * @v name Image name
  176. * @ret image Executable image, or NULL
  177. */
  178. struct image * find_image ( const char *name ) {
  179. struct image *image;
  180. list_for_each_entry ( image, &images, list ) {
  181. if ( strcmp ( image->name, name ) == 0 )
  182. return image;
  183. }
  184. return NULL;
  185. }
  186. /**
  187. * Determine image type
  188. *
  189. * @v image Executable image
  190. * @ret rc Return status code
  191. */
  192. int image_probe ( struct image *image ) {
  193. struct image_type *type;
  194. int rc;
  195. /* Succeed if we already have a type */
  196. if ( image->type )
  197. return 0;
  198. /* Try each type in turn */
  199. for_each_table_entry ( type, IMAGE_TYPES ) {
  200. if ( ( rc = type->probe ( image ) ) == 0 ) {
  201. image->type = type;
  202. DBGC ( image, "IMAGE %s is %s\n",
  203. image->name, type->name );
  204. return 0;
  205. }
  206. DBGC ( image, "IMAGE %s is not %s: %s\n", image->name,
  207. type->name, strerror ( rc ) );
  208. }
  209. DBGC ( image, "IMAGE %s format not recognised\n", image->name );
  210. return -ENOEXEC;
  211. }
  212. /**
  213. * Execute image
  214. *
  215. * @v image Executable image
  216. * @ret rc Return status code
  217. *
  218. * The image must already be registered. Note that executing an image
  219. * may cause it to unregister itself. The caller must therefore
  220. * assume that the image pointer becomes invalid.
  221. */
  222. int image_exec ( struct image *image ) {
  223. struct image *saved_current_image;
  224. struct image *replacement;
  225. struct uri *old_cwuri;
  226. int rc;
  227. /* Sanity check */
  228. assert ( image->flags & IMAGE_REGISTERED );
  229. /* Check that this image can be selected for execution */
  230. if ( ( rc = image_select ( image ) ) != 0 )
  231. return rc;
  232. /* Check that image is trusted (if applicable) */
  233. if ( require_trusted_images && ! ( image->flags & IMAGE_TRUSTED ) ) {
  234. DBGC ( image, "IMAGE %s is not trusted\n", image->name );
  235. return -EACCES_UNTRUSTED;
  236. }
  237. /* Switch current working directory to be that of the image itself */
  238. old_cwuri = uri_get ( cwuri );
  239. churi ( image->uri );
  240. /* Preserve record of any currently-running image */
  241. saved_current_image = current_image;
  242. /* Take out a temporary reference to the image. This allows
  243. * the image to unregister itself if necessary, without
  244. * automatically freeing itself.
  245. */
  246. current_image = image_get ( image );
  247. /* Record boot attempt */
  248. syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
  249. /* Try executing the image */
  250. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  251. DBGC ( image, "IMAGE %s could not execute: %s\n",
  252. image->name, strerror ( rc ) );
  253. /* Do not return yet; we still have clean-up to do */
  254. }
  255. /* Record result of boot attempt */
  256. if ( rc == 0 ) {
  257. syslog ( LOG_NOTICE, "Execution of \"%s\" completed\n",
  258. image->name );
  259. } else {
  260. syslog ( LOG_ERR, "Execution of \"%s\" failed: %s\n",
  261. image->name, strerror ( rc ) );
  262. }
  263. /* Pick up replacement image before we drop the original
  264. * image's temporary reference. The replacement image must
  265. * already be registered, so we don't need to hold a temporary
  266. * reference (which would complicate the tail-recursion).
  267. */
  268. replacement = image->replacement;
  269. if ( replacement )
  270. assert ( replacement->flags & IMAGE_REGISTERED );
  271. /* Drop temporary reference to the original image */
  272. image_put ( image );
  273. /* Restore previous currently-running image */
  274. current_image = saved_current_image;
  275. /* Reset current working directory */
  276. churi ( old_cwuri );
  277. uri_put ( old_cwuri );
  278. /* Tail-recurse into replacement image, if one exists */
  279. if ( replacement ) {
  280. DBGC ( image, "IMAGE <freed> replacing self with IMAGE %s\n",
  281. replacement->name );
  282. if ( ( rc = image_exec ( replacement ) ) != 0 )
  283. return rc;
  284. }
  285. return rc;
  286. }
  287. /**
  288. * Set replacement image
  289. *
  290. * @v replacement Replacement image
  291. * @ret rc Return status code
  292. *
  293. * The replacement image must already be registered, and must remain
  294. * registered until the currently-executing image returns.
  295. */
  296. int image_replace ( struct image *replacement ) {
  297. struct image *image = current_image;
  298. int rc;
  299. /* Sanity check */
  300. assert ( replacement->flags & IMAGE_REGISTERED );
  301. /* Fail unless there is a currently-executing image */
  302. if ( ! image ) {
  303. rc = -ENOTTY;
  304. DBGC ( replacement, "IMAGE %s cannot replace non-existent "
  305. "image: %s\n", replacement->name, strerror ( rc ) );
  306. return rc;
  307. }
  308. /* Check that the replacement image can be executed */
  309. if ( ( rc = image_probe ( replacement ) ) != 0 )
  310. return rc;
  311. /* Clear any existing replacement */
  312. image_put ( image->replacement );
  313. /* Set replacement */
  314. image->replacement = image_get ( replacement );
  315. DBGC ( image, "IMAGE %s will replace self with IMAGE %s\n",
  316. image->name, replacement->name );
  317. return 0;
  318. }
  319. /**
  320. * Select image for execution
  321. *
  322. * @v image Executable image
  323. * @ret rc Return status code
  324. */
  325. int image_select ( struct image *image ) {
  326. struct image *tmp;
  327. int rc;
  328. /* Unselect all other images */
  329. for_each_image ( tmp )
  330. tmp->flags &= ~IMAGE_SELECTED;
  331. /* Check that this image can be executed */
  332. if ( ( rc = image_probe ( image ) ) != 0 )
  333. return rc;
  334. /* Mark image as selected */
  335. image->flags |= IMAGE_SELECTED;
  336. return 0;
  337. }
  338. /**
  339. * Find selected image
  340. *
  341. * @ret image Executable image, or NULL
  342. */
  343. struct image * image_find_selected ( void ) {
  344. struct image *image;
  345. for_each_image ( image ) {
  346. if ( image->flags & IMAGE_SELECTED )
  347. return image;
  348. }
  349. return NULL;
  350. }
  351. /**
  352. * Change image trust requirement
  353. *
  354. * @v require_trusted Require trusted images
  355. * @v permanent Make trust requirement permanent
  356. * @ret rc Return status code
  357. */
  358. int image_set_trust ( int require_trusted, int permanent ) {
  359. /* Update trust requirement, if permitted to do so */
  360. if ( ! require_trusted_images_permanent ) {
  361. require_trusted_images = require_trusted;
  362. require_trusted_images_permanent = permanent;
  363. }
  364. /* Fail if we attempted to change the trust requirement but
  365. * were not permitted to do so.
  366. */
  367. if ( require_trusted_images != require_trusted )
  368. return -EACCES_PERMANENT;
  369. return 0;
  370. }