Coverity 1545175: use after free
[openssl.git] / crypto / rand / rand_egd.c
1 /*
2  * Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11
12 #include <openssl/crypto.h>
13 #include <openssl/e_os2.h>
14 #include <openssl/rand.h>
15
16 /*
17  * Query an EGD
18  */
19
20 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_VOS) || defined(OPENSSL_SYS_UEFI)
21 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
22 {
23     return -1;
24 }
25
26 int RAND_egd(const char *path)
27 {
28     return -1;
29 }
30
31 int RAND_egd_bytes(const char *path, int bytes)
32 {
33     return -1;
34 }
35
36 #else
37
38 # include <unistd.h>
39 # include <stddef.h>
40 # include <sys/types.h>
41 # include <sys/socket.h>
42 # ifndef NO_SYS_UN_H
43 #  include <sys/un.h>
44 # else
45 struct sockaddr_un {
46     short sun_family;           /* AF_UNIX */
47     char sun_path[108];         /* path name (gag) */
48 };
49 # endif                         /* NO_SYS_UN_H */
50 # include <string.h>
51 # include <errno.h>
52
53 # if defined(OPENSSL_SYS_TANDEM)
54 /*
55  * HPNS:
56  *
57  *  This code forces the use of compatibility mode if required on HPE NonStop
58  *  when coreutils PRNGD is used and then restores the previous mode
59  *  after establishing the socket. This is not required on x86 where hardware
60  *  randomization should be used instead of EGD available as of OpenSSL 3.0.
61  *  Use --with-rand-seed=rdcpu when configuring x86 with 3.0 and above.
62  *
63  *  Needs review:
64  *
65  *  The better long-term solution is to either run two EGD's each in one of
66  *  the two modes or revise the EGD code to listen on two different sockets
67  *  (each in one of the two modes) or use the hardware randomizer.
68  */
69 _variable
70 int hpns_socket(int family,
71                 int type,
72                 int protocol,
73                 char* transport)
74 {
75     int  socket_rc;
76     char current_transport[20];
77
78 #  define AF_UNIX_PORTABILITY    "$ZAFN2"
79 #  define AF_UNIX_COMPATIBILITY  "$ZPLS"
80
81     if (!_arg_present(transport) || transport == NULL || transport[0] == '\0')
82         return socket(family, type, protocol);
83
84     socket_transport_name_get(AF_UNIX, current_transport, 20);
85
86     if (strcmp(current_transport, transport) == 0)
87         return socket(family, type, protocol);
88
89     /* set the requested socket transport */
90     if (socket_transport_name_set(AF_UNIX, transport))
91         return -1;
92
93     socket_rc = socket(family, type, protocol);
94
95     /* set mode back to what it was */
96     if (socket_transport_name_set(AF_UNIX, current_transport))
97         return -1;
98
99     return socket_rc;
100 }
101
102 /*#define socket(a,b,c,...) hpns_socket(a,b,c,__VA_ARGS__) */
103
104 static int hpns_connect_attempt = 0;
105
106 # endif /* defined(OPENSSL_SYS_HPNS) */
107
108
109 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
110 {
111     FILE *fp = NULL;
112     struct sockaddr_un addr;
113     int mybuffer, ret = -1, i, numbytes, fd;
114     unsigned char tempbuf[255];
115
116     if (bytes > (int)sizeof(tempbuf))
117         return -1;
118
119     /* Make socket. */
120     memset(&addr, 0, sizeof(addr));
121     addr.sun_family = AF_UNIX;
122     if (strlen(path) >= sizeof(addr.sun_path))
123         return -1;
124     strcpy(addr.sun_path, path);
125     i = offsetof(struct sockaddr_un, sun_path) + strlen(path);
126 #if defined(OPENSSL_SYS_TANDEM)
127     fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_COMPATIBILITY);
128 #else
129     fd = socket(AF_UNIX, SOCK_STREAM, 0);
130 #endif
131     if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
132         return -1;
133     setbuf(fp, NULL);
134
135     /* Try to connect */
136     for (;;) {
137         if (connect(fd, (struct sockaddr *)&addr, i) == 0)
138             break;
139 # ifdef EISCONN
140         if (errno == EISCONN)
141             break;
142 # endif
143         switch (errno) {
144 # ifdef EINTR
145         case EINTR:
146 # endif
147 # ifdef EAGAIN
148         case EAGAIN:
149 # endif
150 # ifdef EINPROGRESS
151         case EINPROGRESS:
152 # endif
153 # ifdef EALREADY
154         case EALREADY:
155 # endif
156             /* No error, try again */
157             break;
158         default:
159 # if defined(OPENSSL_SYS_TANDEM)
160             if (hpns_connect_attempt == 0) {
161                 /* try the other kind of AF_UNIX socket */
162                 close(fd);
163                 fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_PORTABILITY);
164                 if (fd == -1)
165                     return -1;
166                 ++hpns_connect_attempt;
167                 break;  /* try the connect again */
168             }
169 # endif
170
171             ret = -1;
172             goto err;
173         }
174     }
175
176     /* Make request, see how many bytes we can get back. */
177     tempbuf[0] = 1;
178     tempbuf[1] = bytes;
179     if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF)
180         goto err;
181     if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0)
182         goto err;
183     numbytes = tempbuf[0];
184
185     /* Which buffer are we using? */
186     mybuffer = buf == NULL;
187     if (mybuffer)
188         buf = tempbuf;
189
190     /* Read bytes. */
191     i = fread(buf, sizeof(char), numbytes, fp);
192     if (i < numbytes)
193         goto err;
194     ret = numbytes;
195     if (mybuffer)
196         RAND_add(tempbuf, i, i);
197
198  err:
199     if (fp != NULL)
200         fclose(fp);
201     return ret;
202 }
203
204 int RAND_egd_bytes(const char *path, int bytes)
205 {
206     int num;
207
208     num = RAND_query_egd_bytes(path, NULL, bytes);
209     if (num < 0)
210         return -1;
211     if (RAND_status() != 1)
212         return -1;
213     return num;
214 }
215
216 int RAND_egd(const char *path)
217 {
218     return RAND_egd_bytes(path, 255);
219 }
220
221 #endif