2018-02-13 06:25:01

by Shunyong Yang

[permalink] [raw]
Subject: [PATCH] vsprintf: replace space with readable '=' before crng is ready

Before crng is ready, output of "%p" composes of "(ptrval)" and
left padding spaces for alignment as no random address can be
generated. This seems a little strange when default string width
is larger than strlen("(ptrval)").

For example, when irq domain names are built with "%p", the nodes
under /sys/kernel/debug/irq/domains like this on AArch64 system,

[root@y irq]# ls domains/
default irqchip@ (ptrval)-2
irqchip@ (ptrval)-4 \_SB_.TCS0.QIC1 \_SB_.TCS0.QIC3
irqchip@ (ptrval) irqchip@ (ptrval)-3
\_SB_.TCS0.QIC0 \_SB_.TCS0.QIC2

The name "irqchip@ (ptrval)-2" is not so readable in console
output.

This patch replaces space with readable "=" when output needs padding.
Following is the output after applying the patch,

[root@y domains]# ls
default irqchip@(====ptrval====)-2
irqchip@(====ptrval====)-4 \_SB_.TCS0.QIC1 \_SB_.TCS0.QIC3
irqchip@(====ptrval====) irqchip@(====ptrval====)-3 \_SB_.TCS0.QIC0
\_SB_.TCS0.QIC2

There is same problem in some subsystem's dmesg output. Moreover,
someone may call "%p" in a similar case. In addition, the timing of
crng initialization done may vary on different system. So, the change
is made in vsprintf.c.

Link: https://patchwork.kernel.org/patch/10185199/

Cc: Andy Shevchenko <[email protected]>
Cc: Joey Zheng <[email protected]>
Suggested-by: Rasmus Villemoes <[email protected]>
Signed-off-by: Shunyong Yang <[email protected]>
---
lib/vsprintf.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 77ee6ced11b1..9fdc8376752a 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1700,9 +1700,25 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
const int default_width = 2 * sizeof(ptr);

if (unlikely(!have_filled_random_ptr_key)) {
+ char *ptrval_str = "(ptrval)";
+ char str[default_width + 1];
+ int len = strlen(ptrval_str);
+
+ if (default_width > len) {
+ int pos;
+
+ pos = (default_width - len) / 2;
+ memset(str, '=', default_width);
+ memcpy(str + pos + 1, ptrval_str + 1, len - 2);
+ str[0] = '(';
+ str[default_width - 1] = ')';
+ str[default_width] = 0;
+ ptrval_str = str;
+ }
+
spec.field_width = default_width;
/* string length must be less than default_width */
- return string(buf, end, "(ptrval)", spec);
+ return string(buf, end, ptrval_str, spec);
}

#ifdef CONFIG_64BIT
--
1.8.3.1



2018-02-13 07:18:22

by Rasmus Villemoes

[permalink] [raw]
Subject: Re: [PATCH] vsprintf: replace space with readable '=' before crng is ready

On 13 February 2018 at 07:20, Shunyong Yang
<[email protected]> wrote:

>
> This patch replaces space with readable "=" when output needs padding.
> Following is the output after applying the patch,

> Suggested-by: Rasmus Villemoes <[email protected]>
>
> if (unlikely(!have_filled_random_ptr_key)) {
> + char *ptrval_str = "(ptrval)";
> + char str[default_width + 1];
> + int len = strlen(ptrval_str);
> +
> + if (default_width > len) {
> + int pos;
> +
> + pos = (default_width - len) / 2;
> + memset(str, '=', default_width);
> + memcpy(str + pos + 1, ptrval_str + 1, len - 2);
> + str[0] = '(';
> + str[default_width - 1] = ')';
> + str[default_width] = 0;
> + ptrval_str = str;
> + }
> +

I'm sorry, but that's way too convoluted.

> spec.field_width = default_width;
> /* string length must be less than default_width */
> - return string(buf, end, "(ptrval)", spec);
> + return string(buf, end, ptrval_str, spec);
> }

I was thinking of just doing something like

- return string(buf, end, "(ptrval)", spec);
+ return string(buf, end, sizeof(void *) == 8 ?
"(====ptrval====)" : "(ptrval)", spec);

Rasmus

2018-02-13 07:59:13

by Shunyong Yang

[permalink] [raw]
Subject: Re: [PATCH] vsprintf: replace space with readable '=' before crng is ready

Hi,?Rasmus,

On Tue, 2018-02-13 at 08:16 +0100, Rasmus Villemoes wrote:
> On 13 February 2018 at 07:20, Shunyong Yang
> <[email protected]> wrote:
>
> >
> >
> > This patch replaces space with readable "=" when output needs
> > padding.
> > Following is the output after applying the patch,
> >
> > Suggested-by: Rasmus Villemoes <[email protected]>
> >
> > ????????if (unlikely(!have_filled_random_ptr_key)) {
> > +???????????????char *ptrval_str = "(ptrval)";
> > +???????????????char str[default_width + 1];
> > +???????????????int len = strlen(ptrval_str);
> > +
> > +???????????????if (default_width > len) {
> > +???????????????????????int pos;
> > +
> > +???????????????????????pos = (default_width - len) / 2;
> > +???????????????????????memset(str, '=', default_width);
> > +???????????????????????memcpy(str + pos + 1, ptrval_str + 1, len -
> > 2);
> > +???????????????????????str[0] = '(';
> > +???????????????????????str[default_width - 1] = ')';
> > +???????????????????????str[default_width] = 0;
> > +???????????????????????ptrval_str = str;
> > +???????????????}
> > +
> I'm sorry, but that's way too convoluted.
>
> >
> > ????????????????spec.field_width = default_width;
> > ????????????????/* string length must be less than default_width */
> > -???????????????return string(buf, end, "(ptrval)", spec);
> > +???????????????return string(buf, end, ptrval_str, spec);
> > ????????}
> I was thinking of just doing something like
>
> -???????????????return string(buf, end, "(ptrval)", spec);
> +???????????????return string(buf, end, sizeof(void *) == 8 ?
> "(====ptrval====)" : "(ptrval)", spec);
>

Thanks a lot! It's more straightforward considering 32/64-bit system.

Thanks.
Shunyong.

2018-02-13 19:47:19

by Tobin C. Harding

[permalink] [raw]
Subject: Re: [PATCH] vsprintf: replace space with readable '=' before crng is ready

On Tue, Feb 13, 2018 at 08:16:57AM +0100, Rasmus Villemoes wrote:
> On 13 February 2018 at 07:20, Shunyong Yang
> <[email protected]> wrote:
>
> >
> > This patch replaces space with readable "=" when output needs padding.
> > Following is the output after applying the patch,
>
> > Suggested-by: Rasmus Villemoes <[email protected]>
> >
> > if (unlikely(!have_filled_random_ptr_key)) {
> > + char *ptrval_str = "(ptrval)";
> > + char str[default_width + 1];
> > + int len = strlen(ptrval_str);
> > +
> > + if (default_width > len) {
> > + int pos;
> > +
> > + pos = (default_width - len) / 2;
> > + memset(str, '=', default_width);
> > + memcpy(str + pos + 1, ptrval_str + 1, len - 2);
> > + str[0] = '(';
> > + str[default_width - 1] = ')';
> > + str[default_width] = 0;
> > + ptrval_str = str;
> > + }
> > +
>
> I'm sorry, but that's way too convoluted.
>
> > spec.field_width = default_width;
> > /* string length must be less than default_width */
> > - return string(buf, end, "(ptrval)", spec);
> > + return string(buf, end, ptrval_str, spec);
> > }
>
> I was thinking of just doing something like
>
> - return string(buf, end, "(ptrval)", spec);
> + return string(buf, end, sizeof(void *) == 8 ?
> "(====ptrval====)" : "(ptrval)", spec);

Awesome.