From: Christian Marangi <ansuelsmth@gmail.com>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>,
Eric Biederman <ebiederm@xmission.com>,
Kees Cook <keescook@chromium.org>,
Mark Brown <broonie@kernel.org>,
Dave Martin <Dave.Martin@arm.com>,
Catalin Marinas <catalin.marinas@arm.com>,
linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Cc: Christian Marangi <ansuelsmth@gmail.com>, stable@vger.kernel.org
Subject: [PATCH] binfmt_elf: dynamically allocate note.data in parse_elf_properties
Date: Wed, 7 Jun 2023 16:42:27 +0200 [thread overview]
Message-ID: <20230607144227.8956-1-ansuelsmth@gmail.com> (raw)
Dynamically allocate note.data in parse_elf_properties to fix
compilation warning on some arch.
On some arch note.data exceed the stack limit for a single function and
this cause the following compilation warning:
fs/binfmt_elf.c: In function 'parse_elf_properties.isra':
fs/binfmt_elf.c:821:1: error: the frame size of 1040 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]
821 | }
| ^
cc1: all warnings being treated as errors
Fix this by dynamically allocating the array.
Update the sizeof of the union to the biggest element allocated.
Fixes: 00e19ceec80b ("ELF: Add ELF program property parsing support")
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Cc: stable@vger.kernel.org # v5.8+
---
fs/binfmt_elf.c | 36 +++++++++++++++++++++++++-----------
1 file changed, 25 insertions(+), 11 deletions(-)
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 44b4c42ab8e8..90daa623ca13 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -768,7 +768,7 @@ static int parse_elf_properties(struct file *f, const struct elf_phdr *phdr,
{
union {
struct elf_note nhdr;
- char data[NOTE_DATA_SZ];
+ char *data;
} note;
loff_t pos;
ssize_t n;
@@ -785,29 +785,41 @@ static int parse_elf_properties(struct file *f, const struct elf_phdr *phdr,
return -ENOEXEC;
/* If the properties are crazy large, that's too bad (for now): */
- if (phdr->p_filesz > sizeof(note))
+ if (phdr->p_filesz > sizeof(*note.data) * NOTE_DATA_SZ)
return -ENOEXEC;
+ note.data = kcalloc(NOTE_DATA_SZ, sizeof(*note.data), GFP_KERNEL);
+ if (!note.data)
+ return -ENOMEM;
+
pos = phdr->p_offset;
n = kernel_read(f, ¬e, phdr->p_filesz, &pos);
- BUILD_BUG_ON(sizeof(note) < sizeof(note.nhdr) + NOTE_NAME_SZ);
- if (n < 0 || n < sizeof(note.nhdr) + NOTE_NAME_SZ)
- return -EIO;
+ BUILD_BUG_ON(sizeof(*note.data) * NOTE_DATA_SZ < sizeof(note.nhdr) + NOTE_NAME_SZ);
+ if (n < 0 || n < sizeof(note.nhdr) + NOTE_NAME_SZ) {
+ ret = -EIO;
+ goto exit;
+ }
if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
note.nhdr.n_namesz != NOTE_NAME_SZ ||
strncmp(note.data + sizeof(note.nhdr),
- GNU_PROPERTY_TYPE_0_NAME, n - sizeof(note.nhdr)))
- return -ENOEXEC;
+ GNU_PROPERTY_TYPE_0_NAME, n - sizeof(note.nhdr))) {
+ ret = -ENOEXEC;
+ goto exit;
+ }
off = round_up(sizeof(note.nhdr) + NOTE_NAME_SZ,
ELF_GNU_PROPERTY_ALIGN);
- if (off > n)
- return -ENOEXEC;
+ if (off > n) {
+ ret = -ENOEXEC;
+ goto exit;
+ }
- if (note.nhdr.n_descsz > n - off)
- return -ENOEXEC;
+ if (note.nhdr.n_descsz > n - off) {
+ ret = -ENOEXEC;
+ goto exit;
+ }
datasz = off + note.nhdr.n_descsz;
have_prev_type = false;
@@ -817,6 +829,8 @@ static int parse_elf_properties(struct file *f, const struct elf_phdr *phdr,
have_prev_type = true;
} while (!ret);
+exit:
+ kfree(note.data);
return ret == -ENOENT ? 0 : ret;
}
--
2.39.2
next reply other threads:[~2023-06-07 14:42 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-07 14:42 Christian Marangi [this message]
2023-06-07 21:19 ` [PATCH] binfmt_elf: dynamically allocate note.data in parse_elf_properties Kees Cook
2023-06-07 18:31 ` Christian Marangi
2023-06-07 23:37 ` Kees Cook
2023-06-12 8:08 ` Eric W. Biederman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230607144227.8956-1-ansuelsmth@gmail.com \
--to=ansuelsmth@gmail.com \
--cc=Dave.Martin@arm.com \
--cc=brauner@kernel.org \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=ebiederm@xmission.com \
--cc=keescook@chromium.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=stable@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.