Global variable in function randombytes() in NaCl library

Jirka DanÄ›k <[email protected]> Thu, 21 Nov 2013 23:05:15 +0100
Newsgroups gmane.org.djb.miscellaneous
Message-ID <CA+QUnKAjq7YkskBuhhAP9VgYLWLJFzbtMnT_CaiSBKVpN6DL0g@mail.gmail.com>
--089e01493c5aaea80104ebb717b1
Content-Type: text/plain; charset=UTF-8

Hello,

I got into a discussion about the use of a global variable called fd in
the randombytes() in NaCl library. The discussion can be found online at
https://github.com/jedisct1/libsodium/issues/101

The function is short so I'll post it here in its enitreity. It is in file
devurandom.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/* it's really stupid that there isn't a syscall for this */

static int fd = -1;

void randombytes(unsigned char *x,unsigned long long xlen)
{
  int i;

  if (fd == -1) {
    for (;;) {
      fd = open("/dev/urandom",O_RDONLY);
      if (fd != -1) break;
      sleep(1);
    }
  }

  while (xlen > 0) {
    if (xlen < 1048576) i = xlen; else i = 1048576;

    i = read(fd,x,i);
    if (i < 1) {
      sleep(1);
      continue;
    }

    x += i;
    xlen -= i;
  }
}

One of the design goals of NaCl says not to use global variables. fd is
shared between threads and it is not unthinkable that more than one thread
enters the then branch of the first if and will call the open function.
This would leak a file descriptor.

The NaCl page says nothing about thread safety.

(Sorry if this is not the proper place to discuss this, haven't found any
better.)

--089e01493c5aaea80104ebb717b1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello,<div><br></div><div>I got into a discussion about th=
e use of a global variable called fd in the=C2=A0randombytes() in NaCl libr=
ary. The discussion can be found online at <a href=3D"https://github.com/je=
disct1/libsodium/issues/101">https://github.com/jedisct1/libsodium/issues/1=
01</a></div>
<div><br></div><div>The function is short so I&#39;ll post it here in its e=
nitreity. It is in file devurandom.c</div><div><br></div><div><div>#include=
 &lt;sys/types.h&gt;</div><div>#include &lt;sys/stat.h&gt;</div><div>#inclu=
de &lt;fcntl.h&gt;</div>
<div>#include &lt;unistd.h&gt;</div><div><br></div><div>/* it&#39;s really =
stupid that there isn&#39;t a syscall for this */</div><div><br></div><div>=
static int fd =3D -1;</div><div><br></div><div>void randombytes(unsigned ch=
ar *x,unsigned long long xlen)</div>
<div>{</div><div>=C2=A0 int i;</div><div><br></div><div>=C2=A0 if (fd =3D=
=3D -1) {</div><div>=C2=A0 =C2=A0 for (;;) {</div><div>=C2=A0 =C2=A0 =C2=A0=
 fd =3D open(&quot;/dev/urandom&quot;,O_RDONLY);</div><div>=C2=A0 =C2=A0 =
=C2=A0 if (fd !=3D -1) break;</div><div>=C2=A0 =C2=A0 =C2=A0 sleep(1);</div=
>
<div>=C2=A0 =C2=A0 }</div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 whi=
le (xlen &gt; 0) {</div><div>=C2=A0 =C2=A0 if (xlen &lt; 1048576) i =3D xle=
n; else i =3D 1048576;</div><div><br></div><div>=C2=A0 =C2=A0 i =3D read(fd=
,x,i);</div><div>=C2=A0 =C2=A0 if (i &lt; 1) {</div>
<div>=C2=A0 =C2=A0 =C2=A0 sleep(1);</div><div>=C2=A0 =C2=A0 =C2=A0 continue=
;</div><div>=C2=A0 =C2=A0 }</div><div><br></div><div>=C2=A0 =C2=A0 x +=3D i=
;</div><div>=C2=A0 =C2=A0 xlen -=3D i;</div><div>=C2=A0 }</div><div>}</div>=
</div><div><br></div><div>One of the design goals of NaCl says not to use g=
lobal variables. fd is shared between threads and it is not unthinkable tha=
t more than one thread enters the then branch of the first if and will call=
 the open function. This would leak a file descriptor.</div>
<div><br></div><div>The NaCl page says nothing about thread safety.</div><d=
iv><br></div><div>(Sorry if this is not the proper place to discuss this, h=
aven&#39;t found any better.)</div></div>

--089e01493c5aaea80104ebb717b1--