From: "Jan Beulich" Subject: [PATCH] console: avoid wrapping of console pointers Date: Thu, 16 Feb 2017 10:23:06 -0700 Message-ID: <58A5EE0A020000780013ADE4@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartA09950EA.3__=" Return-path: Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cePlw-0005Jv-Ou for xen-devel@lists.xenproject.org; Thu, 16 Feb 2017 17:23:12 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartA09950EA.3__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline We particularly want/need to avoid accessing data outside (ahead of) the ring buffer. Also latch both pointers into local variable to avoid different steps of the calculation being done with different values. Reported-by: Quan Luo Signed-off-by: Jan Beulich --- a/xen/drivers/char/console.c +++ b/xen/drivers/char/console.c @@ -254,20 +254,23 @@ static void conring_puts(const char *str long read_console_ring(struct xen_sysctl_readconsole *op) { XEN_GUEST_HANDLE_PARAM(char) str; - uint32_t idx, len, max, sofar, c; + uint32_t idx, len, max, sofar, c, p; =20 str =3D guest_handle_cast(op->buffer, char), max =3D op->count; sofar =3D 0; =20 - c =3D conringc; - if ( op->incremental && ((int32_t)(op->index - c) > 0) ) + c =3D read_atomic(&conringc); + p =3D read_atomic(&conringp); + if ( op->incremental && + (c <=3D p ? c < op->index && op->index <=3D p + : c < op->index || op->index <=3D p) ) c =3D op->index; =20 - while ( (c !=3D conringp) && (sofar < max) ) + while ( (c !=3D p) && (sofar < max) ) { idx =3D CONRING_IDX_MASK(c); - len =3D conringp - c; + len =3D p - c; if ( (idx + len) > conring_size ) len =3D conring_size - idx; if ( (sofar + len) > max ) @@ -281,10 +284,7 @@ long read_console_ring(struct xen_sysctl if ( op->clear ) { spin_lock_irq(&console_lock); - if ( (uint32_t)(conringp - c) > conring_size ) - conringc =3D conringp - conring_size; - else - conringc =3D c; + conringc =3D p - c > conring_size ? p - conring_size : c; spin_unlock_irq(&console_lock); } =20 --=__PartA09950EA.3__= Content-Type: text/plain; name="console-read-no-wrap.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="console-read-no-wrap.patch" console: avoid wrapping of console pointers=0A=0AWe particularly want/need = to avoid accessing data outside (ahead of)=0Athe ring buffer. Also latch = both pointers into local variable to=0Aavoid different steps of the = calculation being done with different=0Avalues.=0A=0AReported-by: Quan Luo = =0ASigned-off-by: Jan Beulich =0A=0A--= - a/xen/drivers/char/console.c=0A+++ b/xen/drivers/char/console.c=0A@@ = -254,20 +254,23 @@ static void conring_puts(const char *str=0A long = read_console_ring(struct xen_sysctl_readconsole *op)=0A {=0A XEN_GUEST_= HANDLE_PARAM(char) str;=0A- uint32_t idx, len, max, sofar, c;=0A+ = uint32_t idx, len, max, sofar, c, p;=0A =0A str =3D guest_handle_cast= (op->buffer, char),=0A max =3D op->count;=0A sofar =3D 0;=0A = =0A- c =3D conringc;=0A- if ( op->incremental && ((int32_t)(op->index= - c) > 0) )=0A+ c =3D read_atomic(&conringc);=0A+ p =3D read_atomic(= &conringp);=0A+ if ( op->incremental &&=0A+ (c <=3D p ? c < = op->index && op->index <=3D p=0A+ : c < op->index || = op->index <=3D p) )=0A c =3D op->index;=0A =0A- while ( (c !=3D = conringp) && (sofar < max) )=0A+ while ( (c !=3D p) && (sofar < max) = )=0A {=0A idx =3D CONRING_IDX_MASK(c);=0A- len =3D = conringp - c;=0A+ len =3D p - c;=0A if ( (idx + len) > = conring_size )=0A len =3D conring_size - idx;=0A if ( = (sofar + len) > max )=0A@@ -281,10 +284,7 @@ long read_console_ring(struct = xen_sysctl=0A if ( op->clear )=0A {=0A spin_lock_irq(&conso= le_lock);=0A- if ( (uint32_t)(conringp - c) > conring_size )=0A- = conringc =3D conringp - conring_size;=0A- else=0A- = conringc =3D c;=0A+ conringc =3D p - c > conring_size ? p - = conring_size : c;=0A spin_unlock_irq(&console_lock);=0A }=0A = =0A --=__PartA09950EA.3__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__PartA09950EA.3__=--