From: Pierrick Bouvier <pierrick.bouvier@linaro.org>
To: "Volker Rümelin" <vr_qemu@t-online.de>,
"Kevin Wolf" <kwolf@redhat.com>,
"Hanna Reitz" <hreitz@redhat.com>
Cc: Michael Tokarev <mjt@tls.msk.ru>,
qemu-block@nongnu.org, qemu-devel@nongnu.org,
qemu-trivial@nongnu.org
Subject: Re: [PATCH] vvfat: fix out of bounds array write
Date: Thu, 9 Jan 2025 17:39:35 -0800 [thread overview]
Message-ID: <a24bd0e8-d6f7-4297-90b8-b69f6b3080fc@linaro.org> (raw)
In-Reply-To: <20250105135929.6286-1-vr_qemu@t-online.de>
Hi Volker,
On 1/5/25 05:59, Volker Rümelin wrote:
> In function create_long_filname(), the array name[8 + 3] in
> struct direntry_t is used as if it were defined as name[32].
> This is intentional and works. It's nevertheless an out of
> bounds array access. To avoid this problem, this patch adds a
> struct lfn_direntry_t with multiple name arrays. A directory
> entry for a long FAT file name is significantly different from
> a directory entry for a regular FAT file name.
>
> Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
> ---
> block/vvfat.c | 55 ++++++++++++++++++++++++++++++++++++++-------------
> 1 file changed, 41 insertions(+), 14 deletions(-)
>
> diff --git a/block/vvfat.c b/block/vvfat.c
> index 8ffe8b3b9b..626c4f0163 100644
> --- a/block/vvfat.c
> +++ b/block/vvfat.c
> @@ -255,6 +255,17 @@ typedef struct direntry_t {
> uint32_t size;
> } QEMU_PACKED direntry_t;
>
> +typedef struct lfn_direntry_t {
> + uint8_t sequence;
> + uint8_t name01[10];
> + uint8_t attributes;
> + uint8_t direntry_type;
> + uint8_t sfn_checksum;
> + uint8_t name0e[12];
> + uint16_t begin;
> + uint8_t name1c[4];
> +} QEMU_PACKED lfn_direntry_t;
> +
> /* this structure are used to transparently access the files */
>
> typedef struct mapping_t {
> @@ -399,11 +410,28 @@ static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
>
> /* direntry functions */
>
> +static void write_long_filename(lfn_direntry_t *entry, int index, uint8_t c)
> +{
> + if (index < ARRAY_SIZE(entry->name01)) {
> + entry->name01[index] = c;
> + return;
> + }
> + index -= ARRAY_SIZE(entry->name01);
> + if (index < ARRAY_SIZE(entry->name0e)) {
> + entry->name0e[index] = c;
> + return;
> + }
> + index -= ARRAY_SIZE(entry->name0e);
> + if (index < ARRAY_SIZE(entry->name1c)) {
> + entry->name1c[index] = c;
> + }
> +}
> +
> static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
> {
> int number_of_entries, i;
> glong length;
> - direntry_t *entry;
> + lfn_direntry_t *entry;
>
> gunichar2 *longname = g_utf8_to_utf16(filename, -1, NULL, &length, NULL);
> if (!longname) {
> @@ -415,24 +443,23 @@ static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
>
> for(i=0;i<number_of_entries;i++) {
> entry=array_get_next(&(s->directory));
> + entry->sequence = (number_of_entries - i) | (i == 0 ? 0x40 : 0);
> entry->attributes=0xf;
> - entry->reserved[0]=0;
> + entry->direntry_type = 0;
> entry->begin=0;
> - entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
> - }
> - for(i=0;i<26*number_of_entries;i++) {
> - int offset=(i%26);
> - if(offset<10) offset=1+offset;
> - else if(offset<22) offset=14+offset-10;
> - else offset=28+offset-22;
> - entry=array_get(&(s->directory),s->directory.next-1-(i/26));
> + }
> + for (i = 0; i < 26 * number_of_entries; i++) {
> + uint8_t c;
> +
> + entry = array_get(&s->directory, s->directory.next - i / 26 - 1);
> if (i >= 2 * length + 2) {
> - entry->name[offset] = 0xff;
> + c = 0xff;
> } else if (i % 2 == 0) {
> - entry->name[offset] = longname[i / 2] & 0xff;
> + c = longname[i / 2] & 0xff;
> } else {
> - entry->name[offset] = longname[i / 2] >> 8;
> + c = longname[i / 2] >> 8;
> }
> + write_long_filename(entry, i % 26, c);
> }
> g_free(longname);
> return array_get(&(s->directory),s->directory.next-number_of_entries);
> @@ -731,7 +758,7 @@ static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
> /* calculate anew, because realloc could have taken place */
> entry_long=array_get(&(s->directory),long_index);
> while(entry_long<entry && is_long_name(entry_long)) {
> - entry_long->reserved[1]=chksum;
> + ((lfn_direntry_t *)entry_long)->sfn_checksum = chksum;
> entry_long++;
> }
> }
I tested this patch and it correctly fixes the issue reported by ubsan,
while keeping the feature working as expected.
Tested-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Thus, it can be merged (cc to qemu-trivial for quick integration).
Thank you Volker,
Pierrick
parent reply other threads:[~2025-01-10 1:39 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <20250105135929.6286-1-vr_qemu@t-online.de>]
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=a24bd0e8-d6f7-4297-90b8-b69f6b3080fc@linaro.org \
--to=pierrick.bouvier@linaro.org \
--cc=hreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=mjt@tls.msk.ru \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-trivial@nongnu.org \
--cc=vr_qemu@t-online.de \
/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 NNTP newsgroup(s).