2022-06-27 13:17:02

by Yangxi Xiang

[permalink] [raw]
Subject: [PATCH v2] vt: fix memory overlapping when deleting chars in the buffer

A memory overlapping copy occurs when deleting a long line. This memory
overlapping copy can cause data corruption when scr_memcpyw is optimized
to memcpy because memcpy does not ensure its behavior if the destination
buffer overlaps with the source buffer. The line buffer is not always
broken, because the memcpy utilizes the hardware acceleration, whose
result is not deterministic.

Fix this problem by using replacing the scr_memcpyw with scr_memmovew, and
preserving the memcpy optimization when the buffers are not overlapping.

Fixes: 81732c3b2fed ("Fix line garbage in virtual console").
Signed-off-by: Yangxi Xiang <[email protected]>
---
drivers/tty/vt/vt.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index f8c87c4d7399..d87bff9d8ed5 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -853,9 +853,13 @@ static void insert_char(struct vc_data *vc, unsigned int nr)
static void delete_char(struct vc_data *vc, unsigned int nr)
{
unsigned short *p = (unsigned short *) vc->vc_pos;
+ unsigned short cp = (vc->vc_cols - vc->state.x - nr) * 2;

vc_uniscr_delete(vc, nr);
- scr_memcpyw(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2);
+ if (cp > nr)
+ scr_memmovew(p, p + nr, cp);
+ else
+ scr_memcpyw(p, p + nr, cp);
scr_memsetw(p + vc->vc_cols - vc->state.x - nr, vc->vc_video_erase_char,
nr * 2);
vc->vc_need_wrap = 0;
--
2.17.1


2022-06-27 13:37:19

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v2] vt: fix memory overlapping when deleting chars in the buffer

On Mon, Jun 27, 2022 at 08:54:28PM +0800, Yangxi Xiang wrote:
> A memory overlapping copy occurs when deleting a long line. This memory
> overlapping copy can cause data corruption when scr_memcpyw is optimized
> to memcpy because memcpy does not ensure its behavior if the destination
> buffer overlaps with the source buffer. The line buffer is not always
> broken, because the memcpy utilizes the hardware acceleration, whose
> result is not deterministic.
>
> Fix this problem by using replacing the scr_memcpyw with scr_memmovew, and
> preserving the memcpy optimization when the buffers are not overlapping.
>
> Fixes: 81732c3b2fed ("Fix line garbage in virtual console").
> Signed-off-by: Yangxi Xiang <[email protected]>
> ---
> drivers/tty/vt/vt.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
> index f8c87c4d7399..d87bff9d8ed5 100644
> --- a/drivers/tty/vt/vt.c
> +++ b/drivers/tty/vt/vt.c
> @@ -853,9 +853,13 @@ static void insert_char(struct vc_data *vc, unsigned int nr)
> static void delete_char(struct vc_data *vc, unsigned int nr)
> {
> unsigned short *p = (unsigned short *) vc->vc_pos;
> + unsigned short cp = (vc->vc_cols - vc->state.x - nr) * 2;
>
> vc_uniscr_delete(vc, nr);
> - scr_memcpyw(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2);
> + if (cp > nr)
> + scr_memmovew(p, p + nr, cp);
> + else
> + scr_memcpyw(p, p + nr, cp);
> scr_memsetw(p + vc->vc_cols - vc->state.x - nr, vc->vc_video_erase_char,
> nr * 2);
> vc->vc_need_wrap = 0;
> --
> 2.17.1
>

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him
a patch that has triggered this response. He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created. Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- This looks like a new version of a previously submitted patch, but you
did not list below the --- line any changes from the previous version.
Please read the section entitled "The canonical patch format" in the
kernel file, Documentation/SubmittingPatches for what needs to be done
here to properly describe this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot

2022-06-28 08:17:48

by Jiri Slaby

[permalink] [raw]
Subject: Re: [PATCH v2] vt: fix memory overlapping when deleting chars in the buffer

On 27. 06. 22, 14:54, Yangxi Xiang wrote:
> A memory overlapping copy occurs when deleting a long line. This memory
> overlapping copy can cause data corruption when scr_memcpyw is optimized
> to memcpy because memcpy does not ensure its behavior if the destination
> buffer overlaps with the source buffer. The line buffer is not always
> broken, because the memcpy utilizes the hardware acceleration, whose
> result is not deterministic.
>
> Fix this problem by using replacing the scr_memcpyw with scr_memmovew, and
> preserving the memcpy optimization when the buffers are not overlapping.
>
> Fixes: 81732c3b2fed ("Fix line garbage in virtual console").
> Signed-off-by: Yangxi Xiang <[email protected]>
> ---
> drivers/tty/vt/vt.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
> index f8c87c4d7399..d87bff9d8ed5 100644
> --- a/drivers/tty/vt/vt.c
> +++ b/drivers/tty/vt/vt.c
> @@ -853,9 +853,13 @@ static void insert_char(struct vc_data *vc, unsigned int nr)
> static void delete_char(struct vc_data *vc, unsigned int nr)
> {
> unsigned short *p = (unsigned short *) vc->vc_pos;
> + unsigned short cp = (vc->vc_cols - vc->state.x - nr) * 2;
>
> vc_uniscr_delete(vc, nr);
> - scr_memcpyw(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2);
> + if (cp > nr)
> + scr_memmovew(p, p + nr, cp);
> + else
> + scr_memcpyw(p, p + nr, cp);

Why not to use memmove in both cases? I.e. simply switch scr_memcpyw to
scr_memmovew?

thanks,
--
js
suse labs

2022-06-28 08:52:38

by Yangxi Xiang

[permalink] [raw]
Subject: Re: [PATCH] vt: fix memory overlapping when deleting chars in the buffer

> Why not to use memmove in both cases? I.e. simply switch scr_memcpyw to
> scr_memmovew?

Both of them works, and I pick one of them.

Yangxi Xiang

2022-06-28 09:01:26

by Jiri Slaby

[permalink] [raw]
Subject: Re: [PATCH] vt: fix memory overlapping when deleting chars in the buffer

On 28. 06. 22, 10:27, Yangxi Xiang wrote:
>> Why not to use memmove in both cases? I.e. simply switch scr_memcpyw to
>> scr_memmovew?
>
> Both of them works, and I pick one of them.

Sorry, I don't understand.

--
js
suse labs

2022-06-28 09:08:23

by Yangxi Xiang

[permalink] [raw]
Subject: Re: [PATCH] vt: fix memory overlapping when deleting chars in the buffer

>> Both of them works, and I pick one of them.
>
> Sorry, I don't understand.

We can use both scr_memcpyw() and scr_memmovew() for the not
overlapping case (cp <= nr), which is more likely to happen.
In this case I keep using scr_memcpyw().

Yangxi Xiang

2022-06-28 09:47:20

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH] vt: fix memory overlapping when deleting chars in the buffer

On Tue, Jun 28, 2022 at 04:59:22PM +0800, Yangxi Xiang wrote:
> >> Both of them works, and I pick one of them.
> >
> > Sorry, I don't understand.
>
> We can use both scr_memcpyw() and scr_memmovew() for the not
> overlapping case (cp <= nr), which is more likely to happen.
> In this case I keep using scr_memcpyw().

The point is we should just do one type of copy, let's pick the one that
always works and do that, no need to check anything here.

thanks,

greg k-h