Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp5694645ybl; Tue, 27 Aug 2019 08:23:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqyMz/2MWPfiA7oiBzFMLbYJWetMA9zCiJsohnlZDPyqATpTWJkTtltvIWjDmI4tmjiyAnDt X-Received: by 2002:a62:1685:: with SMTP id 127mr13505348pfw.122.1566919432478; Tue, 27 Aug 2019 08:23:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566919432; cv=none; d=google.com; s=arc-20160816; b=k70l9JSWzMTxAcStyhjkEZBil5Z+AmRbgefNPu1n+hIuNd0ADBUyVdWVrulXGHB0aF FyHAOeJfJBIbwnxLfHUh6XTez0lpi6TeefSWcxwHz/9t6t+9c9fx07PMm0qNgkjkWk5K 3Eh1k5Lo55ypIs3tWnzUJ+hEcRU98GXP5+tHpM+7Buwy7tGbAIgAlvoOVrhoKTLmdxZ7 CGMzPCkU4xGL1ez6T/D7Yb2qN3eTKgcbFi1m/qhkOM9flCFaozdHZJ8vSDiJK5a9IMyh Rd+xNqt1Oq59dSJjgzprTWRAXpS/6FFoscBBBDnvv/krpPS4D9boWmI4F3vz4BX9I3Ek Xmiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=0eS0hCHGXapjVp088f5xaG3e4Ro9AGE6L67C7VS52j8=; b=BBI4qPtC2XFENUe0kMAjBzpJbiZTlQQNpSpK9VezEAfxQKaPDm3XQ5JxJPXKbIB5Lu 3GmHp8JeE4O+5ro8ZFtxzABEStoZ9acRN9gsl3H//XcfdABXKdQlEQ1Kr5XH5OEp+E1j Ka95j2ilIytCh9vQRQ9+LjZepafDOU/rUHNreYJIgpf6oExzSK6YFPcT4Yuz26yT3Dea rKOfiTk5xUGnmg+S6OmXJD7mv2bRREc50C+Tlt6zqIM/yPfMZLP0wjTZyjmcGiIYdg7B ZVV5KPX8fhRm2jyQ4YOxkEny40QaSbxe519Nk/Zxp43HI1PYDfIY+yVe1x8JMnY7xVMp FTtQ== 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 t9si12256350pgv.225.2019.08.27.08.23.36; Tue, 27 Aug 2019 08:23:52 -0700 (PDT) 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 S1729935AbfH0PWm (ORCPT + 99 others); Tue, 27 Aug 2019 11:22:42 -0400 Received: from mx2.suse.de ([195.135.220.15]:39158 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727064AbfH0PWk (ORCPT ); Tue, 27 Aug 2019 11:22:40 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 0F03BAD22; Tue, 27 Aug 2019 15:22:38 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id D63BF1E4362; Tue, 27 Aug 2019 17:22:37 +0200 (CEST) Date: Tue, 27 Aug 2019 17:22:37 +0200 From: Jan Kara To: SunKe Cc: viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] fs/sync.c: Fix UBSAN Undefined behaviour in sync_file_range Message-ID: <20190827152237.GC10306@quack2.suse.cz> References: <1562898517-143943-1-git-send-email-sunke32@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1562898517-143943-1-git-send-email-sunke32@huawei.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri 12-07-19 10:28:37, SunKe wrote: > There is a UBSAN report: > UBSAN: Undefined behaviour in ../fs/sync.c:298:10 > signed integer overflow: > -8 + -9223372036854775807 cannot be represented in type 'long long int' > CPU: 0 PID: 15876 Comm: syz-executor.3 Not tainted > Hardware name: QEMU KVM Virtual Machine, BIOS 0.0.0 02/06/2015 > Call trace: > [] dump_backtrace+0x0/0x698 arch/arm64/kernel/traps.c:96 > [] show_stack+0x38/0x60 arch/arm64/kernel/traps.c:234 > [] __dump_stack lib/dump_stack.c:15 [inline] > [] dump_stack+0x1a8/0x230 lib/dump_stack.c:51 > [] ubsan_epilogue+0x34/0x9c lib/ubsan.c:164 > [] handle_overflow+0x228/0x280 lib/ubsan.c:195 > [] __ubsan_handle_add_overflow+0x4c/0x68 lib/ubsan.c:203 > [] SYSC_sync_file_range fs/sync.c:298 [inline] > [] SyS_sync_file_range+0x350/0x3e8 fs/sync.c:285 > [] el0_svc_naked+0x30/0x34 > > When calculate the endbyte, there maybe an overflow, even if no effect > the kernel, but I also want to avoid overflowing and avoid UBSAN reporting. > The original compare is to ensure the offset >= 0 && nbytes >= 0 && no > overflow happened. > > I do the calculate after compare. ensure the offset >= 0 && nbytes >= 0 && > no overflow may happen first. > > Signed-off-by: SunKe Thanks for the patch. The patch looks good to me. You can add: Reviewed-by: Jan Kara Al, care to pickup this fix? Honza > diff --git a/fs/sync.c b/fs/sync.c > index 4d1ff01..5827471 100644 > --- a/fs/sync.c > +++ b/fs/sync.c > @@ -246,15 +246,15 @@ int sync_file_range(struct file *file, loff_t offset, loff_t nbytes, > if (flags & ~VALID_FLAGS) > goto out; > > - endbyte = offset + nbytes; > - > if ((s64)offset < 0) > goto out; > - if ((s64)endbyte < 0) > + if ((s64)nbytes < 0) > goto out; > - if (endbyte < offset) > + if (S64_MAX - offset < nbytes) > goto out; > > + endbyte = offset + nbytes; > + > if (sizeof(pgoff_t) == 4) { > if (offset >= (0x100000000ULL << PAGE_SHIFT)) { > /* > -- > 2.7.4 > -- Jan Kara SUSE Labs, CR