efivar-upstream.patch 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. From 197a0874ea4010061b98b4b55eff65b33b1cd741 Mon Sep 17 00:00:00 2001
  2. From: Robbie Harwood <rharwood@redhat.com>
  3. Date: Mon, 17 Jan 2022 12:34:55 -0500
  4. Subject: [PATCH 1/7] Add -T workaround for GNU ld 2.36
  5. Signed-off-by: Robbie Harwood <rharwood@redhat.com>
  6. Resolves: #195
  7. ---
  8. src/include/workarounds.mk | 4 ++--
  9. 1 file changed, 2 insertions(+), 2 deletions(-)
  10. diff --git a/src/include/workarounds.mk b/src/include/workarounds.mk
  11. index 3118834..143e790 100644
  12. --- a/src/include/workarounds.mk
  13. +++ b/src/include/workarounds.mk
  14. @@ -4,12 +4,12 @@
  15. LD_FLAVOR := $(shell $(LD) --version | grep -E '^(LLD|GNU ld)'|sed 's/ .*//g')
  16. LD_VERSION := $(shell $(LD) --version | grep -E '^(LLD|GNU ld)'|sed 's/.* //')
  17. -# I haven't tested 2.36 here; 2.35 is definitely broken and 2.37 seems to work
  18. +# 2.35 is definitely broken and 2.36 seems to work
  19. LD_DASH_T := $(shell \
  20. if [ "x${LD_FLAVOR}" = xLLD ] ; then \
  21. echo '-T' ; \
  22. elif [ "x${LD_FLAVOR}" = xGNU ] ; then \
  23. - if echo "${LD_VERSION}" | grep -q -E '^2\.3[789]|^2\.[456789]|^[3456789]|^[[:digit:]][[:digit:]]' ; then \
  24. + if echo "${LD_VERSION}" | grep -q -E '^2\.3[6789]|^2\.[456789]|^[3456789]|^[[:digit:]][[:digit:]]' ; then \
  25. echo '-T' ; \
  26. else \
  27. echo "" ; \
  28. --
  29. 2.20.1
  30. From 28789d12ea9f88548263b5f0f4a30841e3a24f0a Mon Sep 17 00:00:00 2001
  31. From: Ted Brandston <tbrandston@google.com>
  32. Date: Mon, 31 Jan 2022 10:33:38 -0500
  33. Subject: [PATCH 2/7] Add `extern "C"` to headers for easier use by C++
  34. Add `extern "C"` directly to all headers except efivar-guids.h, which
  35. is generated. Because it interferes with C compilers, wrap each
  36. `extern` in an `#ifdef __cplusplus`. For efivar-guids.h update the
  37. generator, makeguids.c, to add the desired code.
  38. From https://en.cppreference.com/w/cpp/language/language_linkage:
  39. > extern "C" makes it possible to include header files containing
  40. > declarations of C library functions in a C++ program, but if the
  41. > same header file is shared with a C program, extern "C" (which
  42. > is not allowed in C) must be hidden with an appropriate #ifdef,
  43. > typically __cplusplus.
  44. Signed-off-by: Ted Brandston <tbrandston@google.com>
  45. ---
  46. src/include/efivar/efiboot-creator.h | 8 ++++++++
  47. src/include/efivar/efiboot-loadopt.h | 8 ++++++++
  48. src/include/efivar/efiboot.h | 8 ++++++++
  49. src/include/efivar/efisec-secdb.h | 8 ++++++++
  50. src/include/efivar/efisec-types.h | 8 ++++++++
  51. src/include/efivar/efisec.h | 8 ++++++++
  52. src/include/efivar/efivar-dp.h | 9 +++++++++
  53. src/include/efivar/efivar-time.h | 8 ++++++++
  54. src/include/efivar/efivar-types.h | 8 ++++++++
  55. src/include/efivar/efivar.h | 8 ++++++++
  56. src/makeguids.c | 8 ++++++++
  57. 11 files changed, 89 insertions(+)
  58. diff --git a/src/include/efivar/efiboot-creator.h b/src/include/efivar/efiboot-creator.h
  59. index 308ea49..175417d 100644
  60. --- a/src/include/efivar/efiboot-creator.h
  61. +++ b/src/include/efivar/efiboot-creator.h
  62. @@ -7,6 +7,10 @@
  63. #ifndef _EFIBOOT_CREATOR_H
  64. #define _EFIBOOT_CREATOR_H
  65. +#ifdef __cplusplus
  66. +extern "C" {
  67. +#endif
  68. +
  69. #define EFIBOOT_ABBREV_NONE 0x00000001
  70. #define EFIBOOT_ABBREV_HD 0x00000002
  71. #define EFIBOOT_ABBREV_FILE 0x00000004
  72. @@ -43,6 +47,10 @@ extern ssize_t efi_generate_ipv4_device_path(uint8_t *buf, ssize_t size,
  73. __attribute__((__nonnull__ (3,4,5,6,7)))
  74. __attribute__((__visibility__ ("default")));
  75. +#ifdef __cplusplus
  76. +} /* extern "C" */
  77. +#endif
  78. +
  79. #endif /* _EFIBOOT_CREATOR_H */
  80. // vim:fenc=utf-8:tw=75:noet
  81. diff --git a/src/include/efivar/efiboot-loadopt.h b/src/include/efivar/efiboot-loadopt.h
  82. index 3c723a1..76dc45a 100644
  83. --- a/src/include/efivar/efiboot-loadopt.h
  84. +++ b/src/include/efivar/efiboot-loadopt.h
  85. @@ -7,6 +7,10 @@
  86. #ifndef _EFIBOOT_LOADOPT_H
  87. #define _EFIBOOT_LOADOPT_H 1
  88. +#ifdef __cplusplus
  89. +extern "C" {
  90. +#endif
  91. +
  92. typedef struct efi_load_option_s efi_load_option;
  93. extern ssize_t efi_loadopt_create(uint8_t *buf, ssize_t size,
  94. @@ -59,6 +63,10 @@ extern int efi_loadopt_is_valid(efi_load_option *opt, size_t size)
  95. __attribute__((__nonnull__ (1)))
  96. __attribute__((__visibility__ ("default")));
  97. +#ifdef __cplusplus
  98. +} /* extern "C" */
  99. +#endif
  100. +
  101. #endif /* _EFIBOOT_LOADOPT_H */
  102. // vim:fenc=utf-8:tw=75:noet
  103. diff --git a/src/include/efivar/efiboot.h b/src/include/efivar/efiboot.h
  104. index e52ab8f..b5dfb20 100644
  105. --- a/src/include/efivar/efiboot.h
  106. +++ b/src/include/efivar/efiboot.h
  107. @@ -21,9 +21,17 @@
  108. #include <efivar/efiboot-creator.h>
  109. #include <efivar/efiboot-loadopt.h>
  110. +#ifdef __cplusplus
  111. +extern "C" {
  112. +#endif
  113. +
  114. extern uint32_t efi_get_libefiboot_version(void)
  115. __attribute__((__visibility__("default")));
  116. +#ifdef __cplusplus
  117. +} /* extern "C" */
  118. +#endif
  119. +
  120. #endif /* EFIBOOT_H */
  121. // vim:fenc=utf-8:tw=75:noet
  122. diff --git a/src/include/efivar/efisec-secdb.h b/src/include/efivar/efisec-secdb.h
  123. index 37ddcc2..ece4a7d 100644
  124. --- a/src/include/efivar/efisec-secdb.h
  125. +++ b/src/include/efivar/efisec-secdb.h
  126. @@ -7,6 +7,10 @@
  127. #ifndef EFISEC_SECDB_H_
  128. #define EFISEC_SECDB_H_
  129. +#ifdef __cplusplus
  130. +extern "C" {
  131. +#endif
  132. +
  133. typedef struct efi_secdb efi_secdb_t;
  134. typedef union {
  135. @@ -90,5 +94,9 @@ extern int efi_secdb_visit_entries(efi_secdb_t *secdb,
  136. efi_secdb_visitor_t *visitor,
  137. void *closure);
  138. +#ifdef __cplusplus
  139. +} /* extern "C" */
  140. +#endif
  141. +
  142. #endif /* !EFISEC_SECDB_H_ */
  143. // vim:fenc=utf-8:tw=75:noet
  144. diff --git a/src/include/efivar/efisec-types.h b/src/include/efivar/efisec-types.h
  145. index 4ba04ab..d3e1fe0 100644
  146. --- a/src/include/efivar/efisec-types.h
  147. +++ b/src/include/efivar/efisec-types.h
  148. @@ -10,6 +10,10 @@
  149. #include <stdint.h>
  150. #include <efivar/efivar-types.h>
  151. +#ifdef __cplusplus
  152. +extern "C" {
  153. +#endif
  154. +
  155. /*
  156. * Storage for specific hashes and cryptographic (pkcs1, not pkcs7)
  157. * signatures
  158. @@ -231,5 +235,9 @@ typedef struct {
  159. // uint8_t signing_cert[];
  160. } efi_variable_nonced_authentication_3 __attribute__((aligned (1)));
  161. +#ifdef __cplusplus
  162. +} /* extern "C" */
  163. +#endif
  164. +
  165. #endif /* !SECURITY_H_ */
  166. // vim:fenc=utf-8:tw=75:noet
  167. diff --git a/src/include/efivar/efisec.h b/src/include/efivar/efisec.h
  168. index 2072e5c..22cfda8 100644
  169. --- a/src/include/efivar/efisec.h
  170. +++ b/src/include/efivar/efisec.h
  171. @@ -12,9 +12,17 @@
  172. #include <efivar/efisec-types.h>
  173. #include <efivar/efisec-secdb.h>
  174. +#ifdef __cplusplus
  175. +extern "C" {
  176. +#endif
  177. +
  178. extern uint32_t efi_get_libefisec_version(void)
  179. __attribute__((__visibility__("default")));
  180. +#ifdef __cplusplus
  181. +} /* extern "C" */
  182. +#endif
  183. +
  184. #endif /* EFISEC_H */
  185. // vim:fenc=utf-8:tw=75:noet
  186. diff --git a/src/include/efivar/efivar-dp.h b/src/include/efivar/efivar-dp.h
  187. index c3b34be..bfbf874 100644
  188. --- a/src/include/efivar/efivar-dp.h
  189. +++ b/src/include/efivar/efivar-dp.h
  190. @@ -8,6 +8,10 @@
  191. #include <limits.h>
  192. +#ifdef __cplusplus
  193. +extern "C" {
  194. +#endif
  195. +
  196. #define efidp_encode_bitfield_(name, shift, mask) \
  197. (((name) << (shift)) & (mask))
  198. #define efidp_decode_bitfield_(value, name, shift, mask) \
  199. @@ -1213,6 +1217,11 @@ extern ssize_t efidp_make_generic(uint8_t *buf, ssize_t size, uint8_t type,
  200. #if defined(__clang__)
  201. #pragma clang diagnostic pop
  202. #endif
  203. +
  204. +#ifdef __cplusplus
  205. +} /* extern "C" */
  206. +#endif
  207. +
  208. #endif /* _EFIVAR_DP_H */
  209. // vim:fenc=utf-8:tw=75:noet
  210. diff --git a/src/include/efivar/efivar-time.h b/src/include/efivar/efivar-time.h
  211. index 284e5b4..6783dac 100644
  212. --- a/src/include/efivar/efivar-time.h
  213. +++ b/src/include/efivar/efivar-time.h
  214. @@ -14,6 +14,10 @@
  215. #include <stdbool.h>
  216. +#ifdef __cplusplus
  217. +extern "C" {
  218. +#endif
  219. +
  220. extern int tm_to_efi_time(const struct tm *const s, efi_time_t *d, bool tzadj);
  221. extern int efi_time_to_tm(const efi_time_t * const s, struct tm *d);
  222. @@ -29,5 +33,9 @@ extern char *efi_strptime(const char *s, const char *format, efi_time_t *time);
  223. extern size_t efi_strftime(char *s, size_t max, const char *format,
  224. const efi_time_t *time);
  225. +#ifdef __cplusplus
  226. +} /* extern "C" */
  227. +#endif
  228. +
  229. #endif /* !EFIVAR_TIME_H_ */
  230. // vim:fenc=utf-8:tw=75:noet
  231. diff --git a/src/include/efivar/efivar-types.h b/src/include/efivar/efivar-types.h
  232. index 1d48943..34b333f 100644
  233. --- a/src/include/efivar/efivar-types.h
  234. +++ b/src/include/efivar/efivar-types.h
  235. @@ -10,6 +10,10 @@
  236. #include <stdint.h>
  237. +#ifdef __cplusplus
  238. +extern "C" {
  239. +#endif
  240. +
  241. typedef struct {
  242. uint32_t a;
  243. uint16_t b;
  244. @@ -95,5 +99,9 @@ typedef struct {
  245. #define EFI_VARIABLE_APPEND_WRITE ((uint64_t)0x0000000000000040)
  246. #define EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS ((uint64_t)0x0000000000000080)
  247. +#ifdef __cplusplus
  248. +} /* extern "C" */
  249. +#endif
  250. +
  251. #endif /* EFI_TYPES_H */
  252. // vim:fenc=utf-8:tw=75:noet
  253. diff --git a/src/include/efivar/efivar.h b/src/include/efivar/efivar.h
  254. index 7518a32..91a8323 100644
  255. --- a/src/include/efivar/efivar.h
  256. +++ b/src/include/efivar/efivar.h
  257. @@ -24,6 +24,10 @@
  258. #include <efivar/efivar-guids.h>
  259. #endif
  260. +#ifdef __cplusplus
  261. +extern "C" {
  262. +#endif
  263. +
  264. #define EFI_VARIABLE_HAS_AUTH_HEADER 0x0000000100000000
  265. #define EFI_VARIABLE_HAS_SIGNATURE 0x0000000200000000
  266. @@ -200,6 +204,10 @@ extern FILE * efi_get_logfile(void)
  267. extern uint32_t efi_get_libefivar_version(void)
  268. __attribute__((__visibility__("default")));
  269. +#ifdef __cplusplus
  270. +} /* extern "C" */
  271. +#endif
  272. +
  273. #include <efivar/efivar-dp.h>
  274. #include <efivar/efivar-time.h>
  275. diff --git a/src/makeguids.c b/src/makeguids.c
  276. index e4ff411..376bffb 100644
  277. --- a/src/makeguids.c
  278. +++ b/src/makeguids.c
  279. @@ -163,6 +163,10 @@ main(int argc, char *argv[])
  280. char *strtab = guidnames->strtab;
  281. fprintf(header, "#ifndef EFIVAR_GUIDS_H\n#define EFIVAR_GUIDS_H 1\n\n");
  282. + fprintf(header, "\
  283. +#ifdef __cplusplus\n\
  284. +extern \"C\" {\n\
  285. +#endif\n");
  286. fprintf(header, "\n\
  287. struct efivar_guidname {\n\
  288. efi_guid_t guid;\n\
  289. @@ -283,6 +287,10 @@ struct efivar_guidname {\n\
  290. * Emit the end from here as well.
  291. */
  292. + fprintf(header, "\n\
  293. +#ifdef __cplusplus\n\
  294. +} /* extern \"C\" */\n\
  295. +#endif\n");
  296. fprintf(header, "\n#endif /* EFIVAR_GUIDS_H */\n");
  297. fclose(header);
  298. --
  299. 2.20.1
  300. From 15622b7e5761f3dde3f0e42081380b2b41639a48 Mon Sep 17 00:00:00 2001
  301. From: itd0 <69421122+itd0@users.noreply.github.com>
  302. Date: Mon, 4 Apr 2022 19:59:58 +0200
  303. Subject: [PATCH 3/7] Avoid format error on i686
  304. On i686 definition of off_t and ssize_t differ. Update format length
  305. modifiers as needed to avoid format errors with GCC.
  306. Signed-off-by: itd0 <69421122+itd0@users.noreply.github.com>
  307. ---
  308. src/esl-iter.c | 18 +++++++++---------
  309. src/secdb-dump.c | 2 +-
  310. 2 files changed, 10 insertions(+), 10 deletions(-)
  311. diff --git a/src/esl-iter.c b/src/esl-iter.c
  312. index 26b5cb5..4a1938a 100644
  313. --- a/src/esl-iter.c
  314. +++ b/src/esl-iter.c
  315. @@ -308,13 +308,13 @@ esl_list_iter_next_with_size_correction(esl_list_iter *iter, efi_guid_t *type,
  316. return -1;
  317. }
  318. if (iter->offset < 0) {
  319. - efi_error("iter->offset (%zd) < 0", iter->offset);
  320. + efi_error("iter->offset (%jd) < 0", (intmax_t)iter->offset);
  321. errno = EINVAL;
  322. return -1;
  323. }
  324. if ((uint32_t)iter->offset >= iter->len) {
  325. - efi_error("iter->offset (%zd) >= iter->len (%zd)",
  326. - iter->offset, iter->len);
  327. + efi_error("iter->offset (%jd) >= iter->len (%zd)",
  328. + (intmax_t)iter->offset, iter->len);
  329. errno = EINVAL;
  330. return -1;
  331. }
  332. @@ -335,9 +335,9 @@ esl_list_iter_next_with_size_correction(esl_list_iter *iter, efi_guid_t *type,
  333. iter->len - iter->offset, iter->len - iter->offset,
  334. iter->esl->signature_list_size, iter->esl->signature_list_size);
  335. if (correct_size && (iter->len - iter->offset) > 0) {
  336. - warnx("correcting ESL size from %d to %zd at %lx",
  337. + warnx("correcting ESL size from %d to %jd at %lx",
  338. iter->esl->signature_list_size,
  339. - iter->len - iter->offset, iter->offset);
  340. + (intmax_t)(iter->len - iter->offset), iter->offset);
  341. debug("correcting ESL size from %d to %zd at %lx",
  342. iter->esl->signature_list_size,
  343. iter->len - iter->offset, iter->offset);
  344. @@ -360,9 +360,9 @@ esl_list_iter_next_with_size_correction(esl_list_iter *iter, efi_guid_t *type,
  345. if (iter->len - iter->offset < iter->esl->signature_list_size) {
  346. debug("EFI_SIGNATURE_LIST is malformed");
  347. if (correct_size && (iter->len - iter->offset) > 0) {
  348. - warnx("correcting ESL size from %d to %zd at 0x%lx",
  349. + warnx("correcting ESL size from %d to %jd at 0x%lx",
  350. iter->esl->signature_list_size,
  351. - iter->len - iter->offset, iter->offset);
  352. + (intmax_t)(iter->len - iter->offset), iter->offset);
  353. debug("correcting ESL size from %d to %zd at 0x%lx",
  354. iter->esl->signature_list_size,
  355. iter->len - iter->offset, iter->offset);
  356. @@ -411,9 +411,9 @@ esl_list_iter_next_with_size_correction(esl_list_iter *iter, efi_guid_t *type,
  357. if (iter->esl->signature_list_size > iter->len - iter->offset) {
  358. debug("EFI_SIGNATURE_LIST is malformed");
  359. if (correct_size && (iter->len - iter->offset) > 0) {
  360. - warnx("correcting ESL size from %d to %zd at 0x%lx",
  361. + warnx("correcting ESL size from %d to %jd at 0x%lx",
  362. iter->esl->signature_list_size,
  363. - iter->len - iter->offset, iter->offset);
  364. + (intmax_t)(iter->len - iter->offset), iter->offset);
  365. debug("correcting ESL size from %d to %zd at 0x%lx",
  366. iter->esl->signature_list_size,
  367. iter->len - iter->offset, iter->offset);
  368. diff --git a/src/secdb-dump.c b/src/secdb-dump.c
  369. index 02fb915..17f6441 100644
  370. --- a/src/secdb-dump.c
  371. +++ b/src/secdb-dump.c
  372. @@ -248,7 +248,7 @@ secdb_dump(efi_secdb_t *secdb, bool annotations)
  373. esln += 1;
  374. }
  375. secdb_dump_finish();
  376. - printf("%08lx\n", offset);
  377. + printf("%08zx\n", offset);
  378. fflush(stdout);
  379. }
  380. --
  381. 2.20.1
  382. From aab4e9b10ac9e98588a1b19771cf6f4c8c0a3096 Mon Sep 17 00:00:00 2001
  383. From: Wei Fu <wefu@redhat.com>
  384. Date: Sat, 2 Apr 2022 22:53:36 +0800
  385. Subject: [PATCH 4/7] Fix the -march issue for riscv64
  386. There is an issue on riscv64 system when compiling it natively:
  387. gcc: error: '-march=native': ISA string must begin with rv32 or rv64
  388. This patch set HOST_MARCH= like ia64 to resolve the issue.
  389. Signed-off-by: Wei Fu <wefu@redhat.com>
  390. ---
  391. src/include/defaults.mk | 4 ++++
  392. 1 file changed, 4 insertions(+)
  393. diff --git a/src/include/defaults.mk b/src/include/defaults.mk
  394. index 632b155..b8cc590 100644
  395. --- a/src/include/defaults.mk
  396. +++ b/src/include/defaults.mk
  397. @@ -73,10 +73,14 @@ override SOFLAGS = $(_SOFLAGS) \
  398. HOST_ARCH=$(shell uname -m)
  399. ifneq ($(HOST_ARCH),ia64)
  400. +ifneq ($(HOST_ARCH),riscv64)
  401. HOST_MARCH=-march=native
  402. else
  403. HOST_MARCH=
  404. endif
  405. +else
  406. + HOST_MARCH=
  407. +endif
  408. HOST_CPPFLAGS ?= $(CPPFLAGS)
  409. override _HOST_CPPFLAGS := $(HOST_CPPFLAGS)
  410. override HOST_CPPFLAGS = $(_HOST_CPPFLAGS) \
  411. --
  412. 2.20.1
  413. From cece3ffd5be2f8641eb694513f2b73e5eb97ffd3 Mon Sep 17 00:00:00 2001
  414. From: Natanael Copa <ncopa@alpinelinux.org>
  415. Date: Fri, 28 Jan 2022 12:13:30 +0100
  416. Subject: [PATCH 5/7] efisecdb: fix build with musl libc
  417. Refactor code to use POSIX atexit(3) instead of the GNU specific
  418. on_exit(3).
  419. Resolves: #197
  420. Resolves: #202
  421. Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
  422. ---
  423. src/compiler.h | 2 --
  424. src/efisecdb.c | 68 +++++++++++++++++++-------------------------------
  425. 2 files changed, 26 insertions(+), 44 deletions(-)
  426. diff --git a/src/compiler.h b/src/compiler.h
  427. index e2f18f0..d95fb01 100644
  428. --- a/src/compiler.h
  429. +++ b/src/compiler.h
  430. @@ -7,8 +7,6 @@
  431. #ifndef COMPILER_H_
  432. #define COMPILER_H_
  433. -#include <sys/cdefs.h>
  434. -
  435. /* GCC version checking borrowed from glibc. */
  436. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  437. # define GNUC_PREREQ(maj,min) \
  438. diff --git a/src/efisecdb.c b/src/efisecdb.c
  439. index f882373..6bd5ad9 100644
  440. --- a/src/efisecdb.c
  441. +++ b/src/efisecdb.c
  442. @@ -25,6 +25,10 @@
  443. extern char *optarg;
  444. extern int optind, opterr, optopt;
  445. +static efi_secdb_t *secdb = NULL;
  446. +static list_t infiles;
  447. +static list_t actions;
  448. +
  449. struct hash_param {
  450. char *name;
  451. efi_secdb_type_t algorithm;
  452. @@ -187,12 +191,11 @@ add_action(list_t *list, action_type_t action_type, const efi_guid_t *owner,
  453. }
  454. static void
  455. -free_actions(int status UNUSED, void *actionsp)
  456. +free_actions(void)
  457. {
  458. - list_t *actions = (list_t *)actionsp;
  459. list_t *pos, *tmp;
  460. - for_each_action_safe(pos, tmp, actions) {
  461. + for_each_action_safe(pos, tmp, &actions) {
  462. action_t *action = list_entry(pos, action_t, list);
  463. list_del(&action->list);
  464. @@ -202,12 +205,11 @@ free_actions(int status UNUSED, void *actionsp)
  465. }
  466. static void
  467. -free_infiles(int status UNUSED, void *infilesp)
  468. +free_infiles(void)
  469. {
  470. - list_t *infiles = (list_t *)infilesp;
  471. list_t *pos, *tmp;
  472. - for_each_ptr_safe(pos, tmp, infiles) {
  473. + for_each_ptr_safe(pos, tmp, &infiles) {
  474. ptrlist_t *entry = list_entry(pos, ptrlist_t, list);
  475. list_del(&entry->list);
  476. @@ -216,27 +218,12 @@ free_infiles(int status UNUSED, void *infilesp)
  477. }
  478. static void
  479. -maybe_free_secdb(int status UNUSED, void *voidp)
  480. +maybe_free_secdb(void)
  481. {
  482. - efi_secdb_t **secdbp = (efi_secdb_t **)voidp;
  483. -
  484. - if (secdbp == NULL || *secdbp == NULL)
  485. + if (secdb == NULL)
  486. return;
  487. - efi_secdb_free(*secdbp);
  488. -}
  489. -
  490. -static void
  491. -maybe_do_unlink(int status, void *filep)
  492. -{
  493. - char **file = (char **)filep;
  494. -
  495. - if (status == 0)
  496. - return;
  497. - if (file == NULL || *file == NULL)
  498. - return;
  499. -
  500. - unlink(*file);
  501. + efi_secdb_free(secdb);
  502. }
  503. static void
  504. @@ -323,15 +310,6 @@ parse_input_files(list_t *infiles, char **outfile, efi_secdb_t **secdb,
  505. return status;
  506. }
  507. -/*
  508. - * These need to be static globals so that they're not on main's stack when
  509. - * on_exit() fires.
  510. - */
  511. -static efi_secdb_t *secdb = NULL;
  512. -static list_t infiles;
  513. -static list_t actions;
  514. -static char *outfile = NULL;
  515. -
  516. int
  517. main(int argc, char *argv[])
  518. {
  519. @@ -351,6 +329,7 @@ main(int argc, char *argv[])
  520. bool do_sort_data = false;
  521. bool sort_descending = false;
  522. int status = 0;
  523. + char *outfile = NULL;
  524. const char sopts[] = ":aAc:dfg:h:i:Lo:rs:t:v?";
  525. const struct option lopts[] = {
  526. @@ -376,10 +355,9 @@ main(int argc, char *argv[])
  527. INIT_LIST_HEAD(&infiles);
  528. INIT_LIST_HEAD(&actions);
  529. - on_exit(free_actions, &actions);
  530. - on_exit(free_infiles, &infiles);
  531. - on_exit(maybe_free_secdb, &secdb);
  532. - on_exit(maybe_do_unlink, &outfile);
  533. + atexit(free_actions);
  534. + atexit(free_infiles);
  535. + atexit(maybe_free_secdb);
  536. /*
  537. * parse the command line.
  538. @@ -587,24 +565,30 @@ sort_err:
  539. outfd = open(outfile, flags, 0600);
  540. if (outfd < 0) {
  541. char *tmpoutfile = outfile;
  542. - if (errno == EEXIST)
  543. - outfile = NULL;
  544. + if (errno != EEXIST)
  545. + unlink(outfile);
  546. err(1, "could not open \"%s\"", tmpoutfile);
  547. }
  548. rc = ftruncate(outfd, 0);
  549. - if (rc < 0)
  550. + if (rc < 0) {
  551. + unlink(outfile);
  552. err(1, "could not truncate output file \"%s\"", outfile);
  553. + }
  554. void *output;
  555. size_t size = 0;
  556. rc = efi_secdb_realize(secdb, &output, &size);
  557. - if (rc < 0)
  558. + if (rc < 0) {
  559. + unlink(outfile);
  560. secdb_err(1, "could not realize signature list");
  561. + }
  562. rc = write(outfd, output, size);
  563. - if (rc < 0)
  564. + if (rc < 0) {
  565. + unlink(outfile);
  566. err(1, "could not write signature list");
  567. + }
  568. close(outfd);
  569. xfree(output);
  570. --
  571. 2.20.1
  572. From df09b472419466987f2f30176dd00937e640aa9a Mon Sep 17 00:00:00 2001
  573. From: Natanael Copa <ncopa@alpinelinux.org>
  574. Date: Fri, 28 Jan 2022 12:29:00 +0100
  575. Subject: [PATCH 6/7] efisecdb: do not free optarg
  576. The *outfile passed to parse_input_files can only be either set to
  577. optarg or be NULL. optarg should not be free'd and NULL does not need
  578. to.
  579. Since we no longer use on_exit to unlink outfile we also don't need to
  580. set *outfile to NULL.
  581. Fixes commit d91787035bc1 (efisecdb: add efisecdb)
  582. Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
  583. ---
  584. src/efisecdb.c | 7 ++-----
  585. 1 file changed, 2 insertions(+), 5 deletions(-)
  586. diff --git a/src/efisecdb.c b/src/efisecdb.c
  587. index 6bd5ad9..70fa184 100644
  588. --- a/src/efisecdb.c
  589. +++ b/src/efisecdb.c
  590. @@ -255,8 +255,7 @@ list_guids(void)
  591. * failure.
  592. */
  593. static int
  594. -parse_input_files(list_t *infiles, char **outfile, efi_secdb_t **secdb,
  595. - bool dump)
  596. +parse_input_files(list_t *infiles, efi_secdb_t **secdb, bool dump)
  597. {
  598. int status = 0;
  599. list_t *pos, *tmp;
  600. @@ -297,8 +296,6 @@ parse_input_files(list_t *infiles, char **outfile, efi_secdb_t **secdb,
  601. if (!dump)
  602. exit(1);
  603. status = 1;
  604. - xfree(*outfile);
  605. - *outfile = NULL;
  606. break;
  607. }
  608. }
  609. @@ -528,7 +525,7 @@ sort_err:
  610. efi_secdb_set_bool(secdb, EFI_SECDB_SORT_DATA, do_sort_data);
  611. efi_secdb_set_bool(secdb, EFI_SECDB_SORT_DESCENDING, sort_descending);
  612. - status = parse_input_files(&infiles, &outfile, &secdb, dump);
  613. + status = parse_input_files(&infiles, &secdb, dump);
  614. if (status == 0) {
  615. for_each_action_safe(pos, tmp, &actions) {
  616. action_t *action = list_entry(pos, action_t, list);
  617. --
  618. 2.20.1
  619. From 6be2cb1c0139ac177e754b0767abf1ca1533847f Mon Sep 17 00:00:00 2001
  620. From: Robbie Harwood <rharwood@redhat.com>
  621. Date: Mon, 18 Apr 2022 13:08:18 -0400
  622. Subject: [PATCH 7/7] Fix invalid free in main()
  623. data is allocated by mmap() in prepare_data().
  624. Resolves: #173
  625. Signed-off-by: Robbie Harwood <rharwood@redhat.com>
  626. ---
  627. src/efivar.c | 2 +-
  628. 1 file changed, 1 insertion(+), 1 deletion(-)
  629. diff --git a/src/efivar.c b/src/efivar.c
  630. index 5cd1eb2..09f85ed 100644
  631. --- a/src/efivar.c
  632. +++ b/src/efivar.c
  633. @@ -633,7 +633,7 @@ int main(int argc, char *argv[])
  634. if (sz < 0)
  635. err(1, "Could not import data from \"%s\"", infile);
  636. - free(data);
  637. + munmap(data, data_size);
  638. data = NULL;
  639. data_size = 0;
  640. --
  641. 2.20.1