-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path006-remote-feed-can-force-unbounded-decompression.patch
More file actions
152 lines (150 loc) · 3.39 KB
/
Copy path006-remote-feed-can-force-unbounded-decompression.patch
File metadata and controls
152 lines (150 loc) · 3.39 KB
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
diff --git a/spamd-setup/spamd-setup.c b/spamd-setup/spamd-setup.c
index eb2cb7d..eaf73e4 100644
--- a/spamd-setup/spamd-setup.c
+++ b/spamd-setup/spamd-setup.c
@@ -43,6 +43,8 @@
#define PATH_PFCTL "/sbin/pfctl"
#define PATH_SPAMD_CONF "/etc/mail/spamd.conf"
#define SPAMD_ARG_MAX 256 /* max # of args to an exec */
+#define SPAMD_FEED_MAX (64 * 1024 * 1024) /* decompressed bytes */
+#define SPAMD_LINE_MAX 8192
#define SPAMD_USER "_spamd"
struct cidr {
@@ -467,72 +469,90 @@ do_message(FILE *sdc, char *msg)
struct bl *
add_blacklist(struct bl *bl, size_t *blc, size_t *bls, gzFile gzf, int white)
{
- int i, n, start, bu = 0, bs = 0, serrno = 0;
- char *buf = NULL, *tmp;
- struct bl *blt;
+ int n, serrno = 0;
+ char rbuf[SPAMD_LINE_MAX], line[SPAMD_LINE_MAX + 1];
+ size_t i, lb = 0, nblc = 0, nbls = 0, total = 0;
+ struct bl start, end, *nbl = NULL, *blt;
for (;;) {
- /* read in gzf, then parse */
- if (bu == bs) {
- tmp = realloc(buf, bs + (1024 * 1024) + 1);
- if (tmp == NULL) {
- serrno = errno;
- free(buf);
- buf = NULL;
- bs = 0;
- goto bldone;
- }
- bs += 1024 * 1024;
- buf = tmp;
- }
-
- n = gzread(gzf, buf + bu, bs - bu);
+ n = gzread(gzf, rbuf, sizeof(rbuf));
if (n == 0)
- goto parse;
+ break;
else if (n == -1) {
- serrno = errno;
- goto bldone;
- } else
- bu += n;
- }
- parse:
- start = 0;
- /* we assume that there is an IP for every 14 bytes */
- if (*blc + bu / 7 >= *bls) {
- *bls += bu / 7;
- blt = reallocarray(bl, *bls, sizeof(struct bl));
- if (blt == NULL) {
- *bls -= bu / 7;
- serrno = errno;
+ serrno = errno ? errno : EIO;
goto bldone;
}
- bl = blt;
+ if ((size_t)n > SPAMD_FEED_MAX - total) {
+ serrno = EFBIG;
+ goto bldone;
+ }
+ total += n;
+ for (i = 0; i < (size_t)n; i++) {
+ if (rbuf[i] == '\n') {
+ line[lb] = '\0';
+ if (parse_netblock(line, &start, &end, white)) {
+ if (nblc + 1 >= nbls) {
+ nbls += 1024;
+ blt = reallocarray(nbl, nbls,
+ sizeof(struct bl));
+ if (blt == NULL) {
+ nbls -= 1024;
+ serrno = errno;
+ goto bldone;
+ }
+ nbl = blt;
+ }
+ nbl[nblc++] = start;
+ nbl[nblc++] = end;
+ }
+ lb = 0;
+ } else {
+ if (lb == SPAMD_LINE_MAX) {
+ serrno = EFBIG;
+ goto bldone;
+ }
+ line[lb++] = rbuf[i];
+ }
+ }
}
- for (i = 0; i <= bu; i++) {
- if (*blc + 1 >= *bls) {
- *bls += 1024;
+ if (lb != 0) {
+ line[lb] = '\0';
+ if (parse_netblock(line, &start, &end, white)) {
+ if (nblc + 1 >= nbls) {
+ nbls += 1024;
+ blt = reallocarray(nbl, nbls, sizeof(struct bl));
+ if (blt == NULL) {
+ nbls -= 1024;
+ serrno = errno;
+ goto bldone;
+ }
+ nbl = blt;
+ }
+ nbl[nblc++] = start;
+ nbl[nblc++] = end;
+ }
+ }
+ if (total == 0)
+ errno = EIO;
+ if (nblc != 0) {
+ if (*blc + nblc > *bls) {
+ *bls = *blc + nblc;
blt = reallocarray(bl, *bls, sizeof(struct bl));
if (blt == NULL) {
- *bls -= 1024;
serrno = errno;
goto bldone;
}
bl = blt;
}
- if (i == bu || buf[i] == '\n') {
- buf[i] = '\0';
- if (parse_netblock(buf + start,
- bl + *blc, bl + *blc + 1, white))
- *blc += 2;
- start = i + 1;
- }
+ memcpy(bl + *blc, nbl, nblc * sizeof(struct bl));
+ *blc += nblc;
}
- if (bu == 0)
- errno = EIO;
bldone:
- free(buf);
- if (serrno)
+ free(nbl);
+ if (serrno) {
errno = serrno;
+ return (NULL);
+ }
return (bl);
}