Limit sysctl_tcp_mem and sysctl_udp_mem initializers to prevent integer overflows.
authorRobin Holt <holt@sgi.com>
Wed, 20 Oct 2010 02:03:37 +0000 (02:03 +0000)
committerGreg Kroah-Hartman <gregkh@suse.de>
Thu, 9 Dec 2010 21:27:06 +0000 (13:27 -0800)
[ Upstream fixed this in a different way as parts of the commits:
8d987e5c7510 (net: avoid limits overflow)
a9febbb4bd13 (sysctl: min/max bounds are optional)
27b3d80a7b6a (sysctl: fix min/max handling in __do_proc_doulongvec_minmax())
 -DaveM ]

On a 16TB x86_64 machine, sysctl_tcp_mem[2], sysctl_udp_mem[2], and
sysctl_sctp_mem[2] can integer overflow.  Set limit such that they are
maximized without overflowing.

Signed-off-by: Robin Holt <holt@sgi.com>
To: "David S. Miller" <davem@davemloft.net>
Cc: Willy Tarreau <w@1wt.eu>
Cc: linux-kernel@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: linux-sctp@vger.kernel.org
Cc: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
Cc: "Pekka Savola (ipv6)" <pekkas@netcore.fi>
Cc: James Morris <jmorris@namei.org>
Cc: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
Cc: Patrick McHardy <kaber@trash.net>
Cc: Vlad Yasevich <vladislav.yasevich@hp.com>
Cc: Sridhar Samudrala <sri@us.ibm.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
net/ipv4/tcp.c
net/ipv4/udp.c
net/sctp/protocol.c

index 46783087137dec5777f4da00b0d08da76a5ad1e1..734fe94827dc7716e05581ff3013a2c41644fcda 100644 (file)
@@ -2940,12 +2940,14 @@ void __init tcp_init(void)
 
        /* Set the pressure threshold to be a fraction of global memory that
         * is up to 1/2 at 256 MB, decreasing toward zero with the amount of
-        * memory, with a floor of 128 pages.
+        * memory, with a floor of 128 pages, and a ceiling that prevents an
+        * integer overflow.
         */
        nr_pages = totalram_pages - totalhigh_pages;
        limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
        limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
        limit = max(limit, 128UL);
+       limit = min(limit, INT_MAX * 4UL / 3 / 2);
        sysctl_tcp_mem[0] = limit / 4 * 3;
        sysctl_tcp_mem[1] = limit;
        sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2;
index c322f4429fc91576ac954114658152f9f667630d..31db78ca746985165ac38118aa9ebc7e80850526 100644 (file)
@@ -1832,12 +1832,14 @@ void __init udp_init(void)
        udp_table_init(&udp_table);
        /* Set the pressure threshold up by the same strategy of TCP. It is a
         * fraction of global memory that is up to 1/2 at 256 MB, decreasing
-        * toward zero with the amount of memory, with a floor of 128 pages.
+        * toward zero with the amount of memory, with a floor of 128 pages,
+        * and a ceiling that prevents an integer overflow.
         */
        nr_pages = totalram_pages - totalhigh_pages;
        limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
        limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
        limit = max(limit, 128UL);
+       limit = min(limit, INT_MAX * 4UL / 3 / 2);
        sysctl_udp_mem[0] = limit / 4 * 3;
        sysctl_udp_mem[1] = limit;
        sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
index 612dc878e05c9aaab9970d322e5ba2798f91bd6a..619f96568e8e88bea2ce22186841afe8c8986311 100644 (file)
@@ -1157,7 +1157,8 @@ SCTP_STATIC __init int sctp_init(void)
 
        /* Set the pressure threshold to be a fraction of global memory that
         * is up to 1/2 at 256 MB, decreasing toward zero with the amount of
-        * memory, with a floor of 128 pages.
+        * memory, with a floor of 128 pages, and a ceiling that prevents an
+        * integer overflow.
         * Note this initalizes the data in sctpv6_prot too
         * Unabashedly stolen from tcp_init
         */
@@ -1165,6 +1166,7 @@ SCTP_STATIC __init int sctp_init(void)
        limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
        limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
        limit = max(limit, 128UL);
+       limit = min(limit, INT_MAX * 4UL / 3 / 2);
        sysctl_sctp_mem[0] = limit / 4 * 3;
        sysctl_sctp_mem[1] = limit;
        sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;