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

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