Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp370265ybl; Thu, 5 Dec 2019 22:17:42 -0800 (PST) X-Google-Smtp-Source: APXvYqy6eyBLuaB0/2jRfNH+MIHg9593z3p4eWHwk35rHojAnHmDDQ1nTE9bd9IRECXPFrjC899h X-Received: by 2002:a9d:c29:: with SMTP id 38mr9187029otr.1.1575613062295; Thu, 05 Dec 2019 22:17:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575613062; cv=none; d=google.com; s=arc-20160816; b=S2hFK3paZfG2vNubCYtqJp73wOGnKBNCRU0HnihqnM6/i/k9dZKeZQ2uEnmMmDOFOw qGXRwToeBcbZUwDoj/Ym4JHkvM81tpvfOHHmUsCXoVmHRnEUgcqsaWWa7a3WLfGDhQsb injuXeUzNTCe8nOVOJNiEhJCxYYmNksxmnvmr/I8Uoa3c9cgPGPG12sN43PmasnTlFeo XX5q92lbP/3iU0e8LYhIwOH3eM5dmSudMIP4xCT2V5SSOcyoTQ8MFiIlyQYJpLiIRSZd iVfC0lsIO5JpnFN1VqGfwzTHfNHeMpD1ePzhZI1BJQR6XE1JV5ZBm6e20t5DaViQf8IR jlig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=aU5igi+hXGEOG2QjkV3GIlWqJAN3Fk8A4HK4+I3f2QM=; b=H/79/LySuX3Ksk8ZlHGFTlrDK50+gleb840ZeRO9YXHwYKECmdADQpLa1XTHf24p+P aJzMuvc7P9/RpzFXfpXK4N6Cv1ZdCb7IXXWBS9MkZqrbdmdPFSALXEY5XMPcrt22G119 niylxKPTj95uVNy1iN5pzre50J+MeQxAKVH07sTOiJl9/lFE08DA4847g+TTK+H9PIVC xVpkUrRSuo0S45GNPuypX8imrpJuq3beFJFXmcT84NacFiPs+NWi+I0PkR1wqqJSFHjM vF/8VgLT68pCvCt3N4UsXWIeSeo6ok6Cti8sDuwxBY32RXjuK7zd/Q3Cs2b0unGJTpBp PcKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=fk1qCuSP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d4si5544110ote.135.2019.12.05.22.17.29; Thu, 05 Dec 2019 22:17:42 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=fk1qCuSP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726193AbfLFGQl (ORCPT + 99 others); Fri, 6 Dec 2019 01:16:41 -0500 Received: from mail-ed1-f66.google.com ([209.85.208.66]:33648 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726076AbfLFGQl (ORCPT ); Fri, 6 Dec 2019 01:16:41 -0500 Received: by mail-ed1-f66.google.com with SMTP id l63so4913351ede.0 for ; Thu, 05 Dec 2019 22:16:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=aU5igi+hXGEOG2QjkV3GIlWqJAN3Fk8A4HK4+I3f2QM=; b=fk1qCuSPIEKRmrNCoOSyQZj7+0A2CtGnQ9degU7je+6lu69n6L5g/5TRM4wuDbJ7gd l97vL0uEkTz0Y+rTnfCtmXvC6hF4Qwx5Foj6ph90icCTw6CcdSam2W46RTjWwWcVt1G2 GwsIFD0gQJmDFEKBJPNCv4dO4FhByExQjZoQ8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=aU5igi+hXGEOG2QjkV3GIlWqJAN3Fk8A4HK4+I3f2QM=; b=WLGUGT4MRmerda1XgEjlVCRAd2HBEIUe/J/K0E7Y47iyQI1IEM5/JR9vtc3AsCAcKr i2tGEGLjs/dkzZJWOva5p+GvUVawcZZux35RkPAPj2fsm1BFSUOMvBhId0nWe9UGwull 1PNu9ulkDu3qWL4InQ/stLG82qDeHnpdH01bLNg4xvHKVtUuU09cy8x4uFcb4G4+LW35 XF17VIcIO+j9Jibf98cMcV9kAlrC4EbZfkcrxrkK3duVKHnfIwGecg0D63Q906DqNda4 zXd75uyj8WBCvV64/RK5I08ULNtSUIBbWvTWfZ5ZWHGY/1sIoLVkPIWmsm7JubVRIFIE SVHQ== X-Gm-Message-State: APjAAAVvYc0aHSNttL8GU+mQhr/VmWinRj8vM5ig6eY2z0hqQjljpNpY Cw5HlChU9RJQlJ/5IDvG324TwvN49HfjAQ8AObd7M9sWamo= X-Received: by 2002:a50:fa06:: with SMTP id b6mr1170960edq.231.1575612999059; Thu, 05 Dec 2019 22:16:39 -0800 (PST) MIME-Version: 1.0 References: <20191205234450.GA26369@ircssh-2.c.rugged-nimbus-611.internal> In-Reply-To: From: Sargun Dhillon Date: Thu, 5 Dec 2019 22:16:03 -0800 Message-ID: Subject: Re: [RFC PATCH] ptrace: add PTRACE_GETFD request To: Jann Horn Cc: kernel list , Linux Containers , Linux API , Tycho Andersen Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Dec 5, 2019 at 6:38 PM Jann Horn wrote: > > On Fri, Dec 6, 2019 at 12:44 AM Sargun Dhillon wrote: > > PTRACE_GETFD is a generic ptrace API that allows the tracer to > > get file descriptors from the traceee. > > typo: tracee > > > The primary reason to use this syscall is to allow sandboxers to > > take action on an FD on behalf of the tracee. For example, this > > can be combined with seccomp's user notification feature to extract > > a file descriptor and call privileged syscalls, like binding > > a socket to a privileged port. > [...] > > +/* This gets a file descriptor from a running process. It doesn't require the > > + * process to be stopped. > > + */ > > +#define PTRACE_GETFD 0x420f > [...] > > +static int ptrace_getfd(struct task_struct *child, unsigned long fd) > > I'd make the "fd" parameter of this function an "unsigned int", given > that that's also the argument type of fcheck_files(). > > > +{ > > + struct files_struct *files; > > + struct file *file; > > + int ret = 0; > > + > > + files = get_files_struct(child); > > + if (!files) > > + return -ENOENT; > > + > > + spin_lock(&files->file_lock); > > + file = fcheck_files(files, fd); > > + if (!file) > > + ret = -EBADF; > > + else > > + get_file(file); > > + spin_unlock(&files->file_lock); > > + put_files_struct(files); > > + > > + if (ret) > > + goto out; > > + > > + ret = get_unused_fd_flags(0); > > You're hardcoding the flags for the fd as 0, which means that there is > no way for the caller to enable O_CLOEXEC on the fd in a way that is > race-free against a concurrent execve(). If you can't easily plumb > through an O_CLOEXEC flag from userspace to here, you should probably > hardcode O_CLOEXEC here. > I thought about making addr used for flags. It seems a little weird, given the name, but it'll do the job. Alternatively, it could be a point to an options struct. If we introduce options, one of the nice things we could add is add the ability to cleanse the FD of certain information, like cgroups. > > + if (ret >= 0) > > + fd_install(ret, file); > > + > > + fput(file); > > Annoyingly, this isn't how fd_install() works. fd_install() has > slightly weird semantics and consumes the reference passed to it, so > this should be: > > if (ret >= 0) > fd_install(ret, file); > else > fput(file); > > > +out: > > + return ret; > > +}