Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp596052rwl; Thu, 23 Mar 2023 22:17:58 -0700 (PDT) X-Google-Smtp-Source: AKy350Y2dcqCmbs1R1JJ347TtN7WaZhEno8IgbcEsg12VnsfQBMXYng/IUJ6bTyw+2mQpJoFeoHC X-Received: by 2002:a17:906:4f0b:b0:931:59f:d42 with SMTP id t11-20020a1709064f0b00b00931059f0d42mr1526210eju.29.1679635078515; Thu, 23 Mar 2023 22:17:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679635078; cv=none; d=google.com; s=arc-20160816; b=ZTM1mtf7V669FMwlNamdM2pZwbpJOnVTYCbWqAvJ31bfOpEWTSpZtvMUKrzlSqqgMk s4XEU7Q8bh0kITTXGkwrz8FkuSmG5TqO3bKXuVubD+o2vEUhHOGDN6hTZNuzKCTdZgpx Y5iR0aYEt4aNHnIHXGHG0rtpyytnStQDItoMux5PeB70LMZKkO5P1o/3UI/e9ITb51XI nvumWupUtssvwmxIP09e/2rEV8NlL2Gh9i8FNraw0m56SAfYNoHHLUsf3nknkJ2whvjA qBDFSpnXP6o6eO9Pl3UKfUvgESBeFTkIAcUp/7jwEefrr4Qfc1/lJRdgh+L2alZ4E2wL HHzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VQGPf/wxZw5dZB9k3t/6PRKbx/yttZTmKCb/1ksWzl0=; b=0Lf+3UdMKeo2q7RRh4TmGMZeRQFBYHQ70nD3kpGUlq12VgwMg7qhVx49/QAhhcjWQ6 riQnEB56XDYdTV/qpbo8wvbPaLSl/o+Yi+8h9HQz/di+i+K9cL8owIoWO4hDdQBBIbx/ nYpKnILyn4IhkISHU1xeVnutwrirHR36Vskvm9kawNivOs0zIh2wq+N46YIYvsVw85WP birMiAC3RcNGlKqYhBzTNeE6qkb5VPolVK9jwmaCKm6exyEWLZtsIT8UdUeeaDltv6Hn ksPxuFsO2WGrCMN7zdU3jvu8kS8riwmL27bZRWtpKeqKgTp154u7nYhk9PaX2V/Hd1TH Vjug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=c5ALrxiU; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g4-20020a056402180400b005021f0d574fsi1150735edy.662.2023.03.23.22.17.29; Thu, 23 Mar 2023 22:17:58 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=c5ALrxiU; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230213AbjCXFPl (ORCPT + 99 others); Fri, 24 Mar 2023 01:15:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229919AbjCXFPf (ORCPT ); Fri, 24 Mar 2023 01:15:35 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49E1A5BA9; Thu, 23 Mar 2023 22:15:34 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id o11so881464ple.1; Thu, 23 Mar 2023 22:15:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679634934; x=1682226934; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VQGPf/wxZw5dZB9k3t/6PRKbx/yttZTmKCb/1ksWzl0=; b=c5ALrxiUW19AUSWFhy12QVwNPfxgfp8tHt/7gDna438Nsj9opnzfctw30uwzhjvd2G Iq7yn5RShlnSHmQDqcAxBIw8iYOkm3+OMrKCymekH772QpgGziPeHEvx7Zl5y5xwCpX3 2csWFv2pqly7INK3AbIEyVHFKylRIdn4dMP5yRdrhOUeNk/TiZZjGtSJqVj1ekIxM0Un iAK7nzSgKkTZqTUA9fbiaYYdeTv4xm6gn5Joby6IhEGEaG5vZ3BXfc/ViWcPaXp16Pzb gNce5Mo8A/0AnglGFxba9AHv157QUdbGKHVmwnnNGLdDJWyjDJtrwe9C0LPFJ00nqA6E KyKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679634934; x=1682226934; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VQGPf/wxZw5dZB9k3t/6PRKbx/yttZTmKCb/1ksWzl0=; b=D9U0JhWCa/5qlv0YlwOTonDmAxMbw1JGNurxeRvfa4pGY7LXqiARD+IN9DuhRYJSPN iia90dwMXsBGlTQZk8cjw9FKNkE3oRZZMwP0ddNDfIoac9DdRap2My9SR3c5eZOpcSpn qncPj49YZdawzYgbNksCjdBEImSWxhBXBymVTnltoLDtBFasx7sT1IuRCUw99ezoFjPY 7I0ejdjUHcnjJKZggTLvmN2ReKN62+svp7SxoJpe+AXpyaHt/C8ahMHf2hGg7mkTTf7q WiTa+cuFhZ89hDIDFJC3qP0olMn2T4IBCd+7R/HVqd+eMQuG4sYv+DKnenhP05k8X4+6 KS0Q== X-Gm-Message-State: AAQBX9dNUn7cvvVVSOThvjoAjIMNshhiqb9sAxxtm0rfM4TotzF0W5Pt Pkdpzgd57jQ4fnWavgCmYrk= X-Received: by 2002:a17:902:9682:b0:1a2:1a52:14b5 with SMTP id n2-20020a170902968200b001a21a5214b5mr463332plp.3.1679634933693; Thu, 23 Mar 2023 22:15:33 -0700 (PDT) Received: from ip-172-31-38-16.us-west-2.compute.internal (ec2-52-37-71-140.us-west-2.compute.amazonaws.com. [52.37.71.140]) by smtp.gmail.com with ESMTPSA id x21-20020a170902ea9500b0019b9a075f1fsm13246133plb.80.2023.03.23.22.15.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 22:15:33 -0700 (PDT) From: aloktiagi To: viro@zeniv.linux.org.uk, brauner@kernel.org, David.Laight@ACULAB.COM, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: keescook@chromium.org, hch@infradead.org, aloktiagi , Tycho Andersen Subject: [RFC v3 2/3] file: allow callers to free the old file descriptor after dup2 Date: Fri, 24 Mar 2023 05:15:25 +0000 Message-Id: <20230324051526.963702-2-aloktiagi@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230324051526.963702-1-aloktiagi@gmail.com> References: <20230324051526.963702-1-aloktiagi@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 Allow callers of do_dup2 to get a reference to the file pointer being dup'ed over. The callers can then replace the file with the new file in the eventpoll interface or the file table before freeing it. Signed-off-by: aloktiagi --- Changes in v2: - Make the commit message more clearer. - Address review comment to make the interface cleaner so that the caller cannot forget to initialize the fdfile. --- fs/file.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/fs/file.c b/fs/file.c index 4b2346b8a5ee..1716f07103d8 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1086,7 +1086,7 @@ bool get_close_on_exec(unsigned int fd) } static int do_dup2(struct files_struct *files, - struct file *file, unsigned fd, unsigned flags) + struct file *file, unsigned fd, struct file **fdfile, unsigned flags) __releases(&files->file_lock) { struct file *tofree; @@ -1119,8 +1119,12 @@ __releases(&files->file_lock) __clear_close_on_exec(fd, fdt); spin_unlock(&files->file_lock); - if (tofree) - filp_close(tofree, files); + if (fdfile) { + *fdfile = tofree; + } else { + if (tofree) + filp_close(tofree, files); + } return fd; @@ -1132,6 +1136,7 @@ __releases(&files->file_lock) int replace_fd(unsigned fd, struct file *file, unsigned flags) { int err; + struct file *fdfile = NULL; struct files_struct *files = current->files; if (!file) @@ -1144,7 +1149,10 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) err = expand_files(files, fd); if (unlikely(err < 0)) goto out_unlock; - return do_dup2(files, file, fd, flags); + err = do_dup2(files, file, fd, &fdfile, flags); + if (fdfile) + filp_close(fdfile, files); + return err; out_unlock: spin_unlock(&files->file_lock); @@ -1237,7 +1245,7 @@ static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) goto Ebadf; goto out_unlock; } - return do_dup2(files, file, newfd, flags); + return do_dup2(files, file, newfd, NULL, flags); Ebadf: err = -EBADF; -- 2.34.1