View | Details | Raw Unified | Return to bug 1025068
Collapse All | Expand All

(-)libICE-1.0.9.old/src/iceauth.c (-2 / +76 lines)
Lines 40-45 Author: Ralph Mor, X Consortium Link Here
40
#include <bsd/stdlib.h>	/* for arc4random_buf() */
40
#include <bsd/stdlib.h>	/* for arc4random_buf() */
41
#endif
41
#endif
42
42
43
#ifndef HAVE_GETENTROPY
44
#include <sys/types.h>
45
#include <sys/stat.h>
46
#include <fcntl.h>
47
#include <errno.h>
48
#include <linux/random.h>
49
#include <sys/syscall.h>
50
#endif
51
43
static int was_called_state;
52
static int was_called_state;
44
53
45
#ifndef HAVE_ARC4RANDOM_BUF
54
#ifndef HAVE_ARC4RANDOM_BUF
Lines 78-97 emulate_getrandom_buf ( Link Here
78
    }
87
    }
79
}
88
}
80
89
90
#ifndef HAVE_GETENTROPY
91
static int 
92
emulate_getentropy
93
(
94
	void *buffer, 
95
	size_t length
96
)
97
{
98
    int random_fd = -1;
99
    int res;
100
    size_t filled = 0;
101
    int use_getrandom = 1;
102
103
    if( length > 256 ) {
104
	errno = EIO;
105
	return -1;
106
    }
107
108
    /* try getrandom() first */
109
    res = syscall(SYS_getrandom, (char*)buffer, length, 0);
110
111
    if( (res == -1) && (errno == ENOSYS)) {
112
	/* fallback to /dev/urandrom */    
113
	random_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
114
115
	if( random_fd == -1 ) {
116
	    return -1;
117
	}
118
119
	/* use /dev/urandom further on */
120
	use_getrandom = 0;
121
    }
122
123
    while( filled < length ) {
124
	if (use_getrandom) {
125
	    /*
126
	     * glibc does not contain a syscall wrapper for this in older
127
	     * versions
128
	    */
129
	    res = syscall(SYS_getrandom, (char*)buffer + filled, length - filled, 0);
130
	} else {
131
	    res = read(random_fd, (char*)buffer + filled, length - filled);
132
	}
133
134
	if( res == -1 ) {
135
	    if( errno == EINTR )
136
		continue;
137
138
	    return -1;
139
	} else if ( res == 0 ) {
140
	    // no more bytes available? should not happen
141
	    errno = EIO;
142
	    return -1;
143
	}
144
145
	filled += res;
146
    }
147
148
    return 0;
149
}
150
#endif
151
81
static void
152
static void
82
arc4random_buf (
153
arc4random_buf (
83
	char *auth,
154
	char *auth,
84
	int len
155
	int len
85
)
156
)
86
{
157
{
87
    int	    ret;
158
    int	    ret = -1;
88
159
89
#if HAVE_GETENTROPY
160
#if HAVE_GETENTROPY
90
    /* weak emulation of arc4random through the entropy libc */
161
    /* weak emulation of arc4random through the entropy libc */
91
    ret = getentropy (auth, len);
162
    ret = getentropy (auth, len);
163
#else 
164
    /* drop-in replacement for getentropy() using getrandom() or /dev/urandom directly */
165
    ret = emulate_getentropy(auth, len); 
166
#endif /* HAVE_GETENTROPY */
92
    if (ret == 0)
167
    if (ret == 0)
93
	return;
168
	return;
94
#endif /* HAVE_GETENTROPY */
95
169
96
    emulate_getrandom_buf (auth, len);
170
    emulate_getrandom_buf (auth, len);
97
}
171
}

Return to bug 1025068