Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp2274999pxb; Mon, 11 Jan 2021 05:48:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJxKkKSN2c+lfZcRJ+RbrYOguNX8m/3y4WjHt6ojJmSsDDubQ6WafIUK8wZ2CvuJjQ4357/u X-Received: by 2002:a17:906:358c:: with SMTP id o12mr10827637ejb.301.1610372929619; Mon, 11 Jan 2021 05:48:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610372929; cv=none; d=google.com; s=arc-20160816; b=uf/keENb01+WXWwAFs7qv7QhTSaakDiyvyGGMW2xIKgC9f7a/DH0GHoikIrNdZkECa 8ObZzPjpuS8PoI7lIbf5Yw7rHeeMvlzHP3PpGBUTzbxwAq83sMGUO52M252NGjZr7Aq7 VIilz+qSCB20bd9zLju0DgqG3wvh50mechI3sWc5qy5LRq+iIJrjvoeRtbIgdJJ4XTC+ YQwiP0Jm8O6RRObt0/AIaBPMsNK6YIBIEd0lHeShIlspzJwTFu0wVGHHx6I2BpKqOYik kMbc2YLFL6iUzaPN7RKoKgmq/y/Lu3fj6aZ7snONvc67DKGsHL5o7ImTx8zEgscdjHjf qOiQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sxZiYp+TnLkgMt3IpHzZE6eOV5lW0V8JbItugGmgvKk=; b=xnWUOTDavjACHEP7EB6xzwYjfScmesS/36cgri1YKCseeT4m9phIzGIaCHftQOmgZ/ MG0wrgTrXYZk/0+nv95WLr06BShCQ9rWIYuo08RlCT4PBPzvfiMXqAq+PO5UXf+IYFMo Pgtghw3dBBQqu2h6PCVn8D8vpkVUaUX7/ogOfj+coOg6ppPMddhaFZjkIJLTi5Lwjzh9 jsf86W1Z4j3BGF8qYLG9r4WdT26fJLK1EdEswKwaOa6ANUiuq1ToL2q70iByE7eAlr0S hZbxyMsoyLsx0PdsecxCsY+mLOjmfQGXApeIbtYtuPNd/7ZNwEUmlPj81iXD+tcnddVY KIzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DrhZVlzp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v23si6816805ejy.208.2021.01.11.05.48.25; Mon, 11 Jan 2021 05:48:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DrhZVlzp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733244AbhAKNp7 (ORCPT + 99 others); Mon, 11 Jan 2021 08:45:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:58500 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731368AbhAKNLT (ORCPT ); Mon, 11 Jan 2021 08:11:19 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id BED6B2225E; Mon, 11 Jan 2021 13:10:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610370638; bh=W51fhLRjgV3Y/EbtSzLSZFydC1dnSviWQty8J+sxLGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DrhZVlzpfXJ8BjPvSxxEOrqBXsynEjmX0me06/us7bgVvN3tL/KtSBp2RCT5wCGFt 31cFt6sPliSjQLveC1tsKPT2ydrLyYteNqpzaLGm7GviRt/GXT78QDikcFFTP8b16u r9bJSHKxQVSnitJAilYqInyn2c+wTo8f9GcrUzs4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexey Dobriyan , "Rantala, Tommi T. (Nokia - FI/Espoo)" , Al Viro , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.4 09/92] proc: fix lookup in /proc/net subdirectories after setns(2) Date: Mon, 11 Jan 2021 14:01:13 +0100 Message-Id: <20210111130039.606476477@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210111130039.165470698@linuxfoundation.org> References: <20210111130039.165470698@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexey Dobriyan [ Upstream commit c6c75deda81344c3a95d1d1f606d5cee109e5d54 ] Commit 1fde6f21d90f ("proc: fix /proc/net/* after setns(2)") only forced revalidation of regular files under /proc/net/ However, /proc/net/ is unusual in the sense of /proc/net/foo handlers take netns pointer from parent directory which is old netns. Steps to reproduce: (void)open("/proc/net/sctp/snmp", O_RDONLY); unshare(CLONE_NEWNET); int fd = open("/proc/net/sctp/snmp", O_RDONLY); read(fd, &c, 1); Read will read wrong data from original netns. Patch forces lookup on every directory under /proc/net . Link: https://lkml.kernel.org/r/20201205160916.GA109739@localhost.localdomain Fixes: 1da4d377f943 ("proc: revalidate misc dentries") Signed-off-by: Alexey Dobriyan Reported-by: "Rantala, Tommi T. (Nokia - FI/Espoo)" Cc: Al Viro Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/proc/generic.c | 24 ++++++++++++++++++++++-- fs/proc/internal.h | 7 +++++++ fs/proc/proc_net.c | 16 ---------------- include/linux/proc_fs.h | 8 +++++++- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index d4f353187d67c..8c3dbe13e647c 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -342,6 +342,16 @@ static const struct file_operations proc_dir_operations = { .iterate_shared = proc_readdir, }; +static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags) +{ + return 0; +} + +const struct dentry_operations proc_net_dentry_ops = { + .d_revalidate = proc_net_d_revalidate, + .d_delete = always_delete_dentry, +}; + /* * proc directories can do almost nothing.. */ @@ -464,8 +474,8 @@ struct proc_dir_entry *proc_symlink(const char *name, } EXPORT_SYMBOL(proc_symlink); -struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, - struct proc_dir_entry *parent, void *data) +struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode, + struct proc_dir_entry *parent, void *data, bool force_lookup) { struct proc_dir_entry *ent; @@ -477,10 +487,20 @@ struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, ent->data = data; ent->proc_fops = &proc_dir_operations; ent->proc_iops = &proc_dir_inode_operations; + if (force_lookup) { + pde_force_lookup(ent); + } ent = proc_register(parent, ent); } return ent; } +EXPORT_SYMBOL_GPL(_proc_mkdir); + +struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, + struct proc_dir_entry *parent, void *data) +{ + return _proc_mkdir(name, mode, parent, data, false); +} EXPORT_SYMBOL_GPL(proc_mkdir_data); struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode, diff --git a/fs/proc/internal.h b/fs/proc/internal.h index cd0c8d5ce9a13..269acc165055d 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -299,3 +299,10 @@ extern unsigned long task_statm(struct mm_struct *, unsigned long *, unsigned long *, unsigned long *, unsigned long *); extern void task_mem(struct seq_file *, struct mm_struct *); + +extern const struct dentry_operations proc_net_dentry_ops; +static inline void pde_force_lookup(struct proc_dir_entry *pde) +{ + /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */ + pde->proc_dops = &proc_net_dentry_ops; +} diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c index 76ae278df1c47..313b7c751867f 100644 --- a/fs/proc/proc_net.c +++ b/fs/proc/proc_net.c @@ -39,22 +39,6 @@ static struct net *get_proc_net(const struct inode *inode) return maybe_get_net(PDE_NET(PDE(inode))); } -static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags) -{ - return 0; -} - -static const struct dentry_operations proc_net_dentry_ops = { - .d_revalidate = proc_net_d_revalidate, - .d_delete = always_delete_dentry, -}; - -static void pde_force_lookup(struct proc_dir_entry *pde) -{ - /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */ - pde->proc_dops = &proc_net_dentry_ops; -} - static int seq_open_net(struct inode *inode, struct file *file) { unsigned int state_size = PDE(inode)->state_size; diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index a705aa2d03f91..865d02c224ada 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -21,6 +21,7 @@ extern void proc_flush_task(struct task_struct *); extern struct proc_dir_entry *proc_symlink(const char *, struct proc_dir_entry *, const char *); +struct proc_dir_entry *_proc_mkdir(const char *, umode_t, struct proc_dir_entry *, void *, bool); extern struct proc_dir_entry *proc_mkdir(const char *, struct proc_dir_entry *); extern struct proc_dir_entry *proc_mkdir_data(const char *, umode_t, struct proc_dir_entry *, void *); @@ -99,6 +100,11 @@ static inline struct proc_dir_entry *proc_symlink(const char *name, static inline struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent) {return NULL;} static inline struct proc_dir_entry *proc_create_mount_point(const char *name) { return NULL; } +static inline struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode, + struct proc_dir_entry *parent, void *data, bool force_lookup) +{ + return NULL; +} static inline struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, struct proc_dir_entry *parent, void *data) { return NULL; } static inline struct proc_dir_entry *proc_mkdir_mode(const char *name, @@ -136,7 +142,7 @@ struct net; static inline struct proc_dir_entry *proc_net_mkdir( struct net *net, const char *name, struct proc_dir_entry *parent) { - return proc_mkdir_data(name, 0, parent, net); + return _proc_mkdir(name, 0, parent, net, true); } struct ns_common; -- 2.27.0