Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp58022lqh; Fri, 3 May 2024 13:16:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUr4OFYETwn0PGQrd95JEhik/oXSHIOiegJqyI78ftEI6AO4h3G0hF87jbfsme6/wnFzmh+XyIpFgYWRf9Ilf9rbRmOFh68a2ivtTbFYw== X-Google-Smtp-Source: AGHT+IGr06zaX2c9oypoVa74R3lONTXiB8qxp5UXyyyg0dkgRrdlqFtXzw9mz75ccnjrpPIqKEzI X-Received: by 2002:a17:906:ccce:b0:a55:4d24:6f9a with SMTP id ot14-20020a170906ccce00b00a554d246f9amr2586891ejb.6.1714767402133; Fri, 03 May 2024 13:16:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714767402; cv=pass; d=google.com; s=arc-20160816; b=PSg1i+E/EcuxZ71AGrEdWlqlzctV6EsPu3vvZcQM761H4R0y2ZkT+TenMEc6yjcXZn QI1S8JCQoRj36USkicrUqspvrbLNc1G5/Lhh053ZB4wVFyIfqy6dfSGu1Dpu483xQQMG 7Sfx/ZLecPlYv5HgCvq0WPbLuxgbS9enbJF1k1pMs8OlKJLJq3Z8uh979hkSOY/+NXVR 8OEtX7vPt/V15xeLrYLvMvhjRcegn9yznIhPbzzPIWUYcE+4TObcuTU1rCJy2MF0ec5+ 1Nd+c8Tlk76sSkXpKL/OcHBZcfeHP0jpEHdIFOVxWdr5XlNvqQRZXLijNnAwaHV56kIS m2oA== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=NjXuBsA7zZ0oIxrlLeiGnoyK8wpbBUktncdxO0iL2YE=; fh=q9s0jSYOEO8ILaX1ppynHPfttvClibN9KpOjq9MLLI0=; b=BmXDM0XaC4LzUJ+MFJRv3v/I5k/KxJ8TUrsv5c4M6keeF8qJsL3WTtT9dW7PNAFmSA XWbFgdLAyAoVlhFu4uR0a6y/uNoEleMdzps4nn5BhtIyfToNChs6ZK6LLAOy4OF2iQX/ 5BkzRy8yRewsEYNIBm4quErfGgTcCgomwqEiBuDo4lDK1jq+jSVYqwsdI/H8AVtzTK1C VVDTrE//3CJ/4BLEedGfXZ5U9tMN5ZxpYVM++hFFYO4XwS1qhfBiIhXeo49B28Q3F1t1 sYkex3phqTiKi9JsNLE4HozOHLiJTNeGQCzBAdPKlek6byfjyB2L+x+j+OV9z9bDgZFa op0w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NzJCjyfO; 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-168166-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168166-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 22-20020a170906329600b00a5954b70168si1955466ejw.800.2024.05.03.13.16.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 13:16:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-168166-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=NzJCjyfO; 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-168166-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168166-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 B16551F23FC9 for ; Fri, 3 May 2024 20:16:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 53188158D9F; Fri, 3 May 2024 20:16:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="NzJCjyfO" Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (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 11F8B155321 for ; Fri, 3 May 2024 20:16:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714767390; cv=none; b=F5sVMAXgsQtq7E56yV7S5P50z9Q2tM0S6MZUxIdCwxUPVkkIqvJWAVnC3+h8xbi6dvb/is7AxBP0tn56cses1rjb1fOzAoaUIzRdf2v78pCPo82Y1vwB4kQLhDMREycKWF7UV9H15BeLQ1UH49cwZQih8chsa1YVo1y7Z2UsjQs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714767390; c=relaxed/simple; bh=pf+pNFxmMm1HFvn7js6D1XqSBUysWOw89rU+1scNx50=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=ExIMYCTNSbT7R5q6iNTfEnULz/nxcSwmdItes66wzOAsFxIstb1b7pnLt93gvwu86PLpqGjkudYFaEhYt+B3eCLGj31mSI+u1yh/eiuHjlm0l35NaXJcX55XhUEHE5R4Mjqc8pfXnT495Mqqu4DtpMuo8zH8FkImzhOV67yZ3FA= 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=NzJCjyfO; arc=none smtp.client-ip=209.85.215.176 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-pg1-f176.google.com with SMTP id 41be03b00d2f7-5dca1efad59so35515a12.2 for ; Fri, 03 May 2024 13:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714767387; x=1715372187; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=NjXuBsA7zZ0oIxrlLeiGnoyK8wpbBUktncdxO0iL2YE=; b=NzJCjyfOflwbz9Z3Hp6sa2Ei3LrQrl0eEOMtuvg5lOnbLtM4zte7OjQ/W33RCI3QwK vg9hzdHLWRPC6zIG2NIv26mUMUgWlaD9a1F4qtgBT6fbIZzt+c/fHYfGo4kxAsZHXFAB 2K1hVBIZ4nRbifi6uRXe8ZIfC/rXalGCtyF1k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714767387; x=1715372187; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NjXuBsA7zZ0oIxrlLeiGnoyK8wpbBUktncdxO0iL2YE=; b=KcE53soUx48BG2gs0dDWKQd4VJ2jXYk9bj5TSndvXODt1SvaIfhX5iApCMpb5Gt+V2 UN1ruDmuBntnC9G9xPMZgvx4y7LzBOeADMxzpGpUL/F2fu/Tdp7K02PE5rxOqLGtSl53 Q4MpiB8MHvhZ6SesjyIKLXRyVxrKgEDlAx8kWDN8NQqtQaySNpv/JVRLZ+bhpo7ASPkF GUKdneslZSiyFBCgVgolDNF1/D4xQWfr2eST3JQrg1S2JSoGXcuCvv0/+ImymflgcnBW fUBXzW2u1okAnc8vhs2evIC/lJ+WADFWImsHDii96AbJj29iUK7SdUE6hwaPU1tE6n99 LsSw== X-Forwarded-Encrypted: i=1; AJvYcCWylMrAk46cjE9cmUsj3+pMI60sxpXn7FaKK7CTeMRR70n37hsfp9DYDwlJ+or2OCIGJdcyaCecYJpZcPvk+oWdCJFxvtdVwSxlKXYb X-Gm-Message-State: AOJu0YxkDINti3S4q9OiGeK6EzYa9qiRMArryRmyXJg8K3al3+MRQnbN 3pIOrOaF8xpGDyF1DzzBCmRVFJhLDgxPlQ3TsFAaViqY5BIMK0gPDb+btDE+mg== X-Received: by 2002:a17:90b:886:b0:2b1:3cc7:ae83 with SMTP id bj6-20020a17090b088600b002b13cc7ae83mr3668935pjb.32.1714767387520; Fri, 03 May 2024 13:16:27 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id gc19-20020a17090b311300b002b05e390c59sm3545286pjb.27.2024.05.03.13.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 13:16:27 -0700 (PDT) From: Kees Cook To: Christian Brauner Cc: Kees Cook , Peter Zijlstra , Alexander Viro , Jens Axboe , Jann Horn , Jan Kara , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] fs: WARN when f_count resurrection is attempted Date: Fri, 3 May 2024 13:16:25 -0700 Message-Id: <20240503201620.work.651-kees@kernel.org> X-Mailer: git-send-email 2.34.1 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=1386; i=keescook@chromium.org; h=from:subject:message-id; bh=pf+pNFxmMm1HFvn7js6D1XqSBUysWOw89rU+1scNx50=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmNUYZ53nnbAADcogtkELI+D9ycJdn+pb9aefol 66UgNHNLRaJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZjVGGQAKCRCJcvTf3G3A JszHD/9djHZkCfUTJG1NBb3v6t6W29SB91Q2iuAlm76cE5d6kGsEagP3JkHy5CVPK+fB+YXCFAj 3aDVoCAdyYlyA115UlUtIiPsA8u6bXNHzFHhPTqcPg6FwlpDiXHQ9Z0UifMo60EwUbulmSwNe5c ZgUTI5sWBFE64w4Ypz/qXYH+Q78dMwMwDP5PmkO8dQAxRVJc2PYxJ+g9CEKdKuq4Bm1SH21Ea+U Zs2uWdofnwqQvLVsl82EVnl/RPoYX1AJX4mtPDpMgL/n49+BUT88pTa2w0GIalXNsY0os9rfBBJ i6qNMChvDdenE3QpeMxmWLNlAdCA4dFYwnuqfVlb2Je8/4u6mXalIZiaUDVCpHqFBXuXjvnfW7I F9Cz9Ah80rLXmRNijfTMsB0u93V0diOciv/914qmiaukoJiAzOCNqoMncV/9dfLpsURf08oOy26 yneYGRMD9FgEGxGvUPPR6Vw79tTtwB3/d6oIv0b9RzffmukU5v6qUP2MiAjSjcNmECVVuXsEbQt MmNztwez3USCFAZS2SY3Msz2alKSzLAmMJDYKmd/L4EZWZEplt7xOKc6rDjOiCVUD0C2HtlfICe 46Kb98XHn1ZjswhLT2hykrtD2eVBkS7UGoSMKDxwM5fkmgXlhvFC3RY0njUjpOSM9JQq+b2Uc1X Wx/OMIJ EUHtkgQA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit It should never happen that get_file() is called on a file with f_count equal to zero. If this happens, a use-after-free condition has happened[1], and we need to attempt a best-effort reporting of the situation to help find the root cause more easily. Additionally, this serves as a data corruption indicator that system owners using warn_limit or panic_on_warn would like to have detected. Link: https://lore.kernel.org/lkml/7c41cf3c-2a71-4dbb-8f34-0337890906fc@gmail.com/ [1] Suggested-by: Peter Zijlstra Signed-off-by: Kees Cook --- Cc: Christian Brauner Cc: Alexander Viro Cc: Jens Axboe Cc: Jann Horn Cc: Jan Kara Cc: linux-fsdevel@vger.kernel.org --- include/linux/fs.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index 00fc429b0af0..fa9ea5390f33 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1038,7 +1038,8 @@ struct file_handle { static inline struct file *get_file(struct file *f) { - atomic_long_inc(&f->f_count); + long prior = atomic_long_fetch_inc_relaxed(&f->f_count); + WARN_ONCE(!prior, "struct file::f_count incremented from zero; use-after-free condition present!\n"); return f; } -- 2.34.1