130-binfmt_elf-dynamically-allocate-note.data-in-parse_e.patch 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. From ca71e00839fcdd26f122fb6d9e97903c9fe198f7 Mon Sep 17 00:00:00 2001
  2. From: Christian Marangi <[email protected]>
  3. Date: Sat, 6 May 2023 08:08:35 +0200
  4. Subject: [PATCH] binfmt_elf: dynamically allocate note.data in
  5. parse_elf_properties
  6. Dynamically allocate note.data in parse_elf_properties to fix
  7. compilation warning on some arch.
  8. On some arch note.data exceet the stack limit for a single function and
  9. this cause the following compilation warning:
  10. fs/binfmt_elf.c: In function 'parse_elf_properties.isra':
  11. fs/binfmt_elf.c:821:1: error: the frame size of 1040 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]
  12. 821 | }
  13. | ^
  14. cc1: all warnings being treated as errors
  15. Fix this by dynamically allocating the array.
  16. Fixes: 00e19ceec80b ("ELF: Add ELF program property parsing support")
  17. Signed-off-by: Christian Marangi <[email protected]>
  18. Cc: [email protected] # v5.8+
  19. ---
  20. fs/binfmt_elf.c | 32 +++++++++++++++++++++++---------
  21. 1 file changed, 23 insertions(+), 9 deletions(-)
  22. --- a/fs/binfmt_elf.c
  23. +++ b/fs/binfmt_elf.c
  24. @@ -768,7 +768,7 @@ static int parse_elf_properties(struct f
  25. {
  26. union {
  27. struct elf_note nhdr;
  28. - char data[NOTE_DATA_SZ];
  29. + char *data;
  30. } note;
  31. loff_t pos;
  32. ssize_t n;
  33. @@ -788,26 +788,38 @@ static int parse_elf_properties(struct f
  34. if (phdr->p_filesz > sizeof(note))
  35. return -ENOEXEC;
  36. + note.data = kcalloc(NOTE_DATA_SZ, sizeof(*note.data), GFP_KERNEL);
  37. + if (!note.data)
  38. + return -ENOMEM;
  39. +
  40. pos = phdr->p_offset;
  41. n = kernel_read(f, &note, phdr->p_filesz, &pos);
  42. BUILD_BUG_ON(sizeof(note) < sizeof(note.nhdr) + NOTE_NAME_SZ);
  43. - if (n < 0 || n < sizeof(note.nhdr) + NOTE_NAME_SZ)
  44. - return -EIO;
  45. + if (n < 0 || n < sizeof(note.nhdr) + NOTE_NAME_SZ) {
  46. + ret = -EIO;
  47. + goto exit;
  48. + }
  49. if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
  50. note.nhdr.n_namesz != NOTE_NAME_SZ ||
  51. strncmp(note.data + sizeof(note.nhdr),
  52. - GNU_PROPERTY_TYPE_0_NAME, n - sizeof(note.nhdr)))
  53. - return -ENOEXEC;
  54. + GNU_PROPERTY_TYPE_0_NAME, n - sizeof(note.nhdr))) {
  55. + ret = -ENOEXEC;
  56. + goto exit;
  57. + }
  58. off = round_up(sizeof(note.nhdr) + NOTE_NAME_SZ,
  59. ELF_GNU_PROPERTY_ALIGN);
  60. - if (off > n)
  61. - return -ENOEXEC;
  62. -
  63. - if (note.nhdr.n_descsz > n - off)
  64. - return -ENOEXEC;
  65. + if (off > n) {
  66. + ret = -ENOEXEC;
  67. + goto exit;
  68. + }
  69. +
  70. + if (note.nhdr.n_descsz > n - off) {
  71. + ret = -ENOEXEC;
  72. + goto exit;
  73. + }
  74. datasz = off + note.nhdr.n_descsz;
  75. have_prev_type = false;
  76. @@ -817,6 +829,8 @@ static int parse_elf_properties(struct f
  77. have_prev_type = true;
  78. } while (!ret);
  79. +exit:
  80. + kfree(note.data);
  81. return ret == -ENOENT ? 0 : ret;
  82. }