Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3948935rdh; Tue, 28 Nov 2023 07:58:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IE1VSmIjYrr7KH4hOBA5u771iTpCqZS2rAikc0viysU6zdaGEBJ5Ojv3lWC33QnfMQ3+bP0 X-Received: by 2002:a05:6a21:183:b0:18c:1570:49fb with SMTP id le3-20020a056a21018300b0018c157049fbmr18526222pzb.50.1701187097358; Tue, 28 Nov 2023 07:58:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701187097; cv=none; d=google.com; s=arc-20160816; b=IxcDtAgDoCM/EyHOROyhWmXQHULhDzwW6533mokr9SGcF1T+apg+z/O7P2O3KYZ9Dx GGX6redAEVadpZSGo6DhLatSKdCSuxfK9CdS1jFduQNZk8aZ/2rTlWwkthLfvUrU5azD A9v1P1qzmZ4w8CNg5j4qOnN+MpbvGVMxDjqAT+pHDNX7PtrKVyFr32L6RVu104Mu3Vcz qnrCIC6SrSbvhBufRsxFICOMJeOSYZ5HDjmwqOAsoOpNgh1aVufbtlQloWn7p6jVrajt DauIg9zDD9vvTc+RDtSxnojGqsoEixfy2dtxiu9YYjmeKosSBLuFC7MlZ01uqIoeEUql CGgA== 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:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=4wAhSwnTXqPL8DFCSCSo1D+1BXr5do7lM46ckR+Mmfg=; fh=VUL/ECkOX+BkSJ+FINYenTQ70tG+/BQgesrqPc+2cNo=; b=pS5MdGIdjMlNs9bWhxaICWwCdD+euaJiYNjjtjWL5x2+5irlpnYKVuW+ct3ZvnZDoo XdH7oJNcoIOrCY1yWUW9FmXTv6444ImKNmqbi6Owdm+qSjWxGmZUBRBakOrsp4dvAT6O 2sIMlVdnpISr5tBWTwAsiam450oCl0NQLlDzvY65ML9kb7jX5hpCBhsN/1s//XZVSvYR uKYGhGAJT7ro4JhZgtBBIUoGmauO45RV3HI0sjsJf/aIj3pgHYtQfABgL0ggPXqENsFd tQWMZ8Ng8UEeUiUa2Pt/FH79EkYkIQoVAbaIEhC2JnMWZhH3PvFtgV6qUPqxU1TMzX2M qFyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=w560Ljwh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id t16-20020a056a00139000b006c4d2c8a3fcsi12646501pfg.329.2023.11.28.07.58.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 07:58:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=w560Ljwh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id B679F8067B60; Tue, 28 Nov 2023 07:58:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346173AbjK1P55 (ORCPT + 99 others); Tue, 28 Nov 2023 10:57:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346083AbjK1P54 (ORCPT ); Tue, 28 Nov 2023 10:57:56 -0500 Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com [IPv6:2607:f8b0:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8301F90 for ; Tue, 28 Nov 2023 07:58:02 -0800 (PST) Received: by mail-il1-x12b.google.com with SMTP id e9e14a558f8ab-35c9cba4fa6so1226525ab.0 for ; Tue, 28 Nov 2023 07:58:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1701187082; x=1701791882; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=4wAhSwnTXqPL8DFCSCSo1D+1BXr5do7lM46ckR+Mmfg=; b=w560LjwhUX847ndgWUlE7dC7Sx2B8QfuSv/C5KGtQBSlhiHZBsCc+tF1BdqwvE7oBW /VJVpDkgzCLomZ4ZfkpYqXrhQ92kuu/iy4Yv8jNXxAgjiJIdN6fyvGlaHbdxu3wC/4we d0CEihulT7PVByudMvknrWh7MPI0QYeHe7FjGaQ4Oz+UPs0F0cn3jJsXTC7IdygdBF/k 8zB6R4SpwBcXyivykIJkdbMZDvEEqB0r0PpnIZ2FChOt97ROk4NIov/VW5GHPM2kkMrB fg5hH2E7/D9vVuuWtlPm4OyH9jnKbDwLg1+IWP8PNU1MBxoDO0FEg+Wngu/4TWfK2VIu MKcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701187082; x=1701791882; h=content-transfer-encoding:in-reply-to: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=4wAhSwnTXqPL8DFCSCSo1D+1BXr5do7lM46ckR+Mmfg=; b=qvDzafCttc4E6xcKN1ccZHT6vv0ZT7K4/O05gsYAf9Y1X4UlkniddnBLA8aAjiYoN/ UVPHHQEycN6wTeeBmlc+YgRblVDxFovmZ2z6WYq8PLNZqeOlSipcnW9uWdX08etHfD8D LGauzWR0Re4bZo2Mpa0jxFQO9Sb80GM7oOUBvs/EON0QcRmKT8mGtx+czazsZeBQB42P oGTZUCqJuyVapYmX5GpslgMMGwGnvtrGTfZFcihpeqhp/kTXdSmjN1Wjv95QTHLGnR5I /euAJvGcfdZyqJYKk59z/q6mIicM1V0RCdDrvJo/o+AqpvNSZBLyBmusBvO/m+hIhBXM 6CFg== X-Gm-Message-State: AOJu0YwyAi4E5xW0FtP0RnCdUzaRlqLubj23voDOZabDbgGPkzI2UNcI DJA/q1uaAK1loIVke0ttxXRjAQ== X-Received: by 2002:a6b:7a07:0:b0:7b3:92ea:3438 with SMTP id h7-20020a6b7a07000000b007b392ea3438mr9084569iom.2.1701187081825; Tue, 28 Nov 2023 07:58:01 -0800 (PST) Received: from [192.168.1.116] ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id dp21-20020a056602069500b007b39447e11fsm452312iob.21.2023.11.28.07.58.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 28 Nov 2023 07:58:01 -0800 (PST) Message-ID: Date: Tue, 28 Nov 2023 08:58:00 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: io_uring: risky use of task work, especially wrt fdget() Content-Language: en-US To: Jann Horn , Pavel Begunkov Cc: io-uring , kernel list References: From: Jens Axboe In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 28 Nov 2023 07:58:14 -0800 (PST) On 11/27/23 2:53 PM, Jann Horn wrote: > Hi! > > I noticed something that I think does not currently cause any > significant security issues, but could be problematic in the future: > > io_uring sometimes processes task work in the middle of syscalls, > including between fdget() and fdput(). My understanding of task work > is that it is expected to run in a context similar to directly at > syscall entry/exit: task context, no locks held, sleeping is okay, and > it doesn't execute in the middle of some syscall that expects private > state of the task_struct to stay the same. > > An example of another user of task work is the keyring subsystem, > which does task_work_add() in keyctl_session_to_parent() to change the > cred pointers of another task. > > Several places in io_uring process task work while holding an fdget() > reference to some file descriptor. For example, the io_uring_enter > syscall handler calls io_iopoll_check() while the io_ring_ctx is only > referenced via fdget(). This means that if there were another kernel > subsystem that uses task work to close file descriptors, io_uring > would become unsafe. And io_uring does _almost_ that itself, I think: > io_queue_worker_create() can be run on a workqueue, and uses task work > to launch a worker thread from the context of a userspace thread; and > this worker thread can then accept commands to close file descriptors. > Except it doesn't accept commands to close io_uring file descriptors. > > A closer miss might be io_sync_cancel(), which holds a reference to > some normal file with fdget()/fdput() while calling into > io_run_task_work_sig(). However, from what I can tell, the only things > that are actually done with this file pointer are pointer comparisons, > so this also shouldn't have significant security impact. > > Would it make sense to use fget()/fput() instead of fdget()/fdput() in > io_sync_cancel(), io_uring_enter and io_uring_register? These > functions probably usually run in multithreaded environments anyway > (thanks to the io_uring worker threads), so I would think fdget() > shouldn't bring significant performance savings here? Let me run some testing on that. It's a mistake to think that it's usually multithreaded, generally if you end up using io-wq then it's not a fast path. A fast networked setup, for example, would never touch the threads and hence no threading would be implied by using io_uring. Ditto on the storage front, if you're just reading/writing or eg doing polled IO. That said, those workloads are generally threaded _anyway_ - not because of io_uring, but because that's how these kinds of workloads are written to begin with. So probably won't be much of a concern to do the swap. The only "interesting" part of the above mix of cancel/register/enter is obviously the enter part. The rest are not really fast path. -- Jens Axboe