From mboxrd@z Thu Jan 1 00:00:00 1970 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 0AF77C43219 for ; Wed, 16 Feb 2022 13:19:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232533AbiBPNTd (ORCPT ); Wed, 16 Feb 2022 08:19:33 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:34642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234133AbiBPNSt (ORCPT ); Wed, 16 Feb 2022 08:18:49 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 646EC2AC925 for ; Wed, 16 Feb 2022 05:18:17 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id l12-20020a7bc34c000000b003467c58cbdfso3730667wmj.2 for ; Wed, 16 Feb 2022 05:18:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=algolia.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=PlNi9ciElPwEs1LJoEB8xZkE+EnHB0mIZHsYC5NhAsY=; b=w0jBpzaR5vaPbYOcj2spBHJfOMj3nwWNKsycXYvill3tWXFQ4LXr+vaDsKxJLBa9Ev MdZYWVsdfvgjdAXBoJdyOPeH0kafRB9UvrpwMIucZ4/rjHH/BVKe4kuVFw8Pf8OFEJLo P5EU0NQTIwXw7KDM4xH/9O9Uxes+q5KPbRKtg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=PlNi9ciElPwEs1LJoEB8xZkE+EnHB0mIZHsYC5NhAsY=; b=hnOFMSxwBB2QbL/58EhMfngJWe8t2P5uF24vgFN7IrimxN9+ZCBF7YVIGrQ0GNGkr5 VM8NbM4cits5Sa+Zf+KFCBTnRRnF/AYPTHBQupkfzXkRR2SYnT4iTO9gicgfZ6qPzfZ1 k6Uwz5GNnjTpW3CVufkISdGqmwinhIbfuqV3tevEdsz4HN8VFLFZaSh8ZgIrNk4oRaTL AQ5/NpjLTgLUCzzFSnvD+pRmqNEEQdfyQGsHFf1peELmkECt3jmeYw/BEjglY2wHmfjI 6ITJnxeIuB+AByEEB1tBBnlM0FEYfN1ZBEmqIuRlVL/ohrSNRqizjWmH0cGIY8Lh239q W2hQ== X-Gm-Message-State: AOAM530AjU9d+xNPwhCdnyq4eIXh0rRnIB/EXAhHH21ELjt2soeTI6X2 fdgXsJTlj1zoZkuLrfwwprabgSuqSBJARecw X-Google-Smtp-Source: ABdhPJy0vRrYV6duj8pGGxIELNPUfCUwW0eEI7uQbDouSdpL6nweY9x4I6bCFDU/V85DPITDc0BgTw== X-Received: by 2002:a05:600c:20c7:b0:37b:b739:8177 with SMTP id y7-20020a05600c20c700b0037bb7398177mr1605527wmm.121.1645017495859; Wed, 16 Feb 2022 05:18:15 -0800 (PST) Received: from xavier-xps ([2a01:e0a:830:d971:752e:e19b:a691:2171]) by smtp.gmail.com with ESMTPSA id x7sm28339793wro.21.2022.02.16.05.18.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Feb 2022 05:18:15 -0800 (PST) Date: Wed, 16 Feb 2022 14:18:14 +0100 From: Xavier Roche To: Miklos Szeredi Cc: Al Viro , Matthew Wilcox , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, "Aneesh Kumar K.V" Subject: Re: race between vfs_rename and do_linkat (mv and link) Message-ID: <20220216131814.GA2463301@xavier-xps> References: <20220214210708.GA2167841@xavier-xps> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 16, 2022 at 11:28:18AM +0100, Miklos Szeredi wrote: > Something like this: > diff --git a/fs/namei.c b/fs/namei.c > index 3f1829b3ab5b..dd6908cee49d 100644 Tested-by: Xavier Roche I confirm this completely fixes at least the specific race. Tested on a unpatched and then patched 5.16.5, with the trivial bash test, and then with a C++ torture test. Before: ------- $ time ./linkbug Failed after 4 with No such file or directory real 0m0,004s user 0m0,000s sys 0m0,004s After: ------ (no error after ten minutes of running the program) Torture test program: --------------------- /* Linux rename vs. linkat race condition. * Rationale: both (1) moving a file to a target and (2) linking the target to a file in parallel leads to a race * on Linux kernel. * Sample file courtesy of Xavier Grand at Algolia * g++ -pthread linkbug.c -o linkbug */ #include #include #include #include #include #include #include #include static const char* producedDir = "/tmp"; static const char* producedFile = "/tmp/file.txt"; static const char* producedTmpFile = "/tmp/file.txt.tmp"; static const char* producedThreadDir = "/tmp/tmp"; static const char* producedThreadFile = "/tmp/file.txt.tmp.2"; bool createFile(const char* path) { const int fdOut = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); assert(fdOut != -1); assert(write(fdOut, "Foo", 4) == 4); assert(close(fdOut) == 0); return true; } void func() { int nbSuccess = 0; // Loop producedThread a hardlink of the file while (true) { if (link(producedFile, producedThreadFile) != 0) { std::cout << "Failed after " << nbSuccess << " with " << strerror(errno) << std::endl; exit(EXIT_FAILURE); } else { nbSuccess++; } assert(unlink(producedThreadFile) == 0); } } int main() { // Setup env unlink(producedTmpFile); unlink(producedFile); unlink(producedThreadFile); createFile(producedFile); mkdir(producedThreadDir, 0777); // Async thread doing a hardlink and moving it std::thread t(func); // Loop creating a .tmp and moving it while (true) { assert(createFile(producedTmpFile)); assert(rename(producedTmpFile, producedFile) == 0); } return 0; }