From nobody Thu Apr 9 07:52:25 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F2DEC4321E for ; Thu, 3 Nov 2022 17:03:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232016AbiKCRC7 (ORCPT ); Thu, 3 Nov 2022 13:02:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231732AbiKCRCV (ORCPT ); Thu, 3 Nov 2022 13:02:21 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5671910B64 for ; Thu, 3 Nov 2022 10:02:19 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id m29-20020a05600c3b1d00b003c6bf423c71so3833330wms.0 for ; Thu, 03 Nov 2022 10:02:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=F2qRvwVUVIRjIeHt35WXksU2jyggEsNFCmKZPYDREH0=; b=Pmd1ZWzYe+9SBpCupib/Vm+0gUhLu8QSGX/WqY3HSwPPcz/YftVTkl2CwTIsYVb2Hx iRxgZBzpaLdzKE/udwF49ml/0ZOmV8GOKe5cRb3RCVRE301p8jITcfGm9WjywIF2g8Xe MS6trBTW1PmNZdD0n+5BgFXLdJi4vJOhUJewZepcExh858lg/tAnC5mZ/FzGESnoAx39 SJc5IQEC3TBkHm8T93+qRVKTEUQfZyr2TcS0Afxpukqt78dlah6VmkALLMAJ724Whi8k z9gRB5ylFALOFIvbVwditPZkNd+WnVAr9vAP+gQP40+vcngbYtGlIDsrQz2dI7qnkGTw A4FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=F2qRvwVUVIRjIeHt35WXksU2jyggEsNFCmKZPYDREH0=; b=5yX40MX8eMBBh5baYjVgOJNmm1+85H8AXcg76QdtCDqV7EoyDJSQZK2/OgmdPcnizG Cq0E4qDDQ78bd0T67+SpjbJYH0YQhyguY3+jGYG9aeqbYBhLEPRJte3SK7YjqodiXf3Y 5h6f7FEU0OFTCMKPySuXbgR56DTQgI0v4QfQTZWX/ZC+PwXui7E3kSh0DKYWlOG9K4O9 LuFlwItLDZu8/ETUi/Y7hToM9IPzYdNN4suy7wWp2PcWUiKK64MB4u8pj03rayDE9o1F ViUkBhD6cIllkNHmOZ7OQN7BkP/58123QGEjOmLU3+OfZJtDq9AXuLZKWp1RQC5F5ZHd 7oxg== X-Gm-Message-State: ACrzQf0+itQSReFhtxd0bDiuuOKaqWpA0vzZ4drOICOsa8oUJf5ZEcI2 HTbIKlEzNklUrmUUJ4aCshNDfw== X-Google-Smtp-Source: AMsMyM4+lDbFeMavFHov8ficnEgR3S1GzKaz2JGhFH/Khbt/MtSp6mpNbtemn0R9Nx3Ih2ByL4LE9g== X-Received: by 2002:a1c:440b:0:b0:3cf:4db1:d741 with SMTP id r11-20020a1c440b000000b003cf4db1d741mr20754947wma.197.1667494934967; Thu, 03 Nov 2022 10:02:14 -0700 (PDT) Received: from ryzen.lan (79-73-69-252.dynamic.dsl.as9105.com. [79.73.69.252]) by smtp.gmail.com with ESMTPSA id i15-20020adfa50f000000b002366c3eefccsm1286342wrb.109.2022.11.03.10.02.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 10:02:14 -0700 (PDT) From: Peter Griffin Cc: Peter Griffin , Alexander Viro , Miklos Szeredi , stable@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Will McVicker , Peter Griffin Subject: [PATCH] vfs: vfs_tmpfile: ensure O_EXCL flag is enforced Date: Thu, 3 Nov 2022 17:02:10 +0000 Message-Id: <20221103170210.464155-1-peter.griffin@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If O_EXCL is *not* specified, then linkat() can be used to link the temporary file into the filesystem. If O_EXCL is specified then linkat() should fail (-1). After commit 863f144f12ad ("vfs: open inside ->tmpfile()") the O_EXCL flag is no longer honored by the vfs layer for tmpfile, which means the file can be linked even if O_EXCL flag is specified, which is a change in behaviour for userspace! The open flags was previously passed as a parameter, so it was uneffected by the changes to file->f_flags caused by finish_open(). This patch fixes the issue by storing file->f_flags in a local variable so the O_EXCL test logic is restored. This regression was detected by Android CTS Bionic fcntl() tests running on android-mainline [1]. [1] https://android.googlesource.com/platform/bionic/+/ refs/heads/master/tests/fcntl_test.cpp#352 Fixes: 863f144f12ad ("vfs: open inside ->tmpfile()") To: lkml Cc: Alexander Viro Cc: Miklos Szeredi Cc: stable@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: Will McVicker Cc: Peter Griffin Signed-off-by: Peter Griffin Acked-by: Miklos Szeredi Tested-by: Will McVicker --- fs/namei.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/namei.c b/fs/namei.c index 578c2110df02..9155ecb547ce 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3591,6 +3591,7 @@ static int vfs_tmpfile(struct user_namespace *mnt_use= rns, struct inode *dir =3D d_inode(parentpath->dentry); struct inode *inode; int error; + int open_flag =3D file->f_flags; =20 /* we want directory to be writable */ error =3D inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); @@ -3613,7 +3614,7 @@ static int vfs_tmpfile(struct user_namespace *mnt_use= rns, if (error) return error; inode =3D file_inode(file); - if (!(file->f_flags & O_EXCL)) { + if (!(open_flag & O_EXCL)) { spin_lock(&inode->i_lock); inode->i_state |=3D I_LINKABLE; spin_unlock(&inode->i_lock); --=20 2.34.1