0004-env-fix-memory-leak-in-fw_env-routines.patch 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. From 5ba7e08c4188064286533e896189f75c3d60af57 Mon Sep 17 00:00:00 2001
  2. From: Stefano Babic <sbabic@denx.de>
  3. Date: Wed, 5 Apr 2017 17:23:44 +0200
  4. Subject: [PATCH v1 4/4] env: fix memory leak in fw_env routines
  5. fw_env_open allocates buffers to store the environment, but these
  6. buffers are never freed. This becomes quite nasty using the fw_ tools as
  7. library, because each access to the environment (even just reading a
  8. variable) generates a memory leak equal to the size of the environment.
  9. Fix this renaming fw_env_close() as fw_env_flush(), because the function
  10. really flushes the environment from RAM to storage, and add a
  11. fw_env_close function to free the allocated resources.
  12. Signed-off-by: Stefano Babic <sbabic@denx.de>
  13. ---
  14. tools/env/fw_env.c | 72 ++++++++++++++++++++++++++++++++++++++++++------------
  15. tools/env/fw_env.h | 17 ++++++++++---
  16. 2 files changed, 70 insertions(+), 19 deletions(-)
  17. diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
  18. index fc3f4ce..299e0c9 100644
  19. --- a/tools/env/fw_env.c
  20. +++ b/tools/env/fw_env.c
  21. @@ -278,6 +278,7 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
  22. printf ("%s\n", env);
  23. }
  24. + fw_env_close(opts);
  25. return 0;
  26. }
  27. @@ -300,10 +301,12 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
  28. printf("%s=%s\n", name, val);
  29. }
  30. + fw_env_close(opts);
  31. +
  32. return rc;
  33. }
  34. -int fw_env_close(struct env_opts *opts)
  35. +int fw_env_flush(struct env_opts *opts)
  36. {
  37. int ret;
  38. @@ -472,6 +475,7 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts)
  39. char *name, **valv;
  40. char *value = NULL;
  41. int valc;
  42. + int ret;
  43. if (!opts)
  44. opts = &default_opts;
  45. @@ -491,8 +495,10 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts)
  46. valv = argv + 1;
  47. valc = argc - 1;
  48. - if (env_flags_validate_env_set_params(name, valv, valc) < 0)
  49. + if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
  50. + fw_env_close(opts);
  51. return -1;
  52. + }
  53. len = 0;
  54. for (i = 0; i < valc; ++i) {
  55. @@ -518,7 +524,10 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts)
  56. free(value);
  57. - return fw_env_close(opts);
  58. + ret = fw_env_flush(opts);
  59. + fw_env_close(opts);
  60. +
  61. + return ret;
  62. }
  63. /*
  64. @@ -639,7 +648,9 @@ int fw_parse_script(char *fname, struct env_opts *opts)
  65. if (strcmp(fname, "-") != 0)
  66. fclose(fp);
  67. - ret |= fw_env_close(opts);
  68. + ret |= fw_env_flush(opts);
  69. +
  70. + fw_env_close(opts);
  71. return ret;
  72. }
  73. @@ -1105,11 +1116,11 @@ int fw_env_open(struct env_opts *opts)
  74. {
  75. int crc0, crc0_ok;
  76. unsigned char flag0;
  77. - void *addr0;
  78. + void *addr0 = NULL;
  79. int crc1, crc1_ok;
  80. unsigned char flag1;
  81. - void *addr1;
  82. + void *addr1 = NULL;
  83. int ret;
  84. @@ -1120,14 +1131,15 @@ int fw_env_open(struct env_opts *opts)
  85. opts = &default_opts;
  86. if (parse_config(opts)) /* should fill envdevices */
  87. - return -1;
  88. + return -EINVAL;
  89. addr0 = calloc(1, CUR_ENVSIZE);
  90. if (addr0 == NULL) {
  91. fprintf(stderr,
  92. "Not enough memory for environment (%ld bytes)\n",
  93. CUR_ENVSIZE);
  94. - return -1;
  95. + ret = -ENOMEM;
  96. + goto open_cleanup;
  97. }
  98. /* read environment from FLASH to local buffer */
  99. @@ -1146,8 +1158,10 @@ int fw_env_open(struct env_opts *opts)
  100. }
  101. dev_current = 0;
  102. - if (flash_io (O_RDONLY))
  103. - return -1;
  104. + if (flash_io(O_RDONLY)) {
  105. + ret = -EIO;
  106. + goto open_cleanup;
  107. + }
  108. crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
  109. @@ -1155,7 +1169,7 @@ int fw_env_open(struct env_opts *opts)
  110. ret = env_aes_cbc_crypt(environment.data, 0,
  111. opts->aes_key);
  112. if (ret)
  113. - return ret;
  114. + goto open_cleanup;
  115. }
  116. crc0_ok = (crc0 == *environment.crc);
  117. @@ -1174,7 +1188,8 @@ int fw_env_open(struct env_opts *opts)
  118. fprintf(stderr,
  119. "Not enough memory for environment (%ld bytes)\n",
  120. CUR_ENVSIZE);
  121. - return -1;
  122. + ret = -ENOMEM;
  123. + goto open_cleanup;
  124. }
  125. redundant = addr1;
  126. @@ -1183,8 +1198,10 @@ int fw_env_open(struct env_opts *opts)
  127. * other pointers in environment still point inside addr0
  128. */
  129. environment.image = addr1;
  130. - if (flash_io (O_RDONLY))
  131. - return -1;
  132. + if (flash_io(O_RDONLY)) {
  133. + ret = -EIO;
  134. + goto open_cleanup;
  135. + }
  136. /* Check flag scheme compatibility */
  137. if (DEVTYPE(dev_current) == MTD_NORFLASH &&
  138. @@ -1204,7 +1221,8 @@ int fw_env_open(struct env_opts *opts)
  139. environment.flag_scheme = FLAG_INCREMENTAL;
  140. } else {
  141. fprintf (stderr, "Incompatible flash types!\n");
  142. - return -1;
  143. + ret = -EINVAL;
  144. + goto open_cleanup;
  145. }
  146. crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
  147. @@ -1213,7 +1231,7 @@ int fw_env_open(struct env_opts *opts)
  148. ret = env_aes_cbc_crypt(redundant->data, 0,
  149. opts->aes_key);
  150. if (ret)
  151. - return ret;
  152. + goto open_cleanup;
  153. }
  154. crc1_ok = (crc1 == redundant->crc);
  155. @@ -1285,6 +1303,28 @@ int fw_env_open(struct env_opts *opts)
  156. #endif
  157. }
  158. return 0;
  159. +
  160. +open_cleanup:
  161. + if (addr0)
  162. + free(addr0);
  163. +
  164. + if (addr1)
  165. + free(addr0);
  166. +
  167. + return ret;
  168. +}
  169. +
  170. +/*
  171. + * Simply free allocated buffer with environment
  172. + */
  173. +int fw_env_close(struct env_opts *opts)
  174. +{
  175. + if (environment.image)
  176. + free(environment.image);
  177. +
  178. + environment.image = NULL;
  179. +
  180. + return 0;
  181. }
  182. static int check_device_config(int dev)
  183. diff --git a/tools/env/fw_env.h b/tools/env/fw_env.h
  184. index cf346b3..04bb646 100644
  185. --- a/tools/env/fw_env.h
  186. +++ b/tools/env/fw_env.h
  187. @@ -53,7 +53,7 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
  188. * @opts: how to retrieve environment from flash, defaults are used if NULL
  189. *
  190. * Description:
  191. - * Uses fw_env_open, fw_env_write, fw_env_close
  192. + * Uses fw_env_open, fw_env_write, fw_env_flush
  193. *
  194. * Return:
  195. * 0 on success, -1 on failure (modifies errno)
  196. @@ -70,7 +70,7 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts);
  197. * @opts: encryption key, configuration file, defaults are used if NULL
  198. *
  199. * Description:
  200. - * Uses fw_env_open, fw_env_write, fw_env_close
  201. + * Uses fw_env_open, fw_env_write, fw_env_flush
  202. *
  203. * Return:
  204. * 0 success, -1 on failure (modifies errno)
  205. @@ -138,7 +138,17 @@ char *fw_getenv(char *name);
  206. int fw_env_write(char *name, char *value);
  207. /**
  208. - * fw_env_close - write the environment from RAM cache back to flash
  209. + * fw_env_flush - write the environment from RAM cache back to flash
  210. + *
  211. + * @opts: encryption key, configuration file, defaults are used if NULL
  212. + *
  213. + * Return:
  214. + * 0 on success, -1 on failure (modifies errno)
  215. + */
  216. +int fw_env_flush(struct env_opts *opts);
  217. +
  218. +/**
  219. + * fw_env_close - free allocated structure and close env
  220. *
  221. * @opts: encryption key, configuration file, defaults are used if NULL
  222. *
  223. @@ -147,6 +157,7 @@ int fw_env_write(char *name, char *value);
  224. */
  225. int fw_env_close(struct env_opts *opts);
  226. +
  227. /**
  228. * fw_env_version - return the current version of the library
  229. *
  230. --
  231. 2.7.4