Skip to content

Commit 48596a8

Browse files
Jozsef Kadlecsikummakynes
Jozsef Kadlecsik
authored andcommitted
netfilter: ipset: Fix adding an IPv4 range containing more than 2^31 addresses
Wrong comparison prevented the hash types to add a range with more than 2^31 addresses but reported as a success. Fixes Netfilter's bugzilla id #1005, reported by Oleg Serditov and Oliver Ford. Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 89fcbb5 commit 48596a8

10 files changed

+24
-22
lines changed

net/netfilter/ipset/ip_set_hash_ip.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,12 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
123123
return ret;
124124

125125
ip &= ip_set_hostmask(h->netmask);
126+
e.ip = htonl(ip);
127+
if (e.ip == 0)
128+
return -IPSET_ERR_HASH_ELEM;
126129

127-
if (adt == IPSET_TEST) {
128-
e.ip = htonl(ip);
129-
if (e.ip == 0)
130-
return -IPSET_ERR_HASH_ELEM;
130+
if (adt == IPSET_TEST)
131131
return adtfn(set, &e, &ext, &ext, flags);
132-
}
133132

134133
ip_to = ip;
135134
if (tb[IPSET_ATTR_IP_TO]) {
@@ -148,17 +147,20 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
148147

149148
hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
150149

151-
if (retried)
150+
if (retried) {
152151
ip = ntohl(h->next.ip);
153-
for (; !before(ip_to, ip); ip += hosts) {
154152
e.ip = htonl(ip);
155-
if (e.ip == 0)
156-
return -IPSET_ERR_HASH_ELEM;
153+
}
154+
for (; ip <= ip_to;) {
157155
ret = adtfn(set, &e, &ext, &ext, flags);
158-
159156
if (ret && !ip_set_eexist(ret, flags))
160157
return ret;
161158

159+
ip += hosts;
160+
e.ip = htonl(ip);
161+
if (e.ip == 0)
162+
return 0;
163+
162164
ret = 0;
163165
}
164166
return ret;

net/netfilter/ipset/ip_set_hash_ipmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
149149

150150
if (retried)
151151
ip = ntohl(h->next.ip);
152-
for (; !before(ip_to, ip); ip++) {
152+
for (; ip <= ip_to; ip++) {
153153
e.ip = htonl(ip);
154154
ret = adtfn(set, &e, &ext, &ext, flags);
155155

net/netfilter/ipset/ip_set_hash_ipport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
178178

179179
if (retried)
180180
ip = ntohl(h->next.ip);
181-
for (; !before(ip_to, ip); ip++) {
181+
for (; ip <= ip_to; ip++) {
182182
p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
183183
: port;
184184
for (; p <= port_to; p++) {

net/netfilter/ipset/ip_set_hash_ipportip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
185185

186186
if (retried)
187187
ip = ntohl(h->next.ip);
188-
for (; !before(ip_to, ip); ip++) {
188+
for (; ip <= ip_to; ip++) {
189189
p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
190190
: port;
191191
for (; p <= port_to; p++) {

net/netfilter/ipset/ip_set_hash_ipportnet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
271271

272272
if (retried)
273273
ip = ntohl(h->next.ip);
274-
for (; !before(ip_to, ip); ip++) {
274+
for (; ip <= ip_to; ip++) {
275275
e.ip = htonl(ip);
276276
p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
277277
: port;
@@ -281,7 +281,7 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
281281
ip == ntohl(h->next.ip) &&
282282
p == ntohs(h->next.port)
283283
? ntohl(h->next.ip2) : ip2_from;
284-
while (!after(ip2, ip2_to)) {
284+
while (ip2 <= ip2_to) {
285285
e.ip2 = htonl(ip2);
286286
ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
287287
&cidr);

net/netfilter/ipset/ip_set_hash_net.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
193193
}
194194
if (retried)
195195
ip = ntohl(h->next.ip);
196-
while (!after(ip, ip_to)) {
196+
while (ip <= ip_to) {
197197
e.ip = htonl(ip);
198198
last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
199199
ret = adtfn(set, &e, &ext, &ext, flags);

net/netfilter/ipset/ip_set_hash_netiface.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
255255

256256
if (retried)
257257
ip = ntohl(h->next.ip);
258-
while (!after(ip, ip_to)) {
258+
while (ip <= ip_to) {
259259
e.ip = htonl(ip);
260260
last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
261261
ret = adtfn(set, &e, &ext, &ext, flags);

net/netfilter/ipset/ip_set_hash_netnet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,13 @@ hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
250250
if (retried)
251251
ip = ntohl(h->next.ip[0]);
252252

253-
while (!after(ip, ip_to)) {
253+
while (ip <= ip_to) {
254254
e.ip[0] = htonl(ip);
255255
last = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
256256
ip2 = (retried &&
257257
ip == ntohl(h->next.ip[0])) ? ntohl(h->next.ip[1])
258258
: ip2_from;
259-
while (!after(ip2, ip2_to)) {
259+
while (ip2 <= ip2_to) {
260260
e.ip[1] = htonl(ip2);
261261
last2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]);
262262
ret = adtfn(set, &e, &ext, &ext, flags);

net/netfilter/ipset/ip_set_hash_netport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
241241

242242
if (retried)
243243
ip = ntohl(h->next.ip);
244-
while (!after(ip, ip_to)) {
244+
while (ip <= ip_to) {
245245
e.ip = htonl(ip);
246246
last = ip_set_range_to_cidr(ip, ip_to, &cidr);
247247
e.cidr = cidr - 1;

net/netfilter/ipset/ip_set_hash_netportnet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
291291
if (retried)
292292
ip = ntohl(h->next.ip[0]);
293293

294-
while (!after(ip, ip_to)) {
294+
while (ip <= ip_to) {
295295
e.ip[0] = htonl(ip);
296296
ip_last = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
297297
p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port)
@@ -301,7 +301,7 @@ hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
301301
ip2 = (retried && ip == ntohl(h->next.ip[0]) &&
302302
p == ntohs(h->next.port)) ? ntohl(h->next.ip[1])
303303
: ip2_from;
304-
while (!after(ip2, ip2_to)) {
304+
while (ip2 <= ip2_to) {
305305
e.ip[1] = htonl(ip2);
306306
ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
307307
&e.cidr[1]);

0 commit comments

Comments
 (0)