<div dir="ltr"><font face="arial, sans-serif">Hello, I found an array index out of bounds bug in ngx_inet_add_addr() function.<br>In my case, I want to use ngx_parse_url(cf->pool, u) twice to update my address. </font><div><font face="arial, sans-serif">Consider this situation, my twice function call argument u:  u->url.data is string </font></div><div><font face="arial, sans-serif">of ip address,  </font><span style="font-family:arial,sans-serif">and then, call trace is</span></div><div><font face="arial, sans-serif"> </font></div><div><font face="arial, sans-serif">ngx_inet_add_addr (src/core/ngx_inet.c#L1274)<br></font></div><div><font face="arial, sans-serif">ngx_parse_inet_url (src/core/ngx_inet.c#L968) </font></div><div><font face="arial, sans-serif">ngx_parse_url (src/core/ngx_inet.c#L700)</font></div><div><font face="arial, sans-serif"><br></font><div><div><font face="arial, sans-serif">In first ngx_parse_url() call, u->url.data ip address will successfully add to u->addrs array, </font></div><div><font face="arial, sans-serif">and u->naddrs will  </font><span style="font-family:arial,sans-serif">be increased to 1. And then  the second call ngx_parse_url(), </span></div><div><span style="font-family:arial,sans-serif">u->url.data ip address add to u->addrs array, Because of </span><span style="font-family:arial,sans-serif">in first call n->naddrs was </span></div><div><span style="font-family:arial,sans-serif">increased to 1, so this time our update ip address will add to u->addrs[1],  </span><span style="font-family:arial,sans-serif">but u->addrs </span></div><div><span style="font-family:arial,sans-serif">array were </span>allocated<span style="font-family:arial,sans-serif"> 1 * sizeof(ngx_addr_t).</span></div><div><font face="arial, sans-serif"><br></font></div><font face="arial, sans-serif">src/core/ngx_inet.c#L1275  u->addrs = ngx_palloc(pool, total * nports * sizeof(ngx_addr_t));</font><div><br></div>So the second time I call this function will cause memory error, and it may even make the program crashes.<br><br>In order to avoid this bug, We need to check index of u->addrs.<br></div><div>Could you help me check where there is a problem? Thanks!<div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)"># HG changeset patch</span><br># User Jun Ouyang <<a href="mailto:ouyangjun1999@gmail.com" target="_blank">ouyangjun1999@gmail.com</a>><br># Date 1609070041 -28800<br>#      Sun Dec 27 19:54:01 2020 +0800<br># Node ID 978ff553691d3fec538586cfa88e1e2b9858d4b5<br># Parent  82228f955153527fba12211f52bf102c90f38dfb<br>Multiple call </span>ngx_parse_url<span style="font-family:monospace"> add addr to addrs array cause index out of bounds bug<br><br><span style="font-weight:bold;color:rgb(0,0,0)">diff -r 82228f955153 -r 978ff553691d src/core/ngx_inet.c</span><span style="color:rgb(0,0,0)"></span><br><span style="font-weight:bold;color:rgb(178,24,24)">--- a/src/core/ngx_inet.c       Tue Dec 15 17:41:39 2020 +0300</span><span style="color:rgb(0,0,0)"></span><br><span style="font-weight:bold;color:rgb(24,178,24)">+++ b/src/core/ngx_inet.c       Sun Dec 27 19:54:01 2020 +0800</span><span style="color:rgb(0,0,0)"></span><br><span style="color:rgb(178,24,178)">@@ -1278,6 +1278,10 @@</span><span style="color:rgb(0,0,0)"></span><br>        }<br>    }<br> <br><span style="color:rgb(24,178,24)">+    if (u->naddrs == nports * total) {</span><span style="color:rgb(0,0,0)"></span><br><span style="color:rgb(24,178,24)">+        u->naddrs = 0;</span><span style="color:rgb(0,0,0)"></span><br><span style="color:rgb(24,178,24)">+    }</span><span style="color:rgb(0,0,0)"></span><br><span style="color:rgb(24,178,24)">+</span><span style="color:rgb(0,0,0)"></span><br>    for (i = 0; i < nports; i++) {<br>        sa = ngx_pcalloc(pool, socklen);<br>        if (sa == NULL) {<font color="#888888"><br></font></span></div></div></div><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><u style="color:rgb(0,0,0);font-family:monospace">GPG public key: 4A6D297E6F74638E4D5F8E99152AC7B5F7608B26</u><br></div></div></div></div></div>