Received: by 2002:ac8:5491:0:b0:40f:fb00:664b with SMTP id h17csp636980qtq; Thu, 10 Aug 2023 11:14:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHAHSi1FCe88gKVx7byfuHHcNdOtkKie8yp8RbuDln3PP4E+W8ZeRWzBUbJtxvgtiUXcFoQ X-Received: by 2002:a05:6a20:3d7:b0:140:94b8:3b76 with SMTP id 23-20020a056a2003d700b0014094b83b76mr3552359pzu.20.1691691253375; Thu, 10 Aug 2023 11:14:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691691253; cv=none; d=google.com; s=arc-20160816; b=Ez0YGEKnvej4bbSkQ81gHqHghUFTGeT7WtBllhJjmqdEEsAOlKLyMJrrmg0pMNXRun 4W8nEuS/8Bd3nYrdd84G89+KHivh85Yb5bu2ujbbJMZeyg3qhOwQuh1xIgfUG1LXqmgg HvFZfWO1p/4EV+4BxLfetVwjuO8Hzn+PWnSXj5LAFkpr54X5rAasShLFZVrQpAKDFlwe +DjLlaf+VNmqjnzR4jpSh5cxbKiyHfBD1KGJmEhgbLDFui0aNZd1tPdRs59HxhaNax00 Q7yEwL3w4nEsJZR56i15sG+MGDh2vWEeP1y6+BksN45GdpPuZlQHAXKmM7yqT6V+rtIF UrbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=tge3O5xH64mxBw706PQBRXVPAQR3V2nMkvoZ63xOAkM=; fh=0eDUhtN2KWVJ9R89b24h1YIMWSHk6HgFPnQ7HsxZ3Ao=; b=pvbuysgtmNuUni683fWSSo5c+rh4Lw/xMtM1o9cGuIfwSw4JSLQ3TYPTLaOQwhRtEz zVGNiiUE6HIfOI0eDSUQKY0I7aRIuDL9IWm8r4V1px0ZdCGhH7mw5UE971/4RyElecwX U1lMl2OJvXMmtSV96vgNMTUBIb+sp68S8gPEfQctE+QBPQYsRe8WJIs4Fm/K8iDq+3Oa nrJm3l8FNL9OVqd8c2CfL5DN7ozWg4MBkT0bvcU2By6WUr7Vd/8EyL2u8QW3lJo0bHGa i1Sb8ON/U7AGjXlEtzIKs6PbOINQSyIlfNCjJvxHDfLc6pgEA45ijTdfsBEPLuzoIpQD qoyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VCL8S9eJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e12-20020a056a001a8c00b00686dafa1aa1si1930457pfv.28.2023.08.10.11.14.00; Thu, 10 Aug 2023 11:14:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VCL8S9eJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234787AbjHJQuV (ORCPT + 99 others); Thu, 10 Aug 2023 12:50:21 -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-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 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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