2020-06-16 16:32:26

by Tom Rix

[permalink] [raw]
Subject: [PATCH] fat: add a check to fat_add_new_entries

From: Tom Rix <[email protected]>

Clang static analysis reports a possible null pointer dereference

fs/fat/dir.c:1255:9: warning: Dereference of undefined pointer value [core.NullDereference]
memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
^~~~~~~~~~~~~~

This is because setting of bhs[n] depends on the inner loop executing.

So add a check that the inner loop will be executed.

Signed-off-by: Tom Rix <[email protected]>
---
fs/fat/dir.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/fs/fat/dir.c b/fs/fat/dir.c
index b4ddf48fa444..3eea540486cb 100644
--- a/fs/fat/dir.c
+++ b/fs/fat/dir.c
@@ -1228,6 +1228,13 @@ static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
do {
start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
last_blknr = start_blknr + sbi->sec_per_clus;
+
+ /* overflow */
+ if (unlikely(last_blknr <= start_blknr)) {
+ err = -ENOMEM;
+ goto error_nomem;
+ }
+
while (blknr < last_blknr) {
bhs[n] = sb_getblk(sb, blknr);
if (!bhs[n]) {
--
2.18.1


2020-06-21 17:59:17

by OGAWA Hirofumi

[permalink] [raw]
Subject: Re: [PATCH] fat: add a check to fat_add_new_entries

[email protected] writes:

> start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
> last_blknr = start_blknr + sbi->sec_per_clus;
> +
> + /* overflow */
> + if (unlikely(last_blknr <= start_blknr)) {
> + err = -ENOMEM;
> + goto error_nomem;
> + }
> +

The cluster is 28bits and sec_per_clus is 8bits, so this should never
overflow actually. Is there no way to tell it to clang?

Thanks.
--
OGAWA Hirofumi <[email protected]>