Skip to contentSkip to conversation

simfs: Fix reads beyond the first block

PATCH In review Slava Monich 2 participants 4 messages Jul 30, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 simfs: Fix reads beyond the first block

---
 src/simfs.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
src/simfs.c +8 -9
index 3d4f6283..cf770265 100644
@@ -383,18 +383,18 @@ static void sim_fs_op_read_block_cb(const struct ofono_error *error,
 	}
 
 	start_block = op->offset / 256;
-	end_block = (op->offset + (op->num_bytes - 1)) / 256;
+	end_block = op->num_bytes ? (op->offset + op->num_bytes - 1) / 256 :
+								start_block;
 
 	if (op->current == start_block) {
 		bufoff = 0;
 		dataoff = op->offset % 256;
-		tocopy = MIN(256 - op->offset % 256,
-				op->num_bytes - op->current * 256);
+		tocopy = MIN(256 - dataoff, op->num_bytes);
 	} else {
-		bufoff = (op->current - start_block - 1) * 256 +
+		bufoff = (op->current - start_block) * 256 -
 				op->offset % 256;
 		dataoff = 0;
-		tocopy = MIN(256, op->num_bytes - op->current * 256);
+		tocopy = MIN(256, op->num_bytes - bufoff);
 	}
 
 	DBG("bufoff: %d, dataoff: %d, tocopy: %d",
@@ -463,13 +463,12 @@ static gboolean sim_fs_op_read_block(gpointer user_data)
 			bufoff = 0;
 			seekoff = SIM_CACHE_HEADER_SIZE + op->current * 256 +
 				op->offset % 256;
-			toread = MIN(256 - op->offset % 256,
-					op->num_bytes - op->current * 256);
+			toread = MIN(256 - op->offset % 256, op->num_bytes);
 		} else {
-			bufoff = (op->current - start_block - 1) * 256 +
+			bufoff = (op->current - start_block) * 256 -
 					op->offset % 256;
 			seekoff = SIM_CACHE_HEADER_SIZE + op->current * 256;
-			toread = MIN(256, op->num_bytes - op->current * 256);
+			toread = MIN(256, op->num_bytes - bufoff);
 		}
 
 		DBG("bufoff: %d, seekoff: %d, toread: %d",
Signature · 1 line
2.25.1
#1 lore raw

Hi Slava,

Quoted 4 lines · Slava Monich
---
  src/simfs.c | 17 ++++++++---------
  1 file changed, 8 insertions(+), 9 deletions(-)

Funny how long this bug has been lurking around.

src/simfs.c static void sim_fs_op_read_block_cb(const struct ofono_error *error, · line 388

Quoted 12 lines
src/simfs.c +2 -1
index 3d4f6283..cf770265 100644
@@ -383,18 +383,18 @@ static void sim_fs_op_read_block_cb(const struct ofono_error *error,
  	}
 
  	start_block = op->offset / 256;
-	end_block = (op->offset + (op->num_bytes - 1)) / 256;
+	end_block = op->num_bytes ? (op->offset + op->num_bytes - 1) / 256 :
+								start_block;
 

Curious why this is needed? op->num_bytes should never be zero since it gets set to the file length?

Rest looks good to me.

Regards,
-Denis

#2 lore raw
Quoted 10 lines · Denis Kenzior

Hi Slava,

Quoted 4 lines · Slava Monich

---
  src/simfs.c | 17 ++++++++---------

  1 file changed, 8 insertions(+), 9 deletions(-)

Funny how long this bug has been lurking around.

Until we finally had a crash on reading an icon or something out of SIM. Which most SIMs apparently don't have or else it would've been noticed earlier.

Quoted 7 lines
Quoted 7 lines
src/simfs.c +0 -0
index 3d4f6283..cf770265 100644
@@ -383,18 +383,18 @@ static void sim_fs_op_read_block_cb(const 
 struct ofono_error *error,
       }
Quoted 9 lines
Quoted 5 lines
src/simfs.c +1 -1
         start_block = op->offset / 256;
-    end_block = (op->offset + (op->num_bytes - 1)) / 256;
+    end_block = op->num_bytes ? (op->offset + op->num_bytes - 1) / 

256 :
+                                start_block;

Curious why this is needed?  op->num_bytes should never be zero since it gets set to the file length?

I admit that it's a bit paranoid, but op->num_bytes is assigned without checking and I figured that it wouldn't hurt to do a check here. Feel free to drop this part if it looks like too much of an overkill to you.

Quoted 4 lines

Rest looks good to me.

Regards,
-Denis

Cheers,

-Slava

#3 lore raw

Hi Slava,

Quoted 9 lines · Slava Monich
Quoted 8 lines · Denis Kenzior

Hi Slava,

Quoted 4 lines · Slava Monich

---
  src/simfs.c | 17 ++++++++---------

  1 file changed, 8 insertions(+), 9 deletions(-)
Quoted 6 lines
Quoted 2 lines

Funny how long this bug has been lurking around.

Until we finally had a crash on reading an icon or something out of SIM. Which most SIMs apparently don't have or else it would've been noticed earlier.

Figured :)

Quoted 17 lines
Quoted 16 lines

src/simfs.c static void sim_fs_op_read_block_cb(const struct · line 387

Quoted 12 lines
src/simfs.c +2 -1
index 3d4f6283..cf770265 100644
@@ -383,18 +383,18 @@ static void sim_fs_op_read_block_cb(const struct 
 ofono_error *error,
       }
         start_block = op->offset / 256;
-    end_block = (op->offset + (op->num_bytes - 1)) / 256;
+    end_block = op->num_bytes ? (op->offset + op->num_bytes - 1) / 256 :
+                                start_block;

Curious why this is needed?  op->num_bytes should never be zero since it gets set to the file length?

Quoted 4 lines

I admit that it's a bit paranoid, but op->num_bytes is assigned without checking and I figured that it wouldn't hurt to do a check here. Feel free to drop this part if it looks like too much of an overkill to you.

I'm all for being paranoid, but there's not much sense doing a round-trip to the SIM for a 0 byte read. So I rather we catch this elsewhere.

There's some sanity checking of num_bytes being 0 once the file info has been obtained. It should be reset to file length if it is zero, which is generally what we want for simple binary files.

Invocations of ofono_sim_read_bytes() should probably sanity check the length if they want to be fully paranoid.

I went ahead and applied this patch without this particular chunk.

Regards,
-Denis