Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp852693pxb; Wed, 16 Feb 2022 05:56:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJwLVGAeQtnE+PeokFIaTXRTO2Ybam+AsPoDtOiRBVYqLgnHkEBgWOOIORYOiBB9Gc+BrNtQ X-Received: by 2002:a17:906:9bed:b0:6ce:6bbc:dc8b with SMTP id de45-20020a1709069bed00b006ce6bbcdc8bmr2489557ejc.428.1645019814466; Wed, 16 Feb 2022 05:56:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645019814; cv=none; d=google.com; s=arc-20160816; b=c3E2ROOibVwntlUQ5ThDWh5CWIa8nYRn2kaazLiwii/IgEfksJdGylYrYbdL4RU8MN tuJwCTI1if8r80Aax7DesF74rB7rjQNgfqQ14tizU3TBafnXAwHrMz3ypa77r099qgC9 Z93asxX/0DHrqkdU+mr7rMSepAjUNqXZUPyxZk0rZ9Rm1yFMnzNtTEfmBIpfKmqsSBIu YdmZ28M9+xiPVHEFjJyfHvF4uYYinAGyFP+6CkbLW6yO5niuFUGVUHZ7orh126xbJX2L d0W7UNF10v82S0O4rDsGVP6PiXvoMjysyqim2dYMxgWfww6VvwnjcF8HUzH2bi42ah23 LAqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=PlNi9ciElPwEs1LJoEB8xZkE+EnHB0mIZHsYC5NhAsY=; b=1KEQkKaAo3QrvJ8xa5D9iWx6sZVqt7zcP79c0JbRfRowmf4cUpbxIEZi44kXlgPNs/ 2BcwZITh57eHDYvPcsn/Bs7J3fEPS0JKzIYtek6Eei+9fCzOIuG6/3UaANSch6fGzrWD 0MFKUNrpLeBJr5P/xB/fhzGgHQYncC7J9qOq1MjvpHJU62WCXVY9xrpxqVR5DU5lhnMb dsczm4zQbi62LwqxV9lAk4eprMQ3YSnFDmh8ZIQHry7m+brYk5PXFBQuN8A9OmwKtHWP XzKEVedje7ze36RfrXh1N2Tis3JmF2Y7i4lPc5jt31Gi+zFVDoHAxc7tNZ9tvOGB/vVD 7gag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@algolia.com header.s=google header.b=w0jBpzaR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=algolia.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hb13si4907827ejc.932.2022.02.16.05.56.27; Wed, 16 Feb 2022 05:56:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@algolia.com header.s=google header.b=w0jBpzaR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=algolia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234243AbiBPNTb (ORCPT + 99 others); Wed, 16 Feb 2022 08:19:31 -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-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: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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; }