Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp38454lqh; Fri, 3 May 2024 12:35:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXdG3REu7xfYnIpJ7bRfKL9ztL+hYjW34eY74N8eQX2YMF0OPf+oHzF3ikMTLky05RoVhh8SfotGuXYOVZvEctvR8qenFROQvZ3la3YLg== X-Google-Smtp-Source: AGHT+IHyEwf2R5jwvkSAioeNzaNmCDwXtI64f4w6UhhPRPrd6KH8nKhNrB0HWnReRzCXgwPZHgHD X-Received: by 2002:a05:6358:99a0:b0:18d:90f1:e79b with SMTP id j32-20020a05635899a000b0018d90f1e79bmr4197250rwb.2.1714764932265; Fri, 03 May 2024 12:35:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714764932; cv=pass; d=google.com; s=arc-20160816; b=a6Z4nnkQRU6dEXHPToJOo62TekfXralZ1lrEidVr4vvPPKgjgzyNVPrJlPgaBrnGc2 e5uVk3ZqhbUwkN3gnz20nbwbdhJghVqODo7Gm7VMfdwDhih2B++cnzCHend5c3fatXnS hfPv3BGH2nCbftQpkch7j3FC4Hx1E49N5NDrAFgO0XkCaOcD87bc9s+0pupMwgKbwrNN nlhJnn3VXncYtcFTyv7SF23IjdE9C9us4V7/dZbyDHk8RX6HrU49v5oqTi30JrJJyiM2 BY2CrsyXLsfBzrMsOnMn+7rpo2bAmQmgd9qKK0jnKFz4Uh2f0qsyg4eJW3VQgrl9a9Lt XbLw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=ORetLTXl3HzmbjljZHEUgf3Mmr/eaSDx8Jozq/iEtsM=; fh=knVstdoV/w7scaLTY2QOXm2SK+MfrurhpFttz2kaIjE=; b=TfquC56rtixFlWJXm+ESZJJbBN+wlYbpkSSEwJwGRjxXHPxx8a8RzlKhbuIWGnBAX1 kckvSKrsG8uetiw1bsIVJOcO+eq8pYKSc5FcNBP5cZ8MSLA61OftnV/i0lMaNGXYBEfc jVKVSsxDdl1cDYs2sE8nFUQfIXeQ8kiS5ahampFc1Z/4wwQB6G1ZTg+KJvvU8R583HQa zLvUYqQQDk6tpdiq4eJTTS8Psw0TYQNidtraIvwY0QJvUGTJrAE6W9PxeAg4UyJGya9O OVtdCr+Oe9uRrozZ1m9UQU6sv8p5tW3tlk8j4mahdMnqksKpdVEVPQQc1a1TrW/8EOMB Mk9Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b="kIk/pWhL"; arc=pass (i=1 spf=pass spfdomain=kernel.dk dkim=pass dkdomain=kernel-dk.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-168140-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168140-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id by29-20020a056a02059d00b0061ca45c3db4si381739pgb.349.2024.05.03.12.35.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 12:35:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-168140-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b="kIk/pWhL"; arc=pass (i=1 spf=pass spfdomain=kernel.dk dkim=pass dkdomain=kernel-dk.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-168140-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168140-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D04F9282021 for ; Fri, 3 May 2024 19:35:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 73208158A35; Fri, 3 May 2024 19:35:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="kIk/pWhL" Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19015158866 for ; Fri, 3 May 2024 19:35:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714764914; cv=none; b=gv8Cdz+z9vAbWriwTtdCvtgK2d542du1/a800wFh3mTxTnWjbuTgyRFZB7B1UXpBOi9n37Bsoqe8VmYQ40+SQ/vb0iX0RpswaZkP1c1CBdXWoDu59xMNUSV9izx4qVvp3/nIidiulbjcViLBFv5lmXjdNbBLtxq3SSX1e42yb18= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714764914; c=relaxed/simple; bh=r8Nv21VrvCRT1FuqygB1ITanHjlHw4scuJdDnqqN7w8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=kEOPUc3+Pyqx5LISKTTlITozz/3RcOL6qNJTEzbTwypE7pH334h0mbULmO04uQ6b58eeifnPZVdJcQb5KikCuurynM44gICTNQ5XJ61rdjYQswVWXceUHslXcfe8qdEOtt8oj7ULgnwHpzTHLKKVVw4yd+9KhKHVnphpJI+CDm4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=kIk/pWhL; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2b3e2d3d05cso19905a91.3 for ; Fri, 03 May 2024 12:35:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1714764911; x=1715369711; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=ORetLTXl3HzmbjljZHEUgf3Mmr/eaSDx8Jozq/iEtsM=; b=kIk/pWhLLh24YCRYq99z9MlLqs5IGr1laqUoGKIcb/YeqEOpn34TV5+d0nSq/IzRDC op9OgzM/NDXqRj08cYtt1YQdMojR+dcKkjiZ44pL9uedK1vgPXZn+9GvR7nOh96Ial/e w1foGDzUo9hovPN6zEOoQ8hV7PafXmJf5vqDNjB1TTCdkKQ1wNb0I++90yfVMY+wDfwV f1CPg/rpax5507TL8k8Q6h1za4Fy58/KS1OQk74JpEnZaU7KCV+izDDhcdCaiBy8Aa+E jsvsm77j3EjNFgNTgRizpY3MtRLLAQpb5JGHoWZ5t8fqUsoXbY75SUYAhDzPyhkQxaZh D/nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714764911; x=1715369711; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ORetLTXl3HzmbjljZHEUgf3Mmr/eaSDx8Jozq/iEtsM=; b=iSvopkqgqueDtMbbmtUT9yS76HXzrzkDh/kpXyW9S5uHN84BVSfTmUsTQ21OO1bjzj 6O/tV8sL7IFOQZXjVMBx2XBmRhU3zaV+WLnF/F7T+rT5KZnUWLdhYpaDyzA3T/jWfOD3 K5u4HsYhFMvdWdqsMfTzlNeDGhuifkzG3JGDCiGn40PP2s9Qzl0hj2yIqrknSEv374Ww 42B8Mk9p+DsHouI4bIT4Tz+NNM4utDOF3xLdVIcORzGFeR14nNTegccgkHrNohnnxfIm rkJGCxzzaobUD+v0LV0gSJEa3eu3thV4N9nzAcbxvIHhkBii2iyLxWEuVNmZTkN1cndH gNlg== X-Forwarded-Encrypted: i=1; AJvYcCWDxJDmyV8N6YQx+5Pz55dgvTSjwKQ9e5PNzi6DKMEIouRoIjfj9TDAfaAzYLEu5qnkx15X/Hua9OttzbsZ/hyC4iL4uZcJq+/auhO9 X-Gm-Message-State: AOJu0YzX82kcOiUi5qL9bBGPAaOhNt7QrtmOlBp6aBDfRK/lD7H0VWIS dDRV8Y/wsad53koggtP/9E3U0eS+6lyRRDQ6lzF6KHzlZa45CTlHzwbxtesYrWMzFpeyHBoavpI G X-Received: by 2002:aa7:880c:0:b0:6ec:ef1c:5c55 with SMTP id c12-20020aa7880c000000b006ecef1c5c55mr3736637pfo.2.1714764911345; Fri, 03 May 2024 12:35:11 -0700 (PDT) Received: from [192.168.1.150] ([198.8.77.194]) by smtp.gmail.com with ESMTPSA id gx14-20020a056a001e0e00b006f44be6cef2sm1664133pfb.114.2024.05.03.12.35.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 03 May 2024 12:35:10 -0700 (PDT) Message-ID: Date: Fri, 3 May 2024 13:35:09 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: get_file() unsafe under epoll (was Re: [syzbot] [fs?] [io-uring?] general protection fault in __ep_remove) To: Kees Cook Cc: Bui Quang Minh , Al Viro , Christian Brauner , syzbot , io-uring@vger.kernel.org, jack@suse.cz, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com, Sumit Semwal , =?UTF-8?Q?Christian_K=C3=B6nig?= , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, Laura Abbott References: <0000000000002d631f0615918f1e@google.com> <7c41cf3c-2a71-4dbb-8f34-0337890906fc@gmail.com> <202405031110.6F47982593@keescook> <64b51cc5-9f5b-4160-83f2-6d62175418a2@kernel.dk> <202405031207.9D62DA4973@keescook> Content-Language: en-US From: Jens Axboe In-Reply-To: <202405031207.9D62DA4973@keescook> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 5/3/24 1:22 PM, Kees Cook wrote: > On Fri, May 03, 2024 at 12:49:11PM -0600, Jens Axboe wrote: >> On 5/3/24 12:26 PM, Kees Cook wrote: >>> Thanks for doing this analysis! I suspect at least a start of a fix >>> would be this: >>> >>> diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c >>> index 8fe5aa67b167..15e8f74ee0f2 100644 >>> --- a/drivers/dma-buf/dma-buf.c >>> +++ b/drivers/dma-buf/dma-buf.c >>> @@ -267,9 +267,8 @@ static __poll_t dma_buf_poll(struct file *file, poll_table *poll) >>> >>> if (events & EPOLLOUT) { >>> /* Paired with fput in dma_buf_poll_cb */ >>> - get_file(dmabuf->file); >>> - >>> - if (!dma_buf_poll_add_cb(resv, true, dcb)) >>> + if (!atomic_long_inc_not_zero(&dmabuf->file) && >>> + !dma_buf_poll_add_cb(resv, true, dcb)) >>> /* No callback queued, wake up any other waiters */ >> >> Don't think this is sane at all. I'm assuming you meant: >> >> atomic_long_inc_not_zero(&dmabuf->file->f_count); > > Oops, yes, sorry. I was typed from memory instead of copy/paste. Figured :-) >> but won't fly as you're not under RCU in the first place. And what >> protects it from being long gone before you attempt this anyway? This is >> sane way to attempt to fix it, it's completely opposite of what sane ref >> handling should look like. >> >> Not sure what the best fix is here, seems like dma-buf should hold an >> actual reference to the file upfront rather than just stash a pointer >> and then later _hope_ that it can just grab a reference. That seems >> pretty horrible, and the real source of the issue. > > AFAICT, epoll just doesn't hold any references at all. It depends, > I think, on eventpoll_release() (really eventpoll_release_file()) > synchronizing with epoll_wait() (but I don't see how this happens, and > the race seems to be against ep_item_poll() ...?) > > I'm really confused about how eventpoll manages the lifetime of polled > fds. epoll doesn't hold any references, and it's got some ugly callback to deal with that. It's not ideal, nor pretty, but that's how it currently works. See eventpoll_release() and how it's called. This means that epoll itself is supposedly safe from the file going away, even though it doesn't hold a reference to it. Except that in this case, the file is already gone by the time eventpoll_release() is called. Which presumably is some interaction with the somewhat suspicious file reference management that dma-buf is doing. But I didn't look into that much, outside of noting it looks a bit suspect. >>> Due to this issue I've proposed fixing get_file() to detect pathological states: >>> https://lore.kernel.org/lkml/20240502222252.work.690-kees@kernel.org/ >> >> I don't think this would catch this case, as the memory could just be >> garbage at this point. > > It catches it just fine! :) I tested it against the published PoC. Sure it _may_ catch the issue, but the memory may also just be garbage at that point. Not saying it's a useless addition, outside of the usual gripes of making the hot path slower, just that it won't catch all cases. Which I guess is fine and expected. > And for cases where further allocations have progressed far enough to > corrupt the freed struct file and render the check pointless, nothing > different has happened than what happens today. At least now we have a > window to catch the situation across the time frame before it is both > reallocated _and_ the contents at the f_count offset gets changed to > non-zero. Right. -- Jens Axboe