Re: [PATCH ustream-ssl] ustream-mbedtls: Use getrandom() instead of /dev/urandom

2023-01-29 Thread Hauke Mehrtens

On 1/29/23 15:13, Torsten Duwe wrote:

On Sat, 28 Jan 2023 19:41:13 +0100
Hauke Mehrtens  wrote:


Instead of keeping a file descriptor open just use the getrandom syscall
to get random data. This is supported by the musl, glibc and Linux for
some time now.

This also improves the error handling in case this function returns not
as many bytes as expected.

Signed-off-by: Hauke Mehrtens 
---
  ustream-mbedtls.c | 23 +--
  1 file changed, 5 insertions(+), 18 deletions(-)

diff --git a/ustream-mbedtls.c b/ustream-mbedtls.c
index e79e37b..51ba2fa 100644
--- a/ustream-mbedtls.c
+++ b/ustream-mbedtls.c
@@ -17,6 +17,7 @@
   */
  
  #include 

+#include 
  #include 
  #include 
  #include 
@@ -25,8 +26,6 @@
  #include "ustream-ssl.h"
  #include "ustream-internal.h"
  
-static int urandom_fd = -1;

-
  static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
  {
struct ustream *s = ctx;
@@ -66,21 +65,12 @@ __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, 
void *ssl, struct ustr
mbedtls_ssl_set_bio(ssl, conn, s_ustream_write, s_ustream_read, NULL);
  }
  
-static bool urandom_init(void)

-{
-   if (urandom_fd > -1)
-   return true;
-
-   urandom_fd = open("/dev/urandom", O_RDONLY);
-   if (urandom_fd < 0)
-   return false;
-
-   return true;
-}
-
  static int _urandom(void *ctx, unsigned char *out, size_t len)
  {
-   if (read(urandom_fd, out, len) < 0)
+   ssize_t ret;
+
+   ret = getrandom(out, len, 0);
+   if (ret < 0 || (size_t)ret != len)
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;

[...]

drivers/char/random.c lines 1240- ...
  * Reading from /dev/urandom has the same functionality as calling
  * getrandom(2) with flags=GRND_INSECURE. Because it does not block
  * waiting for the RNG to be ready, it should not be used.

Haven't audited mbedtls, but I assume it reads urandom for "lesser"
entropy when needed. In any case, getrandom(out, len, GRND_INSECURE)
would be the proper replacement.

Torsten


Hi Torsten,

The mapage says this:
> By default, getrandom() draws entropy from the urandom source
> (i.e., the same source as the /dev/urandom device).  This
> behavior can be changed via the flags argument.
https://man7.org/linux/man-pages/man2/getrandom.2.html

GRND_INSECURE is also not documented in the man page.

The option was added to the Linux kernel 5.6 here:
https://git.kernel.org/linus/75551dbf112c992bc6c99a972990b3f272247e23

The documentation says
> GRND_INSECURE  Return non-cryptographic random bytes
We want to use the random bytes in mbedtls for cryptographic operations. 
I think giving no flags is the correct option here.


I think the behavior of /dev/random changed some years ago. This article 
described it a bit:  https://lwn.net/Articles/808575/


As far as I understood the code, giving no flags will guarantee that the 
random pool is initialized (crng_ready() returns true) and otherwise it 
is the same as using GRND_INSECURE. As we use it for cryptographic 
operations I think we should give no flags.


Hauke


___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


Re: [PATCH v2] tplink-safeloader: Add support for Deco M4R V4

2023-01-29 Thread Sander Vanheule
Hi Mark,

On Sun, 2023-01-29 at 15:28 +0100, Mark Ceeha wrote:
> Software version:
>   soft_ver:1.0.0 Build 20211202 Rel. 69482
> 
> Partition table:
>    0004 fs-uboot
>   0004 0040 os-image
>   0044 00b6 file-system
>   00fa 0300 default-mac
>   00fa0300 0100 device-id
>   00fa0400 fc00 product-info
>   00fb 4000 group-info
>   00fb4000 c000 user-config
>   00fc 0001 device-config
>   00fd 0001 default-config
>   00fe 0800 partition-table
>   00fe0800 0100 soft-version
>   00fe0900 0200 support-list
>   00fe0b00 3000 profile
>   00fe3b00 0100 extra-para
>   00ff 0001 radio
> 
> `os-image` and `file-system` have been merged into `firmware`.
> 
> Signed-off-by: Mark Ceeha 
> ---
> v2:
> - Change `soft_ver` to 1.0.0; while the TP-Link stock firmware cares
>   about the version and if it's not older than the current `soft_ver`,
>   it will not allow you to flash OpenWrt anyway as the image is not signed.
>   U-Boot accepts unsigned images, and does not care about the `soft_ver`,
>   so we'll just make it consistent with the initial firmware release
>   this partition table was checked against.
> - Fix style issues pointed out by Sander.

Thanks for the update. I applied this v2 to the firmware-utils repository.

Best,
Sander

___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


[sdwalker/sdwalker.github.io] 08303c: This week's update

2023-01-29 Thread Stephen Walker via openwrt-devel
The sender domain has a DMARC Reject/Quarantine policy which disallows
sending mailing list messages using the original "From" header.

To mitigate this problem, the original message has been wrapped
automatically by the mailing list software.--- Begin Message ---
  Branch: refs/heads/master
  Home:   https://github.com/sdwalker/sdwalker.github.io
  Commit: 08303c4be9c8f7bea7c7a30ff57287d406a94ba1
  
https://github.com/sdwalker/sdwalker.github.io/commit/08303c4be9c8f7bea7c7a30ff57287d406a94ba1
  Author: Stephen Walker 
  Date:   2023-01-29 (Sun, 29 Jan 2023)

  Changed paths:
M uscan/index-21.02.html
M uscan/index-22.03.html
M uscan/index.html

  Log Message:
  ---
  This week's update



--- End Message ---
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


[PATCH v2] tplink-safeloader: Add support for Deco M4R V4

2023-01-29 Thread Mark Ceeha via openwrt-devel
The sender domain has a DMARC Reject/Quarantine policy which disallows
sending mailing list messages using the original "From" header.

To mitigate this problem, the original message has been wrapped
automatically by the mailing list software.--- Begin Message ---
Software version:
  soft_ver:1.0.0 Build 20211202 Rel. 69482

Partition table:
   0004 fs-uboot
  0004 0040 os-image
  0044 00b6 file-system
  00fa 0300 default-mac
  00fa0300 0100 device-id
  00fa0400 fc00 product-info
  00fb 4000 group-info
  00fb4000 c000 user-config
  00fc 0001 device-config
  00fd 0001 default-config
  00fe 0800 partition-table
  00fe0800 0100 soft-version
  00fe0900 0200 support-list
  00fe0b00 3000 profile
  00fe3b00 0100 extra-para
  00ff 0001 radio

`os-image` and `file-system` have been merged into `firmware`.

Signed-off-by: Mark Ceeha 
---
v2:
- Change `soft_ver` to 1.0.0; while the TP-Link stock firmware cares
  about the version and if it's not older than the current `soft_ver`,
  it will not allow you to flash OpenWrt anyway as the image is not signed.
  U-Boot accepts unsigned images, and does not care about the `soft_ver`,
  so we'll just make it consistent with the initial firmware release
  this partition table was checked against.
- Fix style issues pointed out by Sander.
---
 src/tplink-safeloader.c | 37 +
 1 file changed, 37 insertions(+)

diff --git a/src/tplink-safeloader.c b/src/tplink-safeloader.c
index ddb5dff..d9e1605 100644
--- a/src/tplink-safeloader.c
+++ b/src/tplink-safeloader.c
@@ -1612,6 +1612,42 @@ static struct device_info boards[] = {
.last_sysupgrade_partition = "file-system",
},
 
+   /** Firmware layout for the Deco M4R v4 */
+   {
+   .id = "DECO-M4R-V4",
+   .vendor = "",
+   .support_list =
+   "SupportList:\n"
+   
"{product_name:M4R,product_ver:4.0.0,special_id:5553}\n"
+   
"{product_name:M4R,product_ver:4.0.0,special_id:4555}\n"
+   
"{product_name:M4R,product_ver:4.0.0,special_id:4A50}\n"
+   
"{product_name:M4R,product_ver:4.0.0,special_id:4234}\n"
+   
"{product_name:M4R,product_ver:4.0.0,special_id:5A47}\n",
+   .part_trail = 0x00,
+   .soft_ver = SOFT_VER_TEXT("soft_ver:1.0.0\n"),
+
+   .partitions = {
+   {"fs-uboot", 0x0, 0x4},
+   {"firmware", 0x4, 0xf6},
+   {"default-mac", 0xfa, 0x00300},
+   {"device-id", 0xfa0300, 0x00100},
+   {"product-info", 0xfa0400, 0x0fc00},
+   {"group-info", 0xfb, 0x04000},
+   {"user-config", 0xfb4000, 0x0c000},
+   {"device-config", 0xfc, 0x1},
+   {"default-config", 0xfd, 0x1},
+   {"partition-table", 0xfe, 0x00800},
+   {"soft-version", 0xfe0800, 0x00100},
+   {"support-list", 0xfe0900, 0x00200},
+   {"profile", 0xfe0b00, 0x03000},
+   {"extra-para", 0xfe3b00, 0x00100},
+   {"radio", 0xff, 0x1},
+   {NULL, 0, 0}
+   },
+   .first_sysupgrade_partition = "os-image",
+   .last_sysupgrade_partition = "file-system",
+   },
+
/** Firmware layout for the Deco S4 v2 */
{
.id = "DECO-S4-V2",
@@ -3525,6 +3561,7 @@ static void build_image(const char *output,
strcasecmp(info->id, "ARCHER-C60-V3") == 0 ||
strcasecmp(info->id, "ARCHER-C6U-V1") == 0 ||
strcasecmp(info->id, "ARCHER-C6-V3") == 0 ||
+   strcasecmp(info->id, "DECO-M4R-V4") == 0 ||
strcasecmp(info->id, "MR70X") == 0 ||
strcasecmp(info->id, "TLWR1043NV5") == 0) {
const uint8_t extra_para[2] = {0x01, 0x00};
-- 
2.39.0


--- End Message ---
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


Re: [PATCH ustream-ssl] ustream-mbedtls: Use getrandom() instead of /dev/urandom

2023-01-29 Thread Torsten Duwe
On Sat, 28 Jan 2023 19:41:13 +0100
Hauke Mehrtens  wrote:

> Instead of keeping a file descriptor open just use the getrandom syscall
> to get random data. This is supported by the musl, glibc and Linux for
> some time now.
> 
> This also improves the error handling in case this function returns not
> as many bytes as expected.
> 
> Signed-off-by: Hauke Mehrtens 
> ---
>  ustream-mbedtls.c | 23 +--
>  1 file changed, 5 insertions(+), 18 deletions(-)
> 
> diff --git a/ustream-mbedtls.c b/ustream-mbedtls.c
> index e79e37b..51ba2fa 100644
> --- a/ustream-mbedtls.c
> +++ b/ustream-mbedtls.c
> @@ -17,6 +17,7 @@
>   */
>  
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -25,8 +26,6 @@
>  #include "ustream-ssl.h"
>  #include "ustream-internal.h"
>  
> -static int urandom_fd = -1;
> -
>  static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
>  {
>   struct ustream *s = ctx;
> @@ -66,21 +65,12 @@ __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, 
> void *ssl, struct ustr
>   mbedtls_ssl_set_bio(ssl, conn, s_ustream_write, s_ustream_read, NULL);
>  }
>  
> -static bool urandom_init(void)
> -{
> - if (urandom_fd > -1)
> - return true;
> -
> - urandom_fd = open("/dev/urandom", O_RDONLY);
> - if (urandom_fd < 0)
> - return false;
> -
> - return true;
> -}
> -
>  static int _urandom(void *ctx, unsigned char *out, size_t len)
>  {
> - if (read(urandom_fd, out, len) < 0)
> + ssize_t ret;
> +
> + ret = getrandom(out, len, 0);
> + if (ret < 0 || (size_t)ret != len)
>   return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
[...]

drivers/char/random.c lines 1240- ...
 * Reading from /dev/urandom has the same functionality as calling
 * getrandom(2) with flags=GRND_INSECURE. Because it does not block
 * waiting for the RNG to be ready, it should not be used.

Haven't audited mbedtls, but I assume it reads urandom for "lesser"
entropy when needed. In any case, getrandom(out, len, GRND_INSECURE)
would be the proper replacement.

Torsten

___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel