From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12981C04E69 for ; Thu, 10 Aug 2023 16:50:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234894AbjHJQuW (ORCPT ); Thu, 10 Aug 2023 12:50:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbjHJQuU (ORCPT ); Thu, 10 Aug 2023 12:50:20 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E767D10C4 for ; Thu, 10 Aug 2023 09:49:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691686172; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tge3O5xH64mxBw706PQBRXVPAQR3V2nMkvoZ63xOAkM=; b=VCL8S9eJ0Eg+0phM0U70dyD2jh86/g6IMOHqZF4jKKA5nTxiXLXdnWZEd4fRYMqs4xpPoj b+aq+7auN9TkZmeBftEeWrm371254KLSZEH5nxp7qTHG/Z+ju8z+1YZn0UVxggGJZ+wTxp p31wgKXCLz6tB6iWLompNwszJdUUhm0= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-163-1Wa2xPO1Pv-jim4fmb7-kA-1; Thu, 10 Aug 2023 12:49:31 -0400 X-MC-Unique: 1Wa2xPO1Pv-jim4fmb7-kA-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-317a3951296so751267f8f.2 for ; Thu, 10 Aug 2023 09:49:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691686169; x=1692290969; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tge3O5xH64mxBw706PQBRXVPAQR3V2nMkvoZ63xOAkM=; b=HB/xWrzL75fBXaBaZTRGlkBwnDBAC4DEKXNYzMGwmck3RmHUgun8sDAliO3cBfF/7L m5Rz+wRZWSOJo9TA4ISvS7UJt86hRAKRDsuinvA8wR8g/GmVuQbsycQTrG1N0CgVZkZo cdQwbxPnak6ZDFZnNw1vF9d2+CFxA2P9qhB/gM/161j8wAQ14AoHPzIuy+o/jLDAxnUE 5OQ68U9jiBMtAj1G41vMaQfixaXUYi0MYy8Z5RPxPXy3gALcg27A3HSHDj8XUBEyty1R eDvKgegvkUUar4m5rp1XFIJWOdTPmRnosvacANzFmc0/ajvgt84sH3Gg6QSqyo8sZ7HE vclQ== X-Gm-Message-State: AOJu0YzTSXQ3JhMyt+TjNdtvUujtzIaqevOYH7y2nEg527hcAzMExBN6 ZhO70U2rOv0BXJGanyShrRO0FhryoV/YJq/yAbn+J7A/jweJL1nhvS9bFzD/7q8Es6yOcGmjgDR b3rL2Sfu0TmZ1lNiDEhScnQxx X-Received: by 2002:adf:d08f:0:b0:314:15a8:7879 with SMTP id y15-20020adfd08f000000b0031415a87879mr2255466wrh.34.1691686169545; Thu, 10 Aug 2023 09:49:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHq1BI0Gqfj+OV17MjOWpCbOf7zfRKHjnaHO0MoYGdJDjgjD3C4QZsV1UEojpyIp/BuDW68EA== X-Received: by 2002:adf:d08f:0:b0:314:15a8:7879 with SMTP id y15-20020adfd08f000000b0031415a87879mr2255431wrh.34.1691686169133; Thu, 10 Aug 2023 09:49:29 -0700 (PDT) Received: from ?IPV6:2003:cb:c71a:8a00:8200:f041:4b87:a8be? (p200300cbc71a8a008200f0414b87a8be.dip0.t-ipconnect.de. [2003:cb:c71a:8a00:8200:f041:4b87:a8be]) by smtp.gmail.com with ESMTPSA id o17-20020adfe811000000b0031762e89f94sm2657508wrm.117.2023.08.10.09.49.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Aug 2023 09:49:28 -0700 (PDT) Message-ID: <6a7bff41-259b-89f3-1a46-5b5b73d3fea1@redhat.com> Date: Thu, 10 Aug 2023 18:49:26 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH mm-unstable fix] mm: userfaultfd: check for start + len overflow in validate_range: fix Content-Language: en-US To: Ryan Roberts , Axel Rasmussen , Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , Steven Barrett , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, syzbot+42309678e0bc7b32f8e9@syzkaller.appspotmail.com References: <20230714182932.2608735-1-axelrasmussen@google.com> <8fbb5965-28f7-4e9a-ac04-1406ed8fc2d4@arm.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <8fbb5965-28f7-4e9a-ac04-1406ed8fc2d4@arm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10.08.23 17:53, Ryan Roberts wrote: > On 14/07/2023 19:29, Axel Rasmussen wrote: >> This commit removed an extra check for zero-length ranges, and folded it >> into the common validate_range() helper used by all UFFD ioctls. >> >> It failed to notice though that UFFDIO_COPY *only* called validate_range >> on the dst range, not the src range. So removing this check actually let >> us proceed with zero-length source ranges, eventually hitting a BUG >> further down in the call stack. >> >> The correct fix seems clear: call validate_range() on the src range too. >> >> Other ioctls are not affected by this, as they only have one range, not >> two (src + dst). >> >> Reported-by: syzbot+42309678e0bc7b32f8e9@syzkaller.appspotmail.com >> Closes: https://syzkaller.appspot.com/bug?extid=42309678e0bc7b32f8e9 >> Signed-off-by: Axel Rasmussen >> --- >> fs/userfaultfd.c | 3 +++ >> 1 file changed, 3 insertions(+) >> >> diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c >> index 53a7220c4679..36d233759233 100644 >> --- a/fs/userfaultfd.c >> +++ b/fs/userfaultfd.c >> @@ -1759,6 +1759,9 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, >> sizeof(uffdio_copy)-sizeof(__s64))) >> goto out; >> >> + ret = validate_range(ctx->mm, uffdio_copy.src, uffdio_copy.len); >> + if (ret) >> + goto out; >> ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len); >> if (ret) >> goto out; > > > Hi Axel, > > I've just noticed that this patch, now in mm-unstable, regresses the mkdirty mm > selftest: > > # [INFO] detected THP size: 2048 KiB > TAP version 13 > 1..6 > # [INFO] PTRACE write access > ok 1 SIGSEGV generated, page not modified > # [INFO] PTRACE write access to THP > ok 2 SIGSEGV generated, page not modified > # [INFO] Page migration > ok 3 SIGSEGV generated, page not modified > # [INFO] Page migration of THP > ok 4 SIGSEGV generated, page not modified > # [INFO] PTE-mapping a THP > ok 5 SIGSEGV generated, page not modified > # [INFO] UFFDIO_COPY > not ok 6 UFFDIO_COPY failed > Bail out! 1 out of 6 tests failed > # Totals: pass:5 fail:1 xfail:0 xpass:0 skip:0 error:0 > > Whereas all 6 tests pass against v6.5-rc4. > > I'm afraid I don't know the test well and haven't looked at what the issue might > be, but noticed and thought I should point it out. That test (written by me ;) ) essentially does src = malloc(pagesize); dst = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE|MAP_ANON, -1, 0) ... uffdio_copy.dst = (unsigned long) dst; uffdio_copy.src = (unsigned long) src; uffdio_copy.len = pagesize; uffdio_copy.mode = 0; if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy)) { ... So src might not be aligned to a full page. According to the man page: "EINVAL Either dst or len was not a multiple of the system page size, or the range specified by src and len or dst and len was invalid." So, AFAIKT, there is no requirement for src to be page-aligned. Using validate_range() on the src is wrong. -- Cheers, David / dhildenb