Skip to contentSkip to conversation

cmd: sf: prevent overwriting the reserved memory

PATCH Superseded Prasad Kummari 3 participants 7 messages Sep 10, 2024 – Sep 13, 2024

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 cmd: sf: prevent overwriting the reserved memory

Added LMB API to prevent SF command from overwriting reserved memory areas. The current SPI code does not use LMB APIs for loading data into memory addresses. To resolve this, LMB APIs were added to check the load address of an SF command and ensure it does not overwrite reserved memory addresses. Similar checks are used in TFTP, serial load, and boot code to prevent overwriting reserved memory.

---

Changes in V3:
- Removed lmb_init_and_reserve() as part of latest LMB series.
- Error message moved to one place.
- lmb_alloc_addr() is not required because the given memory address is being checked to ensure it is free or not.

Changes in V2:
- Rebased the code changes on top of the next branch.

UT:
Tested on Versal NET board

Versal NET> fdt print /reserved-memory

reserved-memory {                                                               
        ranges;                                                                 
        #size-cells = <0x00000002>;                                             
        #address-cells = <0x00000002>;                                          
        tf-a {                                                                  
                reg = <0x00000000 0x70000000 0x00000000 0x00050000>;            
                no-map;                                                         
        };                                                                      
};                                                                              

Versal NET> sf read 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

relocaddr = 0x000000007febc000

Versal NET> sf read 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

 cmd/sf.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
cmd/sf.c +35 -1
index f43a2e08b3..7bb8bcfce2 100644
@@ -10,6 +10,7 @@
 #include <div64.h>
 #include <dm.h>
 #include <log.h>
+#include <lmb.h>
 #include <malloc.h>
 #include <mapmem.h>
 #include <spi.h>
@@ -272,6 +273,31 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
 	return 0;
 }
 
+#ifdef CONFIG_LMB
+static int do_spi_read_lmb_check(ulong start_addr, loff_t len)
+{
+	phys_size_t max_size;
+	ulong end_addr;
+
+	lmb_dump_all();
+
+	max_size = lmb_get_free_size(start_addr);
+	if (!max_size) {
+		return CMD_RET_FAILURE;
+	}
+
+	end_addr = start_addr + max_size;
+	if (!end_addr)
+		end_addr = ULONG_MAX;
+
+	if ((start_addr + len) > end_addr) {
+		return CMD_RET_FAILURE;
+	}
+
+	return 0;
+}
+#endif
+
 static int do_spi_flash_read_write(int argc, char *const argv[])
 {
 	unsigned long addr;
@@ -315,7 +341,15 @@ static int do_spi_flash_read_write(int argc, char *const argv[])
 		ret = spi_flash_update(flash, offset, len, buf);
 	} else if (strncmp(argv[0], "read", 4) == 0 ||
 			strncmp(argv[0], "write", 5) == 0) {
-		int read;
+		int read, ret;
+
+#ifdef CONFIG_LMB
+		ret = do_spi_read_lmb_check(addr, len);
+		if (ret) {
+			printf("ERROR: trying to overwrite reserved memory...\n");
+			return ret;
+		}
+#endif
 
 		read = strncmp(argv[0], "read", 4) == 0;
 		if (read)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 81 lines · Prasad Kummari

Added LMB API to prevent SF command from overwriting reserved memory areas. The current SPI code does not use LMB APIs for loading data into memory addresses. To resolve this, LMB APIs were added to check the load address of an SF command and ensure it does not overwrite reserved memory addresses. Similar checks are used in TFTP, serial load, and boot code to prevent overwriting reserved memory.

---

Changes in V3:
- Removed lmb_init_and_reserve() as part of latest LMB series.
- Error message moved to one place.
- lmb_alloc_addr() is not required because the given memory address is being checked to ensure it is free or not.

Changes in V2:
- Rebased the code changes on top of the next branch.

UT:
Tested on Versal NET board

Versal NET> fdt print /reserved-memory

reserved-memory {
        ranges;
        #size-cells = <0x00000002>;
        #address-cells = <0x00000002>;
        tf-a {
                reg = <0x00000000 0x70000000 0x00000000 0x00050000>;
                no-map;
        };
};

Versal NET> sf read 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

relocaddr = 0x000000007febc000

Versal NET> sf read 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

 cmd/sf.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
cmd/sf.c +11 -0
index f43a2e08b3..7bb8bcfce2 100644
@@ -10,6 +10,7 @@
 #include <div64.h>
 #include <dm.h>
 #include <log.h>
+#include <lmb.h>
 #include <malloc.h>
 #include <mapmem.h>
 #include <spi.h>
@@ -272,6 +273,31 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
        return 0;
 }
 
+#ifdef CONFIG_LMB
+static int do_spi_read_lmb_check(ulong start_addr, loff_t len)
+{
+       phys_size_t max_size;
+       ulong end_addr;
+
+       lmb_dump_all();
+
+       max_size = lmb_get_free_size(start_addr);
+       if (!max_size) {

cmd/sf.c static int spi_flash_update(struct spi_flash *flash, u32 offset, · line 295

Quoted 10 lines
cmd/sf.c +10 -0
+               return CMD_RET_FAILURE;
+       }
+
+       end_addr = start_addr + max_size;
+       if (!end_addr)
+               end_addr = ULONG_MAX;
+
+       if ((start_addr + len) > end_addr) {
+               return CMD_RET_FAILURE;
+       }

This is one way to get the load address, yes. But please do note that with this method, if the region of memory has already been marked as reserved, the call to lmb_get_free_size() will fail, i.e. return a value of 0. Whereas if you call lmb_alloc_addr(), and if the memory region is marked reserved with the flags set to LMB_NONE, that call will not fail, as we can re-reserve memory marked as LMB_NONE. Hence a better approach would be to use the logic used in the fs module function that I had pointed to in my earlier email.

Also, like I mentioned earlier, it will be better to refactor the functionality, especially if the plan is to introduce this check for loading stuff through other interfaces like nand, mmc etc. You can add a function in the lmb.c like lmb_read_check() which does that.

-sughosh

Quoted 28 lines
cmd/sf.c +14 -2
+
+       return 0;
+}
+#endif
+
 static int do_spi_flash_read_write(int argc, char *const argv[])
 {
        unsigned long addr;
@@ -315,7 +341,15 @@ static int do_spi_flash_read_write(int argc, char *const argv[])
                ret = spi_flash_update(flash, offset, len, buf);
        } else if (strncmp(argv[0], "read", 4) == 0 ||
                        strncmp(argv[0], "write", 5) == 0) {
-               int read;
+               int read, ret;
+
+#ifdef CONFIG_LMB
+               ret = do_spi_read_lmb_check(addr, len);
+               if (ret) {
+                       printf("ERROR: trying to overwrite reserved memory...\n");
+                       return ret;
+               }
+#endif
 
                read = strncmp(argv[0], "read", 4) == 0;
                if (read)
--

2.25.1

#2 lore raw
Quoted 102 lines · Sughosh Ganu
Quoted 81 lines · Prasad Kummari

Added LMB API to prevent SF command from overwriting reserved memory areas. The current SPI code does not use LMB APIs for loading data into memory addresses. To resolve this, LMB APIs were added to check the load address of an SF command and ensure it does not overwrite reserved memory addresses. Similar checks are used in TFTP, serial load, and boot code to prevent overwriting reserved memory.

---

Changes in V3:
- Removed lmb_init_and_reserve() as part of latest LMB series.
- Error message moved to one place.
- lmb_alloc_addr() is not required because the given memory address is being checked to ensure it is free or not.

Changes in V2:
- Rebased the code changes on top of the next branch.

UT:
Tested on Versal NET board

Versal NET> fdt print /reserved-memory

reserved-memory {
         ranges;
         #size-cells = <0x00000002>;
         #address-cells = <0x00000002>;
         tf-a {
                 reg = <0x00000000 0x70000000 0x00000000 0x00050000>;
                 no-map;
         };
};

Versal NET> sf read 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

relocaddr = 0x000000007febc000

Versal NET> sf read 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

  cmd/sf.c | 36 +++++++++++++++++++++++++++++++++++-
  1 file changed, 35 insertions(+), 1 deletion(-)
cmd/sf.c +11 -0
index f43a2e08b3..7bb8bcfce2 100644
@@ -10,6 +10,7 @@
  #include <div64.h>
  #include <dm.h>
  #include <log.h>
+#include <lmb.h>
  #include <malloc.h>
  #include <mapmem.h>
  #include <spi.h>
@@ -272,6 +273,31 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
         return 0;
  }
 
+#ifdef CONFIG_LMB
+static int do_spi_read_lmb_check(ulong start_addr, loff_t len)
+{
+       phys_size_t max_size;
+       ulong end_addr;
+
+       lmb_dump_all();
+
+       max_size = lmb_get_free_size(start_addr);
+       if (!max_size) {

cmd/sf.c static int spi_flash_update(struct spi_flash *flash, u32 offset, · line 295

Quoted 10 lines
cmd/sf.c +10 -0
+               return CMD_RET_FAILURE;
+       }
+
+       end_addr = start_addr + max_size;
+       if (!end_addr)
+               end_addr = ULONG_MAX;
+
+       if ((start_addr + len) > end_addr) {
+               return CMD_RET_FAILURE;
+       }

This is one way to get the load address, yes. But please do note that with this method, if the region of memory has already been marked as reserved, the call to lmb_get_free_size() will fail, i.e. return a value of 0. Whereas if you call lmb_alloc_addr(), and if the memory region is marked reserved with the flags set to LMB_NONE, that call will not fail, as we can re-reserve memory marked as LMB_NONE. Hence a better approach would be to use the logic used in the fs module function that I had pointed to in my earlier email.

Quoted 4 lines

Also, like I mentioned earlier, it will be better to refactor the functionality, especially if the plan is to introduce this check for loading stuff through other interfaces like nand, mmc etc. You can add a function in the lmb.c like lmb_read_check() which does that.

Is this something what you are going to work on?

Thanks,
Michal

#3 lore raw
Quoted 106 lines · Michal Simek
Quoted 102 lines · Sughosh Ganu
Quoted 81 lines · Prasad Kummari

Added LMB API to prevent SF command from overwriting reserved memory areas. The current SPI code does not use LMB APIs for loading data into memory addresses. To resolve this, LMB APIs were added to check the load address of an SF command and ensure it does not overwrite reserved memory addresses. Similar checks are used in TFTP, serial load, and boot code to prevent overwriting reserved memory.

---

Changes in V3:
- Removed lmb_init_and_reserve() as part of latest LMB series.
- Error message moved to one place.
- lmb_alloc_addr() is not required because the given memory address is being checked to ensure it is free or not.

Changes in V2:
- Rebased the code changes on top of the next branch.

UT:
Tested on Versal NET board

Versal NET> fdt print /reserved-memory

reserved-memory {
         ranges;
         #size-cells = <0x00000002>;
         #address-cells = <0x00000002>;
         tf-a {
                 reg = <0x00000000 0x70000000 0x00000000 0x00050000>;
                 no-map;
         };
};

Versal NET> sf read 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

relocaddr = 0x000000007febc000

Versal NET> sf read 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

  cmd/sf.c | 36 +++++++++++++++++++++++++++++++++++-
  1 file changed, 35 insertions(+), 1 deletion(-)
cmd/sf.c +11 -0
index f43a2e08b3..7bb8bcfce2 100644
@@ -10,6 +10,7 @@
  #include <div64.h>
  #include <dm.h>
  #include <log.h>
+#include <lmb.h>
  #include <malloc.h>
  #include <mapmem.h>
  #include <spi.h>
@@ -272,6 +273,31 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
         return 0;
  }
 
+#ifdef CONFIG_LMB
+static int do_spi_read_lmb_check(ulong start_addr, loff_t len)
+{
+       phys_size_t max_size;
+       ulong end_addr;
+
+       lmb_dump_all();
+
+       max_size = lmb_get_free_size(start_addr);
+       if (!max_size) {

cmd/sf.c static int spi_flash_update(struct spi_flash *flash, u32 offset, · line 295

Quoted 10 lines
cmd/sf.c +10 -0
+               return CMD_RET_FAILURE;
+       }
+
+       end_addr = start_addr + max_size;
+       if (!end_addr)
+               end_addr = ULONG_MAX;
+
+       if ((start_addr + len) > end_addr) {
+               return CMD_RET_FAILURE;
+       }

This is one way to get the load address, yes. But please do note that with this method, if the region of memory has already been marked as reserved, the call to lmb_get_free_size() will fail, i.e. return a value of 0. Whereas if you call lmb_alloc_addr(), and if the memory region is marked reserved with the flags set to LMB_NONE, that call will not fail, as we can re-reserve memory marked as LMB_NONE. Hence a better approach would be to use the logic used in the fs module function that I had pointed to in my earlier email.

Quoted 6 lines
Quoted 4 lines

Also, like I mentioned earlier, it will be better to refactor the functionality, especially if the plan is to introduce this check for loading stuff through other interfaces like nand, mmc etc. You can add a function in the lmb.c like lmb_read_check() which does that.

Is this something what you are going to work on?

Can that not be done as part of this work? It is about having a single function in the lmb.c file, which simply does a check for lmb_alloc_addr(). It's not very complicated tbh.

-sughosh

#4 lore raw
Quoted 112 lines · Sughosh Ganu
Quoted 111 lines · Michal Simek
Quoted 106 lines · Sughosh Ganu
Quoted 81 lines · Prasad Kummari

Added LMB API to prevent SF command from overwriting reserved memory areas. The current SPI code does not use LMB APIs for loading data into memory addresses. To resolve this, LMB APIs were added to check the load address of an SF command and ensure it does not overwrite reserved memory addresses. Similar checks are used in TFTP, serial load, and boot code to prevent overwriting reserved memory.

---

Changes in V3:
- Removed lmb_init_and_reserve() as part of latest LMB series.
- Error message moved to one place.
- lmb_alloc_addr() is not required because the given memory address is being checked to ensure it is free or not.

Changes in V2:
- Rebased the code changes on top of the next branch.

UT:
Tested on Versal NET board

Versal NET> fdt print /reserved-memory

reserved-memory {
          ranges;
          #size-cells = <0x00000002>;
          #address-cells = <0x00000002>;
          tf-a {
                  reg = <0x00000000 0x70000000 0x00000000 0x00050000>;
                  no-map;
          };
};

Versal NET> sf read 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

relocaddr = 0x000000007febc000

Versal NET> sf read 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

   cmd/sf.c | 36 +++++++++++++++++++++++++++++++++++-
   1 file changed, 35 insertions(+), 1 deletion(-)
cmd/sf.c +11 -0
index f43a2e08b3..7bb8bcfce2 100644
@@ -10,6 +10,7 @@
   #include <div64.h>
   #include <dm.h>
   #include <log.h>
+#include <lmb.h>
   #include <malloc.h>
   #include <mapmem.h>
   #include <spi.h>
@@ -272,6 +273,31 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
          return 0;
   }
 
+#ifdef CONFIG_LMB
+static int do_spi_read_lmb_check(ulong start_addr, loff_t len)
+{
+       phys_size_t max_size;
+       ulong end_addr;
+
+       lmb_dump_all();
+
+       max_size = lmb_get_free_size(start_addr);
+       if (!max_size) {

cmd/sf.c static int spi_flash_update(struct spi_flash *flash, u32 offset, · line 295

Quoted 10 lines
cmd/sf.c +10 -0
+               return CMD_RET_FAILURE;
+       }
+
+       end_addr = start_addr + max_size;
+       if (!end_addr)
+               end_addr = ULONG_MAX;
+
+       if ((start_addr + len) > end_addr) {
+               return CMD_RET_FAILURE;
+       }

This is one way to get the load address, yes. But please do note that with this method, if the region of memory has already been marked as reserved, the call to lmb_get_free_size() will fail, i.e. return a value of 0. Whereas if you call lmb_alloc_addr(), and if the memory region is marked reserved with the flags set to LMB_NONE, that call will not fail, as we can re-reserve memory marked as LMB_NONE. Hence a better approach would be to use the logic used in the fs module function that I had pointed to in my earlier email.

Also, like I mentioned earlier, it will be better to refactor the functionality, especially if the plan is to introduce this check for loading stuff through other interfaces like nand, mmc etc. You can add a function in the lmb.c like lmb_read_check() which does that.

Quoted 5 lines
Quoted 1 line

Is this something what you are going to work on?

Can that not be done as part of this work? It is about having a single function in the lmb.c file, which simply does a check for lmb_alloc_addr(). It's not very complicated tbh.

It is more about time spent on it. If you know how exactly it should look like it would be easier for you to put it together and Prasad can validate it on our platforms.

Thanks,
Michal

#5 lore raw
Quoted 118 lines · Michal Simek
Quoted 114 lines · Sughosh Ganu
Quoted 112 lines · Michal Simek
Quoted 102 lines · Sughosh Ganu
Quoted 81 lines · Prasad Kummari

Added LMB API to prevent SF command from overwriting reserved memory areas. The current SPI code does not use LMB APIs for loading data into memory addresses. To resolve this, LMB APIs were added to check the load address of an SF command and ensure it does not overwrite reserved memory addresses. Similar checks are used in TFTP, serial load, and boot code to prevent overwriting reserved memory.

---

Changes in V3:
- Removed lmb_init_and_reserve() as part of latest LMB series.
- Error message moved to one place.
- lmb_alloc_addr() is not required because the given memory address is being checked to ensure it is free or not.

Changes in V2:
- Rebased the code changes on top of the next branch.

UT:
Tested on Versal NET board

Versal NET> fdt print /reserved-memory

reserved-memory {
          ranges;
          #size-cells = <0x00000002>;
          #address-cells = <0x00000002>;
          tf-a {
                  reg = <0x00000000 0x70000000 0x00000000 0x00050000>;
                  no-map;
          };
};

Versal NET> sf read 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

relocaddr = 0x000000007febc000

Versal NET> sf read 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x000000007febc000 0x0 0x40
device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

   cmd/sf.c | 36 +++++++++++++++++++++++++++++++++++-
   1 file changed, 35 insertions(+), 1 deletion(-)
cmd/sf.c +11 -0
index f43a2e08b3..7bb8bcfce2 100644
@@ -10,6 +10,7 @@
   #include <div64.h>
   #include <dm.h>
   #include <log.h>
+#include <lmb.h>
   #include <malloc.h>
   #include <mapmem.h>
   #include <spi.h>
@@ -272,6 +273,31 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
          return 0;
   }
 
+#ifdef CONFIG_LMB
+static int do_spi_read_lmb_check(ulong start_addr, loff_t len)
+{
+       phys_size_t max_size;
+       ulong end_addr;
+
+       lmb_dump_all();
+
+       max_size = lmb_get_free_size(start_addr);
+       if (!max_size) {

cmd/sf.c static int spi_flash_update(struct spi_flash *flash, u32 offset, · line 295

Quoted 10 lines
cmd/sf.c +10 -0
+               return CMD_RET_FAILURE;
+       }
+
+       end_addr = start_addr + max_size;
+       if (!end_addr)
+               end_addr = ULONG_MAX;
+
+       if ((start_addr + len) > end_addr) {
+               return CMD_RET_FAILURE;
+       }

This is one way to get the load address, yes. But please do note that with this method, if the region of memory has already been marked as reserved, the call to lmb_get_free_size() will fail, i.e. return a value of 0. Whereas if you call lmb_alloc_addr(), and if the memory region is marked reserved with the flags set to LMB_NONE, that call will not fail, as we can re-reserve memory marked as LMB_NONE. Hence a better approach would be to use the logic used in the fs module function that I had pointed to in my earlier email.

Quoted 4 lines

Also, like I mentioned earlier, it will be better to refactor the functionality, especially if the plan is to introduce this check for loading stuff through other interfaces like nand, mmc etc. You can add a function in the lmb.c like lmb_read_check() which does that.

Is this something what you are going to work on?

Quoted 7 lines
Quoted 3 lines

Can that not be done as part of this work? It is about having a single function in the lmb.c file, which simply does a check for lmb_alloc_addr(). It's not very complicated tbh.

It is more about time spent on it. If you know how exactly it should look like it would be easier for you to put it together and Prasad can validate it on our platforms.

Prasad, Can you try this change? If this works, you can use this. I will work on using this in other functions later.

cmd/sf.c +0 -0
index 7bb8bcfce2..33d88a3531 100644
@@ -343,13 +343,12 @@ static int do_spi_flash_read_write(int argc,

char *const argv[])

                        strncmp(argv[0], "write", 5) == 0) {
                int read, ret;

-#ifdef CONFIG_LMB
- ret = do_spi_read_lmb_check(addr, len);
- if (ret) {
- printf("ERROR: trying to overwrite reserved
memory...\n");
- return ret;
+ if (CONFIG_IS_ENABLED(LMB)) {
+ if (lmb_read_check(addr, len)) {
+ printf("ERROR: trying to overwrite
reserved memory...\n");
+ return CMD_RET_FAILURE;
+ }

                }

-#endif

                read = strncmp(argv[0], "read", 4) == 0;
                if (read)
include/lmb.h +5 -0
index 6ef03f9b63..601055a837 100644
@@ -122,6 +122,11 @@ struct lmb *lmb_get(void);
 int lmb_push(struct lmb *store);
 void lmb_pop(struct lmb *store);
 
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+       return lmb_alloc_addr(addr, len) == addr ? 0 : -1;
+}
+
 #endif /* __KERNEL__ */
 
 #endif /* _LINUX_LMB_H */

-sughosh

#6 lore raw

Hi Sughosh,

Quoted 139 lines

-----Original Message-----
From: Sughosh Ganu <[email protected]>
Sent: Wednesday, September 11, 2024 5:26 PM
To: Simek, Michal <[email protected]>
Cc: Kummari, Prasad <[email protected]>; [email protected]; git (AMD-Xilinx) <[email protected]>; Abbarapu, Venkatesh <[email protected]>; Begari, Padmarao <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3] cmd: sf: prevent overwriting the reserved memory

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 19 lines · Michal Simek
Quoted 15 lines · Sughosh Ganu
Quoted 14 lines · Michal Simek
Quoted 10 lines · Sughosh Ganu
Quoted 9 lines · Prasad Kummari

Added LMB API to prevent SF command from overwriting reserved memory areas. The current SPI code does not use LMB APIs for loading data into memory addresses. To resolve this, LMB APIs were added to check the load address of an SF command and ensure it does not overwrite reserved memory addresses. Similar checks are used in TFTP, serial load, and boot code to prevent overwriting reserved memory.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

---

Changes in V3:
- Removed lmb_init_and_reserve() as part of latest LMB series.
- Error message moved to one place.
- lmb_alloc_addr() is not required because the given memory address

is

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines

being checked to ensure it is free or not.

Changes in V2:
- Rebased the code changes on top of the next branch.

UT:
Tested on Versal NET board

Versal NET> fdt print /reserved-memory reserved-memory {
          ranges;
          #size-cells = <0x00000002>;
          #address-cells = <0x00000002>;
          tf-a {
                  reg = <0x00000000 0x70000000 0x00000000 0x00050000>;
                  no-map;
          };
};

Versal NET> sf read 0x70000000 0x0 0x40 device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x70000000 0x0 0x40 device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

relocaddr = 0x000000007febc000

Versal NET> sf read 0x000000007febc000 0x0 0x40 device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x000000007febc000 0x0 0x40 device 0 offset 0x0, size 0x40
ERROR: trying to overwrite reserved memory...

   cmd/sf.c | 36 +++++++++++++++++++++++++++++++++++-
   1 file changed, 35 insertions(+), 1 deletion(-)
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
cmd/sf.c +0 -0
index f43a2e08b3..7bb8bcfce2 100644
@@ -10,6 +10,7 @@
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

cmd/sf.c static int spi_flash_update(struct spi_flash

Quoted 8 lines
cmd/sf.c +1 -0
   #include <div64.h>
   #include <dm.h>
   #include <log.h>
+#include <lmb.h>
   #include <malloc.h>
   #include <mapmem.h>
   #include <spi.h>
@@ -272,6 +273,31 @@ static int spi_flash_update(struct spi_flash

*flash, u32 offset,

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
cmd/sf.c +19 -0
          return 0;
   }
 
+#ifdef CONFIG_LMB
+static int do_spi_read_lmb_check(ulong start_addr, loff_t len) {
+       phys_size_t max_size;
+       ulong end_addr;
+
+       lmb_dump_all();
+
+       max_size = lmb_get_free_size(start_addr);
+       if (!max_size) {
+               return CMD_RET_FAILURE;
+       }
+
+       end_addr = start_addr + max_size;
+       if (!end_addr)
+               end_addr = ULONG_MAX;
+
+       if ((start_addr + len) > end_addr) {
+               return CMD_RET_FAILURE;
+       }
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

This is one way to get the load address, yes. But please do note that with this method, if the region of memory has already been marked as reserved, the call to lmb_get_free_size() will fail, i.e. return a value of 0. Whereas if you call lmb_alloc_addr(), and if the memory region is marked reserved with the flags set to LMB_NONE, that call will not fail, as we can re-reserve memory marked as LMB_NONE. Hence a better approach would be to use the logic used in the fs module function that I had pointed to in my earlier

email.

Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines

Also, like I mentioned earlier, it will be better to refactor the functionality, especially if the plan is to introduce this check for loading stuff through other interfaces like nand, mmc etc. You can add a function in the lmb.c like lmb_read_check() which does that.

Is this something what you are going to work on?

Can that not be done as part of this work? It is about having a single function in the lmb.c file, which simply does a check for lmb_alloc_addr(). It's not very complicated tbh.

Quoted 6 lines
Quoted 3 lines

It is more about time spent on it. If you know how exactly it should look like it would be easier for you to put it together and Prasad can validate it on our platforms.

Prasad, Can you try this change? If this works, you can use this. I will work on using this in other functions later.

[Prasad]: I have tried the changes below, and they are working as expected. I have sent v4 with the latest changes.

Quoted 42 lines
cmd/sf.c +5 -6
index 7bb8bcfce2..33d88a3531 100644
@@ -343,13 +343,12 @@ static int do_spi_flash_read_write(int argc, char
 *const argv[])
                        strncmp(argv[0], "write", 5) == 0) {
                int read, ret;
 
-#ifdef CONFIG_LMB
-               ret = do_spi_read_lmb_check(addr, len);
-               if (ret) {
-                       printf("ERROR: trying to overwrite reserved
 memory...\n");
-                       return ret;
+               if (CONFIG_IS_ENABLED(LMB)) {
+                       if (lmb_read_check(addr, len)) {
+                               printf("ERROR: trying to overwrite
 reserved memory...\n");
+                               return CMD_RET_FAILURE;
+                       }
                }
-#endif
                read = strncmp(argv[0], "read", 4) == 0;
                if (read)
include/lmb.h index 6ef03f9b63..601055a837 renamed from include/lmb.h +0 -0

100644

include/lmb.h +3 -1
@@ -122,6 +122,11 @@ struct lmb *lmb_get(void);  int lmb_push(struct lmb
 *store);  void lmb_pop(struct lmb *store);
 
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len) {
+       return lmb_alloc_addr(addr, len) == addr ? 0 : -1; }
+
 #endif /* __KERNEL__ */
 
 #endif /* _LINUX_LMB_H */
 
-sughosh