Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1539736rwl; Fri, 24 Mar 2023 11:54:26 -0700 (PDT) X-Google-Smtp-Source: AKy350YKqoQ2YxK6HQkua6gPy1BmS1LLAV38+yteEpefeElGpn//31bjkxrdKraqwgY2kRjUifJX X-Received: by 2002:a62:1c4e:0:b0:626:2984:8a76 with SMTP id c75-20020a621c4e000000b0062629848a76mr3914766pfc.34.1679684065909; Fri, 24 Mar 2023 11:54:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679684065; cv=none; d=google.com; s=arc-20160816; b=jnYtCtZt11gxyyU8EIDJ1CeLTkGCVJKQ0EohiIykYcp19mEMWqXNXNxb8GTWyWs2wu nuAgT2BmBzcbnErX7ahUfX3WEmuAUPqIJXqwfTJEkzQ/WnHs+qblKUN2i9xQkIzzHxWY KxHJ7+PsYT7DRROPPx6tly8zfFUbLEXYaE7F0wEwEHQNOXQVjv+jTxIz5znLx2TafX43 XAeg0JPk99NtI2NBKCIlqp8ZT2LxTGemYeNJURHUgNe+Vwy3TCK7EsrFHvscF1smSswE NkTLjnVDCUTRMsARL/f126esWkyi6JKfEET/7OVCFUN8Z3LFKoI+SyNqFISZyzcMtXgK o4dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=CmX8oFFG41gIcNbRGGbko2pxAeYMy78pQFHfFVH9Y70=; b=vc1wY3XQDE1ByU62/vFK6Jy8IUyO33WzcPLUCuoLbCKjgHkuPewodDgprzERHlGWqQ RUIDgcLNMAQ7mzBRaHznoj9DtSLPyBMh53Xa/Hec2TUkllKpPzdFiCTCc/616FlWs3At jKfZa24YhugqpbBeOkYZUpYmYB0LLtIDYSoC8/+10DSZ94+XZy8qSmpk5RyxBHnCpDOA 5FW7prrK2dWJJ+YnJZPh+Qv5zfhgIWNMV+0UQ7P85vYEH9H9TG+cAgGzsHdU/3EreD0n 9+FAZZuNF4DXlDIvfW3K/oFlFIRiLtTFMbc0+z5DuQZSJjXq9rz7+bOYXkX4L+KPU8F/ i0uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lKwKT2jv; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w8-20020a63f508000000b004ff6eee715bsi22098580pgh.460.2023.03.24.11.54.14; Fri, 24 Mar 2023 11:54:25 -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=@kernel.org header.s=k20201202 header.b=lKwKT2jv; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231636AbjCXShM (ORCPT + 99 others); Fri, 24 Mar 2023 14:37:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230190AbjCXShL (ORCPT ); Fri, 24 Mar 2023 14:37:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 256E712B; Fri, 24 Mar 2023 11:37:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A5A1762C3A; Fri, 24 Mar 2023 18:37:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D20BAC433EF; Fri, 24 Mar 2023 18:37:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679683029; bh=qATMgxUc7/Cj2AjJ1pQbCXfHyJibt2CWK82bpsYLbc4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=lKwKT2jvATosVkXDCUEzPEw02cvUIrtlKc+rNC8B3kZGwALTtjAqCdTI4Q5GpoGGN 9Y7si8l171gHXPBR5mBw2WvcJD36A4J0gLyjokARGzgfQhq6b0ae4WG9HmptivDfRZ sPVSIKdZgIiskRlKC/AGFhkuBOtqobk2hRvrIggEtIVQVcHBKlOpb1uVAX6mWbz1eK Y+bJm45ekeVWYX46WrgRSy6HqWenyWfcRsAAiQ9ATFhph3aaM5DJtnezcP+g2kGTPR lnTV9mxkv383cFwpwcmSVU6qbn9yFxUE22v4eHWyHOp3ISB5zDTYdtiX1wEbHWfU51 TG9KVF5SI638w== Date: Fri, 24 Mar 2023 18:37:00 +0000 From: Will Deacon To: Elliot Berman Cc: Alex Elder , Srinivas Kandagatla , Prakruthi Deepak Heragu , Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , Konrad Dybcio , Arnd Bergmann , Greg Kroah-Hartman , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Andy Gross , Catalin Marinas , Jassi Brar , linux-arm-msm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH v11 12/26] gunyah: vm_mgr: Add/remove user memory regions Message-ID: <20230324183659.GB28266@willie-the-truck> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> <20230304010632.2127470-13-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230304010632.2127470-13-quic_eberman@quicinc.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-5.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable 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 Fri, Mar 03, 2023 at 05:06:18PM -0800, Elliot Berman wrote: > When launching a virtual machine, Gunyah userspace allocates memory for > the guest and informs Gunyah about these memory regions through > SET_USER_MEMORY_REGION ioctl. > > Co-developed-by: Prakruthi Deepak Heragu > Signed-off-by: Prakruthi Deepak Heragu > Signed-off-by: Elliot Berman > --- > drivers/virt/gunyah/Makefile | 2 +- > drivers/virt/gunyah/vm_mgr.c | 44 ++++++ > drivers/virt/gunyah/vm_mgr.h | 25 ++++ > drivers/virt/gunyah/vm_mgr_mm.c | 229 ++++++++++++++++++++++++++++++++ > include/uapi/linux/gunyah.h | 29 ++++ > 5 files changed, 328 insertions(+), 1 deletion(-) > create mode 100644 drivers/virt/gunyah/vm_mgr_mm.c [...] > +int gh_vm_mem_alloc(struct gh_vm *ghvm, struct gh_userspace_memory_region *region) > +{ > + struct gh_vm_mem *mapping, *tmp_mapping; > + struct gh_rm_mem_entry *mem_entries; > + phys_addr_t curr_page, prev_page; > + struct gh_rm_mem_parcel *parcel; > + int i, j, pinned, ret = 0; > + size_t entry_size; > + u16 vmid; > + > + if (!region->memory_size || !PAGE_ALIGNED(region->memory_size) || > + !PAGE_ALIGNED(region->userspace_addr) || !PAGE_ALIGNED(region->guest_phys_addr)) > + return -EINVAL; > + > + if (region->guest_phys_addr + region->memory_size < region->guest_phys_addr) > + return -EOVERFLOW; > + > + ret = mutex_lock_interruptible(&ghvm->mm_lock); > + if (ret) > + return ret; > + > + mapping = __gh_vm_mem_find_by_label(ghvm, region->label); > + if (mapping) { > + mutex_unlock(&ghvm->mm_lock); > + return -EEXIST; > + } > + > + mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); > + if (!mapping) { > + mutex_unlock(&ghvm->mm_lock); > + return -ENOMEM; > + } > + > + mapping->parcel.label = region->label; > + mapping->guest_phys_addr = region->guest_phys_addr; > + mapping->npages = region->memory_size >> PAGE_SHIFT; > + parcel = &mapping->parcel; > + parcel->mem_handle = GH_MEM_HANDLE_INVAL; /* to be filled later by mem_share/mem_lend */ > + parcel->mem_type = GH_RM_MEM_TYPE_NORMAL; > + > + /* Check for overlap */ > + list_for_each_entry(tmp_mapping, &ghvm->memory_mappings, list) { > + if (!((mapping->guest_phys_addr + (mapping->npages << PAGE_SHIFT) <= > + tmp_mapping->guest_phys_addr) || > + (mapping->guest_phys_addr >= > + tmp_mapping->guest_phys_addr + (tmp_mapping->npages << PAGE_SHIFT)))) { > + ret = -EEXIST; > + goto free_mapping; > + } > + } > + > + list_add(&mapping->list, &ghvm->memory_mappings); > + > + mapping->pages = kcalloc(mapping->npages, sizeof(*mapping->pages), GFP_KERNEL); > + if (!mapping->pages) { > + ret = -ENOMEM; > + mapping->npages = 0; /* update npages for reclaim */ > + goto reclaim; > + } > + > + pinned = pin_user_pages_fast(region->userspace_addr, mapping->npages, > + FOLL_WRITE | FOLL_LONGTERM, mapping->pages); > + if (pinned < 0) { > + ret = pinned; > + mapping->npages = 0; /* update npages for reclaim */ > + goto reclaim; > + } else if (pinned != mapping->npages) { > + ret = -EFAULT; > + mapping->npages = pinned; /* update npages for reclaim */ > + goto reclaim; > + } I think Fuad mentioned this on an older version of these patches, but it looks like you're failing to account for the pinned memory here which is a security issue depending on who is able to issue the ioctl() calling into here. Specifically, I'm thinking that your kXalloc() calls should be using GFP_KERNEL_ACCOUNT in this function and also that you should be calling account_locked_vm() for the pages being pinned. Finally, what happens if userspace passes in a file mapping? Will