Skip to content

Commit 81f097d

Browse files
z00467499gregkh
authored andcommitted
ipvs: Fix signed integer overflow when setsockopt timeout
[ Upstream commit 53ab60b ] There is a UBSAN bug report as below: UBSAN: Undefined behaviour in net/netfilter/ipvs/ip_vs_ctl.c:2227:21 signed integer overflow: -2147483647 * 1000 cannot be represented in type 'int' Reproduce program: #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #define IPPROTO_IP 0 #define IPPROTO_RAW 255 #define IP_VS_BASE_CTL (64+1024+64) #define IP_VS_SO_SET_TIMEOUT (IP_VS_BASE_CTL+10) /* The argument to IP_VS_SO_GET_TIMEOUT */ struct ipvs_timeout_t { int tcp_timeout; int tcp_fin_timeout; int udp_timeout; }; int main() { int ret = -1; int sockfd = -1; struct ipvs_timeout_t to; sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); if (sockfd == -1) { printf("socket init error\n"); return -1; } to.tcp_timeout = -2147483647; to.tcp_fin_timeout = -2147483647; to.udp_timeout = -2147483647; ret = setsockopt(sockfd, IPPROTO_IP, IP_VS_SO_SET_TIMEOUT, (char *)(&to), sizeof(to)); printf("setsockopt return %d\n", ret); return ret; } Return -EINVAL if the timeout value is negative or max than 'INT_MAX / HZ'. Signed-off-by: ZhangXiaoxu <zhangxiaoxu5@huawei.com> Acked-by: Simon Horman <horms@verge.net.au> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e5bff43 commit 81f097d

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

net/netfilter/ipvs/ip_vs_ctl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,18 @@ static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user
22172217
u->tcp_fin_timeout,
22182218
u->udp_timeout);
22192219

2220+
#ifdef CONFIG_IP_VS_PROTO_TCP
2221+
if (u->tcp_timeout < 0 || u->tcp_timeout > (INT_MAX / HZ) ||
2222+
u->tcp_fin_timeout < 0 || u->tcp_fin_timeout > (INT_MAX / HZ)) {
2223+
return -EINVAL;
2224+
}
2225+
#endif
2226+
2227+
#ifdef CONFIG_IP_VS_PROTO_UDP
2228+
if (u->udp_timeout < 0 || u->udp_timeout > (INT_MAX / HZ))
2229+
return -EINVAL;
2230+
#endif
2231+
22202232
#ifdef CONFIG_IP_VS_PROTO_TCP
22212233
if (u->tcp_timeout) {
22222234
pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);

0 commit comments

Comments
 (0)