2018-06-28 15:29:16

by Steve Dickson

[permalink] [raw]
Subject: [PATCH] xdrstdio_create buffers do not output encoded values on ppc

From: Daniel Sands <[email protected]>

The cause is that the xdr_putlong uses a long to store the
converted value, then passes it to fwrite as a byte buffer.
Only the first 4 bytes are written, which is okay for a LE
system after byteswapping, but writes all zeroes on BE systems.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1261738

Signed-off-by: Steve Dickson <[email protected]>
---
src/xdr_stdio.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/xdr_stdio.c b/src/xdr_stdio.c
index 4410262..b415f61 100644
--- a/src/xdr_stdio.c
+++ b/src/xdr_stdio.c
@@ -103,10 +103,10 @@ xdrstdio_getlong(xdrs, lp)
XDR *xdrs;
long *lp;
{
-
- if (fread(lp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
+ u_int32_t mycopy;
+ if (fread(&mycopy, sizeof(u_int32_t), 1, (FILE *)xdrs->x_private) != 1)
return (FALSE);
- *lp = (long)ntohl((u_int32_t)*lp);
+ *lp = (long)ntohl(mycopy);
return (TRUE);
}

@@ -115,9 +115,9 @@ xdrstdio_putlong(xdrs, lp)
XDR *xdrs;
const long *lp;
{
- long mycopy = (long)htonl((u_int32_t)*lp);
+ u_int32_t mycopy = (u_int32_t)htonl((u_int32_t)*lp);

- if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
+ if (fwrite(&mycopy, sizeof(u_int32_t), 1, (FILE *)xdrs->x_private) != 1)
return (FALSE);
return (TRUE);
}
--
2.17.1



2018-06-28 16:17:28

by Trond Myklebust

[permalink] [raw]
Subject: Re: [PATCH] xdrstdio_create buffers do not output encoded values on ppc

T24gVGh1LCAyMDE4LTA2LTI4IGF0IDExOjI5IC0wNDAwLCBTdGV2ZSBEaWNrc29uIHdyb3RlOg0K
PiBGcm9tOiBEYW5pZWwgU2FuZHMgPGRuc2FuZHNAc2FuZGlhLmdvdj4NCj4gDQo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GluZyBsaWtlDQoNCmlmICgobG9u
ZykodV9pbnQzMl90KSpscCAhPSAqbHApIHJldHVybiAoRkFMU0UpOw0KDQotLSANClRyb25kIE15
a2xlYnVzdA0KTGludXggTkZTIGNsaWVudCBtYWludGFpbmVyLCBIYW1tZXJzcGFjZQ0KdHJvbmQu
bXlrbGVidXN0QGhhbW1lcnNwYWNlLmNvbQ0KDQo=

2018-06-28 16:40:59

by Steve Dickson

[permalink] [raw]
Subject: Re: [PATCH] xdrstdio_create buffers do not output encoded values on ppc



On 06/28/2018 12:17 PM, Trond Myklebust wrote:
> On Thu, 2018-06-28 at 11:29 -0400, Steve Dickson wrote:
>> From: Daniel Sands <[email protected]>
>>
>> The cause is that the xdr_putlong uses a long to store the
>> converted value, then passes it to fwrite as a byte buffer.
>> Only the first 4 bytes are written, which is okay for a LE
>> system after byteswapping, but writes all zeroes on BE systems.
>>
>> Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1261738
>>
>> Signed-off-by: Steve Dickson <[email protected]>
>> ---
>> src/xdr_stdio.c | 10 +++++-----
>> 1 file changed, 5 insertions(+), 5 deletions(-)
>>
>> diff --git a/src/xdr_stdio.c b/src/xdr_stdio.c
>> index 4410262..b415f61 100644
>> --- a/src/xdr_stdio.c
>> +++ b/src/xdr_stdio.c
>> @@ -103,10 +103,10 @@ xdrstdio_getlong(xdrs, lp)
>> XDR *xdrs;
>> long *lp;
>> {
>> -
>> - if (fread(lp, sizeof(int32_t), 1, (FILE *)xdrs->x_private)
>> != 1)
>> + u_int32_t mycopy;
>> + if (fread(&mycopy, sizeof(u_int32_t), 1, (FILE *)xdrs-
>>> x_private) != 1)
>> return (FALSE);
>> - *lp = (long)ntohl((u_int32_t)*lp);
>> + *lp = (long)ntohl(mycopy);
>> return (TRUE);
>> }
>>
>> @@ -115,9 +115,9 @@ xdrstdio_putlong(xdrs, lp)
>> XDR *xdrs;
>> const long *lp;
>> {
>> - long mycopy = (long)htonl((u_int32_t)*lp);
>> + u_int32_t mycopy = (u_int32_t)htonl((u_int32_t)*lp);
>>
>> - if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs-
>>> x_private) != 1)
>> + if (fwrite(&mycopy, sizeof(u_int32_t), 1, (FILE *)xdrs-
>>> x_private) != 1)
>> return (FALSE);
>> return (TRUE);
>> }
>
> Hmm... Given that most setups today tend to be 64-bit, shouldn't there
> also be bounds checking in the above 'xdrstdio_putlong()' in order to
> make it safe?
>
> Something like
>
> if ((long)(u_int32_t)*lp != *lp) return (FALSE);
>
Sorry... I'm not following this... why is this necessary
and what are you making safe?

steved.

2018-06-29 01:47:10

by Trond Myklebust

[permalink] [raw]
Subject: Re: [PATCH] xdrstdio_create buffers do not output encoded values on ppc

T24gVGh1LCAyMDE4LTA2LTI4IGF0IDEyOjQwIC0wNDAwLCBTdGV2ZSBEaWNrc29uIHdyb3RlOg0K
PiANCj4gT24gMDYvMjgvMjAxOCAxMjoxNyBQTSwgVHJvbmQgTXlrbGVidXN0IHdyb3RlOg0KPiA+
IE9uIFRodSwgMjAxOC0wNi0yOCBhdCAxMToyOSAtMDQwMCwgU3RldmUgRGlja3NvbiB3cm90ZToN
Cj4gPiA+IEZyb206IERhbmllbCBTYW5kcyA8ZG5zYW5kc0BzYW5kaWEuZ292Pg0KPiA+ID4gDQo+
ID4gPiBUaGUgY2F1c2UgaXMgdGhhdCB0aGUgeGRyX3B1dGxvbmcgdXNlcyBhIGxvbmcgdG8gc3Rv
cmUgdGhlDQo+ID4gPiBjb252ZXJ0ZWQgdmFsdWUsIHRoZW4gcGFzc2VzIGl0IHRvIGZ3cml0ZSBh
cyBhIGJ5dGUgYnVmZmVyLg0KPiA+ID4gT25seSB0aGUgZmlyc3QgNCBieXRlcyBhcmUgd3JpdHRl
biwgd2hpY2ggaXMgb2theSBmb3IgYSBMRQ0KPiA+ID4gc3lzdGVtIGFmdGVyIGJ5dGVzd2FwcGlu
ZywgYnV0IHdyaXRlcyBhbGwgemVyb2VzIG9uIEJFIHN5c3R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