Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp225442ybl; Thu, 5 Dec 2019 18:39:51 -0800 (PST) X-Google-Smtp-Source: APXvYqwc8/T1i6aDnatTz0vCIR2P0xGzA35RM9Qez/rVwEh3e1ZREmnnx2QVcgU2GlknJq7uPRiB X-Received: by 2002:a54:4f8e:: with SMTP id g14mr10134588oiy.144.1575599991717; Thu, 05 Dec 2019 18:39:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575599991; cv=none; d=google.com; s=arc-20160816; b=XrblveZ9fV9LENZm2CExN4soNOlkzlLdXXrLx+ffnIrrEqANQA6VHcxlgaDgg6XERV PR8SC5FkPc3bckQPPTWxuft+JKJajxCiIdmrFKvh/kHHfPwDQlaPQ0m6UePUQfW64o8S L1SN+w+yB1U7uIebyvEOX6heRde5fJYYR+u63H9Nn4SI2yF9eenRmmy2KzC1/0Lw32Fi zeS4FGmi7Q1YueJLIs4I4qoeHAIqA1nl/11NSquAkBtQfs+JbyFT7Ul6TItUhZviF4AN K1yLmBf0wOH0sF2APTnl9iyVqCqNKwoILN0YOIT6T4woW1D6c8WLXcdkUM93Oa1hnSeA CzaQ== 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=WFmFb2O60k6iYs0X9+H/MSbUhF0d/S8/aF7lYjok+wI=; b=RxJO4976hoGjOmHDaqkzhQIl9mgj6SeGGnqYLZcD1iGY4bh5kvbXZPW2E8QzMYMIsj CrPMqIZiMd86bYPVPN1Imzcaf8AiaSMhdLualCt74Es7kdHAK8ggny38fooRyaH5fvFC XZdw4WaKwUXiPSLTgw7hLO1CKrmRcJax2BURkiOWd3+13wbgu6ehI/kEQ/PQSI5T9YLi 52IVHw0iX3PaSi0peqI8kNe8dAfMjsPKA/WRA3UZ0iU7dVENn5tP1WpYhXftIJ82W1+T MR1lLGVq99p2MoMczf9CGqmoitRWpHxZ0VAJ+IWn8dlPwIoBOfznHRTdXVVA1PGn2WJB quQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=H8QBSUvC; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y6si3313642oih.217.2019.12.05.18.39.36; Thu, 05 Dec 2019 18:39:51 -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=@google.com header.s=20161025 header.b=H8QBSUvC; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726350AbfLFCi5 (ORCPT + 99 others); Thu, 5 Dec 2019 21:38:57 -0500 Received: from mail-oi1-f196.google.com ([209.85.167.196]:46609 "EHLO mail-oi1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726207AbfLFCi5 (ORCPT ); Thu, 5 Dec 2019 21:38:57 -0500 Received: by mail-oi1-f196.google.com with SMTP id a124so4760354oii.13 for ; Thu, 05 Dec 2019 18:38:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WFmFb2O60k6iYs0X9+H/MSbUhF0d/S8/aF7lYjok+wI=; b=H8QBSUvC9VM0kHXxxjTDieQkuTjcuspYdm6y0f3H5m7hmfmANioFe6hykGF1SbIliu 4t0UNZ0siJUXyKFQeKI0J2TimHjiSkfomA/6Oc+S2JvBYebQVfOx/ltO/m3WL1NLYDhL sk2sBvM5DiRLb53l9K3OTrESzXmChN8G2NEjrgZb1DaHqwLuVSZqgMJVrUJ7WNQ4D4ei BnFntV5sYwf5zJm378oe7bxWkBmYhc0TLpYReaXshBE78StM11noZt17Xp0XzzPyzZ40 V/13612qYEx0/M3cF1ad4jteC3NUEd9hBMphnZw7D6aFMsZM9ZpUfgear0kPWY3Y3Xoa qFlQ== 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=WFmFb2O60k6iYs0X9+H/MSbUhF0d/S8/aF7lYjok+wI=; b=KHCWHxFvCmowqfTitn3MdJDN5GwF+IrwgTOM3SLWb+sC9QMzv1l7lj5qKZmCpJrOVF 2j2RWF7KBdt0U6+0wlR7L3m7jm8LWF+b2cSX12sShwbB1EUkO709zhMF9trvqUUnyd+A P1T2uR7nXgw5qR459P0t1IdkszaXZ2JRoT+KnWUucfasSzBjDwBSnY7QghSolmT3R+s7 Rwdq4arblA6x6XzFSVKi8kBMh08N955elWGnTofbohxu251WYeKNNI7X/nCc+G4jejiq Pqtxp0eNVuwaVIUGdZDHbHZaa2WNFj1vI3B+eV1zt77nSpqFmKeub/eG66lb8E/kr3Xz PH3g== X-Gm-Message-State: APjAAAWWKIYj9DcbY79W3NqmV2dumPC6ZF6Xu4vkgfyVKj+KHBuj3Ybv rIuMrIgcgdJZkq9kjwLWU3gkOOnkqf7Yo3prWvLfWg== X-Received: by 2002:aca:c7cb:: with SMTP id x194mr10763839oif.157.1575599935867; Thu, 05 Dec 2019 18:38:55 -0800 (PST) MIME-Version: 1.0 References: <20191205234450.GA26369@ircssh-2.c.rugged-nimbus-611.internal> In-Reply-To: <20191205234450.GA26369@ircssh-2.c.rugged-nimbus-611.internal> From: Jann Horn Date: Fri, 6 Dec 2019 03:38:29 +0100 Message-ID: Subject: Re: [RFC PATCH] ptrace: add PTRACE_GETFD request To: Sargun Dhillon 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 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. > + 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; > +}