Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1077962ybl; Wed, 28 Aug 2019 09:18:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqyDzPQoYC4a60p0wAxm6RJSh310rwZ6G87mYVLTHp0EjxlaziT24ATQVy8ePZ+A+eiO5yat X-Received: by 2002:a62:7996:: with SMTP id u144mr5699091pfc.228.1567009112606; Wed, 28 Aug 2019 09:18:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567009112; cv=none; d=google.com; s=arc-20160816; b=AvOcpEVn7nBrTkNtcHDQhm86w8ZEcwf4kqWiimwGKcX5kt2811kRBDBEKQ3J0aC/+N DNDEl0+IFtvBWf8n9bW8sYXr3f/WqZwmdb+jSPlFhkh5pCmWbtBBjgfBLvmjw2z7/6IZ 80lVOHFzI3S6KDJM5du9i+lbFaLZ6669C6nk0gbS2umlem9m8NNpRHBsHSQ4mPLU7fNy 0pgvCf6RQAOSu0asnrJ9tR8uAvBExhDnXBMvDtQ8On5vfeduABSrN4MWzp/lmoA9noBP 48GUIK0BKqRabl2rJy9WAwlDHx0hM1DAfneAq3F1n6d6zKZl1Kz0cpKmlPNy/LSlH0ve ELGg== 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:mail-followup-to :reply-to:message-id:subject:cc:to:from:date; bh=D3QhAP9Mn11JAOtKzF6euD/O0RWI1LuCqRxjn5ZEfOU=; b=HpGfRfIS3A9kJdtFur0K+xaVM+dMTSSH9/yz3DAGJCmMnyD+756vcED60nl/JtB5xR h74Njs0WFbOCbrYrljo0RigIw8tEEIAEGQXcBn9nUankXHvOB1oQp8tHAfq0zcw2GLgn +8qcv4IKvpizBe8ftP1ENNzDcgzGv6IKEK8ebGryUnY3jtylJEuvb3c5PyYv+Xik/7+g vWRaZu+A9JhEhSmcIoO42Jv5DdWNt402h8a8REtxx0gGxX2mQBDZ/73YNWKX9picTJJo vhnr0NGogu5MlkVWb/rVhw8wmiEEcbJRHHyoW8V/euEcL/9mW+zqmg3PfAlHM9e1YRD6 5U/w== 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 c193si3010462pfb.115.2019.08.28.09.18.15; Wed, 28 Aug 2019 09:18:32 -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 S1726606AbfH1QPt (ORCPT + 99 others); Wed, 28 Aug 2019 12:15:49 -0400 Received: from mx2.suse.de ([195.135.220.15]:54658 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726395AbfH1QPt (ORCPT ); Wed, 28 Aug 2019 12:15:49 -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 4DA50B04F; Wed, 28 Aug 2019 16:15:48 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 8CD26DA809; Wed, 28 Aug 2019 18:16:10 +0200 (CEST) Date: Wed, 28 Aug 2019 18:16:09 +0200 From: David Sterba To: Jan Kara Cc: SunKe , 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: <20190828161609.GE2752@twin.jikos.cz> Reply-To: dsterba@suse.cz Mail-Followup-To: dsterba@suse.cz, Jan Kara , SunKe , viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org References: <1562898517-143943-1-git-send-email-sunke32@huawei.com> <20190827152237.GC10306@quack2.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190827152237.GC10306@quack2.suse.cz> User-Agent: Mutt/1.5.23.1-rc1 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 27, 2019 at 05:22:37PM +0200, Jan Kara wrote: > 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 I don't have the original mail in my mailbox to reply, let me qote the code here again: @@ -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; Can this be replaced by check_add_overflow? This can handle signed/unsigned types while the opencoding obscures the meaning. And a shameless plug, I sent a fix for another UB report, in remap_verify_area https://lore.kernel.org/lkml/20190808123942.19592-1-dsterba@suse.com/ that I'd like to get merged.