1 From 8ec2305bb0e0b598e7b20e680f3ee55286e7ecd2 Mon Sep 17 00:00:00 2001
2 From: ZhangXiaoxu <zhangxiaoxu5@huawei.com>
3 Date: Thu, 10 Jan 2019 16:39:06 +0800
4 Subject: ipvs: Fix signed integer overflow when setsockopt timeout
6 [ Upstream commit 53ab60baa1ac4f20b080a22c13b77b6373922fd7 ]
8 There is a UBSAN bug report as below:
9 UBSAN: Undefined behaviour in net/netfilter/ipvs/ip_vs_ctl.c:2227:21
10 signed integer overflow:
11 -2147483647 * 1000 cannot be represented in type 'int'
15 #include <sys/types.h>
16 #include <sys/socket.h>
19 #define IPPROTO_RAW 255
21 #define IP_VS_BASE_CTL (64+1024+64)
22 #define IP_VS_SO_SET_TIMEOUT (IP_VS_BASE_CTL+10)
24 /* The argument to IP_VS_SO_GET_TIMEOUT */
25 struct ipvs_timeout_t {
34 struct ipvs_timeout_t to;
36 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
38 printf("socket init error\n");
42 to.tcp_timeout = -2147483647;
43 to.tcp_fin_timeout = -2147483647;
44 to.udp_timeout = -2147483647;
46 ret = setsockopt(sockfd,
52 printf("setsockopt return %d\n", ret);
56 Return -EINVAL if the timeout value is negative or max than 'INT_MAX / HZ'.
58 Signed-off-by: ZhangXiaoxu <zhangxiaoxu5@huawei.com>
59 Acked-by: Simon Horman <horms@verge.net.au>
60 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
61 Signed-off-by: Sasha Levin <sashal@kernel.org>
63 net/netfilter/ipvs/ip_vs_ctl.c | 12 ++++++++++++
64 1 file changed, 12 insertions(+)
66 diff --git a/net/netfilter/ipvs/ip_vs_ctl.c b/net/netfilter/ipvs/ip_vs_ctl.c
67 index 3167ec76903a..56c62b65923f 100644
68 --- a/net/netfilter/ipvs/ip_vs_ctl.c
69 +++ b/net/netfilter/ipvs/ip_vs_ctl.c
70 @@ -2217,6 +2217,18 @@ static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user
74 +#ifdef CONFIG_IP_VS_PROTO_TCP
75 + if (u->tcp_timeout < 0 || u->tcp_timeout > (INT_MAX / HZ) ||
76 + u->tcp_fin_timeout < 0 || u->tcp_fin_timeout > (INT_MAX / HZ)) {
81 +#ifdef CONFIG_IP_VS_PROTO_UDP
82 + if (u->udp_timeout < 0 || u->udp_timeout > (INT_MAX / HZ))
86 #ifdef CONFIG_IP_VS_PROTO_TCP
88 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);