Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1447644lqm; Thu, 2 May 2024 15:35:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXPX11FG1d7w8R71+bGepCsruKN6CvlNOcTpVXVrFBn9PFsxM7/wLUQouLjJB0ZtBAnCzodLQHgVmaA8qEsJZm4ogSiOBzPNQ/sr/0QTA== X-Google-Smtp-Source: AGHT+IFk9WBmjxg6DVHSUG4uqmxZCm7DIYKp6UCbr+PIDT3F5OskS8iR6ZsRvvcZzkb0S1sKiXxz X-Received: by 2002:a17:90a:fb83:b0:2b0:9544:59e7 with SMTP id cp3-20020a17090afb8300b002b0954459e7mr5844797pjb.23.1714689314094; Thu, 02 May 2024 15:35:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714689314; cv=pass; d=google.com; s=arc-20160816; b=uh40kGZzrAC8G9pU0lThpBaDRGvb2zr4JIvw0cQgpXNxHvQOXV29gOFaTWahqbmXRd NF3rtKJKhW+gs5M3OPzlfOAC+6kglZWQHYo4klDy7l6PPohKdOipqRkze1wrpXMqDu2y PPKNS55EQbbC8+nPNNOcDQNB9WyMhRg+scr/bkLby48dJVTT5ULf7KZJjQMr/ZjfhHwS /6IPxQ+ENu07cr6oPGXKBfb0R+egpBodQLW8wJbdZTmEX8c0BkUdDWvcheE0zP9SL02U r6CHp/sJnmBGzgeb9YdYhhud9PXU959gdECkyIOIKYYHHm25jP9edrgERiNP9E/0OdIu Z63g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Nhj8hJv8Nz4SLO0kIrZY8268X0YP9luJWZdcMXgW554=; fh=ApereM05PKMRyZ13Jk6MlvSB5sZNHZ8+YChwiIuq4p8=; b=BclOY62PJ2+HcZouUYMW35GCnKADvejNXR3EsD1cNtTa9Yo2nJ28tz1ATInYRs1Wpc G2GWkX7jnPdp2Xf33aw3mLXtX8fXGVJ0NjBCsLsf64A3o2vHph+n5FO9aY18fQFqGZzc bHRvsUhEf1Q1/HHxeylxtNUBzUJ+G1ITyXeU5rN3lfTTGLAfd45DAmlPRJXYFSkb0c2e OAXaoqWoV0BRjkoyU/WKedq/gi+vVkyjtVlOpX3h5w99m1p7K+eFTzdILJ5QXeCl2ofU 96LOT5zaKs1iXrZ1l3/Qx0ZwE2j2J5hB4BBUnyM0x1WLlxZ46tyyMtv9PmSjv7xfaCvl Naag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MHfM2F0L; 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-167007-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167007-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e1-20020a63f541000000b005d8bef48a86si1093870pgk.876.2024.05.02.15.35.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 15:35:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-167007-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MHfM2F0L; 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-167007-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167007-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B4B902814E9 for ; Thu, 2 May 2024 22:35:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D85115E5B9; Thu, 2 May 2024 22:33:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="MHfM2F0L" Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 5D649433CF for ; Thu, 2 May 2024 22:33:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714689231; cv=none; b=WcGsu9fmOSnNbFhuVbqWMHkapzAhqEMTSIJMBg45dKtef4WoejY2AIwlGxaegAWvlpYGvNzjJ/o7gKxive1GY0KhEE26EOjxlbY6Sx9tBgHrtXAWOKV+XRuUxPlnRZ8p6olgQsghLNgx8IM8k13vvuOMbu8stJ8XmmmO3f3Yjpc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714689231; c=relaxed/simple; bh=aku0Ov6TP/MVYBgFlGuMSUdhnxeal+hPQHrvCM+9JCg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SfAw5E71VeFLSfa2Bp8va495MrC0RndU/yOuHGzFwbtms7PM7gTMDpGUiuh+kYdxy2w6wbhXaZ0LMW8T6X3cg9R/xAVjHVBoiWa4RQZWtAqHSAhmwLEpo5ycbViQ3e/zbjBBq4SudV1l6NL/QmYnLOLy7770LusnTtYRoEs31S4= 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=MHfM2F0L; arc=none smtp.client-ip=209.85.216.53 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-f53.google.com with SMTP id 98e67ed59e1d1-2b3c1ea9a68so838684a91.1 for ; Thu, 02 May 2024 15:33:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714689229; x=1715294029; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Nhj8hJv8Nz4SLO0kIrZY8268X0YP9luJWZdcMXgW554=; b=MHfM2F0LjCk+8EUjh9rcul74jhMusbATkG+JI+0vONTQiZ9qLpGT0TMRAX7ifHH5V9 F590du9Unm3NlLg7xzMFZLEuDWxbu1FkPvyhG+P0CKXhTTs6ApN15IDTx8D6oZJQptJC XQw8U79NpNamGRkGsbTnsAkDiH+c+c9unS1ks= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714689229; x=1715294029; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Nhj8hJv8Nz4SLO0kIrZY8268X0YP9luJWZdcMXgW554=; b=mCprRvunoQMqYW7wjLHhxAqjLCOjF8nhzSJ3OkRToWBfbfQajS/lSXvK9hdIvE7H3j rsfCwqnRGo06WUt8WAJlctFJJ87O3USstCmOcgcz2aYILCPzWipmyKqrqLRbe5D1FkyP 8x9gwA6lva7JC4A0QjkclwRpJJZUwmVsDNVpZMBjAiSFDF8Rp4PUBhYGjGcTqH48eaYP 7sbpPQA28smBq2auO/GgR6nruj3wFOiyCBrTcM3QJbClaMOAiZh4C/6sL8Bhz6O6Luuu q548P+Y+XgdfVFORSvbpIyAx4TubKV9FuoBbI+LpDoo+s8do2AoV253hxSZu3PUAr5my NRgA== X-Forwarded-Encrypted: i=1; AJvYcCWdGDzO4NqzSodABevf+6RFKWxwFtEm+oA5ksAGrYEHy21eMPf9xOmGT5eCnbVXrU/Dv2knHad0kC1dV8kM1uje6qOmZhimY5wn487d X-Gm-Message-State: AOJu0YxP9t7qaTTEa95PdWu/uNZpKBEv0oz890hFzhRZtb97lrwMhMg3 6sxY5uIR043xY5sl0/xylvZcF4a4lJF0X3AEyUkizUpOx2HgFAF3BjgyK9aLhw== X-Received: by 2002:a17:90a:ac18:b0:2b2:9783:d0ca with SMTP id o24-20020a17090aac1800b002b29783d0camr1477782pjq.12.1714689228740; Thu, 02 May 2024 15:33:48 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id fw14-20020a17090b128e00b002a2d4bf345bsm3698747pjb.55.2024.05.02.15.33.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 15:33:47 -0700 (PDT) From: Kees Cook To: Christian Brauner Cc: Kees Cook , 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: [PATCH 5/5] fs: Convert struct file::f_count to refcount_long_t Date: Thu, 2 May 2024 15:33:40 -0700 Message-Id: <20240502223341.1835070-5-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240502222252.work.690-kees@kernel.org> References: <20240502222252.work.690-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3356; i=keescook@chromium.org; h=from:subject; bh=aku0Ov6TP/MVYBgFlGuMSUdhnxeal+hPQHrvCM+9JCg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmNBTDsTosM5PigT8MgtAILfCBKJBIs1YpjR+n9 i4hU3z7b2GJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZjQUwwAKCRCJcvTf3G3A JpwAD/47yb9BpbWzPuD0heUR6EIq/XA2DmBYJDN5EAEcjQh8e95gPvrbNsBQnT+lojr4owqsbV7 yzKXeVQsU4C13GRWVgHZOs4DuQkYYtC0AJVHQ9LrjXn9Rgkfb928ujjbYMNhZpvfGxJ///oDZEx SLgxaj8AKWkuPDgKyktn25SCmT7MnZbHQ5h+s3Eu+U0PO1IeAY8qwit7+cWw0KtH0SmVPGyzLSg OD7OAFb7j5XoajUhOQcltZOHvQI8FEQQsMvOJxUtioIyuro1Ah2yAhiJ+Gb4IiuzreOOS5mpQY9 rAeyVw1zgu2gR6iJ7gpPfq40oFIp3yulcggvyRh6GeikDzJhwpr9MIdIEnYpgeyTm/PgomJjyNR KVeoluWeWITtKvPSs1kXp33TxqrsjXk2F06HFgRPXqyLP4d/qcZhl7q6Vs8GmNlEbtLMqxJscV2 o/JWSiqQ4VeQfHwCAZNY9enOk5CCC6Z+YwOof8OUIKsivBxLc7dLGjX+X01ttLx5GWsmPr7/RHY jAKBvlDq1GORFiHN/58xNQNqu1fGiicNh5Wyuk7D22DfoO0k+gWAoz/7JKx6Q+N1zM4URIMfePa ZwcN0ap5AIn9iwCooiNslmX4gAjKTcV7LO1fvZy8mgzRK2khRr0rpHK6qi52Vq3AMNylI0Nw9qZ qJGKfgmX6LueF0A== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit Underflow of f_count needs to be more carefully detected than it currently is. The results of get_file() should be checked, but the first step is detection. Redefine f_count from atomic_long_t to refcount_long_t. Signed-off-by: Kees Cook --- Cc: Christian Brauner Cc: Alexander Viro Cc: Jan Kara Cc: linux-fsdevel@vger.kernel.org --- fs/file.c | 4 ++-- fs/file_table.c | 6 +++--- include/linux/fs.h | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/fs/file.c b/fs/file.c index 3b683b9101d8..570424dd634b 100644 --- a/fs/file.c +++ b/fs/file.c @@ -865,7 +865,7 @@ static struct file *__get_file_rcu(struct file __rcu **f) if (!file) return NULL; - if (unlikely(!atomic_long_inc_not_zero(&file->f_count))) + if (unlikely(!refcount_long_inc_not_zero(&file->f_count))) return ERR_PTR(-EAGAIN); file_reloaded = rcu_dereference_raw(*f); @@ -987,7 +987,7 @@ static inline struct file *__fget_files_rcu(struct files_struct *files, * barrier. We only really need an 'acquire' one to * protect the loads below, but we don't have that. */ - if (unlikely(!atomic_long_inc_not_zero(&file->f_count))) + if (unlikely(!refcount_long_inc_not_zero(&file->f_count))) continue; /* diff --git a/fs/file_table.c b/fs/file_table.c index 4f03beed4737..f29e7b94bca1 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -167,7 +167,7 @@ static int init_file(struct file *f, int flags, const struct cred *cred) * fget-rcu pattern users need to be able to handle spurious * refcount bumps we should reinitialize the reused file first. */ - atomic_long_set(&f->f_count, 1); + refcount_long_set(&f->f_count, 1); return 0; } @@ -470,7 +470,7 @@ static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput); void fput(struct file *file) { - if (atomic_long_dec_and_test(&file->f_count)) { + if (refcount_long_dec_and_test(&file->f_count)) { struct task_struct *task = current; if (unlikely(!(file->f_mode & (FMODE_BACKING | FMODE_OPENED)))) { @@ -503,7 +503,7 @@ void fput(struct file *file) */ void __fput_sync(struct file *file) { - if (atomic_long_dec_and_test(&file->f_count)) + if (refcount_long_dec_and_test(&file->f_count)) __fput(file); } diff --git a/include/linux/fs.h b/include/linux/fs.h index 210bbbfe9b83..b8f6cce7c39d 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1001,7 +1001,7 @@ struct file { */ spinlock_t f_lock; fmode_t f_mode; - atomic_long_t f_count; + refcount_long_t f_count; struct mutex f_pos_lock; loff_t f_pos; unsigned int f_flags; @@ -1038,7 +1038,7 @@ struct file_handle { static inline struct file *get_file(struct file *f) { - if (unlikely(!atomic_long_inc_not_zero(&f->f_count))) + if (unlikely(!refcount_long_inc_not_zero(&f->f_count))) return NULL; return f; } @@ -1046,7 +1046,7 @@ static inline struct file *get_file(struct file *f) struct file *get_file_rcu(struct file __rcu **f); struct file *get_file_active(struct file **f); -#define file_count(x) atomic_long_read(&(x)->f_count) +#define file_count(x) refcount_long_read(&(x)->f_count) #define MAX_NON_LFS ((1UL<<31) - 1) -- 2.34.1