public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
From: Zhihao Cheng <chengzhihao1@huawei.com>
To: Anton Moryakov <ant.v.moryakov@gmail.com>,
	<linux-mtd@lists.infradead.org>
Subject: Re: [PATCH mtd-utils] ubi-utils: FIX DIVISION BY ZERO in ubinize.c
Date: Wed, 18 Dec 2024 10:24:32 +0800	[thread overview]
Message-ID: <c376dd50-e92f-3658-df09-a8e6e6ba45f7@huawei.com> (raw)
In-Reply-To: <20241217140006.268082-1-ant.v.moryakov@gmail.com>

在 2024/12/17 22:00, Anton Moryakov 写道:
> Report of the static analyzer:
> Variable vi->alignment, whose possible value set allows a zero value at ubinize.c:375, is used as a denominator at ubinize.c:410.
> 
> If you look at the code more closely, it will be clear that the vi->alignment parameter is obtained from an external file passed as a command line argument.
> 
> A check was also performed if you pass a test.ini file of the following type to the input:
> 
> [jffs2-volume]
> mode=ubi
> image=../jffs2.img
> vol_id=1
> vol_size=30MiB
> vol_type=dynamic
> vol_name=jffs2_volume
> vol_flags=autoresize
> vol_alignment=0
> 
> and execute the command:
> 
> ./ubinize -o ubi.img -p 16KiB -m 512 -s 256 test.ini
> 
> we will get the result:
> 
> Floating point exception (core dumped)
> 
> Corrections explained:
> 
> Triggers found by static analyzer Svace.
> 
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
> 
> ---
>   ubi-utils/ubinize.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
> 
> diff --git a/ubi-utils/ubinize.c b/ubi-utils/ubinize.c
> index ac8c1e5..afffaa0 100644
> --- a/ubi-utils/ubinize.c
> +++ b/ubi-utils/ubinize.c
> @@ -406,6 +406,14 @@ static int read_section(const struct ubigen_info *ui, const char *sname,
>   		}
>   	}
>   
> +
> +	/* Validate alignment to avoid division by zero */
> +	if (vi->alignment <= 0) {
> +		fprintf(stderr, "Error: alignment value (%d) is invalid. It must be greater than 0.\n", vi->alignment);
> +		vi->alignment = 1; /* Set a safe default value */
> +		fprintf(stderr, "Warning: alignment set to default value (1).\n");
> +	}
> +

How about changing like this?
diff --git a/ubi-utils/ubinize.c b/ubi-utils/ubinize.c
index ac8c1e5..9c950b1 100644
--- a/ubi-utils/ubinize.c
+++ b/ubi-utils/ubinize.c
@@ -375,8 +375,8 @@ static int read_section(const struct ubigen_info 
*ui, const char *sname,
         vi->alignment = iniparser_getint(args.dict, buf, -1);
         if (vi->alignment == -1)
                 vi->alignment = 1;
-       else if (vi->id < 0)
-               return errmsg("negative volume alignment %d in section 
\"%s\"",
+       else if (vi->alignment <= 0)
+               return errmsg("not positive volume alignment %d in 
section \"%s\"",
                               vi->alignment, sname);

         verbose(args.verbose, "volume alignment: %d", vi->alignment);

I find that the 'vi->id' is misspelled as 'vi->alignment'.
>   	/* Initialize the rest of the volume information */
>   	vi->data_pad = ui->leb_size % vi->alignment;
>   	vi->usable_leb_size = ui->leb_size - vi->data_pad;
> 


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2024-12-18  2:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17 14:00 [PATCH mtd-utils] ubi-utils: FIX DIVISION BY ZERO in ubinize.c Anton Moryakov
2024-12-18  2:24 ` Zhihao Cheng [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-12-24 19:40 Anton Moryakov
2024-12-25  1:19 ` Zhihao Cheng

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=c376dd50-e92f-3658-df09-a8e6e6ba45f7@huawei.com \
    --to=chengzhihao1@huawei.com \
    --cc=ant.v.moryakov@gmail.com \
    --cc=linux-mtd@lists.infradead.org \
    /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