1 From 92988de9edaa8e54456640dbcd866e74c558c911 Mon Sep 17 00:00:00 2001
2 From: Robin Holt <holt@sgi.com>
3 Date: Wed, 20 Oct 2010 02:03:37 +0000
4 Subject: Limit sysctl_tcp_mem and sysctl_udp_mem initializers to prevent integer overflows.
6 From: Robin Holt <holt@sgi.com>
8 [ Upstream fixed this in a different way as parts of the commits:
9 8d987e5c7510 (net: avoid limits overflow)
10 a9febbb4bd13 (sysctl: min/max bounds are optional)
11 27b3d80a7b6a (sysctl: fix min/max handling in __do_proc_doulongvec_minmax())
14 On a 16TB x86_64 machine, sysctl_tcp_mem[2], sysctl_udp_mem[2], and
15 sysctl_sctp_mem[2] can integer overflow. Set limit such that they are
16 maximized without overflowing.
18 Signed-off-by: Robin Holt <holt@sgi.com>
19 To: "David S. Miller" <davem@davemloft.net>
20 Cc: Willy Tarreau <w@1wt.eu>
21 Cc: linux-kernel@vger.kernel.org
22 Cc: netdev@vger.kernel.org
23 Cc: linux-sctp@vger.kernel.org
24 Cc: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
25 Cc: "Pekka Savola (ipv6)" <pekkas@netcore.fi>
26 Cc: James Morris <jmorris@namei.org>
27 Cc: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
28 Cc: Patrick McHardy <kaber@trash.net>
29 Cc: Vlad Yasevich <vladislav.yasevich@hp.com>
30 Cc: Sridhar Samudrala <sri@us.ibm.com>
31 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
33 net/ipv4/tcp.c | 4 +++-
34 net/ipv4/udp.c | 4 +++-
35 net/sctp/protocol.c | 4 +++-
36 3 files changed, 9 insertions(+), 3 deletions(-)
40 @@ -2754,12 +2754,14 @@ void __init tcp_init(void)
42 /* Set the pressure threshold to be a fraction of global memory that
43 * is up to 1/2 at 256 MB, decreasing toward zero with the amount of
44 - * memory, with a floor of 128 pages.
45 + * memory, with a floor of 128 pages, and a ceiling that prevents an
48 nr_pages = totalram_pages - totalhigh_pages;
49 limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
50 limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
51 limit = max(limit, 128UL);
52 + limit = min(limit, INT_MAX * 4UL / 3 / 2);
53 sysctl_tcp_mem[0] = limit / 4 * 3;
54 sysctl_tcp_mem[1] = limit;
55 sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2;
58 @@ -1722,11 +1722,13 @@ void __init udp_init(void)
60 /* Set the pressure threshold up by the same strategy of TCP. It is a
61 * fraction of global memory that is up to 1/2 at 256 MB, decreasing
62 - * toward zero with the amount of memory, with a floor of 128 pages.
63 + * toward zero with the amount of memory, with a floor of 128 pages,
64 + * and a ceiling that prevents an integer overflow.
66 limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
67 limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
68 limit = max(limit, 128UL);
69 + limit = min(limit, INT_MAX * 4UL / 3 / 2);
70 sysctl_udp_mem[0] = limit / 4 * 3;
71 sysctl_udp_mem[1] = limit;
72 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
73 --- a/net/sctp/protocol.c
74 +++ b/net/sctp/protocol.c
75 @@ -1179,7 +1179,8 @@ SCTP_STATIC __init int sctp_init(void)
77 /* Set the pressure threshold to be a fraction of global memory that
78 * is up to 1/2 at 256 MB, decreasing toward zero with the amount of
79 - * memory, with a floor of 128 pages.
80 + * memory, with a floor of 128 pages, and a ceiling that prevents an
82 * Note this initalizes the data in sctpv6_prot too
83 * Unabashedly stolen from tcp_init
85 @@ -1187,6 +1188,7 @@ SCTP_STATIC __init int sctp_init(void)
86 limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
87 limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
88 limit = max(limit, 128UL);
89 + limit = min(limit, INT_MAX * 4UL / 3 / 2);
90 sysctl_sctp_mem[0] = limit / 4 * 3;
91 sysctl_sctp_mem[1] = limit;
92 sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;