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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5626BC04FF6 for ; Thu, 11 Apr 2024 16:11:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9E7DF10F1C6; Thu, 11 Apr 2024 16:11:56 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=amd.com header.i=@amd.com header.b="Mn7RGTS0"; dkim-atps=neutral Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2048.outbound.protection.outlook.com [40.107.244.48]) by gabe.freedesktop.org (Postfix) with ESMTPS id B8B7710F1C6 for ; Thu, 11 Apr 2024 16:11:53 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N4UCNTSGkAIU9ftEwEkps+3DXnFHrHCGsUujvsM83PhRv9yN8VDUgHT5co0JyWnLtWllZOq6s3UyBK5jVhTZwQ/EVAM/eTDYfRZz/gb5hB+MneI1Tugxd0hvd60GQVqs6c8VJejx1SkbmWGQm0jYyK0aCucb8vT0gdDWazQF74NzOZ99CQ2k8jl4vC4gApui+lBQpyZ4X2Dala2mSNwQU9eME/JfY8+L7Gs53Wh8hJhAC7lHvp6yaOFr2tHjGr2IAQ2RUw4flNC5AukG8Q5merqQQKJcLxaJGFFZF76Ovmc70peX4wyz64W39bN5Qq2ndKgduyhdIdmo4wCu+24+UQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=krp1QRuUdQRQGPMVcSISTiVP0NTVudb5SPTGxdjy680=; b=ZAaNRow7NTibb3kBLuqUek3k2Gz5NUZ4BXPGpBcn5fsWH6w+DH4ptFSCUh47sQVb7Pbaau08b+F6tyeCLHph51fS3Cl+XRI+Rl5MMCGhz7VTWEnlWLT9up4nxHZFLfliUXeOCdoGB99vozLYv1/m1z93Hwd3Vy3D7wJ5njkh2yj6aEzEnwFzTMVN4r8d6whAmrmQ0woK5Q+SxKFodtWkgftLqggoo2h0rG7WtPJo7Bi6a2CtbOQLimktIFTR+I4/8tPfhuttj88hS7bVRFhRx46Ms0cDnvcR6xhCPS820ErO5CdgoKnAu8577wkEBaki7b0GXAEixrsH+xT1OS1QEQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=krp1QRuUdQRQGPMVcSISTiVP0NTVudb5SPTGxdjy680=; b=Mn7RGTS0Ay2mmnsiPO+iTNuR5vlhnPTmUt895LI37nTsVVI2Ir//pSDB9nDTnqvocFY4316AVxuxOr59XyHISnNM/R48D9xd1rXR8gqe2tz26JWd2gVfkfQxVWgz1pHRZBorcxZXv6sh5XedCDyGSdKFFmIaVENRe/u8IZZzX0c= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from PH7PR12MB5685.namprd12.prod.outlook.com (2603:10b6:510:13c::22) by DM4PR12MB9072.namprd12.prod.outlook.com (2603:10b6:8:be::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.55; Thu, 11 Apr 2024 16:11:50 +0000 Received: from PH7PR12MB5685.namprd12.prod.outlook.com ([fe80::f2b6:1034:76e8:f15a]) by PH7PR12MB5685.namprd12.prod.outlook.com ([fe80::f2b6:1034:76e8:f15a%6]) with mapi id 15.20.7409.053; Thu, 11 Apr 2024 16:11:50 +0000 Message-ID: <2eed2681-d469-4fc0-99b1-599b09cc024c@amd.com> Date: Thu, 11 Apr 2024 18:11:44 +0200 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] drm/amdgpu: validate the parameters of amdgpu_vm_bo_clear_mappings To: Jann Horn Cc: xinhui pan , amd-gfx@lists.freedesktop.org, alexander.deucher@amd.com, Vlad Stolyarov References: <20240411032844.41839-1-xinhui.pan@amd.com> <81623e99-58d0-4ab8-9e8e-6b75351a6729@amd.com> Content-Language: en-US From: =?UTF-8?Q?Christian_K=C3=B6nig?= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-ClientProxiedBy: VI1PR07CA0249.eurprd07.prod.outlook.com (2603:10a6:803:b4::16) To PH7PR12MB5685.namprd12.prod.outlook.com (2603:10b6:510:13c::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH7PR12MB5685:EE_|DM4PR12MB9072:EE_ X-MS-Office365-Filtering-Correlation-Id: 8e059acd-aecd-43c3-c660-08dc5a421825 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: f1a4rd/OIrnoQY+dCxvETBqTGTpVgv0DylZx4qmdzRsSzSZxUNetSuMDRFYN90ojCAc4SzKqrMmwnAhcGL8vf6vt0f+4h3qDjc9/ZW2cxQfBnmoSgf84PQN+gigFiJQfwmAxqoFT+n8vmFfw/MR4YCW5kzBKkRQ0N9LsXf96N9+oceGb4Wc4GCLb3Yd4lHjKoTSgGxxHXIsIyDbsd4qRxHaOYSEn6xFcTLGGYPhv4tciD3MJPts3Bn4EZOwvXu+W73PQNeaQYfrb0KbOa9T03goeb+FKzD9QTJ13PDPv2ZtLLyIktZQPxaW9NYmmRV0DxdwzitrFL4qwt9Mn0W4LWUABl4atAha/e81S0KOX21DLc0SNKVlXXHnk5XOLs+8Cw74NNoRBjpjsKhCvMjWAbZ3z3XlHi2Yq6D2KpbfPTLaH8TLRoaIg2OGXdlFDMOkfmfY1qtOtBvv22acJDRon82+DC2fXdoaERKHOkvgwp8BuP6/sjVj66ThUI7CaB/F+uDad4yM3ajDjmv1qs8wZwdZ2zN9WPsu6JyibOSI7v2upROYiL+uZu5JSC8OQM/slA4d6OVi82uFgRIX5JPTDjPz6fDl68x9uJpywUa0vwcnZ3+xPdkxBUHlPL+ws7YeQ5ZJE/g7+C0gqjOsmqn+JbMZvBfxE8lBrzrCjEMDAPrc= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PH7PR12MB5685.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(1800799015)(376005)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?alF1aGRkL2xCM3JXK21SV3c5eVNTNXMrdzRROVcvMU9TOTdzc3BpazJXK1VY?= =?utf-8?B?TUpveW9tTHV5VFQ3eGFBc2laeHBNa1EzbU5sczduMGhrRGxVS0p2Z3JUWklu?= =?utf-8?B?elhvNzRGQlFRQmdxKzNsQW9IS3JRQTNWa1UzMmJmN0tUK3VuWXROVEpNZm03?= =?utf-8?B?SFB1ZzBBWjFZemxuS25neHF0TzZKYWhuY3NqV293RTAxM2tWLzhpam5jeHNG?= =?utf-8?B?azhLQmxSWnZnM2FKR2g4TWROTktETStuTWQvMCtXY0tWN2hOSFA1a1lnWmw2?= =?utf-8?B?YjN6dE1mQ1l0SlhwcStYSVFkb0RGN0E2Tmpyc2Y5SDBwV0JHT1RzdUdpUGJ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MS-Exchange-CrossTenant-Network-Message-Id: 8e059acd-aecd-43c3-c660-08dc5a421825 X-MS-Exchange-CrossTenant-AuthSource: PH7PR12MB5685.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2024 16:11:50.0883 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: oBOyWSKKzZ5nfoVPV99eVznaKqKvGKIeMJhqzZdPq9NfvuZxtRObBNvpuNYQqJQx X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB9072 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: amd-gfx-bounces@lists.freedesktop.org Sender: "amd-gfx" Am 11.04.24 um 17:44 schrieb Jann Horn: > On Thu, Apr 11, 2024 at 12:25 PM Christian König > wrote: >> Am 11.04.24 um 05:28 schrieb xinhui pan: >>> Ensure there is no address overlapping. >>> >>> Reported-by: Vlad Stolyarov >>> Signed-off-by: xinhui pan >>> --- >>> drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 ++++++ >>> 1 file changed, 6 insertions(+) >>> >>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c >>> index 8af3f0fd3073..f1315a854192 100644 >>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c >>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c >>> @@ -1852,6 +1852,12 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, >>> LIST_HEAD(removed); >>> uint64_t eaddr; >>> >>> + /* validate the parameters */ >>> + if (saddr & ~PAGE_MASK || size & ~PAGE_MASK) >>> + return -EINVAL; >> Well as general rule: *never* use PAGE_MASK and other PAGE_* macros >> here. This is GPUVM and not related to the CPUVM space. >> >>> + if (saddr + size <= saddr) >>> + return -EINVAL; >>> + >> Mhm, so basically size is not checked for a wraparound? > Yeah, exactly. > >>> eaddr = saddr + size - 1; >>> saddr /= AMDGPU_GPU_PAGE_SIZE; >>> eaddr /= AMDGPU_GPU_PAGE_SIZE; >> If that's the case then I would rather check for saddr < eaddr here. > FWIW, it would probably a good idea to keep the added check analogous > to other functions called from amdgpu_gem_va_ioctl() like > amdgpu_vm_bo_replace_map(), which also checks "if (saddr + size <= > saddr || offset + size <= offset)" before the division. I would also change that function as well. The eaddr needs to be checked against the max_pfn as well and we currently shift that around for this check which looks quite ugly. Only the overflow check can probably be before it. > >> But that actually shouldn't matter since this code here: >> >> /* Now gather all removed mappings */ >> tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); >> while (tmp) { >> >> Then shouldn't return anything, so the operation is basically a NO-OP then. > That's not how it works; the interval tree is not designed to be fed > bogus ranges that end before they start. (Or at least I don't think it > is - if it is, it is buggy.) I think basically if the specified start > and end addresses are both within an rbtree node, this rbtree node is > returned from the lookup, even if the specified end address is before > the specified start address. Ah, yeah that makes sense. The search functions checks if a node only partially intersects with start and end and not if it is covered by it. Thanks, Christian. > > A more verbose example: > Let's assume the interval tree contains a single entry from address A > to address D. > Looking at the _iter_first implementation in interval_tree_generic.h, > when it is called with a start address C which is between A and D, and > an end address B (result of an addition that wraps around to an > address below C but above A), it does the following: > > 1. bails out if "node->ITSUBTREE < start" (meaning if the specified > start address C is after the range covered by the root node - which is > not the case) > 2. bails out if "ITSTART(leftmost) > last" (meaning if the specified > end address is smaller than the entry start address A - which is not > the case) > 3. enters _subtree_search. in there: > 4. the root node has no children, so "node->ITRB.rb_left" is NULL > 5. the specified end address B is after the node's start address A, so > "Cond1" is fulfilled > 6. the specified start address C is before the node's end address D, > so "Cond2" is fulfilled > 7. the root node is returned from the lookup