Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1457609lqm; Thu, 2 May 2024 16:03:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWLVYXLUWqkK/2wkMgy7Eek3HjvN8oIL3ZEzt7gg80cTSEJbZ/Tn0X3hASNnWg0IR5yLWbPGWy+vtOemj1vxxh6GyGgNe6KnGW0TBxLdg== X-Google-Smtp-Source: AGHT+IEkKknzvThZa0I6Gm3cmcndeJGJEl6R66NZWMs0Jg3vT9qkpgjqITbV0u05tkcvgqxCKD3f X-Received: by 2002:a2e:9c49:0:b0:2e1:61de:9d03 with SMTP id t9-20020a2e9c49000000b002e161de9d03mr855816ljj.18.1714691019219; Thu, 02 May 2024 16:03:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714691019; cv=pass; d=google.com; s=arc-20160816; b=LIUaBrlsB4vl5Kj6F29dWZuup3Ph0U6EpnhA8J5PEpMSsTbsjqjwrj+ioPuo446xCk ws7Tkjkovhw2Z4PVHxgU6HaYifRA0IOjTBUEMyG+i64OyTOCevSoLX7rw8KUkUSEemYY D48ILAPg41RAHXiNf4zyv5QgKxZZLYroW+WnJOsnmtFHu+FSmxrJ0eLhprk1K0fsMzsJ S9dudnxsPbLZ8f4lqz5ykKq8Bvi0AV+dxPVublc3UO8TkiRxjs5DgBhrLKPGVjuKI59m AhGbM0NhQoPqbvDgiSGE7QCuxfdTkEqyckyvMw9whTjDxdRFshTiYfFGyJKXHEB+zQ+i Nk3g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=I4RW3z/Zjpo6qwpklLmujf0x6xZ5b4UXLeUz+5BU7aE=; fh=HEE9K2oMRkyzUfh6DRLwjmm3RKSJxCGeH6NquSoRd78=; b=nz52iWuyYi8SNw3dgdLcuYpJCsbHyysE00pE9GOnVmagqqurooPwqPoVMWqGO72aNP UKiFqYunLZDHNCpwA9edxlErhDIuZWaIvmVnXUwdZSzfZV3LgQXo8DagsxtPmdVWJt5K JF65+8tqgl3X2wAWOhZZ5zSH6sVr9WUOll9tXvH0x8/xisheUPy2vR0/Dgzo0geXoiuJ SKPAaFEmfqDSBZGxv0hA/FyoTyUBd18WJjRX2llr4gyM/nWmIs83yTtufd3Pm1pNYfj1 UNhQK9mc+bS8Qv723J/QlIG5WCVWDZDZoTN8mqZjnIgYb8+icl3372IZPNPWoYdFD7OP 8waQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bCYv6QEZ; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-167032-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167032-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n7-20020a05640206c700b00572b3091baesi902674edy.396.2024.05.02.16.03.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 16:03:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-167032-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bCYv6QEZ; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-167032-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167032-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 am.mirrors.kernel.org (Postfix) with ESMTPS id C3ED11F21130 for ; Thu, 2 May 2024 23:03:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2E7601CD26; Thu, 2 May 2024 23:03:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="bCYv6QEZ" Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 037FA1C683 for ; Thu, 2 May 2024 23:03:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714691007; cv=none; b=VHmIwe/mKYKVq+39i2UVo9QTIM0fs6K3fXqm9STQWOdacBqQHf88nnX1AkAyiPJlno55cEfyKfwbzxiHqX/UmsDGXq++kQEoIqoea0uSzG8ovkdBNDQZ+8BpVQqXgBu7rUdgHhUVEdzjI7dnAJvrxrcLviaJ1SETBrqmJvEuWSE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714691007; c=relaxed/simple; bh=7UC0ZZLULS68YLtbitTAvikIsTdjDiGgT/XDM6ZThpQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=CRVeWx8Mhx79ZBoGABvihXpfjMJaqYskUNojZLklCQ22quVHxxAuX+mVlsdQiNL2C4T3cKddPMtsJDF4JJj0ndO64aq2cXLmCHk78j8XeFfRedlo2zO/1FeAUgDfZdNajAKwR8NVzcTRwjentuPF927hbaNWDmR6aXdHb2TyeoE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=bCYv6QEZ; arc=none smtp.client-ip=209.85.216.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2b27c660174so2747038a91.1 for ; Thu, 02 May 2024 16:03:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714691005; x=1715295805; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=I4RW3z/Zjpo6qwpklLmujf0x6xZ5b4UXLeUz+5BU7aE=; b=bCYv6QEZR4K8pT3YYHNyKEL8tICIe8BNi04eAHT8+BoAEmrnQjxMDoSpEu16DQqGsV tekRkOz6/0+gJYt4A+jDLaoRzVFz5DAGb1MC2sQ3yPao8ICpa5OIYv9XD8/2KCRnnEJ4 5RWDN5W2C5CUru3MKpM8OMNOAmyR4vwQCTwzY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714691005; x=1715295805; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=I4RW3z/Zjpo6qwpklLmujf0x6xZ5b4UXLeUz+5BU7aE=; b=nUxT1uM2rFzdDVbfDMs74kotnNvchZxp8uxHRvkK+xIzd9EM9l1rKwumfN5OPB8QCG Pb41YdCTQTugWFZl5bA6ZiROEISMOgyO1SJAVPeJbFvZZ+HKgVMCfOd5585bfWpDVaNP 5CSLv2OMxKD1jJ0BfYzw3ot2ypOHqn/3iUjPsPDSYUyhY1MdYjQ//JjDeiEnmWYa0rWp OMQbIIh08yuWVtrsDODxpQeN+tOOgYslSm7IxPZn7RWAbcWGCvkiXUaHJzYSD2KgfBQa XJUBSgHIfXecgkIQfMSez6jNgOtnVNlfTNBDG3Ew4dQYMaTv5hWSKczhIfm4hJDyfoib 6AKw== X-Forwarded-Encrypted: i=1; AJvYcCUYIMnkNlJHCXfeagStd6pNDpO2awwxA6FPv8K1ofwy1+UDTTAXyRyqUmSH5Gai4lplu3/rNTbijR3rPR8WdzkwE3cSKVtrrAVpEznw X-Gm-Message-State: AOJu0YwXFnUFyx8RIAciDCeQUewF9rvWHlDQy0YkN49CE3zWyjaUlkkf Cbzuz+SFuS917l2zesABG0cwVnIWlqd7a7lqndrEsGY+TK1Q6smLkTOTnKn9Tw== X-Received: by 2002:a17:90a:c70a:b0:2b2:7e94:c5fa with SMTP id o10-20020a17090ac70a00b002b27e94c5famr1292136pjt.5.1714691005284; Thu, 02 May 2024 16:03:25 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id gn23-20020a17090ac79700b002b2b608c10esm1807658pjb.56.2024.05.02.16.03.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 16:03:24 -0700 (PDT) Date: Thu, 2 May 2024 16:03:24 -0700 From: Kees Cook To: Jann Horn Cc: Christian Brauner , Alexander Viro , Jan Kara , linux-fsdevel@vger.kernel.org, Zack Rusin , Broadcom internal kernel review list , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , Andi Shyti , Lucas De Marchi , Matt Atwood , Matthew Auld , Nirmoy Das , Jonathan Cavitt , Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Kent Overstreet , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Andrew Morton , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kbuild@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH 1/5] fs: Do not allow get_file() to resurrect 0 f_count Message-ID: <202405021600.F5C68084D@keescook> References: <20240502222252.work.690-kees@kernel.org> <20240502223341.1835070-1-keescook@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Fri, May 03, 2024 at 12:53:56AM +0200, Jann Horn wrote: > On Fri, May 3, 2024 at 12:34 AM Kees Cook wrote: > > If f_count reaches 0, calling get_file() should be a failure. Adjust to > > use atomic_long_inc_not_zero() and return NULL on failure. In the future > > get_file() can be annotated with __must_check, though that is not > > currently possible. > [...] > > static inline struct file *get_file(struct file *f) > > { > > - atomic_long_inc(&f->f_count); > > + if (unlikely(!atomic_long_inc_not_zero(&f->f_count))) > > + return NULL; > > return f; > > } > > Oh, I really don't like this... > > In most code, if you call get_file() on a file and see refcount zero, > that basically means you're in a UAF write situation, or that you > could be in such a situation if you had raced differently. It's > basically just like refcount_inc() in that regard. Shouldn't the system attempt to not make things worse if it encounters an inc-from-0 condition? Yes, we've already lost the race for a UaF condition, but maybe don't continue on. > And get_file() has semantics just like refcount_inc(): The caller > guarantees that it is already holding a reference to the file; and if Yes, but if that guarantee is violated, we should do something about it. > the caller is wrong about that, their subsequent attempt to clean up > the reference that they think they were already holding will likely > lead to UAF too. If get_file() sees a zero refcount, there is no safe > way to continue. And all existing callers of get_file() expect the > return value to be the same as the non-NULL pointer they passed in, so > they'll either ignore the result of this check and barrel on, or oops > with a NULL deref. > > For callers that want to actually try incrementing file refcounts that > could be zero, which is only possible under specific circumstances, we > have helpers like get_file_rcu() and get_file_active(). So what's going on in here: https://lore.kernel.org/linux-hardening/20240502223341.1835070-2-keescook@chromium.org/ > Can't you throw a CHECK_DATA_CORRUPTION() or something like that in > there instead? I'm open to suggestions, but given what's happening with struct dma_buf above, it seems like this is a state worth checking for? -- Kees Cook