From: Mark Wielaard <mark@klomp.org>
To: Aleksei Vetrov <vvvvvv@google.com>
Cc: elfutils-devel@sourceware.org, kernel-team@android.com,
maennich@google.com
Subject: Re: [PATCH v2 1/1] libdw: check that DWARF strings are null-terminated
Date: Fri, 17 Feb 2023 00:45:50 +0100 [thread overview]
Message-ID: <20230216234550.GI6028@gnu.wildebeest.org> (raw)
In-Reply-To: <77e629c6d4992af3701c2e4bd79dae2df99a1737.1676406334.git.vvvvvv@google.com>
Hi Aleksei,
On Tue, Feb 14, 2023 at 08:30:02PM +0000, Aleksei Vetrov via Elfutils-devel wrote:
> It is expected from libdw to return strings that are null-terminated to
> avoid overflowing ELF data.
>
> * Add calculation of a safe prefix inside string sections, where any
> string will be null-terminated.
>
> * Check if offset overflows the safe prefix in dwarf_formstring.
This is a very nice sanity/hardening fix.
> + /* If the section contains string data, we want to know a size of a prefix
> + where any string will be null-terminated. */
> + enum string_section_index string_section_idx = scn_to_string_section_idx[cnt];
> + if (string_section_idx < STR_SCN_IDX_last)
> + {
> + size_t size = data->d_size;
> + /* Reduce the size by the number of non-zero bytes at the end of the
> + section. */
> + while (size > 0 && *((const char *) data->d_buf + size - 1) != '\0')
> + --size;
> + result->string_section_size[string_section_idx] = size;
> + }
Checking from the end is smart. In the normal case the debug string
section will end with a zero terminator (or zero padding), so this
should be really quick.
> @@ -171,7 +174,7 @@ dwarf_formstring (Dwarf_Attribute *attrp)
> else
> off = read_8ubyte_unaligned (dbg, datap);
>
> - if (off > dbg->sectiondata[IDX_debug_str]->d_size)
> + if (off >= data_size)
> goto invalid_offset;
> }
O, the original check was actually one off.
Looks good. Pushed as is.
Thanks,
Mark
prev parent reply other threads:[~2023-02-16 23:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-14 20:00 [PATCH] " vvvvvv
2023-02-14 20:30 ` [PATCH v2 0/1] " Aleksei Vetrov
2023-02-14 20:30 ` [PATCH v2 1/1] " Aleksei Vetrov
2023-02-16 23:45 ` Mark Wielaard [this message]
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=20230216234550.GI6028@gnu.wildebeest.org \
--to=mark@klomp.org \
--cc=elfutils-devel@sourceware.org \
--cc=kernel-team@android.com \
--cc=maennich@google.com \
--cc=vvvvvv@google.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).