From patchwork Fri Jan 12 22:05:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuxuan Luo X-Patchwork-Id: 1886261 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TBbFN4MN8z1yQ5 for ; Sat, 13 Jan 2024 09:05:27 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1rOPeQ-0005pa-5G; Fri, 12 Jan 2024 22:05:18 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1rOPeM-0005pE-GX for kernel-team@lists.ubuntu.com; Fri, 12 Jan 2024 22:05:14 +0000 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 5384D3F1EE for ; Fri, 12 Jan 2024 22:05:14 +0000 (UTC) Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-680c651928eso111562676d6.2 for ; Fri, 12 Jan 2024 14:05:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705097112; x=1705701912; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3oSinhbSUk/tGJUsgct1m5qw5FohP6zVr6zag7vUBko=; b=v9ko9quZgBOE3l2dqbkH4b6dXAJCHxnMhd7mseJtPzGCP6y4d3mvJqWvzPFYoggWLb tmoRkNizKOkO3AS7k+dhyQcB8L+p2yY0o2rLF6mDR1W1mOsGNr8AxX9IS90fJjuJLSGi xhxgpSY54/LJ9XSgnApX1cjuXkK/w09Q2qxFGR83E/+rgZWFcyskpa9AD/OM6g4JosNf QJ0Gf/t6oOSLeiDlpf7FjxkpNczoS3mtpnq6QfaqmbYoK+Gjra7X9Pza0GTP7k9mUOro wXwH8kthR8dkyYoKbkHIJr9JeLKeJGTW1fQDASexwNKP3Q3qB7jE/wkP9Ki/d/aWSZKg bjmA== X-Gm-Message-State: AOJu0YxHf0DAbKwtzFqSErfucX6ojw3soksueBGv7xtAAduo1kdgT2Hp Fro8fq0CBFquyGDL8KhTt9aGyLJ09unei6VUmKgTDh/grb8LipqXwRVPn+UxlV2OM++4XMkgiDb fH+ntdTzRFGoIYb55YrNhEsvKV9A/Jswhry6WjAULzezXv+9KTItEU/D6dt8= X-Received: by 2002:ac8:4e8c:0:b0:429:ccd2:f618 with SMTP id 12-20020ac84e8c000000b00429ccd2f618mr1666840qtp.79.1705097112339; Fri, 12 Jan 2024 14:05:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IEuCTGipjF+azlQV8BWEc2upsCJZ5QHf2B0vNKPbl7I+KcuPotOnjZ2UQU+Dt6TM8VoJrw+gw== X-Received: by 2002:ac8:4e8c:0:b0:429:ccd2:f618 with SMTP id 12-20020ac84e8c000000b00429ccd2f618mr1666826qtp.79.1705097111808; Fri, 12 Jan 2024 14:05:11 -0800 (PST) Received: from cache-ubuntu.hsd1.nj.comcast.net ([2001:67c:1562:8007::aac:4795]) by smtp.gmail.com with ESMTPSA id fw6-20020a05622a4a8600b00427f89957d9sm1696020qtb.79.2024.01.12.14.05.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 14:05:11 -0800 (PST) From: Yuxuan Luo To: kernel-team@lists.ubuntu.com Subject: [SRU][Mantic][PATCH 1/1] io_uring: don't allow discontig pages for IORING_SETUP_NO_MMAP Date: Fri, 12 Jan 2024 17:05:08 -0500 Message-Id: <20240112220508.113317-2-yuxuan.luo@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240112220508.113317-1-yuxuan.luo@canonical.com> References: <20240112220508.113317-1-yuxuan.luo@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Jens Axboe io_sqes_map() is used rather than io_mem_alloc(), if the application passes in memory for mapping rather than have the kernel allocate it and then mmap(2) the ranges. This then calls __io_uaddr_map() to perform the page mapping and pinning, which checks if we end up with the same pages, if more than one page is mapped. But this check is incorrect and only checks if the first and last pages are the same, where it really should be checking if the mapped pages are contigous. This allows mapping a single normal page, or a huge page range. Down the line we can add support for remapping pages to be virtually contigous, which is really all that io_uring cares about. Cc: stable@vger.kernel.org Fixes: 03d89a2de25b ("io_uring: support for user allocated memory for rings/sqes") Reported-by: Jann Horn Signed-off-by: Jens Axboe (cherry picked from commit 820d070feb668aab5bc9413c285a1dda2a70e076) CVE-2023-6560 Signed-off-by: Yuxuan Luo --- io_uring/io_uring.c | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index b4e8cc4c77be6..43d192dcc934d 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2690,6 +2690,7 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, { struct page **page_array; unsigned int nr_pages; + void *page_addr; int ret, i; *npages = 0; @@ -2711,27 +2712,29 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, io_pages_free(&page_array, ret > 0 ? ret : 0); return ret < 0 ? ERR_PTR(ret) : ERR_PTR(-EFAULT); } - /* - * Should be a single page. If the ring is small enough that we can - * use a normal page, that is fine. If we need multiple pages, then - * userspace should use a huge page. That's the only way to guarantee - * that we get contigious memory, outside of just being lucky or - * (currently) having low memory fragmentation. - */ - if (page_array[0] != page_array[ret - 1]) - goto err; - /* - * Can't support mapping user allocated ring memory on 32-bit archs - * where it could potentially reside in highmem. Just fail those with - * -EINVAL, just like we did on kernels that didn't support this - * feature. - */ + page_addr = page_address(page_array[0]); for (i = 0; i < nr_pages; i++) { - if (PageHighMem(page_array[i])) { - ret = -EINVAL; + ret = -EINVAL; + + /* + * Can't support mapping user allocated ring memory on 32-bit + * archs where it could potentially reside in highmem. Just + * fail those with -EINVAL, just like we did on kernels that + * didn't support this feature. + */ + if (PageHighMem(page_array[i])) goto err; - } + + /* + * No support for discontig pages for now, should either be a + * single normal page, or a huge page. Later on we can add + * support for remapping discontig pages, for now we will + * just fail them with EINVAL. + */ + if (page_address(page_array[i]) != page_addr) + goto err; + page_addr += PAGE_SIZE; } *pages = page_array;