Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1052935ybp; Fri, 11 Oct 2019 08:18:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqxRLfocykZdK1tra+rFHYezd4YI0GZ17qYjXuvBbEwkmyVGWavGgty76iYLk0uFrJycI9cS X-Received: by 2002:a05:6000:112:: with SMTP id o18mr14519229wrx.323.1570807080542; Fri, 11 Oct 2019 08:18:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570807080; cv=none; d=google.com; s=arc-20160816; b=FI+1wZOmVKin+zAdSBCKG4G5RcFrPk3eOPqdzbGEovtPiH1MHBhr8KA5J+Rb0q+exD Vp71uYepKxvHFOTeXt88p2J4ztwJRFbG2+KDlv8ruwV2U2CK7jlvmKy5SonZ5ODMoWBJ 2ORLCDyiKgNzQefFDNCt8Fn1ZzlxXwJkQeb/pOxGUTJdnErqoSwZNAzPAat6EaJG4h7c 0+M0OCnQ/S7ygGk9bfXs7spS0LOv/iXuHapqgpRtsvEcGhjOMxurijSR5by2PEmKCtf8 PwsbvenIepWQ7uWuwKlQp1a7JEc8GVkHcs6BmAg+PIJdaFVLmmBPNfDoUSolAYY2SJL7 mbsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=PuOeMb7X0l144Evkfw6WVrtOA3l54jL9GDb1HToHhAI=; b=tA0EkKUpSYpm3vLKFIYyIKaGEpekvrUWBqxJaCe6PawuIxTLi+AWLUM191symnudiI 3rRKa7/lTswQPRKEpyWgmb4xC6048btxAFSPpX6JgayxO7rM2wv4cTPf4+lAKta0vNFX hB3CYumWQqZr27SGGG16YezLVP3bjj07LY726g+CaIKTW8pW8Jz+WhXcdHy4IlvTgamA NTKJt77ld3wvRGIZHhDv0HidJXo2TqahqEkiID/ocKt78bHYBnAWAf/ON3UVDfYPC/tt UxcKU/S37jPLNSn9gtJP5Q8nK3N5nYtbngnZThXQyG0GgYPMix3vzIH+FG1y8Qe02aJP 59Wg== ARC-Authentication-Results: i=1; mx.google.com; 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 z5si6429736edk.157.2019.10.11.08.17.37; Fri, 11 Oct 2019 08:18:00 -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; 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 S1727399AbfJKPRI (ORCPT + 99 others); Fri, 11 Oct 2019 11:17:08 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:45784 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726331AbfJKPRI (ORCPT ); Fri, 11 Oct 2019 11:17:08 -0400 Received: from [213.220.153.21] (helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iIwf8-0002I7-Kc; Fri, 11 Oct 2019 15:17:02 +0000 Date: Fri, 11 Oct 2019 17:17:01 +0200 From: Christian Brauner To: Jann Horn 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" Subject: Re: [PATCH v3 1/2] pidfd: show pids for nested pid namespaces in fdinfo Message-ID: <20191011151700.hdvztoxonpvogadv@wittgenstein> References: <20191009160532.20674-1-ckellner@redhat.com> <20191011122323.7770-1-ckellner@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 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 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. Christian