From: "Jan Beulich" Subject: Re: [PATCH] trace: further security fixes: add compiler memory barriers Date: Mon, 05 Jul 2010 09:57:20 +0100 Message-ID: <4C31BA9002000078000097F6@vpn.id2.novell.com> References: <4C31AD8C02000078000097D0@vpn.id2.novell.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: George Dunlap , Keir Fraser Cc: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org >>> On 05.07.10 at 10:06, Keir Fraser wrote: > I think I already applied this patch. Indeed - I forgot to check the staging tree before sending, and assumed you would be waiting for George's ack. Jan > On 05/07/2010 09:01, "Jan Beulich" wrote: >=20 >> This is to ensure fields shared writably with Dom0 get read only once >> for any consistency checking followed by actual calculations. >>=20 >> I realized there was another multiple-read issue, a fix for which is >> also included (which at once simplifies __insert_record()). >>=20 >> Signed-off-by: Jan Beulich >>=20 >> --- a/xen/common/trace.c 2010-07-01 10:05:54.000000000 +0200 >> +++ b/xen/common/trace.c 2010-07-01 14:01:47.000000000 +0200 >> @@ -437,11 +437,13 @@ static inline bool_t bogus(u32 prod, u32 >> static inline u32 calc_unconsumed_bytes(const struct t_buf *buf) >> { >> u32 prod =3D buf->prod, cons =3D buf->cons; >> - s32 x =3D prod - cons; >> + s32 x; >> =20 >> + barrier(); /* must read buf->prod and buf->cons only once */ >> if ( bogus(prod, cons) ) >> return data_size; >> =20 >> + x =3D prod - cons; >> if ( x < 0 ) >> x +=3D 2*data_size; >> =20 >> @@ -453,12 +455,14 @@ static inline u32 calc_unconsumed_bytes( >> =20 >> static inline u32 calc_bytes_to_wrap(const struct t_buf *buf) >> { >> - u32 prod =3D buf->prod; >> - s32 x =3D data_size - prod; >> + u32 prod =3D buf->prod, cons =3D buf->cons; >> + s32 x; >> =20 >> - if ( bogus(prod, buf->cons) ) >> + barrier(); /* must read buf->prod and buf->cons only once */ >> + if ( bogus(prod, cons) ) >> return 0; >> =20 >> + x =3D data_size - prod; >> if ( x <=3D 0 ) >> x +=3D data_size; >> =20 >> @@ -473,11 +477,14 @@ static inline u32 calc_bytes_avail(const >> return data_size - calc_unconsumed_bytes(buf); >> } >> =20 >> -static inline struct t_rec *next_record(const struct t_buf *buf) >> +static inline struct t_rec *next_record(const struct t_buf *buf, >> + uint32_t *next) >> { >> - u32 x =3D buf->prod; >> + u32 x =3D buf->prod, cons =3D buf->cons; >> =20 >> - if ( !tb_init_done || bogus(x, buf->cons) ) >> + barrier(); /* must read buf->prod and buf->cons only once */ >> + *next =3D x; >> + if ( !tb_init_done || bogus(x, cons) ) >> return NULL; >> =20 >> if ( x >=3D data_size ) >> @@ -504,23 +511,21 @@ static inline void __insert_record(volat >> BUG_ON(local_rec_size !=3D rec_size); >> BUG_ON(extra & 3); >> =20 >> + rec =3D next_record(buf, &next); >> + if ( !rec ) >> + return; >> /* Double-check once more that we have enough space. >> * Don't bugcheck here, in case the userland tool is doing >> * something stupid. */ >> - next =3D calc_bytes_avail(buf); >> - if ( next < rec_size ) >> + if ( (unsigned char *)rec + rec_size > this_cpu(t_data) + = data_size ) >> { >> if ( printk_ratelimit() ) >> printk(XENLOG_WARNING >> - "%s: avail=3D%u (size=3D%08x prod=3D%08x cons=3D%08x= ) rec=3D%u\n", >> - __func__, next, data_size, buf->prod, buf->cons, >> rec_size); >> + "%s: size=3D%08x prod=3D%08x cons=3D%08x rec=3D%u\n"= , >> + __func__, data_size, next, buf->cons, rec_size); >> return; >> } >> - rmb(); >> =20 >> - rec =3D next_record(buf); >> - if ( !rec ) >> - return; >> rec->event =3D event; >> rec->extra_u32 =3D extra_word; >> dst =3D (unsigned char *)rec->u.nocycles.extra_u32; >> @@ -537,9 +542,6 @@ static inline void __insert_record(volat >> =20 >> wmb(); >> =20 >> - next =3D buf->prod; >> - if ( bogus(next, buf->cons) ) >> - return; >> next +=3D rec_size; >> if ( next >=3D 2*data_size ) >> next -=3D 2*data_size; >>=20 >>=20 >>=20