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 E1864C77B61 for ; Mon, 20 Mar 2023 20:25:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231205AbjCTUZp (ORCPT ); Mon, 20 Mar 2023 16:25:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231239AbjCTUZO (ORCPT ); Mon, 20 Mar 2023 16:25:14 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 813082BF3B for ; Mon, 20 Mar 2023 13:25:12 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id i5so4791142eda.0 for ; Mon, 20 Mar 2023 13:25:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; t=1679343910; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Crb5gaca3LAL3QSUXcVibd0Ph4xO6N/3m0QFmJdNuPA=; b=BLRXv6G1ID56F82XpWEhsLLXGi6naVJ5iSbdkQA7SE/Atqr0psA60I1mjhCS3ExRmt kO7EB0NChmWHwqiRx7d86y1GMB4mdZiKi4Ci8uHv8ZAi7MT/jE9/gM7ABk3rBXSbr1Rd RSrIKRwSr296G/Su9ZKl0smQU9mGe8tS6BCT8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679343910; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Crb5gaca3LAL3QSUXcVibd0Ph4xO6N/3m0QFmJdNuPA=; b=u2E9K8SYPfclpRs+KNTA9tuBVCCPdTQZQaKSORDd4lTp7GYHHCQG9UluVhXxS8PXZT VzdgxnDcp1RJ6z1cyER92cJfy4ZL9Kw383uaR3BwaUlcuxGuFzAQHBxT6D3M7LXwhpRQ RcaW7HSsq9NbkaYgel91SilY/Y+Bc20eqThXnFSoIJqRYFLXC3kJyx7jq/ioI0n2PjNQ VjDUypYHnALxIXpCjVk64wguOWZ2/YqBjX7b2t/R2ma+F/g9Ht6h54YqKl65jjEf9nfZ RH1KQ7aH1MjlJqMtObQgYxyv8qN6AFAIEZqCF9wjrmmO0TYvEufHxJF0V8UlEC3g7B85 4MTw== X-Gm-Message-State: AO0yUKWAntIzk1/yIhfsSYmSTFjSdT5PqHaA3P8hd3M/KSvT0oaMsN/a t/Cn5UO88qBthH7DjtE/9PKAeD7b7Eh0Hd8+52dMJ8tS X-Google-Smtp-Source: AK7set/fAftRZY1rONnK0McfDovGkSsXzhpIRPTmgTmFC3xi//VX0nvkgYcKgxgVbSv0r4iSEiSHgg== X-Received: by 2002:a17:906:538e:b0:932:177a:12a5 with SMTP id g14-20020a170906538e00b00932177a12a5mr349824ejo.66.1679343910644; Mon, 20 Mar 2023 13:25:10 -0700 (PDT) Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com. [209.85.208.50]) by smtp.gmail.com with ESMTPSA id v12-20020a17090610cc00b008f767c69421sm4812026ejv.44.2023.03.20.13.25.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Mar 2023 13:25:10 -0700 (PDT) Received: by mail-ed1-f50.google.com with SMTP id cy23so51591930edb.12 for ; Mon, 20 Mar 2023 13:25:09 -0700 (PDT) X-Received: by 2002:a17:906:2c04:b0:931:6e39:3d0b with SMTP id e4-20020a1709062c0400b009316e393d0bmr142412ejh.15.1679343909563; Mon, 20 Mar 2023 13:25:09 -0700 (PDT) MIME-Version: 1.0 References: <20230320071442.172228-1-pedro.falcato@gmail.com> <20230320115153.7n5cq4wl2hmcbndf@wittgenstein> In-Reply-To: From: Linus Torvalds Date: Mon, 20 Mar 2023 13:24:52 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] do_open(): Fix O_DIRECTORY | O_CREAT behavior To: Pedro Falcato Cc: Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 20, 2023 at 12:27=E2=80=AFPM Pedro Falcato wrote: > > 1) Pre v5.7 Linux did the open-dir-if-exists-else-create-regular-file > we all know and """love""". So I think we should fall back to this as a last resort, as a "well, it's our historical behavior". > 2) Post 5.7, we started returning this buggy -ENOTDIR error, even when > successfully creating a file. Yeah, I think this is the worst of the bunch and has no excuse (unless some crazy program has started depending on it, which sounds really *really* unlikely). > 3) NetBSD just straight up returns EINVAL on open(O_DIRECTORY | O_CREAT) > 4) FreeBSD's open(O_CREAT | O_DIRECTORY) succeeds if the file exists > and is a directory. Fails with -ENOENT if it falls onto the "O_CREAT" > path (i.e it doesn't try to create the file at all, just ENOENT's; > this changed relatively recently, in 2015) Either of these sound sensible to me. I suspect (3) is the clearest case. And (4) might be warranted just because it's closer to what we used to do, and it's *possible* that somebody happens to use O_DIRECTORY | O_CREAT on directories that exist, and never noticed how broken that was. And (4) has another special case: O_EXCL. Because I'm really hoping that O_DIRECTORY | O_EXCL will always fail. Is the proper patch something along the lines of this? --- a/fs/open.c +++ b/fs/open.c @@ -1186,6 +1186,8 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op) /* Deal with the mode. */ if (WILL_CREATE(flags)) { + if (flags & O_DIRECTORY) + return -EINVAL; if (how->mode & ~S_IALLUGO) return -EINVAL; op->mode =3D how->mode | S_IFREG; I dunno. Not tested, not thought about very much. What about O_PATH? I guess it's fine to create a file and only get a path fd to the result? Linus