summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeremy Condra <gcondra@google.com>2012-05-21 14:20:59 -0700
committerGeremy Condra <gcondra@google.com>2012-05-24 15:26:12 -0700
commitb23f193dcc0fa74b5be1978f85cc1c6811493c86 (patch)
tree966ca3c99c6fe3f01205d7c9c320cd24ec94d0fe
parent08e72d0161e39e99ff1003bf1ce894f37d7b7eb4 (diff)
downloadbionic-b23f193dcc0fa74b5be1978f85cc1c6811493c86.zip
bionic-b23f193dcc0fa74b5be1978f85cc1c6811493c86.tar.gz
bionic-b23f193dcc0fa74b5be1978f85cc1c6811493c86.tar.bz2
Ensure that the port number and TXID are properly randomized.
This fix reads from /dev/urandom to get the required entropy. Bug: 6535492 Change-Id: Ibc2fec3f71a67607b608ad9b767b0b6504993c1d
-rw-r--r--libc/netbsd/resolv/res_init.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/libc/netbsd/resolv/res_init.c b/libc/netbsd/resolv/res_init.c
index ffd4054..56a25af 100644
--- a/libc/netbsd/resolv/res_init.c
+++ b/libc/netbsd/resolv/res_init.c
@@ -99,6 +99,8 @@ __RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $");
#include <netdb.h>
#ifdef ANDROID_CHANGES
+#include <errno.h>
+#include <fcntl.h>
#include <sys/system_properties.h>
#endif /* ANDROID_CHANGES */
@@ -716,10 +718,44 @@ net_mask(in) /* XXX - should really use system's version of this */
return (htonl(IN_CLASSC_NET));
}
+#ifdef ANDROID_CHANGES
+static int
+real_randomid(u_int *random_value) {
+ /* open the nonblocking random device, returning -1 on failure */
+ int random_device = open("/dev/urandom", O_RDONLY);
+ if (random_device < 0) {
+ return -1;
+ }
+
+ /* read from the random device, returning -1 on failure (or too many retries)*/
+ u_int retry = 5;
+ for (retry; retry > 0; retry--) {
+ int retval = read(random_device, random_value, sizeof(u_int));
+ if (retval == sizeof(u_int)) {
+ *random_value &= 0xffff;
+ close(random_device);
+ return 0;
+ } else if ((retval < 0) && (errno != EINTR)) {
+ break;
+ }
+ }
+
+ close(random_device);
+ return -1;
+}
+#endif /* ANDROID_CHANGES */
+
u_int
res_randomid(void) {
+#ifdef ANDROID_CHANGES
+ int status = 0;
+ u_int output = 0;
+ status = real_randomid(&output);
+ if (status != -1) {
+ return output;
+ }
+#endif /* ANDROID_CHANGES */
struct timeval now;
-
gettimeofday(&now, NULL);
return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
}