[openssl-dev] Doubt about memory realloc logic in BUF_MEM_grow_clean and BUF_MEM_grow

Raja ashok raja.ashok at huawei.com
Mon Apr 17 16:08:33 UTC 2017


Hi All,

I am having a doubt BUF_MEM_grow and BUF_MEM_grow_clean. After reallocation of buffer we are memsetting the buffer from ¡°str->length¡± index.

    } else {
        str->data = ret;
        str->max = n;
        memset(&str->data[str->length], 0, len - str->length);
        str->length = len;
    }

I feel we should memset from ¡°str->max¡±.

1)      One usage of this BUF_MEM is init buffer (s->init_buf) in SSL handshake for storing peer¡¯s message and framing response message.

2)      Initially in ssl3_connect and ssl3_accept we are creating this s->init_buf of size 16348 bytes.

3)      Later in ssl3_get_message we are using this buffer for receiving peer¡¯s handshake message of length ¡°l¡±. Here we are calling BUF_MEM_grow_clean with ¡°(l + 4 + 16)¡± as length. If (l + 4 +16) is lesser than 16348, then ¡°init_buf->length¡± is set to that.

4)      After parsing the received msg and we are framing the response message on the same s->init_buf. For example consider ssl3_send_certificate_request, here we are copying Cert types and sign hash algorithm, after that we are calling mem grow before copying X509 name of CA cert.

5)      Here, if the data (cert types and sign hash alg) copied before calling mem grow is more than (l + 4 + 16) of previously received msg, then it will leads to some data loss inside BUF_MEM_grow_clean we are memsetting from ¡°data[str->length]¡±.

In OpenSSL, BUF_MEM is used in many modules like ASN, PEM and SSL. So as per my understanding better we should memset from ¡°max¡± index in case of realloc, like below pseudocode.

    } else {
        str->data = ret;
        memset(&str->data[str->max], 0, n ¨C str->max);
        str->max = n;
        str->length = len;
    }

This we should not do for OPENSSL_malloc, in that case we can memset complete buffer.

Regards,
Ashok

________________________________
[Company_logo]

Raja Ashok V K
Huawei Technologies
Bangalore, India
http://www.huawei.com
________________________________
±¾Óʼþ¼°Æ丽¼þº¬ÓлªÎª¹«Ë¾µÄ±£ÃÜÐÅÏ¢£¬½öÏÞÓÚ·¢Ë͸øÉÏÃæµØÖ·ÖÐÁгöµÄ¸öÈË»òȺ×é¡£½û
Ö¹ÈκÎÆäËûÈËÒÔÈκÎÐÎʽʹÓ㨰üÀ¨µ«²»ÏÞÓÚÈ«²¿»ò²¿·ÖµØй¶¡¢¸´ÖÆ¡¢»òÉ¢·¢£©±¾ÓʼþÖÐ
µÄÐÅÏ¢¡£Èç¹ûÄú´íÊÕÁ˱¾Óʼþ£¬ÇëÄúÁ¢¼´µç»°»òÓʼþ֪ͨ·¢¼þÈ˲¢É¾³ý±¾Óʼþ£¡
This e-mail and its attachments contain confidential information from HUAWEI, which
is intended only for the person or entity whose address is listed above. Any use of the
information contained herein in any way (including, but not limited to, total or partial
disclosure, reproduction, or dissemination) by persons other than the intended
recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by
phone or email immediately and delete it!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mta.openssl.org/pipermail/openssl-dev/attachments/20170417/ffb164fa/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 6737 bytes
Desc: image001.jpg
URL: <http://mta.openssl.org/pipermail/openssl-dev/attachments/20170417/ffb164fa/attachment-0001.jpg>


More information about the openssl-dev mailing list