Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp5667247imb; Thu, 7 Mar 2019 22:49:51 -0800 (PST) X-Google-Smtp-Source: APXvYqxX92Z6F2KVXC9ziBmo+EIMWMRTelAuTkzaZmBD7A/qohnogYxrc2tnLSAYDn1WU5P+LDUU X-Received: by 2002:a17:902:87:: with SMTP id a7mr17132086pla.295.1552027791716; Thu, 07 Mar 2019 22:49:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1552027791; cv=none; d=google.com; s=arc-20160816; b=sHA9Dt4OEGBneM9eZ75jx/HMwyyQuabpptEmOKMHAai4v+/d403cgYNl5A3J145zyp y2Aw9EVO3aCyNKSgP/5cT/TwRYmnPvr/7YHbXb3CVAYDRXNydB9+o4yHx/MqwGJDPy5Z vYxZy4wKMI5GBVGK6YbpENQlzSKH2Q5XH22f92zmzmTWMh++LW6a0Cdg2oPYc0eFzHMv YyF9BOyUML2IiFIETNBAnvxXeHi2/xbUAIjyyt9tKjmERz7Qo1wetaqu+5Kog5MYtJgw LSjSEhlEPCUqmycPKQqPCjo92Qych+zJ3zVhiGKLJ7+3WIMCOLb/Cjn9QuKWMC1p/0eb i/Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=jxMdhsDUN7t6iCeERvh3tp8YSDg9ooPohElN1sBXBms=; b=lmiHdUF1XXChxjoMJMghha4sNTdfhnedDZyhnLeGpVWkDaSAOepYU9rjm1z4b4Sv+y ZMw47WuzZZbBrhiQyAj1I2fZgtF4upv7mPp4MoUiedHrT2+DedEW92NbZtmUwTeUq+DI y3OW2sOz8ST/fZM+u5hKRD9K9vOfF/8NbP4h+jUsTThdU+y/mZELhBHtXoxW0TWu5Y7I LoiJt6vE7/soRllM27b0Qljdafk79FobhHC/JTNzKe2/B9VDVp3owuWVtdsU9LO78v98 w6dsd15Phlh/nkQGNlwCwQGJcg1S1YtANiY4u6kMa1zSqYe4pg5rIK99OH6A8Nvfc3JY 1B9Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d127si5734183pgc.303.2019.03.07.22.49.36; Thu, 07 Mar 2019 22:49:51 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726297AbfCHGrq (ORCPT + 99 others); Fri, 8 Mar 2019 01:47:46 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:52646 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725372AbfCHGrp (ORCPT ); Fri, 8 Mar 2019 01:47:45 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id BE25B47C25AD5E53302B for ; Fri, 8 Mar 2019 14:47:43 +0800 (CST) Received: from [127.0.0.1] (10.184.52.56) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.408.0; Fri, 8 Mar 2019 14:47:39 +0800 Subject: Re: [PATCH v3] posix-cpu-timers: Avoid undefined behaviour in timespec64_to_ns() To: Arnd Bergmann CC: Thomas Gleixner , Deepa Dinamani , Linux Kernel Mailing List References: <1551861646-52541-1-git-send-email-wangxiongfeng2@huawei.com> From: Xiongfeng Wang Message-ID: Date: Fri, 8 Mar 2019 14:47:11 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.184.52.56] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/3/6 18:38, Arnd Bergmann wrote: > On Wed, Mar 6, 2019 at 9:41 AM Xiongfeng Wang wrote: >> >> >> diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c >> index 0e84bb7..4b57566 100644 >> --- a/kernel/time/posix-timers.c >> +++ b/kernel/time/posix-timers.c >> @@ -856,6 +856,10 @@ static int do_timer_settime(timer_t timer_id, int flags, >> if (!timespec64_valid(&new_spec64->it_interval) || >> !timespec64_valid(&new_spec64->it_value)) >> return -EINVAL; >> + if (new_spec64->it_interval.tv_sec > KTIME_SEC_MAX) >> + new_spec64->it_interval.tv_sec = KTIME_SEC_MAX; >> + if (new_spec64->it_value.tv_sec > KTIME_SEC_MAX) >> + new_spec64->it_value.tv_sec = KTIME_SEC_MAX; >> > > I looked at the calculation we do later, and I think this can still overflow > if tv_nsec is too large. The largest timespec value we can support is > > (struct timespec64) { .tv_sec = 9223372036, .tv_nsec = 854775807 } > > Your patch caps the tv_sec value to 9223372036, but it does not > cap the tv_nsec. The easiest fix would be to always set tv_nsec > to 0 if tv_sec>=9223372036, or a more correct calculation would > have to limit tv_nsec if tv_sec==9223372036. I don't know if that > matters or not (it should not, unless we explicitly compare the > ktime_t for equality with KTIME_MAX later). Thanks for your advice. I will send another one to set tv_nsec to 0 if tv_sec>=9223372036. Do need to add a helper to clamp timespec64, such as 'saturate_timespec64_valid()' to limit it between 0 and KTIME_MAX(or KTIME_SEC_MAX, it's easier.) Thanks, Xiongfeng > > Arnd > > . >