1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
/* $NetBSD: ratelimit.c,v 1.2 2021/10/12 22:51:28 rillig Exp $ */
/*-
* Copyright (c) 2021 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by James Browning, Gabe Coffland, Alex Gavin, and Solomon Ritzow.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: ratelimit.c,v 1.2 2021/10/12 22:51:28 rillig Exp $");
#include <sys/queue.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include "inetd.h"
union addr {
struct in_addr ipv4_addr;
/* ensure aligned for comparison in rl_ipv6_eq (already is on 64-bit) */
#ifdef INET6
struct in6_addr ipv6_addr __attribute__((aligned(16)));
#endif
char other_addr[NI_MAXHOST];
};
static void rl_reset(struct servtab *, time_t);
static time_t rl_time(void);
static void rl_get_name(struct servtab *, int, union addr *);
static void rl_drop_connection(struct servtab *, int);
static struct rl_ip_node *rl_add(struct servtab *, union addr *);
static struct rl_ip_node *rl_try_get_ip(struct servtab *, union addr *);
static bool rl_ip_eq(struct servtab *, union addr *, struct rl_ip_node *);
#ifdef INET6
static bool rl_ipv6_eq(struct in6_addr *, struct in6_addr *);
#endif
#ifdef DEBUG_ENABLE
static void rl_print_found_node(struct servtab *, struct rl_ip_node *);
#endif
static void rl_log_address_exceed(struct servtab *, struct rl_ip_node *);
static const char *rl_node_tostring(struct servtab *, struct rl_ip_node *, char[NI_MAXHOST]);
static bool rl_process_service_max(struct servtab *, int, time_t *);
static bool rl_process_ip_max(struct servtab *, int, time_t *);
/* Return 0 on allow, -1 if connection should be blocked */
int
rl_process(struct servtab *sep, int ctrl)
{
time_t now = -1;
DPRINTF(SERV_FMT ": processing rate-limiting",
SERV_PARAMS(sep));
DPRINTF(SERV_FMT ": se_service_max "
"%zu and se_count %zu", SERV_PARAMS(sep),
sep->se_service_max, sep->se_count);
if (sep->se_count == 0) {
now = rl_time();
sep->se_time = now;
}
if (!rl_process_service_max(sep, ctrl, &now)
|| !rl_process_ip_max(sep, ctrl, &now)) {
return -1;
}
DPRINTF(SERV_FMT ": running service ", SERV_PARAMS(sep));
/* se_count is only incremented if rl_process will return 0 */
sep->se_count++;
return 0;
}
/*
* Get the identifier for the remote peer based on sep->se_socktype and
* sep->se_family
*/
static void
rl_get_name(struct servtab *sep, int ctrl, union addr *out)
{
union {
struct sockaddr_storage ss;
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} addr;
/* Get the sockaddr of socket ctrl */
switch (sep->se_socktype) {
case SOCK_STREAM: {
socklen_t len = sizeof(struct sockaddr_storage);
if (getpeername(ctrl, &addr.sa, &len) == -1) {
/* error, log it and skip ip rate limiting */
syslog(LOG_ERR,
SERV_FMT " failed to get peer name of the "
"connection", SERV_PARAMS(sep));
exit(EXIT_FAILURE);
}
break;
}
case SOCK_DGRAM: {
struct msghdr header = {
.msg_name = &addr.sa,
.msg_namelen = sizeof(struct sockaddr_storage),
/* scatter/gather and control info is null */
};
ssize_t count;
/* Peek so service can still get the packet */
count = recvmsg(ctrl, &header, MSG_PEEK);
if (count == -1) {
syslog(LOG_ERR,
"failed to get dgram source address: %s; exiting",
strerror(errno));
exit(EXIT_FAILURE);
}
break;
}
default:
DPRINTF(SERV_FMT ": ip_max rate limiting not supported for "
"socktype", SERV_PARAMS(sep));
syslog(LOG_ERR, SERV_FMT
": ip_max rate limiting not supported for socktype",
SERV_PARAMS(sep));
exit(EXIT_FAILURE);
}
/* Convert addr to to rate limiting address */
switch (sep->se_family) {
case AF_INET:
out->ipv4_addr = addr.sin.sin_addr;
break;
#ifdef INET6
case AF_INET6:
out->ipv6_addr = addr.sin6.sin6_addr;
break;
#endif
default: {
int res = getnameinfo(&addr.sa,
(socklen_t)addr.sa.sa_len,
out->other_addr, NI_MAXHOST,
NULL, 0,
NI_NUMERICHOST
);
if (res != 0) {
syslog(LOG_ERR,
SERV_FMT ": failed to get name info of "
"the incoming connection: %s; exiting",
SERV_PARAMS(sep), gai_strerror(res));
exit(EXIT_FAILURE);
}
break;
}
}
}
static void
rl_drop_connection(struct servtab *sep, int ctrl)
{
if (sep->se_wait == 0 && sep->se_socktype == SOCK_STREAM) {
/*
* If the fd isn't a listen socket,
* close the individual connection too.
*/
close(ctrl);
return;
}
if (sep->se_socktype != SOCK_DGRAM) {
return;
}
/*
* Drop the single datagram the service would have
* consumed if nowait. If this is a wait service, this
* will consume 1 datagram, and further received packets
* will be removed in the same way.
*/
struct msghdr header = {
/* All fields null, just consume one message */
};
ssize_t count;
count = recvmsg(ctrl, &header, 0);
if (count == -1) {
syslog(LOG_ERR,
SERV_FMT ": failed to consume nowait dgram: %s",
SERV_PARAMS(sep), strerror(errno));
exit(EXIT_FAILURE);
}
DPRINTF(SERV_FMT ": dropped dgram message",
SERV_PARAMS(sep));
}
static time_t
rl_time(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) {
syslog(LOG_ERR, "clock_gettime for rate limiting failed: %s; "
"exiting", strerror(errno));
/* Exit inetd if rate limiting fails */
exit(EXIT_FAILURE);
}
return ts.tv_sec;
}
/* Add addr to IP tracking or return NULL if malloc fails */
static struct rl_ip_node *
rl_add(struct servtab *sep, union addr *addr)
{
struct rl_ip_node *node;
size_t node_size, bufsize;
#ifdef DEBUG_ENABLE
char buffer[NI_MAXHOST];
#endif
switch(sep->se_family) {
case AF_INET:
/* ip_node to end of IPv4 address */
node_size = offsetof(struct rl_ip_node, ipv4_addr)
+ sizeof(struct in_addr);
break;
case AF_INET6:
/* ip_node to end of IPv6 address */
node_size = offsetof(struct rl_ip_node, ipv6_addr)
+ sizeof(struct in6_addr);
break;
default:
/* ip_node to other_addr plus size of string + NULL */
bufsize = strlen(addr->other_addr) + sizeof(char);
node_size = offsetof(struct rl_ip_node, other_addr) + bufsize;
break;
}
node = malloc(node_size);
if (node == NULL) {
if (errno == ENOMEM) {
return NULL;
} else {
syslog(LOG_ERR, "malloc failed unexpectedly: %s",
strerror(errno));
exit(EXIT_FAILURE);
}
}
node->count = 0;
/* copy the data into the new allocation */
switch(sep->se_family) {
case AF_INET:
node->ipv4_addr = addr->ipv4_addr;
break;
case AF_INET6:
/* Hopefully this is inlined, means the same thing as memcpy */
__builtin_memcpy(&node->ipv6_addr, &addr->ipv6_addr,
sizeof(struct in6_addr));
break;
default:
strlcpy(node->other_addr, addr->other_addr, bufsize);
break;
}
/* initializes 'entries' member to NULL automatically */
SLIST_INSERT_HEAD(&sep->se_rl_ip_list, node, entries);
DPRINTF(SERV_FMT ": add '%s' to rate limit tracking (%zu byte record)",
SERV_PARAMS(sep), rl_node_tostring(sep, node, buffer), node_size);
return node;
}
static void
rl_reset(struct servtab *sep, time_t now)
{
DPRINTF(SERV_FMT ": %ji seconds passed; resetting rate limiting ",
SERV_PARAMS(sep), (intmax_t)(now - sep->se_time));
sep->se_count = 0;
sep->se_time = now;
if (sep->se_ip_max != SERVTAB_UNSPEC_SIZE_T) {
rl_clear_ip_list(sep);
}
}
void
rl_clear_ip_list(struct servtab *sep)
{
while (!SLIST_EMPTY(&sep->se_rl_ip_list)) {
struct rl_ip_node *node = SLIST_FIRST(&sep->se_rl_ip_list);
SLIST_REMOVE_HEAD(&sep->se_rl_ip_list, entries);
free(node);
}
}
/* Get the node associated with addr, or NULL */
static struct rl_ip_node *
rl_try_get_ip(struct servtab *sep, union addr *addr)
{
struct rl_ip_node *cur;
SLIST_FOREACH(cur, &sep->se_rl_ip_list, entries) {
if (rl_ip_eq(sep, addr, cur)) {
return cur;
}
}
return NULL;
}
/* Return true if passed service rate limiting checks, false if blocked */
static bool
rl_process_service_max(struct servtab *sep, int ctrl, time_t *now)
{
if (sep->se_count >= sep->se_service_max) {
if (*now == -1) {
/* Only get the clock time if we didn't already */
*now = rl_time();
}
if (*now - sep->se_time > CNT_INTVL) {
rl_reset(sep, *now);
} else {
syslog(LOG_ERR, SERV_FMT
": max spawn rate (%zu in %ji seconds) "
"already met; closing for %ju seconds",
SERV_PARAMS(sep),
sep->se_service_max,
(intmax_t)CNT_INTVL,
(uintmax_t)RETRYTIME);
DPRINTF(SERV_FMT
": max spawn rate (%zu in %ji seconds) "
"already met; closing for %ju seconds",
SERV_PARAMS(sep),
sep->se_service_max,
(intmax_t)CNT_INTVL,
(uintmax_t)RETRYTIME);
rl_drop_connection(sep, ctrl);
/* Close the server for 10 minutes */
close_sep(sep);
if (!timingout) {
timingout = true;
alarm(RETRYTIME);
}
return false;
}
}
return true;
}
/* Return true if passed IP rate limiting checks, false if blocked */
static bool
rl_process_ip_max(struct servtab *sep, int ctrl, time_t *now) {
if (sep->se_ip_max != SERVTAB_UNSPEC_SIZE_T) {
struct rl_ip_node *node;
union addr addr;
rl_get_name(sep, ctrl, &addr);
node = rl_try_get_ip(sep, &addr);
if (node == NULL) {
node = rl_add(sep, &addr);
if (node == NULL) {
/* If rl_add can't allocate, reject request */
DPRINTF("Cannot allocate rl_ip_node");
return false;
}
}
#ifdef DEBUG_ENABLE
else {
/*
* in a separate function to prevent large stack
* frame
*/
rl_print_found_node(sep, node);
}
#endif
DPRINTF(
SERV_FMT ": se_ip_max %zu and ip_count %zu",
SERV_PARAMS(sep), sep->se_ip_max, node->count);
if (node->count >= sep->se_ip_max) {
if (*now == -1) {
*now = rl_time();
}
if (*now - sep->se_time > CNT_INTVL) {
rl_reset(sep, *now);
node = rl_add(sep, &addr);
if (node == NULL) {
DPRINTF("Cannot allocate rl_ip_node");
return false;
}
} else {
if (debug && node->count == sep->se_ip_max) {
/*
* Only log first failed request to
* prevent DoS attack writing to system
* log
*/
rl_log_address_exceed(sep, node);
} else {
DPRINTF(SERV_FMT
": service not started",
SERV_PARAMS(sep));
}
rl_drop_connection(sep, ctrl);
/*
* Increment so debug-syslog message will
* trigger only once
*/
if (node->count < SIZE_MAX) {
node->count++;
}
return false;
}
}
node->count++;
}
return true;
}
static bool
rl_ip_eq(struct servtab *sep, union addr *addr, struct rl_ip_node *cur) {
switch(sep->se_family) {
case AF_INET:
if (addr->ipv4_addr.s_addr == cur->ipv4_addr.s_addr) {
return true;
}
break;
#ifdef INET6
case AF_INET6:
if (rl_ipv6_eq(&addr->ipv6_addr, &cur->ipv6_addr)) {
return true;
}
break;
#endif
default:
if (strncmp(cur->other_addr, addr->other_addr, NI_MAXHOST)
== 0) {
return true;
}
break;
}
return false;
}
#ifdef INET6
static bool
rl_ipv6_eq(struct in6_addr *a, struct in6_addr *b)
{
#if UINTMAX_MAX >= UINT64_MAX
{ /* requires 8 byte aligned structs */
uint64_t *ap = (uint64_t *)a->s6_addr;
uint64_t *bp = (uint64_t *)b->s6_addr;
return (ap[0] == bp[0]) & (ap[1] == bp[1]);
}
#else
{ /* requires 4 byte aligned structs */
uint32_t *ap = (uint32_t *)a->s6_addr;
uint32_t *bp = (uint32_t *)b->s6_addr;
return ap[0] == bp[0] && ap[1] == bp[1] &&
ap[2] == bp[2] && ap[3] == bp[3];
}
#endif
}
#endif
static const char *
rl_node_tostring(struct servtab *sep, struct rl_ip_node *node,
char buffer[NI_MAXHOST])
{
switch (sep->se_family) {
case AF_INET:
#ifdef INET6
case AF_INET6:
#endif
/* ipv4_addr/ipv6_addr share same address */
return inet_ntop(sep->se_family, (void*)&node->ipv4_addr,
(char*)buffer, NI_MAXHOST);
default:
return (char *)&node->other_addr;
}
}
#ifdef DEBUG_ENABLE
/* Separate function due to large buffer size */
static void
rl_print_found_node(struct servtab *sep, struct rl_ip_node *node)
{
char buffer[NI_MAXHOST];
DPRINTF(SERV_FMT ": found record for address '%s'",
SERV_PARAMS(sep), rl_node_tostring(sep, node, buffer));
}
#endif
/* Separate function due to large buffer sie */
static void
rl_log_address_exceed(struct servtab *sep, struct rl_ip_node *node)
{
char buffer[NI_MAXHOST];
const char * name = rl_node_tostring(sep, node, buffer);
syslog(LOG_ERR, SERV_FMT
": max ip spawn rate (%zu in "
"%ji seconds) for "
"'%." TOSTRING(NI_MAXHOST) "s' "
"already met; service not started",
SERV_PARAMS(sep),
sep->se_ip_max,
(intmax_t)CNT_INTVL,
name);
DPRINTF(SERV_FMT
": max ip spawn rate (%zu in "
"%ji seconds) for "
"'%." TOSTRING(NI_MAXHOST) "s' "
"already met; service not started",
SERV_PARAMS(sep),
sep->se_ip_max,
(intmax_t)CNT_INTVL,
name);
}
|