Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1068678ybp; Fri, 11 Oct 2019 08:31:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqwqTTnOPSx4WojohwT67CjN5SDF7u8c8YGyZn5NzqaICL0i2l4FVcH00rKdgr8uvncRdWGH X-Received: by 2002:aa7:d28d:: with SMTP id w13mr13971145edq.184.1570807908872; Fri, 11 Oct 2019 08:31:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570807908; cv=none; d=google.com; s=arc-20160816; b=PyhWntnBZAIYWvR6ivfiPr9lsDHlLS5zGXIfFVrJvlcNMoVa+xVp99C/2j0YKk7zi+ S9PVziAbX7eG2AkZqu6mhHhiFpJ2ZVdibxb0QbuXcX4geWb5Ymb82i2ydIen7sQIT4T2 BJsWQ1yi4NkiyM2qHvYI/Oo5f8PqLPDPu9x8dKqnCIGuw95gVZm/S6T13XGLSczgaZsS /cb8tiACQCIviwoXyVQwcC1AmmRV86LKjfq38m9HZf41nF7ImVuVABvMzmlcZeYBNMvs eQtDNkewGj7qOrIBvwQ7JKZe+EyrIihYPkIbt8+l/cLB25lDFEljG+XNQAcDVX4PHG2E MjXw== 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=G65rnrPhLbwiKa12jxK6VsnV1tu58T/eP6dY6zrzvZM=; b=KEwKBk6REFx8TIA4VwMNWFDQgie/TU9n+UiX4QFpETX19c1N6f+1EpVJQYH9fQ3VvK ii73KuVMOb3p8bojlVe5DI81tShwJt5rZ+miox8WmkNr0E4SYSQEMrXzUcttJr6mAn7/ gTbX/1S1ZFugVXzZkdu3uCSkkalRVRXjMZdkmOJZnpFMCvbZjCfN+AUvVck8XSLYj5OK RwIbwpW+heFXdkPOiIrbjiVksxoEcellZuYOnDNlo1iXXDvyFfnlAh/c818XRLjWSzmV dgZ8W9rmMbx+/EW6jx3jLmxmjkyq1v8rp9eFSLsh0Xt3DJdC+oQ31B+8Va5e3Zatk5ML S7AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=axMZJb56; 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 g8si6259282edb.335.2019.10.11.08.31.23; Fri, 11 Oct 2019 08:31:48 -0700 (PDT) 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=axMZJb56; 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 S1728152AbfJKPab (ORCPT + 99 others); Fri, 11 Oct 2019 11:30:31 -0400 Received: from mail-oi1-f194.google.com ([209.85.167.194]:42043 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726827AbfJKPaa (ORCPT ); Fri, 11 Oct 2019 11:30:30 -0400 Received: by mail-oi1-f194.google.com with SMTP id i185so8305440oif.9 for ; Fri, 11 Oct 2019 08:30:30 -0700 (PDT) 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=G65rnrPhLbwiKa12jxK6VsnV1tu58T/eP6dY6zrzvZM=; b=axMZJb56FTNco00uonEJke76cy2AQd2gHUV3BqSx2OB0U6z6GuUjQ/Xz6xiljm5g5Z lW6rWqLQwABKmQ3T7lwpMck0t6kyXZ5t7+incgZFUVfOJ76gcLCkthIk+XDFt9lHczAC nktxww/gmoLYI6OOJY4ZZRSwXdcoT/+Rp8la7qB1uV8Ir/rkNeVYs4rrDBatIyui1oQ0 EebxyMVyHzNptgHHQHUH9c8utm6XDIp/POf1aKlASei1mcB37YHRw6a48R6YDxMiFyMM gyT8fjsL/3HGGKf84fttmBK6bz4mOXpQDT+vUlJJfYdBoc9ont8aU7zgzfPQZ5v1aaj3 IL9A== 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=G65rnrPhLbwiKa12jxK6VsnV1tu58T/eP6dY6zrzvZM=; b=iPv26K9oco91ln6y7F9LpVCqclvW3NZIOvivHSulLItyas/IL1cJnLkLhymGyXrnPV C1d1A89OHI4ecPDw6rmEOW6oNBB8kkzLGMRzrdDcunHfzmjiashnBH983SRSMkRkARKl O7A5MS+BHVWp6CWsRbbbpY7L0doVsgKB6YT/bzajgESaJD5RytsIpe4fzISECCmBl0Kx j6hkoIsCfT27ELV9DIir5UhweyfNwOMso6qFBvqS3z7pVAH9pgrBURXgs68VMJS1U986 jIb87AL0K2vYP3K6rb9uP8tNp+CohUwbueImm4dx6WAdjEuUT8GhHuX8XllnsjdCnhPQ 73vQ== X-Gm-Message-State: APjAAAX4MLwLETXyU7qObs39gYqdMtWZwtRHLkeMAPULYHRadJN3O5bA 3/As0gHx2XvdCo6cawFbYx1+10nDk5fJUL0LJGlYqg== X-Received: by 2002:aca:da41:: with SMTP id r62mr12192042oig.47.1570807829434; Fri, 11 Oct 2019 08:30:29 -0700 (PDT) MIME-Version: 1.0 References: <20191009160532.20674-1-ckellner@redhat.com> <20191011122323.7770-1-ckellner@redhat.com> <20191011151700.hdvztoxonpvogadv@wittgenstein> In-Reply-To: <20191011151700.hdvztoxonpvogadv@wittgenstein> From: Jann Horn Date: Fri, 11 Oct 2019 17:30:03 +0200 Message-ID: Subject: Re: [PATCH v3 1/2] pidfd: show pids for nested pid namespaces in fdinfo To: Christian Brauner Cc: Christian Kellner , Christian Brauner , kernel list , Linux API , Christian Kellner , Andrew Morton , "Peter Zijlstra (Intel)" , Ingo Molnar , Michal Hocko , Thomas Gleixner , Elena Reshetova , Roman Gushchin , Andrea Arcangeli , Al Viro , Aleksa Sarai , "Dmitry V. Levin" 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, Oct 11, 2019 at 5:17 PM Christian Brauner wrote: > > On Fri, Oct 11, 2019 at 04:55:59PM +0200, Jann Horn wrote: > > On Fri, Oct 11, 2019 at 2:23 PM Christian Kellner wrote: > > > The fdinfo file for a process file descriptor already contains the > > > pid of the process in the callers namespaces. Additionally, if pid > > > namespaces are configured, show the process ids of the process in > > > all nested namespaces in the same format as in the procfs status > > > file, i.e. "NSPid:\t%d\%d...". This allows the easy identification > > > of the processes in nested namespaces. > > [...] > > > #ifdef CONFIG_PROC_FS > > > +static inline void print_pidfd_nspid(struct seq_file *m, struct pid *pid, > > > + struct pid_namespace *ns) > > > > `ns` is the namespace of the PID namespace of the procfs instance > > through which the file descriptor is being viewed. > > > > > +{ > > > +#ifdef CONFIG_PID_NS > > > + int i; > > > + > > > + seq_puts(m, "\nNSpid:"); > > > + for (i = ns->level; i <= pid->level; i++) { > > > > ns->level is the level of the PID namespace associated with the procfs > > instance through which the file descriptor is being viewed. pid->level > > is the level of the PID associated with the pidfd. > > > > > + ns = pid->numbers[i].ns; > > > + seq_put_decimal_ull(m, "\t", pid_nr_ns(pid, ns)); > > > + } > > > +#endif > > > +} > > > > I think you assumed that `ns` is always going to contain `pid`. > > However, that's not the case. Consider the following scenario: > > > > - the init_pid_ns has two child PID namespaces, A and B (each with > > its own mount namespace and procfs instance) > > - process P1 lives in A > > - process P2 lives in B > > - P1 opens a pidfd for itself > > - P1 passes the pidfd to P2 (e.g. via a unix domain socket) > > - P2 reads /proc/self/fdinfo/$pidfd > > > > Now the loop will print the ID of P1 in A. I don't think that's what > > you intended? You might want to bail out if "pid_nr_ns(pid, ns) == 0", > > or something like that. > > I assumed the same thing happens when you pass around an fd for > /proc/self/status and that's why I didn't object to this behavior. I don't see how /proc/$pid/status is relevant. In the /proc/$pid/status case, the output is the list of PIDs starting at the PID namespace the procfs is associated with; and the process is always contained in that namespace, which also means that the first PID listed is the one in the PID namespace of the procfs instance. In the pidfd case, the process is not necessarily contained in that namespace, and the output doesn't make sense.