From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=44.246.1.125 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795976591; b=a6fV6OAbySJa8Y57Yq4LHSQbdc+yzGxMOVbcz0JhtX035h/7I89avaLasUNPJ0dU+G5Z A5jd31LCDxv1hL3zEpaC1oQlFq0QpCK0QdcWLgKYfjFZ2aEqeyrzmUNG5GEqgdhLZy/8e ZI0Qj/qBEDPipqog8iXXPs9GT6lFr0LTiVwNiuylvvOoNrI2rfgI4LQwnzuC17o681yUx 0eHw7xdZbynpGLUbvTED7h9HtD/PuOFND975tVDYzU56Gi+UE31laFGxXv1qF8ihc1Rhx taNIRIctjbYqjmx1xlnZ/YcbK32loR3M8MEpiLThoA2GckTyNLhgCxlgkD8Rm/1iuOw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795976591; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=zrHTcQlV4AyVqIqroeNGeen426XVcR1UEvzisJZZJLE=; b=hUC8/nLoy3NQZ+oNcIH4S+i/LjTXB3W+iV3txMwv32xwq4+D/0mxBwERaXWy8qnTYvSE L0qQsIYV/CbXKl5VL8lqHIqnCwPZrztlM83JjzjTzEge42bvdkBHE+2Pd9TBQkNB11wac 1Xy7BfsO0a5TQdYB57djA2hb1KhfYvRTMl7RcTSwbgcR1c+fNnTXshTVXAg6EZ3Ua3ami ytatTjhUsDybSBF6SMqvBpx13TdAstLqMVYCuOAGCzoqTowvPyzMwlOZkfxmHa52N2FKu ZJZ/Cf/gYLGAaOXGaQ8dQXhMP/2LO2fhk/TvbECY+YZ3VuHOiZWMOO5lkJuhdg9dYyw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.com; spf=pass smtp.mailfrom=amazon.de; dkim=pass header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=rrQqNOXR; arc=none smtp.remote-ip=44.246.1.125 Received: from pdx-out-002.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-002.esa.us-west-2.outbound.mail-perimeter.amazon.com [44.246.1.125]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32B32606A327; Sun, 29 Nov 2026 18:23:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 32B32606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=rrQqNOXR DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795976591; x=1827512591; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zrHTcQlV4AyVqIqroeNGeen426XVcR1UEvzisJZZJLE=; b=rrQqNOXRZ5Np46ScJWeXiXm9AC23Mngruvl/9+apTCaNDhn730+lYs29 /3V4L4uAdpk1t5xzYF6J5gqBaEVWytVHuNUO6ivUxVRxA/OsJZJ3ou9xc GB1tJNvGtg8dbmaPDsJk6jnZcwSgUUWHQXZQYsd88MtvTgOBDnFfc7vrh Zm+XScu+fgKEO3M34Piu8TK4qBsn8jqSQFc4tiOAV8+DKLCY3shIQOAvW n2bWjrAKXwf8THcPN8bHHKkEavAe3o5ST7t8UyX+fXHX2cJ5XNgvfG6ma tJPIGfwI9d2DaHx/rZRiUMMcGBvCIghlfOxGG2z5e/oOckSPoTweaiT5Q g==; X-CSE-ConnectionGUID: DOOQeiFGQqCVtS0lwKL5rA== X-CSE-MsgGUID: ASWpDROLRrm3wvOesBYlvg== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="35393288" Received: from ip-10-5-0-115.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.0.115]) by internal-pdx-out-002.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:23:08 +0000 Received: from EX19MTAUWC002.ant.amazon.com [205.251.233.111:13354] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.27.231:2525] with esmtp (Farcaster) id 68fd1d9e-4dd0-40b0-b919-23981bc7cdf6; Sun, 29 Nov 2026 18:23:08 +0000 (UTC) X-Farcaster-Flow-ID: 68fd1d9e-4dd0-40b0-b919-23981bc7cdf6 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWC002.ant.amazon.com (10.250.64.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Sun, 29 Nov 2026 18:23:08 +0000 Received: from ip-10-253-83-51.amazon.com (172.19.99.218) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Sun, 29 Nov 2026 18:23:04 +0000 From: Alexander Graf To: Greg Kroah-Hartman , Arnd Bergmann CC: The AWS Nitro Enclaves Team , , Paolo Bonzini , "Stefano Garzarella" , Stefan Hajnoczi , Marek Szyprowski , Robin Murphy , Andrew Morton , "David Hildenbrand" , Mike Rapoport , "Jonathan Corbet" , Shuah Khan , "Randy Dunlap" , , , , , , , , , Subject: [PATCH v2 02/23] nitro_enclaves: Release the reference on a rejected region page Date: Sun, 29 Nov 2026 18:21:31 +0000 Message-ID: <493c6558a8562dc43118d5924ce1526546319a06.1795965038.git.graf@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWA004.ant.amazon.com (10.13.139.19) To EX19D001UWA001.ant.amazon.com (10.13.138.214) A process that passes ordinary anonymous memory to NE_SET_USER_MEMORY_REGION can leak one page reference each time the driver rejects it with NE_ERR_MEM_NOT_HUGE_PAGE. Any process with permission to open /dev/nitro_enclaves and create an enclave can repeat that, which may lead to situations where the system runs out of memory. The ioctl takes a reference on one page at a time and only counts it in ne_mem_region->nr_pages once that page has passed both of the checks that follow, ne_sanity_check_user_mem_region_page() and ne_merge_phys_contig_memory_regions(). The put_pages unwind walks [0, nr_pages), so it misses the page whose check just failed. Count the reference as soon as get_user_pages_unlocked() reports it, so that the unwind covers it. Fixes: 7dc9d4309fdb ("nitro_enclaves: Add logic for setting an enclave memory region") Cc: stable@vger.kernel.org Signed-off-by: Alexander Graf --- drivers/virt/nitro_enclaves/ne_misc_dev.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index 0d7bdad924b73..a50dc4f737b55 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -963,8 +963,8 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, gup_rc = get_user_pages_unlocked(mem_region.userspace_addr + memory_size, 1, ne_mem_region->pages + i, FOLL_GET); - if (gup_rc < 0) { - rc = gup_rc; + if (gup_rc != 1) { + rc = gup_rc < 0 ? gup_rc : -EFAULT; dev_err_ratelimited(ne_misc_dev.this_device, "Error in get user pages [rc=%d]\n", rc); @@ -972,6 +972,12 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, goto put_pages; } + /* + * Count the reference before the checks below, so that the + * put_pages unwind releases this page as well. + */ + ne_mem_region->nr_pages++; + rc = ne_sanity_check_user_mem_region_page(ne_enclave, ne_mem_region->pages[i]); if (rc < 0) goto put_pages; @@ -983,8 +989,6 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, goto put_pages; memory_size += page_size(ne_mem_region->pages[i]); - - ne_mem_region->nr_pages++; } while (memory_size < mem_region.memory_size); if ((ne_enclave->nr_mem_regions + phys_contig_mem_regions.num) > -- 2.47.1