Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/binddynport.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2018, Oracle America, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of "Oracle America, Inc." nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/time.h>
32
33 #include <netdb.h>
34 #include <netinet/in.h>
35
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <syslog.h>
41
42 #include <rpc/rpc.h>
43
44 #include "reentrant.h"
45 #include "rpc_com.h"
46
47 extern pthread_mutex_t port_lock;
48
49 /*
50 * Dynamic port range as defined in RFC 6335 Section 6.
51 * This range avoids all IANA-assigned service port
52 * numbers.
53 */
54 enum {
55 LOWPORT = 49152,
56 ENDPORT = 65534,
57 NPORTS = ENDPORT - LOWPORT + 1,
58 };
59
60 /*
61 * This function decodes information about given port from provided array and
62 * return if port is reserved or not.
63 *
64 * @reserved_ports an array of size at least "NPORTS / (8*sizeof(char)) + 1".
65 * @port port number within range LOWPORT and ENDPORT
66 *
67 * Returns 0 if port is not reserved, non-negative if port is reserved.
68 */
69 static int is_reserved(char *reserved_ports, int port) {
70 port -= LOWPORT;
71 if (port < 0 || port >= NPORTS)
72 return 0;
73 return reserved_ports[port/(8*sizeof(char))] & 1<<(port%(8*sizeof(char)));
74 }
75
76 /*
77 * This function encodes information about given *reserved* port into provided
78 * array. Don't call this function for ports which are not reserved.
79 *
80 * @reserved_ports an array of size at least "NPORTS / (8*sizeof(char)) + 1".
81 * @port port number within range LOWPORT and ENDPORT
82 *
83 */
84 static void set_reserved(char *reserved_ports, int port) {
85 port -= LOWPORT;
86 if (port < 0 || port >= NPORTS)
87 return;
88 reserved_ports[port/(8*sizeof(char))] |= 1<<(port%(8*sizeof(char)));
89 }
90
91 /*
92 * Parse local reserved ports obtained from
93 * /proc/sys/net/ipv4/ip_local_reserved_ports into bit array.
94 *
95 * @reserved_ports a zeroed array of size at least
96 * "NPORTS / (8*sizeof(char)) + 1". Will be used for bit-wise encoding of
97 * reserved ports.
98 *
99 * On each call, reserved ports are read from /proc and bit-wise stored into
100 * provided array
101 *
102 * Returns 0 on success, -1 on failure.
103 */
104
105 static int parse_reserved_ports(char *reserved_ports) {
106 int from=0, to;
107 char delimiter = ',';
108 int res;
109 FILE * file_ptr = fopen("/proc/sys/net/ipv4/ip_local_reserved_ports","r");
110 if (file_ptr == NULL) {
111 (void) syslog(LOG_ERR,
112 "Unable to open open /proc/sys/net/ipv4/ip_local_reserved_ports.");
113 return -1;
114 }
115 do {
116 if ((res = fscanf(file_ptr, "%d", &to)) != 1) {
117 if (res == EOF) break;
118 goto err;
119 }
120 if (delimiter != '-') {
121 from = to;
122 }
123 for (int i = from; i <= to; ++i) {
124 set_reserved(reserved_ports, i);
125 }
126 } while ((res = fscanf(file_ptr, "%c", &delimiter)) == 1);
127 if (res != EOF)
128 goto err;
129 fclose(file_ptr);
130 return 0;
131 err:
132 (void) syslog(LOG_ERR,
133 "An error occurred while parsing ip_local_reserved_ports.");
134 fclose(file_ptr);
135 return -1;
136 }
137
138 /*
139 * Bind a socket to a dynamically-assigned IP port.
140 *
141 * @fd is an open but unbound socket.
142 *
143 * On each call, a port number is chosen at random from
144 * within the dynamic/private port range, even if the
145 * caller has CAP_NET_ADMIN_BIND.
146 *
147 * Returns 0 on success, -1 on failure. errno may be
148 * set to a non-determinant value.
149 *
150 * This function is re-entrant.
151 */
152 int __binddynport(int fd)
153 {
154 struct sockaddr_storage ss;
155 #ifdef INET6
156 struct sockaddr_in6 *sin6;
157 #endif
158 struct sockaddr_in *sin;
159 static unsigned int seed;
160 in_port_t port, *portp;
161 struct sockaddr *sap;
162 socklen_t salen;
163 int i, res, array_size;
164 char *reserved_ports = NULL;
165
166 if (__rpc_sockisbound(fd))
167 return 0;
168
169 res = -1;
170 sap = (struct sockaddr *)(void *)&ss;
171 salen = sizeof(ss);
172 memset(sap, 0, salen);
173
174 mutex_lock(&port_lock);
175
176 if (getsockname(fd, sap, &salen) == -1)
177 goto out;
178
179 switch (ss.ss_family) {
180 case AF_INET:
181 sin = (struct sockaddr_in *)(void *)&ss;
182 portp = &sin->sin_port;
183 salen = sizeof(struct sockaddr_in);
184 break;
185 #ifdef INET6
186 case AF_INET6:
187 sin6 = (struct sockaddr_in6 *)(void *)&ss;
188 portp = &sin6->sin6_port;
189 salen = sizeof(struct sockaddr_in6);
190 break;
191 #endif
192 default:
193 goto out;
194 }
195
196 if (!seed) {
197 struct timeval tv;
198
199 gettimeofday(&tv, NULL);
200 seed = tv.tv_usec * getpid();
201 }
202 array_size = NPORTS / (8*sizeof(char)) + 1;
203 reserved_ports = malloc(array_size);
204 if (!reserved_ports) {
205 goto out;
206 }
207 memset(reserved_ports, 0, array_size);
208 if (parse_reserved_ports(reserved_ports) < 0)
209 goto out;
210
211 port = (rand_r(&seed) % NPORTS) + LOWPORT;
212 for (i = 0; i < NPORTS; ++i) {
213 *portp = htons(port);
214 if (!is_reserved(reserved_ports, port++)) {
215 res = bind(fd, sap, salen);
216 if (res >= 0) {
217 res = 0;
218 break;
219 }
220 if (errno != EADDRINUSE)
221 break;
222 }
223 if (port > ENDPORT)
224 port = LOWPORT;
225 }
226
227 out:
228 free(reserved_ports);
229 mutex_unlock(&port_lock);
230 return res;
231 }