Skip to contentSkip to conversation

Add pidfs filesystem

PATCH In review Alexey Gladkov 12 participants 36 messages Feb 18, 2017 – Mar 30, 2017

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add pidfs filesystem

The pidfs filesystem contains a subset of the /proc file system which contains only information about the processes.

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

Originally the idea was that the container will be mounted only pidfs and additional required files will be mounted on top using the overlayfs. But I found out that /proc does not support overlayfs and does not allow to mount anything on top or under it.

My question is whether it's possible to add overlayfs support for /proc?

Cc Kirill A. Shutemov <[email protected]>
---
 Documentation/filesystems/pidfs.txt | 16 ++++++++
 fs/proc/Kconfig                     |  8 ++++
 fs/proc/inode.c                     |  8 +++-
 fs/proc/internal.h                  |  2 +
 fs/proc/root.c                      | 76 ++++++++++++++++++++++++++++++++++---
 fs/proc/self.c                      |  6 +++
 fs/proc/thread_self.c               |  6 +++
 include/linux/pid_namespace.h       |  5 +++
 8 files changed, 119 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/pidfs.txt
Documentation/filesystems/pidfs.txt new file +16 -0
new file mode 100644
index 0000000..ce958a5
@@ -0,0 +1,16 @@
+The PIDFS Filesystem
+====================
+
+The pidfs filesystem contains a subset of the /proc file system which contains
+only information about the processes. The link self points to the process
+reading the file system. All other special files and directories in /proc are
+not available in this filesystem.
+
+The pidfs is not an independent filesystem, its implementation shares code
+with /proc.
+
+All mount options applicable to /proc filesystem are also applicable
+to pidfs filesystem. For example, access to the information in /proc/[pid]
+directories can be restricted using hidepid option.
+
+To get more information about the processes read the proc.txt
fs/proc/Kconfig +8 -0
index 1ade120..fa568f6 100644
@@ -43,6 +43,14 @@ config PROC_VMCORE
         help
         Exports the dump image of crashed kernel in ELF format.
 
+config PROC_PIDFS
+	bool "pidfs file system support"
+	depends on PROC_FS
+	default n
+	help
+	  The pidfs filesystem contains a subset of the /proc file system
+	  which contains only information only about the processes.
+
 config PROC_SYSCTL
 	bool "Sysctl support (/proc/sys)" if EXPERT
 	depends on PROC_FS
fs/proc/inode.c +6 -2
index 783bc19..1be65b4 100644
@@ -474,12 +474,16 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
 int proc_fill_super(struct super_block *s, void *data, int silent)
 {
 	struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
+	struct proc_dir_entry *fs_root = &proc_root;
 	struct inode *root_inode;
 	int ret;
 
 	if (!proc_parse_options(data, ns))
 		return -EINVAL;
 
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type)
+		fs_root = &pidfs_root;
+
 	/* User space would break if executables or devices appear on proc */
 	s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
 	s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
@@ -496,8 +500,8 @@ int proc_fill_super(struct super_block *s, void *data, int silent)
 	 */
 	s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
 	
-	pde_get(&proc_root);
-	root_inode = proc_get_inode(s, &proc_root);
+	pde_get(fs_root);
+	root_inode = proc_get_inode(s, fs_root);
 	if (!root_inode) {
 		pr_err("proc_fill_super: get root inode failed\n");
 		return -ENOMEM;
fs/proc/internal.h +2 -0
index 2de5194..a7c068c 100644
@@ -267,6 +267,8 @@ static inline void proc_tty_init(void) {}
 /*
  * root.c
  */
+extern struct file_system_type pidfs_fs_type;
+extern struct proc_dir_entry pidfs_root;
 extern struct proc_dir_entry proc_root;
 extern int proc_parse_options(char *options, struct pid_namespace *pid);
 
fs/proc/root.c +70 -6

134 lines not on this page · raw message

fs/proc/self.c +6 -0
index 4024595..dea7e17 100644
@@ -74,6 +74,12 @@ int proc_setup_self(struct super_block *s)
 		pr_err("proc_fill_super: can't allocate /proc/self\n");
 		return PTR_ERR(self);
 	}
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type) {
+		ns->pidfs_self = self;
+		return 0;
+	}
+
 	ns->proc_self = self;
 	return 0;
 }
fs/proc/thread_self.c +6 -0
index 595b90a97..274c618 100644
@@ -76,6 +76,12 @@ int proc_setup_thread_self(struct super_block *s)
 		pr_err("proc_fill_super: can't allocate /proc/thread_self\n");
 		return PTR_ERR(thread_self);
 	}
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type) {
+		ns->pidfs_thread_self = thread_self;
+		return 0;
+	}
+
 	ns->proc_thread_self = thread_self;
 	return 0;
 }
include/linux/pid_namespace.h +5 -0
index 34cce96..fca3a76 100644
@@ -46,6 +46,11 @@ struct pid_namespace {
 	int hide_pid;
 	int reboot;	/* group exit code if this pidns was rebooted */
 	struct ns_common ns;
+#ifdef CONFIG_PROC_PIDFS
+	struct vfsmount *pidfs_mnt;
+	struct dentry *pidfs_self;
+	struct dentry *pidfs_thread_self;
+#endif
 };
 
 extern struct pid_namespace init_pid_ns;
Signature · 5 lines
2.10.2


-- 
Rgrds, legion
#1 lore raw
url: https://github.com/0day-ci/linux/commits/Alexey-Gladkov/Add-pidfs-filesystem/20170219-070129 config: x86_64-randconfig-x003-201708 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3) 6.2.0…

Hi Alexey,

[auto build test WARNING on linus/master]
[also build test WARNING on v4.10-rc8 next-20170217]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Alexey-Gladkov/Add-pidfs-filesystem/20170219-070129
config: x86_64-randconfig-x003-201708 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

In file included from include/uapi/linux/stddef.h:1:0,

                    from include/linux/stddef.h:4,
                    from include/uapi/linux/posix_types.h:4,
                    from include/uapi/linux/types.h:13,
                    from include/linux/types.h:5,
                    from include/uapi/linux/capability.h:16,
                    from include/linux/capability.h:15,
                    from include/linux/sched.h:15,
                    from include/linux/uaccess.h:4,
                    from fs/proc/root.c:9:
   fs/proc/root.c: In function 'proc_kill_sb':
   fs/proc/root.c:107:9: error: 'struct pid_namespace' has no member named 'pidfs_self'; did you mean 'proc_self'?
      if (ns->pidfs_self)
            ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> fs/proc/root.c:107:3: note: in expansion of macro 'if'
      if (ns->pidfs_self)
      ^~
   fs/proc/root.c:107:9: error: 'struct pid_namespace' has no member named 'pidfs_self'; did you mean 'proc_self'?
      if (ns->pidfs_self)
            ^
   include/linux/compiler.h:149:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
>> fs/proc/root.c:107:3: note: in expansion of macro 'if'
      if (ns->pidfs_self)
      ^~
   fs/proc/root.c:107:9: error: 'struct pid_namespace' has no member named 'pidfs_self'; did you mean 'proc_self'?
      if (ns->pidfs_self)
            ^
   include/linux/compiler.h:160:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
>> fs/proc/root.c:107:3: note: in expansion of macro 'if'
      if (ns->pidfs_self)
      ^~
   fs/proc/root.c:108:11: error: 'struct pid_namespace' has no member named 'pidfs_self'; did you mean 'proc_self'?
       dput(ns->pidfs_self);
              ^~
   In file included from include/uapi/linux/stddef.h:1:0,
                    from include/linux/stddef.h:4,
                    from include/uapi/linux/posix_types.h:4,
                    from include/uapi/linux/types.h:13,
                    from include/linux/types.h:5,
                    from include/uapi/linux/capability.h:16,
                    from include/linux/capability.h:15,
                    from include/linux/sched.h:15,
                    from include/linux/uaccess.h:4,
                    from fs/proc/root.c:9:
   fs/proc/root.c:110:9: error: 'struct pid_namespace' has no member named 'pidfs_thread_self'; did you mean 'proc_thread_self'?
      if (ns->pidfs_thread_self)
            ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
   fs/proc/root.c:110:3: note: in expansion of macro 'if'
      if (ns->pidfs_thread_self)
      ^~
   fs/proc/root.c:110:9: error: 'struct pid_namespace' has no member named 'pidfs_thread_self'; did you mean 'proc_thread_self'?
      if (ns->pidfs_thread_self)
            ^
   include/linux/compiler.h:149:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
   fs/proc/root.c:110:3: note: in expansion of macro 'if'
      if (ns->pidfs_thread_self)
      ^~
   fs/proc/root.c:110:9: error: 'struct pid_namespace' has no member named 'pidfs_thread_self'; did you mean 'proc_thread_self'?
      if (ns->pidfs_thread_self)
            ^
   include/linux/compiler.h:160:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
   fs/proc/root.c:110:3: note: in expansion of macro 'if'
      if (ns->pidfs_thread_self)
      ^~
   fs/proc/root.c:111:11: error: 'struct pid_namespace' has no member named 'pidfs_thread_self'; did you mean 'proc_thread_self'?
       dput(ns->pidfs_thread_self);
              ^~
   fs/proc/root.c: In function 'pid_ns_prepare_proc':
   fs/proc/root.c:282:5: error: 'struct pid_namespace' has no member named 'pidfs_mnt'; did you mean 'proc_mnt'?
      ns->pidfs_mnt = mnt;
        ^~
   fs/proc/root.c: In function 'pid_ns_release_proc':
   fs/proc/root.c:292:18: error: 'struct pid_namespace' has no member named 'pidfs_mnt'; did you mean 'proc_mnt'?
      kern_unmount(ns->pidfs_mnt);
                     ^~

vim +/if +107 fs/proc/root.c

     3	 *
     4	 *  Copyright (C) 1991, 1992 Linus Torvalds
     5	 *
     6	 *  proc root directory handling functions
     7	 */
     8	
   > 9	#include <linux/uaccess.h>
    10	
    11	#include <linux/errno.h>
    12	#include <linux/time.h>
    13	#include <linux/proc_fs.h>
    14	#include <linux/stat.h>
    15	#include <linux/init.h>
    16	#include <linux/sched.h>
    17	#include <linux/module.h>
    18	#include <linux/bitops.h>
    19	#include <linux/user_namespace.h>
    20	#include <linux/mount.h>
    21	#include <linux/pid_namespace.h>
    22	#include <linux/parser.h>
    23	
    24	#include "internal.h"
    25	
    26	enum {
    27		Opt_gid, Opt_hidepid, Opt_err,
    28	};
    29	
    30	static const match_table_t tokens = {
    31		{Opt_hidepid, "hidepid=%u"},
    32		{Opt_gid, "gid=%u"},
    33		{Opt_err, NULL},
    34	};
    35	
    36	int proc_parse_options(char *options, struct pid_namespace *pid)
    37	{
    38		char *p;
    39		substring_t args[MAX_OPT_ARGS];
    40		int option;
    41	
    42		if (!options)
    43			return 1;
    44	
    45		while ((p = strsep(&options, ",")) != NULL) {
    46			int token;
    47			if (!*p)
    48				continue;
    49	
    50			args[0].to = args[0].from = NULL;
    51			token = match_token(p, tokens, args);
    52			switch (token) {
    53			case Opt_gid:
    54				if (match_int(&args[0], &option))
    55					return 0;
    56				pid->pid_gid = make_kgid(current_user_ns(), option);
    57				break;
    58			case Opt_hidepid:
    59				if (match_int(&args[0], &option))
    60					return 0;
    61				if (option < 0 || option > 2) {
    62					pr_err("proc: hidepid value must be between 0 and 2.\n");
    63					return 0;
    64				}
    65				pid->hide_pid = option;
    66				break;
    67			default:
    68				pr_err("proc: unrecognized mount option \"%s\" "
    69				       "or missing value\n", p);
    70				return 0;
    71			}
    72		}
    73	
    74		return 1;
    75	}
    76	
    77	int proc_remount(struct super_block *sb, int *flags, char *data)
    78	{
    79		struct pid_namespace *pid = sb->s_fs_info;
    80	
    81		sync_filesystem(sb);
    82		return !proc_parse_options(data, pid);
    83	}
    84	
    85	static struct dentry *proc_mount(struct file_system_type *fs_type,
    86		int flags, const char *dev_name, void *data)
    87	{
    88		struct pid_namespace *ns;
    89	
    90		if (flags & MS_KERNMOUNT) {
    91			ns = data;
    92			data = NULL;
    93		} else {
    94			ns = task_active_pid_ns(current);
    95		}
    96	
    97		return mount_ns(fs_type, flags, data, ns, ns->user_ns, proc_fill_super);
    98	}
    99	
   100	static void proc_kill_sb(struct super_block *sb)
   101	{
   102		struct pid_namespace *ns;
   103	
   104		ns = (struct pid_namespace *)sb->s_fs_info;
   105	
   106		if (IS_ENABLED(CONFIG_PROC_PIDFS) && sb->s_type == &pidfs_fs_type) {
 > 107			if (ns->pidfs_self)

108 dput(ns->pidfs_self);
109
110 if (ns->pidfs_thread_self)

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#2 lore raw
In reply to Alexey Gladkov
url: https://github.com/0day-ci/linux/commits/Alexey-Gladkov/Add-pidfs-filesystem/20170219-070129 config: x86_64-randconfig-x001-201708 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3) 6.2.0…

Hi Alexey,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.10-rc8 next-20170217]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Alexey-Gladkov/Add-pidfs-filesystem/20170219-070129
config: x86_64-randconfig-x001-201708 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

fs/proc/root.c: In function 'proc_kill_sb':

>> fs/proc/root.c:107:9: error: 'struct pid_namespace' has no member named 'pidfs_self'; did you mean 'proc_self'?
      if (ns->pidfs_self)
            ^~
   fs/proc/root.c:108:11: error: 'struct pid_namespace' has no member named 'pidfs_self'; did you mean 'proc_self'?
       dput(ns->pidfs_self);
              ^~
>> fs/proc/root.c:110:9: error: 'struct pid_namespace' has no member named 'pidfs_thread_self'; did you mean 'proc_thread_self'?
      if (ns->pidfs_thread_self)
            ^~
   fs/proc/root.c:111:11: error: 'struct pid_namespace' has no member named 'pidfs_thread_self'; did you mean 'proc_thread_self'?
       dput(ns->pidfs_thread_self);
              ^~
   fs/proc/root.c: In function 'pid_ns_prepare_proc':
>> fs/proc/root.c:282:5: error: 'struct pid_namespace' has no member named 'pidfs_mnt'; did you mean 'proc_mnt'?
      ns->pidfs_mnt = mnt;
        ^~
   fs/proc/root.c: In function 'pid_ns_release_proc':
   fs/proc/root.c:292:18: error: 'struct pid_namespace' has no member named 'pidfs_mnt'; did you mean 'proc_mnt'?
      kern_unmount(ns->pidfs_mnt);
                     ^~
--
   fs/proc/self.c: In function 'proc_setup_self':
>> fs/proc/self.c:66:5: error: 'struct pid_namespace' has no member named 'pidfs_self'; did you mean 'proc_self'?
      ns->pidfs_self = self;
        ^~
--
   fs/proc/thread_self.c: In function 'proc_setup_thread_self':
>> fs/proc/thread_self.c:67:5: error: 'struct pid_namespace' has no member named 'pidfs_thread_self'; did you mean 'proc_thread_self'?
      ns->pidfs_thread_self = thread_self;
        ^~

vim +107 fs/proc/root.c

101 {
102 struct pid_namespace *ns;
103
104 ns = (struct pid_namespace *)sb->s_fs_info;
105
106 if (IS_ENABLED(CONFIG_PROC_PIDFS) && sb->s_type == &pidfs_fs_type) {

 > 107			if (ns->pidfs_self)
   108				dput(ns->pidfs_self);
   109	
 > 110			if (ns->pidfs_thread_self)
   111				dput(ns->pidfs_thread_self);
   112		} else {
   113			if (ns->proc_self)
   114				dput(ns->proc_self);
   115	
   116			if (ns->proc_thread_self)
   117				dput(ns->proc_thread_self);
   118		}
   119	
   120		kill_anon_super(sb);
   121		put_pid_ns(ns);
   122	}
   123	
   124	static struct file_system_type proc_fs_type = {
   125		.name		= "proc",
   126		.mount		= proc_mount,
   127		.kill_sb	= proc_kill_sb,
   128		.fs_flags	= FS_USERNS_MOUNT,
   129	};
   130	
   131	struct file_system_type pidfs_fs_type = {
   132		.name		= "pidfs",
   133		.mount		= proc_mount,
   134		.kill_sb	= proc_kill_sb,
   135		.fs_flags	= FS_USERNS_MOUNT,
   136	};
   137	
   138	void __init proc_root_init(void)
   139	{
   140		int err;
   141	
   142		proc_init_inodecache();
   143		set_proc_pid_nlink();
   144		err = register_filesystem(&proc_fs_type);
   145		if (err)
   146			return;
   147	
   148		err = register_filesystem(&pidfs_fs_type);
   149		if (err)
   150			return;
   151	
   152		proc_self_init();
   153		proc_thread_self_init();
   154		proc_symlink("mounts", NULL, "self/mounts");
   155	
   156		proc_net_init();
   157	
   158	#ifdef CONFIG_SYSVIPC
   159		proc_mkdir("sysvipc", NULL);
   160	#endif
   161		proc_mkdir("fs", NULL);
   162		proc_mkdir("driver", NULL);
   163		proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
   164	#if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
   165		/* just give it a mountpoint */
   166		proc_create_mount_point("openprom");
   167	#endif
   168		proc_tty_init();
   169		proc_mkdir("bus", NULL);
   170		proc_sys_init();
   171	}
   172	
   173	static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
   174	{
   175		generic_fillattr(d_inode(dentry), stat);
   176		stat->nlink = proc_root.nlink + nr_processes();
   177		return 0;
   178	}
   179	
   180	static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
   181	{
   182		if (!proc_pid_lookup(dir, dentry, flags))
   183			return NULL;
   184		
   185		return proc_lookup(dir, dentry, flags);
   186	}
   187	
   188	static int proc_root_readdir(struct file *file, struct dir_context *ctx)
   189	{
   190		if (ctx->pos < FIRST_PROCESS_ENTRY) {
   191			int error = proc_readdir(file, ctx);
   192			if (unlikely(error <= 0))
   193				return error;
   194			ctx->pos = FIRST_PROCESS_ENTRY;
   195		}
   196	
   197		return proc_pid_readdir(file, ctx);
   198	}
   199	
   200	static int pidfs_root_readdir(struct file *file, struct dir_context *ctx)
   201	{
   202		if (ctx->pos < FIRST_PROCESS_ENTRY)
   203			ctx->pos = FIRST_PROCESS_ENTRY;
   204	
   205		return proc_pid_readdir(file, ctx);
   206	}
   207	
   208	/*
   209	 * The root /proc directory is special, as it has the
   210	 * <pid> directories. Thus we don't use the generic
   211	 * directory handling functions for that..
   212	 */
   213	static const struct file_operations proc_root_operations = {
   214		.read		 = generic_read_dir,
   215		.iterate_shared	 = proc_root_readdir,
   216		.llseek		= generic_file_llseek,
   217	};
   218	
   219	static const struct file_operations pidfs_root_operations = {
   220		.read		 = generic_read_dir,
   221		.iterate_shared	 = pidfs_root_readdir,
   222		.llseek		= generic_file_llseek,
   223	};
   224	
   225	/*
   226	 * proc root can do almost nothing..
   227	 */
   228	static const struct inode_operations proc_root_inode_operations = {
   229		.lookup		= proc_root_lookup,
   230		.getattr	= proc_root_getattr,
   231	};
   232	
   233	static const struct inode_operations pidfs_root_inode_operations = {
   234		.lookup		= proc_pid_lookup,
   235		.getattr	= proc_root_getattr,
   236	};
   237	
   238	/*
   239	 * This is the root "inode" in the /proc tree..
   240	 */
   241	struct proc_dir_entry proc_root = {
   242		.low_ino	= PROC_ROOT_INO, 
   243		.namelen	= 5, 
   244		.mode		= S_IFDIR | S_IRUGO | S_IXUGO, 
   245		.nlink		= 2, 
   246		.count		= ATOMIC_INIT(1),
   247		.proc_iops	= &proc_root_inode_operations, 
   248		.proc_fops	= &proc_root_operations,
   249		.parent		= &proc_root,
   250		.subdir		= RB_ROOT,
   251		.name		= "/proc",
   252	};
   253	
   254	struct proc_dir_entry pidfs_root = {
   255		.low_ino	= PROC_ROOT_INO,
   256		.namelen	= 6,
   257		.mode		= S_IFDIR | S_IRUGO | S_IXUGO,
   258		.nlink		= 2,
   259		.count		= ATOMIC_INIT(1),
   260		.proc_iops	= &pidfs_root_inode_operations,
   261		.proc_fops	= &pidfs_root_operations,
   262		.parent		= &pidfs_root,
   263		.subdir		= RB_ROOT,
   264		.name		= "/pidfs",
   265	};
   266	
   267	int pid_ns_prepare_proc(struct pid_namespace *ns)
   268	{
   269		struct vfsmount *mnt;
   270	
   271		mnt = kern_mount_data(&proc_fs_type, ns);
   272		if (IS_ERR(mnt))
   273			return PTR_ERR(mnt);
   274	
   275		ns->proc_mnt = mnt;
   276	
   277		if (IS_ENABLED(CONFIG_PROC_PIDFS)) {
   278			mnt = kern_mount_data(&pidfs_fs_type, ns);
   279			if (IS_ERR(mnt))
   280				return PTR_ERR(mnt);
   281	
 > 282			ns->pidfs_mnt = mnt;

283 }
284 return 0;
285 }

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw
In reply to Alexey Gladkov
Quoted 2 lines · Alexey Gladkov

The pidfs filesystem contains a subset of the /proc file system which contains only information about the processes.

My summary of your motivation.

It hurts when I create a container with a processes with uid 0 inside of it. This generates lots of hacks to attempt to limit uid 0.

My answer: Don't run a container with a real uid 0 inside of it.

Any reasonable permission check will on proc files
will keep you safe if your container does not have a real uid 0 in it.

That said I am not opposed in principle to a pidfs. And the idea of using overlay for this purpose is intriguing. I have not looked at it in enough detail comment on the technical merits.

Eric

Quoted 337 lines

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

Originally the idea was that the container will be mounted only pidfs and additional required files will be mounted on top using the overlayfs. But I found out that /proc does not support overlayfs and does not allow to mount anything on top or under it.

My question is whether it's possible to add overlayfs support for /proc?

Cc Kirill A. Shutemov <[email protected]>
---
 Documentation/filesystems/pidfs.txt | 16 ++++++++
 fs/proc/Kconfig                     |  8 ++++
 fs/proc/inode.c                     |  8 +++-
 fs/proc/internal.h                  |  2 +
 fs/proc/root.c                      | 76 ++++++++++++++++++++++++++++++++++---
 fs/proc/self.c                      |  6 +++
 fs/proc/thread_self.c               |  6 +++
 include/linux/pid_namespace.h       |  5 +++
 8 files changed, 119 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/pidfs.txt
Documentation/filesystems/pidfs.txt new file +16 -0
new file mode 100644
index 0000000..ce958a5
@@ -0,0 +1,16 @@
+The PIDFS Filesystem
+====================
+
+The pidfs filesystem contains a subset of the /proc file system which contains
+only information about the processes. The link self points to the process
+reading the file system. All other special files and directories in /proc are
+not available in this filesystem.
+
+The pidfs is not an independent filesystem, its implementation shares code
+with /proc.
+
+All mount options applicable to /proc filesystem are also applicable
+to pidfs filesystem. For example, access to the information in /proc/[pid]
+directories can be restricted using hidepid option.
+
+To get more information about the processes read the proc.txt
fs/proc/Kconfig +8 -0
index 1ade120..fa568f6 100644
@@ -43,6 +43,14 @@ config PROC_VMCORE
         help
         Exports the dump image of crashed kernel in ELF format.
 
+config PROC_PIDFS
+	bool "pidfs file system support"
+	depends on PROC_FS
+	default n
+	help
+	  The pidfs filesystem contains a subset of the /proc file system
+	  which contains only information only about the processes.
+
 config PROC_SYSCTL
 	bool "Sysctl support (/proc/sys)" if EXPERT
 	depends on PROC_FS
fs/proc/inode.c +6 -2
index 783bc19..1be65b4 100644
@@ -474,12 +474,16 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
 int proc_fill_super(struct super_block *s, void *data, int silent)
 {
 	struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
+	struct proc_dir_entry *fs_root = &proc_root;
 	struct inode *root_inode;
 	int ret;
 
 	if (!proc_parse_options(data, ns))
 		return -EINVAL;
 
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type)
+		fs_root = &pidfs_root;
+
 	/* User space would break if executables or devices appear on proc */
 	s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
 	s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
@@ -496,8 +500,8 @@ int proc_fill_super(struct super_block *s, void *data, int silent)
 	 */
 	s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
 
-	pde_get(&proc_root);
-	root_inode = proc_get_inode(s, &proc_root);
+	pde_get(fs_root);
+	root_inode = proc_get_inode(s, fs_root);
 	if (!root_inode) {
 		pr_err("proc_fill_super: get root inode failed\n");
 		return -ENOMEM;
fs/proc/internal.h +2 -0
index 2de5194..a7c068c 100644
@@ -267,6 +267,8 @@ static inline void proc_tty_init(void) {}
 /*
  * root.c
  */
+extern struct file_system_type pidfs_fs_type;
+extern struct proc_dir_entry pidfs_root;
 extern struct proc_dir_entry proc_root;
 extern int proc_parse_options(char *options, struct pid_namespace *pid);
 
fs/proc/root.c +70 -6
index 4bd0373..de16ac1 100644
@@ -102,10 +102,21 @@ static void proc_kill_sb(struct super_block *sb)
 	struct pid_namespace *ns;
 
 	ns = (struct pid_namespace *)sb->s_fs_info;
-	if (ns->proc_self)
-		dput(ns->proc_self);
-	if (ns->proc_thread_self)
-		dput(ns->proc_thread_self);
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && sb->s_type == &pidfs_fs_type) {
+		if (ns->pidfs_self)
+			dput(ns->pidfs_self);
+
+		if (ns->pidfs_thread_self)
+			dput(ns->pidfs_thread_self);
+	} else {
+		if (ns->proc_self)
+			dput(ns->proc_self);
+
+		if (ns->proc_thread_self)
+			dput(ns->proc_thread_self);
+	}
+
 	kill_anon_super(sb);
 	put_pid_ns(ns);
 }
@@ -117,6 +128,13 @@ static struct file_system_type proc_fs_type = {
 	.fs_flags	= FS_USERNS_MOUNT,
 };
 
+struct file_system_type pidfs_fs_type = {
+	.name		= "pidfs",
+	.mount		= proc_mount,
+	.kill_sb	= proc_kill_sb,
+	.fs_flags	= FS_USERNS_MOUNT,
+};
+
 void __init proc_root_init(void)
 {
 	int err;
@@ -127,6 +145,10 @@ void __init proc_root_init(void)
 	if (err)
 		return;
 
+	err = register_filesystem(&pidfs_fs_type);
+	if (err)
+		return;
+
 	proc_self_init();
 	proc_thread_self_init();
 	proc_symlink("mounts", NULL, "self/mounts");
@@ -148,8 +170,7 @@ void __init proc_root_init(void)
 	proc_sys_init();
 }
 
-static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
-)
+static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
 {
 	generic_fillattr(d_inode(dentry), stat);
 	stat->nlink = proc_root.nlink + nr_processes();
@@ -176,6 +197,14 @@ static int proc_root_readdir(struct file *file, struct dir_context *ctx)
 	return proc_pid_readdir(file, ctx);
 }
 
+static int pidfs_root_readdir(struct file *file, struct dir_context *ctx)
+{
+	if (ctx->pos < FIRST_PROCESS_ENTRY)
+		ctx->pos = FIRST_PROCESS_ENTRY;
+
+	return proc_pid_readdir(file, ctx);
+}
+
 /*
  * The root /proc directory is special, as it has the
  * <pid> directories. Thus we don't use the generic
@@ -187,6 +216,12 @@ static const struct file_operations proc_root_operations = {
 	.llseek		= generic_file_llseek,
 };
 
+static const struct file_operations pidfs_root_operations = {
+	.read		 = generic_read_dir,
+	.iterate_shared	 = pidfs_root_readdir,
+	.llseek		= generic_file_llseek,
+};
+
 /*
  * proc root can do almost nothing..
  */
@@ -195,6 +230,11 @@ static const struct inode_operations proc_root_inode_operations = {
 	.getattr	= proc_root_getattr,
 };
 
+static const struct inode_operations pidfs_root_inode_operations = {
+	.lookup		= proc_pid_lookup,
+	.getattr	= proc_root_getattr,
+};
+
 /*
  * This is the root "inode" in the /proc tree..
  */
@@ -211,6 +251,19 @@ struct proc_dir_entry proc_root = {
 	.name		= "/proc",
 };
 
+struct proc_dir_entry pidfs_root = {
+	.low_ino	= PROC_ROOT_INO,
+	.namelen	= 6,
+	.mode		= S_IFDIR | S_IRUGO | S_IXUGO,
+	.nlink		= 2,
+	.count		= ATOMIC_INIT(1),
+	.proc_iops	= &pidfs_root_inode_operations,
+	.proc_fops	= &pidfs_root_operations,
+	.parent		= &pidfs_root,
+	.subdir		= RB_ROOT,
+	.name		= "/pidfs",
+};
+
 int pid_ns_prepare_proc(struct pid_namespace *ns)
 {
 	struct vfsmount *mnt;
@@ -220,10 +273,21 @@ int pid_ns_prepare_proc(struct pid_namespace *ns)
 		return PTR_ERR(mnt);
 
 	ns->proc_mnt = mnt;
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS)) {
+		mnt = kern_mount_data(&pidfs_fs_type, ns);
+		if (IS_ERR(mnt))
+			return PTR_ERR(mnt);
+
+		ns->pidfs_mnt = mnt;
+	}
 	return 0;
 }
 
 void pid_ns_release_proc(struct pid_namespace *ns)
 {
 	kern_unmount(ns->proc_mnt);
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS))
+		kern_unmount(ns->pidfs_mnt);
 }
fs/proc/self.c +6 -0
index 4024595..dea7e17 100644
@@ -74,6 +74,12 @@ int proc_setup_self(struct super_block *s)
 		pr_err("proc_fill_super: can't allocate /proc/self\n");
 		return PTR_ERR(self);
 	}
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type) {
+		ns->pidfs_self = self;
+		return 0;
+	}
+
 	ns->proc_self = self;
 	return 0;
 }
fs/proc/thread_self.c +6 -0
index 595b90a97..274c618 100644
@@ -76,6 +76,12 @@ int proc_setup_thread_self(struct super_block *s)
 		pr_err("proc_fill_super: can't allocate /proc/thread_self\n");
 		return PTR_ERR(thread_self);
 	}
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type) {
+		ns->pidfs_thread_self = thread_self;
+		return 0;
+	}
+
 	ns->proc_thread_self = thread_self;
 	return 0;
 }
include/linux/pid_namespace.h +5 -0
index 34cce96..fca3a76 100644
@@ -46,6 +46,11 @@ struct pid_namespace {
 	int hide_pid;
 	int reboot;	/* group exit code if this pidns was rebooted */
 	struct ns_common ns;
+#ifdef CONFIG_PROC_PIDFS
+	struct vfsmount *pidfs_mnt;
+	struct dentry *pidfs_self;
+	struct dentry *pidfs_thread_self;
+#endif
 };
 
 extern struct pid_namespace init_pid_ns;
Signature · 1 line
2.10.2
#4 lore raw
Quoted 7 lines · Eric W. Biederman
Quoted 2 lines · Alexey Gladkov

The pidfs filesystem contains a subset of the /proc file system which contains only information about the processes.

My summary of your motivation.

Quoted 7 lines

It hurts when I create a container with a processes with uid 0 inside of it. This generates lots of hacks to attempt to limit uid 0.

My answer: Don't run a container with a real uid 0 inside of it.

Any reasonable permission check will on proc files
will keep you safe if your container does not have a real uid 0 in it.

Right now this approach is all we have for not virtualized objects in /proc. We can only hope that those who create files in /proc are intelligent enough.

We can't protect ourselves from the appearance of something unwanted in /proc.

Quoted 3 lines

That said I am not opposed in principle to a pidfs. And the idea of using overlay for this purpose is intriguing. I have not looked at it in enough detail comment on the technical merits.

I'm currently researching the way of adding support overlayfs for /proc.

But before going any further, I would like to discuss in the right direction I think or not.

Quoted 337 lines
Quoted 337 lines

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

Originally the idea was that the container will be mounted only pidfs and additional required files will be mounted on top using the overlayfs. But I found out that /proc does not support overlayfs and does not allow to mount anything on top or under it.

My question is whether it's possible to add overlayfs support for /proc?

Cc Kirill A. Shutemov <[email protected]>
---
 Documentation/filesystems/pidfs.txt | 16 ++++++++
 fs/proc/Kconfig                     |  8 ++++
 fs/proc/inode.c                     |  8 +++-
 fs/proc/internal.h                  |  2 +
 fs/proc/root.c                      | 76 ++++++++++++++++++++++++++++++++++---
 fs/proc/self.c                      |  6 +++
 fs/proc/thread_self.c               |  6 +++
 include/linux/pid_namespace.h       |  5 +++
 8 files changed, 119 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/pidfs.txt
Documentation/filesystems/pidfs.txt new file +16 -0
new file mode 100644
index 0000000..ce958a5
@@ -0,0 +1,16 @@
+The PIDFS Filesystem
+====================
+
+The pidfs filesystem contains a subset of the /proc file system which contains
+only information about the processes. The link self points to the process
+reading the file system. All other special files and directories in /proc are
+not available in this filesystem.
+
+The pidfs is not an independent filesystem, its implementation shares code
+with /proc.
+
+All mount options applicable to /proc filesystem are also applicable
+to pidfs filesystem. For example, access to the information in /proc/[pid]
+directories can be restricted using hidepid option.
+
+To get more information about the processes read the proc.txt
fs/proc/Kconfig +8 -0
index 1ade120..fa568f6 100644
@@ -43,6 +43,14 @@ config PROC_VMCORE
         help
         Exports the dump image of crashed kernel in ELF format.
 
+config PROC_PIDFS
+	bool "pidfs file system support"
+	depends on PROC_FS
+	default n
+	help
+	  The pidfs filesystem contains a subset of the /proc file system
+	  which contains only information only about the processes.
+
 config PROC_SYSCTL
 	bool "Sysctl support (/proc/sys)" if EXPERT
 	depends on PROC_FS
fs/proc/inode.c +6 -2
index 783bc19..1be65b4 100644
@@ -474,12 +474,16 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
 int proc_fill_super(struct super_block *s, void *data, int silent)
 {
 	struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
+	struct proc_dir_entry *fs_root = &proc_root;
 	struct inode *root_inode;
 	int ret;
 
 	if (!proc_parse_options(data, ns))
 		return -EINVAL;
 
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type)
+		fs_root = &pidfs_root;
+
 	/* User space would break if executables or devices appear on proc */
 	s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
 	s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
@@ -496,8 +500,8 @@ int proc_fill_super(struct super_block *s, void *data, int silent)
 	 */
 	s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
 
-	pde_get(&proc_root);
-	root_inode = proc_get_inode(s, &proc_root);
+	pde_get(fs_root);
+	root_inode = proc_get_inode(s, fs_root);
 	if (!root_inode) {
 		pr_err("proc_fill_super: get root inode failed\n");
 		return -ENOMEM;
fs/proc/internal.h +2 -0
index 2de5194..a7c068c 100644
@@ -267,6 +267,8 @@ static inline void proc_tty_init(void) {}
 /*
  * root.c
  */
+extern struct file_system_type pidfs_fs_type;
+extern struct proc_dir_entry pidfs_root;
 extern struct proc_dir_entry proc_root;
 extern int proc_parse_options(char *options, struct pid_namespace *pid);
 
fs/proc/root.c +70 -6
index 4bd0373..de16ac1 100644
@@ -102,10 +102,21 @@ static void proc_kill_sb(struct super_block *sb)
 	struct pid_namespace *ns;
 
 	ns = (struct pid_namespace *)sb->s_fs_info;
-	if (ns->proc_self)
-		dput(ns->proc_self);
-	if (ns->proc_thread_self)
-		dput(ns->proc_thread_self);
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && sb->s_type == &pidfs_fs_type) {
+		if (ns->pidfs_self)
+			dput(ns->pidfs_self);
+
+		if (ns->pidfs_thread_self)
+			dput(ns->pidfs_thread_self);
+	} else {
+		if (ns->proc_self)
+			dput(ns->proc_self);
+
+		if (ns->proc_thread_self)
+			dput(ns->proc_thread_self);
+	}
+
 	kill_anon_super(sb);
 	put_pid_ns(ns);
 }
@@ -117,6 +128,13 @@ static struct file_system_type proc_fs_type = {
 	.fs_flags	= FS_USERNS_MOUNT,
 };
 
+struct file_system_type pidfs_fs_type = {
+	.name		= "pidfs",
+	.mount		= proc_mount,
+	.kill_sb	= proc_kill_sb,
+	.fs_flags	= FS_USERNS_MOUNT,
+};
+
 void __init proc_root_init(void)
 {
 	int err;
@@ -127,6 +145,10 @@ void __init proc_root_init(void)
 	if (err)
 		return;
 
+	err = register_filesystem(&pidfs_fs_type);
+	if (err)
+		return;
+
 	proc_self_init();
 	proc_thread_self_init();
 	proc_symlink("mounts", NULL, "self/mounts");
@@ -148,8 +170,7 @@ void __init proc_root_init(void)
 	proc_sys_init();
 }
 
-static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
-)
+static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
 {
 	generic_fillattr(d_inode(dentry), stat);
 	stat->nlink = proc_root.nlink + nr_processes();
@@ -176,6 +197,14 @@ static int proc_root_readdir(struct file *file, struct dir_context *ctx)
 	return proc_pid_readdir(file, ctx);
 }
 
+static int pidfs_root_readdir(struct file *file, struct dir_context *ctx)
+{
+	if (ctx->pos < FIRST_PROCESS_ENTRY)
+		ctx->pos = FIRST_PROCESS_ENTRY;
+
+	return proc_pid_readdir(file, ctx);
+}
+
 /*
  * The root /proc directory is special, as it has the
  * <pid> directories. Thus we don't use the generic
@@ -187,6 +216,12 @@ static const struct file_operations proc_root_operations = {
 	.llseek		= generic_file_llseek,
 };
 
+static const struct file_operations pidfs_root_operations = {
+	.read		 = generic_read_dir,
+	.iterate_shared	 = pidfs_root_readdir,
+	.llseek		= generic_file_llseek,
+};
+
 /*
  * proc root can do almost nothing..
  */
@@ -195,6 +230,11 @@ static const struct inode_operations proc_root_inode_operations = {
 	.getattr	= proc_root_getattr,
 };
 
+static const struct inode_operations pidfs_root_inode_operations = {
+	.lookup		= proc_pid_lookup,
+	.getattr	= proc_root_getattr,
+};
+
 /*
  * This is the root "inode" in the /proc tree..
  */
@@ -211,6 +251,19 @@ struct proc_dir_entry proc_root = {
 	.name		= "/proc",
 };
 
+struct proc_dir_entry pidfs_root = {
+	.low_ino	= PROC_ROOT_INO,
+	.namelen	= 6,
+	.mode		= S_IFDIR | S_IRUGO | S_IXUGO,
+	.nlink		= 2,
+	.count		= ATOMIC_INIT(1),
+	.proc_iops	= &pidfs_root_inode_operations,
+	.proc_fops	= &pidfs_root_operations,
+	.parent		= &pidfs_root,
+	.subdir		= RB_ROOT,
+	.name		= "/pidfs",
+};
+
 int pid_ns_prepare_proc(struct pid_namespace *ns)
 {
 	struct vfsmount *mnt;
@@ -220,10 +273,21 @@ int pid_ns_prepare_proc(struct pid_namespace *ns)
 		return PTR_ERR(mnt);
 
 	ns->proc_mnt = mnt;
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS)) {
+		mnt = kern_mount_data(&pidfs_fs_type, ns);
+		if (IS_ERR(mnt))
+			return PTR_ERR(mnt);
+
+		ns->pidfs_mnt = mnt;
+	}
 	return 0;
 }
 
 void pid_ns_release_proc(struct pid_namespace *ns)
 {
 	kern_unmount(ns->proc_mnt);
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS))
+		kern_unmount(ns->pidfs_mnt);
 }
fs/proc/self.c +6 -0
index 4024595..dea7e17 100644
@@ -74,6 +74,12 @@ int proc_setup_self(struct super_block *s)
 		pr_err("proc_fill_super: can't allocate /proc/self\n");
 		return PTR_ERR(self);
 	}
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type) {
+		ns->pidfs_self = self;
+		return 0;
+	}
+
 	ns->proc_self = self;
 	return 0;
 }
fs/proc/thread_self.c +6 -0
index 595b90a97..274c618 100644
@@ -76,6 +76,12 @@ int proc_setup_thread_self(struct super_block *s)
 		pr_err("proc_fill_super: can't allocate /proc/thread_self\n");
 		return PTR_ERR(thread_self);
 	}
+
+	if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type) {
+		ns->pidfs_thread_self = thread_self;
+		return 0;
+	}
+
 	ns->proc_thread_self = thread_self;
 	return 0;
 }
include/linux/pid_namespace.h +5 -0
index 34cce96..fca3a76 100644
@@ -46,6 +46,11 @@ struct pid_namespace {
 	int hide_pid;
 	int reboot;	/* group exit code if this pidns was rebooted */
 	struct ns_common ns;
+#ifdef CONFIG_PROC_PIDFS
+	struct vfsmount *pidfs_mnt;
+	struct dentry *pidfs_self;
+	struct dentry *pidfs_thread_self;
+#endif
 };
 
 extern struct pid_namespace init_pid_ns;
Signature · 1 line
2.10.2
Signature · 1 line
Rgrds, legion
#5 lore raw
In reply to Eric W. Biederman
Quoted 11 lines · Eric W. Biederman
Quoted 2 lines · Alexey Gladkov

The pidfs filesystem contains a subset of the /proc file system which contains only information about the processes.

My summary of your motivation.

It hurts when I create a container with a processes with uid 0 inside of it. This generates lots of hacks to attempt to limit uid 0.

My answer: Don't run a container with a real uid 0 inside of it.

I agree. Unless I miss something I'd say use a user namespace to get decent permission checks in /proc (and /sys).

Signature · 2 lines
Thanks,
//richard
#6 lore raw
In reply to Alexey Gladkov
Quoted 4 lines · Alexey Gladkov

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

I leave this to you and Eric, but imo it would be nice to avoid another filesystem.

Quoted 5 lines

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

But perhaps we can use mnt_flags? For example, lets abuse MNT_NODEV, see the simple patch below. Not sure it is correct/complete, just to illustrate the idea.

With this patch you can mount proc with -onodev and it will only show pids/self/thread_self:

	# mkdir /tmp/D
	# mount -t proc -o nodev none /tmp/D
	# ls /tmp/D
	1   11	13  15	17  19	20  22	24  28	3   31	33  4  56  7  9     thread-self
	10  12	14  16	18  2	21  23	27  29	30  32	34  5  6   8  self
	# cat /tmp/D/meminfo
	cat: /tmp/D/meminfo: No such file or directory
	# ls /tmp/D/irq
	ls: cannot open directory /tmp/D/irq: No such file or directory

No?

Oleg.

fs/proc/generic.c +12 -0
@@ -305,11 +305,22 @@ int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
 
 int proc_readdir(struct file *file, struct dir_context *ctx)
 {
+	int mnt_flags = file->f_path.mnt->mnt_flags;
 	struct inode *inode = file_inode(file);
 
+	if (mnt_flags & MNT_NODEV)
+		return 1;
+
 	return proc_readdir_de(PDE(inode), file, ctx);
 }
 
+static int proc_dir_open(struct inode *inode, struct file *file)
+{
+	if (file->f_path.mnt->mnt_flags & MNT_NODEV)
+		return -ENOENT;
+	return 0;
+}
+
 /*
  * These are the generic /proc directory operations. They
  * use the in-memory "struct proc_dir_entry" tree to parse
@@ -319,6 +330,7 @@ static const struct file_operations proc_dir_operations = {
 	.llseek			= generic_file_llseek,
 	.read			= generic_read_dir,
 	.iterate_shared		= proc_readdir,
+	.open			= proc_dir_open,
 };
 
 /*
fs/proc/inode.c +4 -0
@@ -318,12 +318,16 @@ proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
 
 static int proc_reg_open(struct inode *inode, struct file *file)
 {
+	int mnt_flags = file->f_path.mnt->mnt_flags;
 	struct proc_dir_entry *pde = PDE(inode);
 	int rv = 0;
 	int (*open)(struct inode *, struct file *);
 	int (*release)(struct inode *, struct file *);
 	struct pde_opener *pdeo;
 
+	if (mnt_flags & MNT_NODEV)
+		return -ENOENT;
+
 	/*
 	 * Ensure that
 	 * 1) PDE's ->release hook will be called no matter what
#7 lore raw
Quoted 32 lines · Oleg Nesterov
Quoted 4 lines · Alexey Gladkov

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

I leave this to you and Eric, but imo it would be nice to avoid another filesystem.

Quoted 5 lines

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

But perhaps we can use mnt_flags? For example, lets abuse MNT_NODEV, see the simple patch below. Not sure it is correct/complete, just to illustrate the idea.

With this patch you can mount proc with -onodev and it will only show pids/self/thread_self:

	# mkdir /tmp/D
	# mount -t proc -o nodev none /tmp/D
	# ls /tmp/D
	1   11	13  15	17  19	20  22	24  28	3   31	33  4  56  7  9     thread-self
	10  12	14  16	18  2	21  23	27  29	30  32	34  5  6   8  self
	# cat /tmp/D/meminfo
	cat: /tmp/D/meminfo: No such file or directory
	# ls /tmp/D/irq
	ls: cannot open directory /tmp/D/irq: No such file or directory
Quoted 1 line

No?

Yes!!! If this whole effort with pidfs and overlayfs will move forward, I would prefer seeing the nodev procfs version, rather than another fs.

As far as the overlayfs part is concerned, having an overlayfs mounted on /proc inside container may result in problems as applications sometimes check for /proc containing procfs (by checking statfs.f_type == PROC_SUPER_MAGIC or by reading the /proc/mounts).

-- Pavel

Quoted 58 lines

Oleg.

fs/proc/generic.c +12 -0
@@ -305,11 +305,22 @@ int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
 
 int proc_readdir(struct file *file, struct dir_context *ctx)
 {
+	int mnt_flags = file->f_path.mnt->mnt_flags;
 	struct inode *inode = file_inode(file);
 
+	if (mnt_flags & MNT_NODEV)
+		return 1;
+
 	return proc_readdir_de(PDE(inode), file, ctx);
 }
 
+static int proc_dir_open(struct inode *inode, struct file *file)
+{
+	if (file->f_path.mnt->mnt_flags & MNT_NODEV)
+		return -ENOENT;
+	return 0;
+}
+
 /*
  * These are the generic /proc directory operations. They
  * use the in-memory "struct proc_dir_entry" tree to parse
@@ -319,6 +330,7 @@ static const struct file_operations proc_dir_operations = {
 	.llseek			= generic_file_llseek,
 	.read			= generic_read_dir,
 	.iterate_shared		= proc_readdir,
+	.open			= proc_dir_open,
 };
 
 /*
fs/proc/inode.c +4 -0
@@ -318,12 +318,16 @@ proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
 
 static int proc_reg_open(struct inode *inode, struct file *file)
 {
+	int mnt_flags = file->f_path.mnt->mnt_flags;
 	struct proc_dir_entry *pde = PDE(inode);
 	int rv = 0;
 	int (*open)(struct inode *, struct file *);
 	int (*release)(struct inode *, struct file *);
 	struct pde_opener *pdeo;
 
+	if (mnt_flags & MNT_NODEV)
+		return -ENOENT;
+
 	/*
 	 * Ensure that
 	 * 1) PDE's ->release hook will be called no matter what

.

#8 lore raw
Quoted 33 lines · Pavel Emelyanov
Quoted 32 lines · Oleg Nesterov
Quoted 4 lines · Alexey Gladkov

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

I leave this to you and Eric, but imo it would be nice to avoid another filesystem.

Quoted 5 lines

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

But perhaps we can use mnt_flags? For example, lets abuse MNT_NODEV, see the simple patch below. Not sure it is correct/complete, just to illustrate the idea.

With this patch you can mount proc with -onodev and it will only show pids/self/thread_self:

	# mkdir /tmp/D
	# mount -t proc -o nodev none /tmp/D
	# ls /tmp/D
	1   11	13  15	17  19	20  22	24  28	3   31	33  4  56  7  9     thread-self
	10  12	14  16	18  2	21  23	27  29	30  32	34  5  6   8  self
	# cat /tmp/D/meminfo
	cat: /tmp/D/meminfo: No such file or directory
	# ls /tmp/D/irq
	ls: cannot open directory /tmp/D/irq: No such file or directory
Quoted 4 lines
Quoted 1 line

No?

Yes!!! If this whole effort with pidfs and overlayfs will move forward, I would prefer seeing the nodev procfs version, rather than another fs.

But this is not procfs anymore. If someone will wait for procfs here it will be disappointed :)

Quoted 4 lines

As far as the overlayfs part is concerned, having an overlayfs mounted on /proc inside container may result in problems as applications sometimes check for /proc containing procfs (by checking statfs.f_type == PROC_SUPER_MAGIC or by reading the /proc/mounts).

It is not a replacement for procfs. It's a subset of procfs. If someone wants the procfs in the code we should not deceive him.

No?

Quoted 60 lines

-- Pavel

Quoted 58 lines

Oleg.

fs/proc/generic.c +12 -0
@@ -305,11 +305,22 @@ int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
 
 int proc_readdir(struct file *file, struct dir_context *ctx)
 {
+	int mnt_flags = file->f_path.mnt->mnt_flags;
 	struct inode *inode = file_inode(file);
 
+	if (mnt_flags & MNT_NODEV)
+		return 1;
+
 	return proc_readdir_de(PDE(inode), file, ctx);
 }
 
+static int proc_dir_open(struct inode *inode, struct file *file)
+{
+	if (file->f_path.mnt->mnt_flags & MNT_NODEV)
+		return -ENOENT;
+	return 0;
+}
+
 /*
  * These are the generic /proc directory operations. They
  * use the in-memory "struct proc_dir_entry" tree to parse
@@ -319,6 +330,7 @@ static const struct file_operations proc_dir_operations = {
 	.llseek			= generic_file_llseek,
 	.read			= generic_read_dir,
 	.iterate_shared		= proc_readdir,
+	.open			= proc_dir_open,
 };
 
 /*
fs/proc/inode.c +4 -0
@@ -318,12 +318,16 @@ proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
 
 static int proc_reg_open(struct inode *inode, struct file *file)
 {
+	int mnt_flags = file->f_path.mnt->mnt_flags;
 	struct proc_dir_entry *pde = PDE(inode);
 	int rv = 0;
 	int (*open)(struct inode *, struct file *);
 	int (*release)(struct inode *, struct file *);
 	struct pde_opener *pdeo;
 
+	if (mnt_flags & MNT_NODEV)
+		return -ENOENT;
+
 	/*
 	 * Ensure that
 	 * 1) PDE's ->release hook will be called no matter what

.

Signature · 1 line
Rgrds, legion
#9 lore raw
Quoted 34 lines · Alexey Gladkov
Quoted 33 lines · Pavel Emelyanov
Quoted 32 lines · Oleg Nesterov
Quoted 4 lines · Alexey Gladkov

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

I leave this to you and Eric, but imo it would be nice to avoid another filesystem.

Quoted 5 lines

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

But perhaps we can use mnt_flags? For example, lets abuse MNT_NODEV, see the simple patch below. Not sure it is correct/complete, just to illustrate the idea.

With this patch you can mount proc with -onodev and it will only show pids/self/thread_self:

	# mkdir /tmp/D
	# mount -t proc -o nodev none /tmp/D
	# ls /tmp/D
	1   11	13  15	17  19	20  22	24  28	3   31	33  4  56  7  9     thread-self
	10  12	14  16	18  2	21  23	27  29	30  32	34  5  6   8  self
	# cat /tmp/D/meminfo
	cat: /tmp/D/meminfo: No such file or directory
	# ls /tmp/D/irq
	ls: cannot open directory /tmp/D/irq: No such file or directory
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

No?

Yes!!! If this whole effort with pidfs and overlayfs will move forward, I would prefer seeing the nodev procfs version, rather than another fs.

But this is not procfs anymore. If someone will wait for procfs here it will be disappointed :)

Well, it depends on what files he's looking for in there. This is what overlay part should come for.

Quoted 9 lines
Quoted 4 lines

As far as the overlayfs part is concerned, having an overlayfs mounted on /proc inside container may result in problems as applications sometimes check for /proc containing procfs (by checking statfs.f_type == PROC_SUPER_MAGIC or by reading the /proc/mounts).

It is not a replacement for procfs. It's a subset of procfs. If someone wants the procfs in the code we should not deceive him.

No?

But this is what we actually do -- Docker does with bind-mounts, LXC does with lxcfs, OpenVZ does with kernel patches. Every time a container starts the regular /proc is mutated not to show some information.

-- Pavel

#10 lore raw
In reply to Oleg Nesterov
Quoted 32 lines · Oleg Nesterov
Quoted 4 lines · Alexey Gladkov

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

I leave this to you and Eric, but imo it would be nice to avoid another filesystem.

Quoted 5 lines

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

But perhaps we can use mnt_flags? For example, lets abuse MNT_NODEV, see the simple patch below. Not sure it is correct/complete, just to illustrate the idea.

With this patch you can mount proc with -onodev and it will only show pids/self/thread_self:

	# mkdir /tmp/D
	# mount -t proc -o nodev none /tmp/D
	# ls /tmp/D
	1   11	13  15	17  19	20  22	24  28	3   31	33  4  56  7  9     thread-self
	10  12	14  16	18  2	21  23	27  29	30  32	34  5  6   8  self
	# cat /tmp/D/meminfo
	cat: /tmp/D/meminfo: No such file or directory
	# ls /tmp/D/irq
	ls: cannot open directory /tmp/D/irq: No such file or directory
Quoted 1 line

No?

I'm embarrassed that we start the change procfs by abuse something. It is very difficult to explain why this option leads to such consequences on procfs.

Also with this change it won't be the same procfs. We can't to name it as procfs because procfs have cpuinfo, meminfo, etc.

What do you think about this ?

Signature · 1 line
Rgrds, legion
#11 lore raw
In reply to Oleg Nesterov
Quoted 32 lines · Oleg Nesterov
Quoted 4 lines · Alexey Gladkov

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

I leave this to you and Eric, but imo it would be nice to avoid another filesystem.

Quoted 5 lines

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

But perhaps we can use mnt_flags? For example, lets abuse MNT_NODEV, see the simple patch below. Not sure it is correct/complete, just to illustrate the idea.

With this patch you can mount proc with -onodev and it will only show pids/self/thread_self:

	# mkdir /tmp/D
	# mount -t proc -o nodev none /tmp/D
	# ls /tmp/D
	1   11	13  15	17  19	20  22	24  28	3   31	33  4  56  7  9     thread-self
	10  12	14  16	18  2	21  23	27  29	30  32	34  5  6   8  self
	# cat /tmp/D/meminfo
	cat: /tmp/D/meminfo: No such file or directory
	# ls /tmp/D/irq
	ls: cannot open directory /tmp/D/irq: No such file or directory
Quoted 1 line

No?

I like the idea of using mnt_flags to turn procfs into pidfs, thus avoiding yet another filesystem, but MNT_NODEV has a different meaning, namely "do not interpret character or block special devices on the file system". I've actually found a system nearby that already mounts /proc with nodev:

# grep 'proc.*nodev' /proc/mounts
proc /var/lib/vz/root/1002/proc proc rw,nosuid,nodev,noexec,relatime,gid=19,hidepid=2 0 0
proc /var/lib/vz/root/1003/proc proc rw,nosuid,nodev,noexec,relatime,gid=19,hidepid=2 0 0
proc /var/lib/vz/root/1004/proc proc rw,nosuid,nodev,noexec,relatime,gid=19,hidepid=2 0 0
proc /var/lib/vz/root/1005/proc proc rw,nosuid,nodev,noexec,relatime,gid=19,hidepid=2 0 0
Signature · 1 line
ldv
#12 lore raw
Quoted 6 lines · Dmitry V. Levin
Quoted 4 lines · Oleg Nesterov

But perhaps we can use mnt_flags? For example, lets abuse MNT_NODEV, see the simple patch below. Not sure it is correct/complete, just to illustrate the idea.

...

Quoted 2 lines

I like the idea of using mnt_flags to turn procfs into pidfs, thus avoiding yet another filesystem, but MNT_NODEV has a different meaning,

Yes, yes, see "abuse MNT_NODEV" above. The patch just tries to illustrate the idea.

Oleg.

#13 lore raw
Quoted 8 lines · Oleg Nesterov
Quoted 6 lines · Dmitry V. Levin
Quoted 4 lines · Oleg Nesterov

But perhaps we can use mnt_flags? For example, lets abuse MNT_NODEV, see the simple patch below. Not sure it is correct/complete, just to illustrate the idea.

Quoted 7 lines

...

Quoted 2 lines

I like the idea of using mnt_flags to turn procfs into pidfs, thus avoiding yet another filesystem, but MNT_NODEV has a different meaning,

Yes, yes, see "abuse MNT_NODEV" above. The patch just tries to illustrate the idea.

I think I can try to use subtype for this. In this case, it is still the same procfs, but with the subtype:

mount -t proc.pidfs ...

Signature · 1 line
Rgrds, legion
#14 lore raw

RFC 1/1 Add option to mount only a pids subset

In reply to Oleg Nesterov

After discussion with Oleg Nesterov I reimplement my patch as an additional option for /proc. This option affects the mountpoint. It means that in one pid namespace it possible to have both the whole traditional /proc and /proc with only pids subset.

However, it remains an open question about overlayfs support in the /proc.

== Overview ==

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/sys, /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an option for /proc, flags applied to /proc have an effect on this subset of filesystem.

Originally the idea was that the container will be mounted only pid sunset and additional required files will be mounted on top using the overlayfs. But I found out that /proc does not support overlayfs and does not allow to mount anything on top or under it.

== TODO ==

There is still work to do:

* Show pidonly via proc_show_options.
* Add overlayfs support.

---
 fs/internal.h         |  1 +
 fs/namespace.c        |  9 ++++++++
 fs/proc/generic.c     |  4 ++++
 fs/proc/inode.c       |  7 +++++-
 fs/proc/internal.h    |  8 +++++++
 fs/proc/root.c        | 62 +++++++++++++++++++++++++++++++++++++++++++++++----
 fs/stat.c             |  4 ++++
 fs/super.c            | 20 +++++++++++++++++
 include/linux/fs.h    |  2 ++
 include/linux/mount.h |  1 +
 10 files changed, 113 insertions(+), 5 deletions(-)
fs/internal.h +1 -0
index 4fcf517..cb44ca7 100644
@@ -88,6 +88,7 @@ extern struct file *get_empty_filp(void);
  * super.c
  */
 extern int do_remount_sb(struct super_block *, int, void *, int);
+extern int do_mount_sb(struct vfsmount *mnt, int flags, void *data);
 extern bool trylock_super(struct super_block *sb);
 extern struct dentry *mount_fs(struct file_system_type *,
 			       int, const char *, void *);
fs/namespace.c +9 -0
index e6c234b..6cb2bcb 100644
@@ -938,6 +938,7 @@ static struct mount *skip_mnt_tree(struct mount *p)
 struct vfsmount *
 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
 {
+	int err;
 	struct mount *mnt;
 	struct dentry *root;
 
@@ -962,6 +963,14 @@ vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void
 	mnt->mnt.mnt_sb = root->d_sb;
 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
 	mnt->mnt_parent = mnt;
+
+	err = do_mount_sb(&mnt->mnt, flags, data);
+	if(err) {
+		mnt_free_id(mnt);
+		free_vfsmnt(mnt);
+		return ERR_PTR(err);
+	}
+
 	lock_mount_hash();
 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
 	unlock_mount_hash();
fs/proc/generic.c +4 -0
index 7eb3cef..dd3da60 100644
@@ -306,6 +306,10 @@ int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
 int proc_readdir(struct file *file, struct dir_context *ctx)
 {
 	struct inode *inode = file_inode(file);
+	struct proc_options *opts = file->f_path.mnt->fs_data;
+
+	if (opts && opts->pid_only)
+		return 1;
 
 	return proc_readdir_de(PDE(inode), file, ctx);
 }
fs/proc/inode.c +6 -1
index 783bc19..51b1712 100644
@@ -108,7 +108,6 @@ static int proc_show_options(struct seq_file *seq, struct dentry *root)
 		seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
 	if (pid->hide_pid != 0)
 		seq_printf(seq, ",hidepid=%u", pid->hide_pid);
-
 	return 0;
 }
 
@@ -118,6 +117,8 @@ static const struct super_operations proc_sops = {
 	.drop_inode	= generic_delete_inode,
 	.evict_inode	= proc_evict_inode,
 	.statfs		= simple_statfs,
+	.getattr_fs	= proc_getattrfs,
+	.mount_fs	= proc_mount_cb,
 	.remount_fs	= proc_remount,
 	.show_options	= proc_show_options,
 };
@@ -323,6 +324,10 @@ static int proc_reg_open(struct inode *inode, struct file *file)
 	int (*open)(struct inode *, struct file *);
 	int (*release)(struct inode *, struct file *);
 	struct pde_opener *pdeo;
+	struct proc_options *opts = file->f_path.mnt->fs_data;
+
+	if (opts && opts->pid_only)
+		return -ENOENT;
 
 	/*
 	 * Ensure that
fs/proc/internal.h +8 -0
index 2de5194..7fdbfee 100644
@@ -267,11 +267,19 @@ static inline void proc_tty_init(void) {}
 /*
  * root.c
  */
+struct proc_options {
+	kgid_t pid_gid;
+	int hide_pid;
+	int pid_only;
+};
+
 extern struct proc_dir_entry proc_root;
 extern int proc_parse_options(char *options, struct pid_namespace *pid);
 
 extern void proc_self_init(void);
 extern int proc_remount(struct super_block *, int *, char *);
+extern int proc_mount_cb(struct vfsmount *mnt, int *flags, char *data);
+extern int proc_getattrfs(struct vfsmount *, struct dentry *, struct kstat *);
 
 /*
  * task_[no]mmu.c
fs/proc/root.c +58 -4

106 lines not on this page · raw message

fs/stat.c +4 -0
index bc045c7..1e26308 100644
@@ -10,6 +10,7 @@
 #include <linux/file.h>
 #include <linux/highuid.h>
 #include <linux/fs.h>
+#include <linux/mount.h>
 #include <linux/namei.h>
 #include <linux/security.h>
 #include <linux/syscalls.h>
@@ -53,6 +54,9 @@ int vfs_getattr_nosec(struct path *path, struct kstat *stat)
 {
 	struct inode *inode = d_backing_inode(path->dentry);
 
+	if (path->mnt->mnt_sb->s_op->getattr_fs)
+		return path->mnt->mnt_sb->s_op->getattr_fs(path->mnt, path->dentry, stat);
+
 	if (inode->i_op->getattr)
 		return inode->i_op->getattr(path->mnt, path->dentry, stat);
 
fs/super.c +20 -0
index c183835..478dd5b 100644
@@ -832,6 +832,26 @@ int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
 	return retval;
 }
 
+/**
+ *	do_mount_sb - asks filesystem to finish mount with options.
+ *	@mnt:	vfsmount in question
+ *	@flags:	numeric part of options
+ *	@data:	the rest of options
+ *
+ *	Alters the mount options of a mounted filesystem.
+ */
+int do_mount_sb(struct vfsmount *mnt, int flags, void *data)
+{
+	if (mnt->mnt_sb->s_writers.frozen != SB_UNFROZEN)
+		return -EBUSY;
+
+	if (mnt->mnt_sb->s_op->mount_fs) {
+		return mnt->mnt_sb->s_op->mount_fs(mnt, &flags, data);
+	}
+
+	return 0;
+}
+
 static void do_emergency_remount(struct work_struct *work)
 {
 	struct super_block *sb, *p = NULL;
include/linux/fs.h +2 -0
index 83de8b6..5bd1b84 100644
@@ -1759,6 +1759,8 @@ struct super_operations {
 	int (*thaw_super) (struct super_block *);
 	int (*unfreeze_fs) (struct super_block *);
 	int (*statfs) (struct dentry *, struct kstatfs *);
+	int (*getattr_fs) (struct vfsmount *, struct dentry *, struct kstat *);
+	int (*mount_fs) (struct vfsmount *, int *, char *);
 	int (*remount_fs) (struct super_block *, int *, char *);
 	void (*umount_begin) (struct super_block *);
 
include/linux/mount.h +1 -0
index 1172cce..4bd364e 100644
@@ -67,6 +67,7 @@ struct vfsmount {
 	struct dentry *mnt_root;	/* root of the mounted tree */
 	struct super_block *mnt_sb;	/* pointer to superblock */
 	int mnt_flags;
+	void *fs_data;			/* fs-specific data */
 };
 
 struct file; /* forward dec */
Signature · 1 line
2.10.2
#15 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 6 lines · Alexey Gladkov

After discussion with Oleg Nesterov I reimplement my patch as an additional option for /proc. This option affects the mountpoint. It means that in one pid namespace it possible to have both the whole traditional /proc and /proc with only pids subset.

I like this. I think you should split it into two patches, though:
one that reworks how procfs gets mounted and one that makes adds the new functionality.

Djajal had some concerns about the first part breaking applications that use stat and expect certain behavior. This should be manageable, though, but making stat work appropriately.

#16 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 9 lines · Andy Lutomirski
Quoted 6 lines · Alexey Gladkov

After discussion with Oleg Nesterov I reimplement my patch as an additional option for /proc. This option affects the mountpoint. It means that in one pid namespace it possible to have both the whole traditional /proc and /proc with only pids subset.

Quoted 7 lines

I like this. I think you should split it into two patches, though:
one that reworks how procfs gets mounted and one that makes adds the new functionality.

Djajal had some concerns about the first part breaking applications that use stat and expect certain behavior. This should be manageable, though, but making stat work appropriately.

I'm bit lost in the two discussion, however the main concern I was discussing with Andy was if you have per superblock proc mounts then each mount will end up with its own device ID st_dev, right now they share the same ID if they are in the same pid namespace, but if we change that then we may break the following:
http://man7.org/linux/man-pages/man7/namespaces.7.html

Both new NS_GET_PARENT and NS_GET_USERNS ioctl() that return an fd, suggests to follow up with fstat() to identify the namespaces.. "By applying fstat(2) to the returned file descriptor, one obtains a stat structure whose st_dev (resident device) and st_ino (inode number) fields together identify the owning/parent namespace."

Other /proc/self/ns/* comparison and stat() logic...

Andy suggested that we may have the same st_dev for mounts in the same pid namespace... I'm not sure which side effect this may bring!

Thanks!

Signature · 1 line
tixxdz
#17 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 31 lines · Djalal Harouni
Quoted 9 lines · Andy Lutomirski
Quoted 6 lines · Alexey Gladkov

After discussion with Oleg Nesterov I reimplement my patch as an additional option for /proc. This option affects the mountpoint. It means that in one pid namespace it possible to have both the whole traditional /proc and /proc with only pids subset.

Quoted 7 lines

I like this. I think you should split it into two patches, though:
one that reworks how procfs gets mounted and one that makes adds the new functionality.

Djajal had some concerns about the first part breaking applications that use stat and expect certain behavior. This should be manageable, though, but making stat work appropriately.

I'm bit lost in the two discussion, however the main concern I was discussing with Andy was if you have per superblock proc mounts then each mount will end up with its own device ID st_dev, right now they share the same ID if they are in the same pid namespace, but if we change that then we may break the following:
http://man7.org/linux/man-pages/man7/namespaces.7.html

Both new NS_GET_PARENT and NS_GET_USERNS ioctl() that return an fd, suggests to follow up with fstat() to identify the namespaces.. "By applying fstat(2) to the returned file descriptor, one obtains a stat structure whose st_dev (resident device) and st_ino (inode number) fields together identify the owning/parent namespace."

Quoted 4 lines

Other /proc/self/ns/* comparison and stat() logic...

Andy suggested that we may have the same st_dev for mounts in the same pid namespace... I'm not sure which side effect this may bring!

Well right now it is less of an issue than you image. I suggest you stat /proc and /proc/self/ns/* and look at st_dev.

That said anything that changes today's proc needs to be tested with a range of distro's especially those using selinux and apparmor as they tend to have policies that are very sensitive to the implementation details. Such that seemingly innocent changes can cause systems to stop booting.

Eric

#18 lore raw

Re: [RFC] Add option to mount only a pids subset

In reply to Djalal Harouni
Quoted 6 lines · Djalal Harouni

I'm bit lost in the two discussion, however the main concern I was discussing with Andy was if you have per superblock proc mounts then each mount will end up with its own device ID st_dev, right now they share the same ID if they are in the same pid namespace, but if we change that then we may break the following:
http://man7.org/linux/man-pages/man7/namespaces.7.html

In fact, nothing has changed. I added a parameter that affects the mountpoint, not the entire pid namespace.

The procfs will still be global. The device ID will be the same as before.

Quoted 10 lines

Both new NS_GET_PARENT and NS_GET_USERNS ioctl() that return an fd, suggests to follow up with fstat() to identify the namespaces.. "By applying fstat(2) to the returned file descriptor, one obtains a stat structure whose st_dev (resident device) and st_ino (inode number) fields together identify the owning/parent namespace."

Other /proc/self/ns/* comparison and stat() logic...

Andy suggested that we may have the same st_dev for mounts in the same pid namespace... I'm not sure which side effect this may bring!

Basically we have here a issue because other proc options (hidepid for example) affect the entire pid namespace, but, I guess, have to affect the mountpoint.

# grep ^proc /proc/mounts 
proc /proc proc rw,relatime 0 0
# mount -t proc proc /tmp/proc
# mount -o remount,hidepid=2 -t proc proc /tmp/proc
# grep ^proc /proc/mounts 
proc /proc proc rw,relatime,hidepid=2 0 0
proc /tmp/proc proc rw,relatime,hidepid=2 0 0
Signature · 1 line
Rgrds, legion
#19 lore raw

Re: [RFC] Add option to mount only a pids subset

In reply to Andy Lutomirski
Quoted 11 lines · Andy Lutomirski
Quoted 6 lines · Alexey Gladkov

After discussion with Oleg Nesterov I reimplement my patch as an additional option for /proc. This option affects the mountpoint. It means that in one pid namespace it possible to have both the whole traditional /proc and /proc with only pids subset.

I like this. I think you should split it into two patches, though:
one that reworks how procfs gets mounted and one that makes adds the new functionality.

Sure, but first I wanted to discuss the idea. My patch isn't very useful without the ability to add additional files.

I will split it into parts in the new version.

Signature · 1 line
Rgrds, legion
#20 lore raw

Re: [RFC] Add option to mount only a pids subset

In reply to Alexey Gladkov

I can't really review this... but in any case I think you should split this patch to separate the vfs and proc changes.

fs/namespace.c vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void · line 972

Quoted 12 lines · Alexey Gladkov
fs/namespace.c +7 -0
@@ -962,6 +963,14 @@ vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void
 	mnt->mnt.mnt_sb = root->d_sb;
 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
 	mnt->mnt_parent = mnt;
+
+	err = do_mount_sb(&mnt->mnt, flags, data);
+	if(err) {
+		mnt_free_id(mnt);
+		free_vfsmnt(mnt);
+		return ERR_PTR(err);
+	}

This duplicates the error handling, we do the same if mount_fs() fails. Perhaps you should move these 2 lines into cleanup block and add goto's.

fs/proc/root.c int proc_remount(struct super_block *sb, int *flags, char *data) · line 110

Quoted 8 lines
fs/proc/root.c +8 -0
+int proc_getattrfs(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
+{
+	struct inode *inode = d_inode(dentry);
+	struct pid *pid = proc_pid(dentry->d_inode);
+	struct proc_options *opts = mnt->fs_data;
+
+	if (opts && opts->pid_only && mnt->mnt_root != dentry && !pid)
+		return -ENOENT;

Hmm. I don't quite understand why do we need this, and how this should work.

Yes, "/bin/ls /pidonly-proc/sys" or opendir(/pidonly-proc/sys) should fail, but only because they both do stat() ?

Afaics you still can do open("/pidonly-proc/sys") + getdents() and this should work ?

I still think proc_dir_operations.open() makes more sense. Yes, as you pointed out we also need to update proc_sys_dir_file_operations too and may be something else...

fs/proc/root.c int proc_remount(struct super_block *sb, int *flags, char *data) · line 118

Quoted 8 lines
fs/proc/root.c +8 -0
+
+	if (!inode->i_op->getattr) {
+		generic_fillattr(inode, stat);
+		return 0;
+	}
+
+	return inode->i_op->getattr(mnt, dentry, stat);
+}

Oh, it would be nice to not duplicate the code from the caller, imo.

Oleg.

#21 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 31 lines · Oleg Nesterov

I can't really review this... but in any case I think you should split this patch to separate the vfs and proc changes.

fs/namespace.c vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void · line 972

Quoted 12 lines · Alexey Gladkov
fs/namespace.c +7 -0
@@ -962,6 +963,14 @@ vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void
 	mnt->mnt.mnt_sb = root->d_sb;
 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
 	mnt->mnt_parent = mnt;
+
+	err = do_mount_sb(&mnt->mnt, flags, data);
+	if(err) {
+		mnt_free_id(mnt);
+		free_vfsmnt(mnt);
+		return ERR_PTR(err);
+	}

This duplicates the error handling, we do the same if mount_fs() fails. Perhaps you should move these 2 lines into cleanup block and add goto's.

fs/proc/root.c int proc_remount(struct super_block *sb, int *flags, char *data) · line 110

Quoted 8 lines
fs/proc/root.c +8 -0
+int proc_getattrfs(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
+{
+	struct inode *inode = d_inode(dentry);
+	struct pid *pid = proc_pid(dentry->d_inode);
+	struct proc_options *opts = mnt->fs_data;
+
+	if (opts && opts->pid_only && mnt->mnt_root != dentry && !pid)
+		return -ENOENT;

Hmm. I don't quite understand why do we need this, and how this should work.

Quoted 5 lines

Yes, "/bin/ls /pidonly-proc/sys" or opendir(/pidonly-proc/sys) should fail, but only because they both do stat() ?

Afaics you still can do open("/pidonly-proc/sys") + getdents() and this should work ?

Yes, you're right! I thought that getattr is called always together with open(). I wanted to prevent all attempts open() for not-pid directories.

Quoted 3 lines

I still think proc_dir_operations.open() makes more sense. Yes, as you pointed out we also need to update proc_sys_dir_file_operations too and may be something else...

My main task was to hide all possible direcitrices from the /proc (in pidonly mode)... even those which we do not know. In this case we can't rely on the fact that everyone will follow the rules and to properly handle open().

My current attempt was to force filesystem level check of mountpoint flag. This is necessary to avoid even the theoretical possibility of ignoring "pidonly" parameter.

I guess I need to add callback to vfs_open or something to can be sure that we will not open the wrong file or directory in pidonly mode.

Signature · 1 line
Rgrds, legion
maintainer
#22 lore raw

Re: [RFC] Add option to mount only a pids subset

In reply to Alexey Gladkov
Quoted 18 lines · Alexey Gladkov
include/linux/fs.h +2 -0
index 83de8b6..5bd1b84 100644
@@ -1759,6 +1759,8 @@ struct super_operations {
 	int (*thaw_super) (struct super_block *);
 	int (*unfreeze_fs) (struct super_block *);
 	int (*statfs) (struct dentry *, struct kstatfs *);
+	int (*getattr_fs) (struct vfsmount *, struct dentry *, struct kstat *);
+	int (*mount_fs) (struct vfsmount *, int *, char *);
 	int (*remount_fs) (struct super_block *, int *, char *);
 	void (*umount_begin) (struct super_block *);
 
include/linux/mount.h +0 -0
index 1172cce..4bd364e 100644

include/linux/mount.h struct vfsmount { · line 70

Quoted 4 lines
include/linux/mount.h +1 -0
@@ -67,6 +67,7 @@ struct vfsmount {
 	struct dentry *mnt_root;	/* root of the mounted tree */
 	struct super_block *mnt_sb;	/* pointer to superblock */
 	int mnt_flags;
+	void *fs_data;			/* fs-specific data */

No. This is really asking for trouble - you *can't* hang
fs-specific data structures off vfsmount. Lifetimes make no sense whatsoever.

BTW, your patch leaks supreblock reference on failure, and
is kludgy as hell wrt to what it's doing in procfs itself, but that's secondary - the quoted part is enough for a hard NAK on architectural grounds. Don't go there.

maintainer
#23 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 29 lines · Al Viro
Quoted 18 lines · Alexey Gladkov
include/linux/fs.h +2 -0
index 83de8b6..5bd1b84 100644
@@ -1759,6 +1759,8 @@ struct super_operations {
 	int (*thaw_super) (struct super_block *);
 	int (*unfreeze_fs) (struct super_block *);
 	int (*statfs) (struct dentry *, struct kstatfs *);
+	int (*getattr_fs) (struct vfsmount *, struct dentry *, struct kstat *);
+	int (*mount_fs) (struct vfsmount *, int *, char *);
 	int (*remount_fs) (struct super_block *, int *, char *);
 	void (*umount_begin) (struct super_block *);
 
include/linux/mount.h +0 -0
index 1172cce..4bd364e 100644

include/linux/mount.h struct vfsmount { · line 70

Quoted 4 lines
include/linux/mount.h +1 -0
@@ -67,6 +67,7 @@ struct vfsmount {
 	struct dentry *mnt_root;	/* root of the mounted tree */
 	struct super_block *mnt_sb;	/* pointer to superblock */
 	int mnt_flags;
+	void *fs_data;			/* fs-specific data */

No. This is really asking for trouble - you *can't* hang
fs-specific data structures off vfsmount. Lifetimes make no sense whatsoever.

Quoted 4 lines

BTW, your patch leaks supreblock reference on failure, and
is kludgy as hell wrt to what it's doing in procfs itself, but that's secondary - the quoted part is enough for a hard NAK on architectural grounds. Don't go there.

PS: AFAICS, simple mount --bind of your pid-only mount will suddenly expose the full thing. And as for the lifetimes making no sense... note that you are simply not freeing these structures of yours. Try to handle that and you'll get a serious PITA all over the place.

What are you trying to achieve, anyway? Why not add a second vfsmount pointer per pid_namespace and make it initialized on demand, at the first attempt of no-pid mount? Just have a separate no-pid instance created for those namespaces where it had been asked for, with separate superblock and dentry tree not containing anything other that pid-only parts + self + thread-self...

#24 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 6 lines · Al Viro

PS: AFAICS, simple mount --bind of your pid-only mount will suddenly expose the full thing. And as for the lifetimes making no sense... note that you are simply not freeing these structures of yours. Try to handle that and you'll get a serious PITA all over the place.

Quoted 6 lines

What are you trying to achieve, anyway? Why not add a second vfsmount pointer per pid_namespace and make it initialized on demand, at the first attempt of no-pid mount? Just have a separate no-pid instance created for those namespaces where it had been asked for, with separate superblock and dentry tree not containing anything other that pid-only parts + self + thread-self...

Can't we just make procfs work like most other filesystems and have each mount have its own superblock? If we need to do something funky to stat() output to keep existing userspace working, I think that's okay.

As far as I can tell, proc_mnt is very nearly useless -- it seems to be used for proc_flush_task (which claims to be purely an optimization and could be preserved in the common case where there's only one relevant mount) and for sysctl_binary. For the latter, we could create proc_mnt but make actual user-initiated mounts be new superblocks anyway.

maintainer
#25 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 14 lines · Andy Lutomirski
Quoted 6 lines · Al Viro

PS: AFAICS, simple mount --bind of your pid-only mount will suddenly expose the full thing. And as for the lifetimes making no sense... note that you are simply not freeing these structures of yours. Try to handle that and you'll get a serious PITA all over the place.

Quoted 6 lines

What are you trying to achieve, anyway? Why not add a second vfsmount pointer per pid_namespace and make it initialized on demand, at the first attempt of no-pid mount? Just have a separate no-pid instance created for those namespaces where it had been asked for, with separate superblock and dentry tree not containing anything other that pid-only parts + self + thread-self...

Quoted 4 lines

Can't we just make procfs work like most other filesystems and have each mount have its own superblock? If we need to do something funky to stat() output to keep existing userspace working, I think that's okay.

First of all, most of the filesystems do *NOT* guarantee anything of that sort. And what's the point of having more instances than necessary, anyway?

Quoted 6 lines

As far as I can tell, proc_mnt is very nearly useless -- it seems to be used for proc_flush_task (which claims to be purely an optimization and could be preserved in the common case where there's only one relevant mount) and for sysctl_binary. For the latter, we could create proc_mnt but make actual user-initiated mounts be new superblocks anyway.

Again, what for? It won't salvage that kludge... It's not as if it had been hard to have separate pid-only instance created when asked for (and reused every time when we are asked for pid-only). What's the point of ever having more than two instances per pidns? IDGI...

Folks, there is no one-to-one correspondence between mountpoints and superblocks. Not since 2000 or so. Just don't try to shove your per-superblock stuff into vfsmount; it simply won't work. If you want a separate instance for that thing, then just go ahead and have ->mount() decide which one to use (and whether to create a new one). All there is to it...

#26 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 15 lines · Al Viro
Quoted 14 lines · Andy Lutomirski
Quoted 12 lines · Al Viro

PS: AFAICS, simple mount --bind of your pid-only mount will suddenly expose the full thing. And as for the lifetimes making no sense... note that you are simply not freeing these structures of yours. Try to handle that and you'll get a serious PITA all over the place.

What are you trying to achieve, anyway? Why not add a second vfsmount pointer per pid_namespace and make it initialized on demand, at the first attempt of no-pid mount? Just have a separate no-pid instance created for those namespaces where it had been asked for, with separate superblock and dentry tree not containing anything other that pid-only parts + self + thread-self...

Quoted 8 lines
Quoted 4 lines

Can't we just make procfs work like most other filesystems and have each mount have its own superblock? If we need to do something funky to stat() output to keep existing userspace working, I think that's okay.

First of all, most of the filesystems do *NOT* guarantee anything of that sort. And what's the point of having more instances than necessary, anyway?

I mean that, if I do:

mount -t proc -o foobar none a
mount -t proc -o baz none b

Then I think that the second mount should create a whole new proc instance rather than just a new vfsmount. Then the options could differ, which would solve a bunch of problems.

Quoted 5 lines

Again, what for? It won't salvage that kludge... It's not as if it had been hard to have separate pid-only instance created when asked for (and reused every time when we are asked for pid-only). What's the point of ever having more than two instances per pidns? IDGI...

I can easily procfs growing more than one interesting option. Pid-only and hidepid come to mind, and that's already six possible combinations. The current hidepid implementation is really awful.

Quoted 7 lines

Folks, there is no one-to-one correspondence between mountpoints and superblocks. Not since 2000 or so. Just don't try to shove your per-superblock stuff into vfsmount; it simply won't work. If you want a separate instance for that thing, then just go ahead and have ->mount() decide which one to use (and whether to create a new one). All there is to it...

That's what I mean. I just don't see the point of going all-out in trying to reuse superblocks.

#27 lore raw

PATCH 1/1 proc: allow to change proc mount options per mount

As per the discussion with Andy, and following what Al Viro suggested maybe this can work ? the patch is still buggy on top of Linus' tree 093b995e3b

Currently hidepid mount option is propagated to all proc mounts that are in the same pid namespace. This patch make it possible to have proc mounts with different options inside the same pid namespace.

Since this may break userspace or code that checks/expects some device identifiers, this mode is only supported when theo ption "version=2" is passed.

[Buggy patch]

[tixxdz@fedora-kvm linux]$ sudo mount -t proc none /test [tixxdz@fedora-kvm linux]$ sudo mount -t proc -o hidepid=2,version=2 none /test2 [tixxdz@fedora-kvm linux]$ ls -l /proc | head -6
total 0

dr-xr-xr-x.  9 root    root                  0 Mar 23 17:25 1
dr-xr-xr-x.  9 root    root                  0 Mar 23 17:25 10
dr-xr-xr-x.  9 root    root                  0 Mar 23 17:25 100
dr-xr-xr-x.  9 gdm     gdm                   0 Mar 23 17:25 1005
dr-xr-xr-x.  9 root    root                  0 Mar 23 17:25 101
[tixxdz@fedora-kvm linux]$ ls -l /test | head -16
total 0
dr-xr-xr-x.  9 root    root                  0 Mar 23 17:25 1
dr-xr-xr-x.  9 root    root                  0 Mar 23 17:25 10
dr-xr-xr-x.  9 root    root                  0 Mar 23 17:25 100
dr-xr-xr-x.  9 gdm     gdm                   0 Mar 23 17:25 1005
dr-xr-xr-x.  9 root    root                  0 Mar 23 17:25 101
[tixxdz@fedora-kvm linux]$ ls -l /test2 | head -6
total 0
dr-xr-xr-x.  9 tixxdz tixxdz               0 Mar 23 17:27 1182
dr-xr-xr-x.  9 tixxdz tixxdz               0 Mar 23 17:27 1197
dr-xr-xr-x.  9 tixxdz tixxdz               0 Mar 23 17:27 1199
dr-xr-xr-x.  9 tixxdz tixxdz               0 Mar 23 17:27 1222
dr-xr-xr-x.  9 tixxdz tixxdz               0 Mar 23 17:27 1225
---
 fs/locks.c              |   6 +-
 fs/proc/base.c          |  51 +++++++++-----
 fs/proc/generic.c       |   5 ++
 fs/proc/inode.c         |  10 +--
 fs/proc/root.c          | 181 ++++++++++++++++++++++++++++++++++++++++++++++--
 fs/proc/self.c          |   8 ++-
 fs/proc/thread_self.c   |   6 +-
 fs/proc_namespace.c     |  14 ++--
 include/linux/proc_fs.h |   9 +++
 9 files changed, 248 insertions(+), 42 deletions(-)
fs/locks.c +4 -2
index 2681132..dab5058 100644
@@ -2617,7 +2617,8 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl,
 	unsigned int fl_pid;
 
 	if (fl->fl_nspid) {
-		struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
+		struct proc_fs_info *fs_info = proc_sb(file_inode(f->file)->i_sb);
+		struct pid_namespace *proc_pidns = fs_info->pid_ns;
 
 		/* Don't let fl_pid change based on who is reading the file */
 		fl_pid = pid_nr_ns(fl->fl_nspid, proc_pidns);
@@ -2701,7 +2702,8 @@ static int locks_show(struct seq_file *f, void *v)
 {
 	struct locks_iterator *iter = f->private;
 	struct file_lock *fl, *bfl;
-	struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
+	struct proc_fs_info *fs_info = proc_sb(file_inode(f->file)->i_sb);
+	struct pid_namespace *proc_pidns = fs_info->pid_ns;
 
 	fl = hlist_entry(v, struct file_lock, fl_link);
 
fs/proc/base.c +33 -18

154 lines not on this page · raw message

fs/proc/generic.c +5 -0
index ee27feb..49c8cb9 100644
@@ -28,6 +28,11 @@
 
 static DEFINE_RWLOCK(proc_subdir_lock);
 
+struct proc_fs_info *proc_sb(struct super_block *sb)
+{
+	return sb->s_fs_info;
+}
+
 static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
 {
 	if (len < de->namelen)
fs/proc/inode.c +6 -4
index 2cc7a80..4743943 100644
@@ -103,7 +103,8 @@ void __init proc_init_inodecache(void)
 static int proc_show_options(struct seq_file *seq, struct dentry *root)
 {
 	struct super_block *sb = root->d_sb;
-	struct pid_namespace *pid = sb->s_fs_info;
+	struct proc_fs_info *fs_info = proc_sb(sb);
+	struct pid_namespace *pid = fs_info->pid_ns;
 
 	if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
 		seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
@@ -473,11 +474,12 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
 
 int proc_fill_super(struct super_block *s, void *data, int silent)
 {
-	struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
+	struct proc_fs_info *fs_info = proc_sb(s);
+	struct pid_namespace *ns = get_pid_ns(fs_info->pid_ns);
 	struct inode *root_inode;
 	int ret;
 
-	if (!proc_parse_options(data, ns))
+	if (fs_info->version == 1 && !proc_parse_options(data, ns))
 		return -EINVAL;
 
 	/* User space would break if executables or devices appear on proc */
@@ -495,7 +497,7 @@ int proc_fill_super(struct super_block *s, void *data, int silent)
 	 * top of it
 	 */
 	s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
-	
+
 	pde_get(&proc_root);
 	root_inode = proc_get_inode(s, &proc_root);
 	if (!root_inode) {
fs/proc/root.c +175 -6

239 lines not on this page · raw message

fs/proc/self.c +5 -3
index 39857f6..9f95174 100644
@@ -10,7 +10,8 @@ static const char *proc_self_get_link(struct dentry *dentry,
 				      struct inode *inode,
 				      struct delayed_call *done)
 {
-	struct pid_namespace *ns = inode->i_sb->s_fs_info;
+	struct proc_fs_info *fs_info = proc_sb(inode->i_sb);
+	struct pid_namespace *ns = fs_info->pid_ns;
 	pid_t tgid = task_tgid_nr_ns(current, ns);
 	char *name;
 
@@ -34,9 +35,10 @@ static unsigned self_inum;
 int proc_setup_self(struct super_block *s)
 {
 	struct inode *root_inode = d_inode(s->s_root);
-	struct pid_namespace *ns = s->s_fs_info;
+	struct proc_fs_info *fs_info = proc_sb(s);
+	struct pid_namespace *ns = fs_info->pid_ns;
 	struct dentry *self;
-	
+
 	inode_lock(root_inode);
 	self = d_alloc_name(s->s_root, "self");
 	if (self) {
fs/proc/thread_self.c +4 -2
index 20614b6..13d9aef 100644
@@ -10,7 +10,8 @@ static const char *proc_thread_self_get_link(struct dentry *dentry,
 					     struct inode *inode,
 					     struct delayed_call *done)
 {
-	struct pid_namespace *ns = inode->i_sb->s_fs_info;
+	struct proc_fs_info *fs_info = proc_sb(inode->i_sb);
+	struct pid_namespace *ns = fs_info->pid_ns;
 	pid_t tgid = task_tgid_nr_ns(current, ns);
 	pid_t pid = task_pid_nr_ns(current, ns);
 	char *name;
@@ -34,8 +35,9 @@ static unsigned thread_self_inum;
 
 int proc_setup_thread_self(struct super_block *s)
 {
+	struct proc_fs_info *fs_info = proc_sb(s);
+	struct pid_namespace *ns = fs_info->pid_ns;
 	struct inode *root_inode = d_inode(s->s_root);
-	struct pid_namespace *ns = s->s_fs_info;
 	struct dentry *thread_self;
 
 	inode_lock(root_inode);
fs/proc_namespace.c +7 -7
index b5713fe..d0ae937 100644
@@ -36,23 +36,23 @@ static unsigned mounts_poll(struct file *file, poll_table *wait)
 	return res;
 }
 
-struct proc_fs_info {
+struct proc_fs_opts {
 	int flag;
 	const char *str;
 };
 
 static int show_sb_opts(struct seq_file *m, struct super_block *sb)
 {
-	static const struct proc_fs_info fs_info[] = {
+	static const struct proc_fs_opts fs_opts[] = {
 		{ MS_SYNCHRONOUS, ",sync" },
 		{ MS_DIRSYNC, ",dirsync" },
 		{ MS_MANDLOCK, ",mand" },
 		{ MS_LAZYTIME, ",lazytime" },
 		{ 0, NULL }
 	};
-	const struct proc_fs_info *fs_infop;
+	const struct proc_fs_opts *fs_infop;
 
-	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
+	for (fs_infop = fs_opts; fs_infop->flag; fs_infop++) {
 		if (sb->s_flags & fs_infop->flag)
 			seq_puts(m, fs_infop->str);
 	}
@@ -62,7 +62,7 @@ static int show_sb_opts(struct seq_file *m, struct super_block *sb)
 
 static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
 {
-	static const struct proc_fs_info mnt_info[] = {
+	static const struct proc_fs_opts mnt_opts[] = {
 		{ MNT_NOSUID, ",nosuid" },
 		{ MNT_NODEV, ",nodev" },
 		{ MNT_NOEXEC, ",noexec" },
@@ -71,9 +71,9 @@ static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
 		{ MNT_RELATIME, ",relatime" },
 		{ 0, NULL }
 	};
-	const struct proc_fs_info *fs_infop;
+	const struct proc_fs_opts *fs_infop;
 
-	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
+	for (fs_infop = mnt_opts; fs_infop->flag; fs_infop++) {
 		if (mnt->mnt_flags & fs_infop->flag)
 			seq_puts(m, fs_infop->str);
 	}
include/linux/proc_fs.h +9 -0
index 2d2bf59..27a1e85 100644
@@ -7,10 +7,18 @@
 #include <linux/types.h>
 #include <linux/fs.h>
 
+struct proc_fs_info {
+	struct pid_namespace *pid_ns;
+	int version;
+	kgid_t pid_gid;
+	int hide_pid;
+};
+
 struct proc_dir_entry;
 
 #ifdef CONFIG_PROC_FS
 
+extern struct proc_fs_info *proc_sb(struct super_block *sb);
 extern void proc_root_init(void);
 extern void proc_flush_task(struct task_struct *);
 
@@ -53,6 +61,7 @@ static inline void proc_flush_task(struct task_struct *task)
 {
 }
 
+extern inline struct proc_fs_info *proc_sb(struct super_block *sb) { return NULL;}
 static inline struct proc_dir_entry *proc_symlink(const char *name,
 		struct proc_dir_entry *parent,const char *dest) { return NULL;}
 static inline struct proc_dir_entry *proc_mkdir(const char *name,
Signature · 1 line
2.10.2
#28 lore raw

RFC 1/1 Add option to mount only a pids subset

In reply to Al Viro

Al Viro, this patch looks better ?

== Overview ==

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/sys, /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an option for /proc, flags applied to /proc have an effect on this subset of filesystem.

Originally the idea was that the container will be mounted only pid sunset and additional required files will be mounted on top using the overlayfs.

But I found out that /proc does not support overlayfs and does not allow to mount anything on top or under it.

== TODO ==

There is still work to do:

* Add overlayfs support.

---
 fs/proc/generic.c             |   5 ++
 fs/proc/inode.c               |   2 +
 fs/proc/internal.h            |   7 +++
 fs/proc/root.c                | 113 ++++++++++++++++++++++++++++++++++++++++--
 include/linux/pid_namespace.h |   1 +
 5 files changed, 123 insertions(+), 5 deletions(-)
fs/proc/generic.c +5 -0
index ee27feb..50bb1e9 100644
@@ -23,6 +23,7 @@
 #include <linux/spinlock.h>
 #include <linux/completion.h>
 #include <linux/uaccess.h>
+#include <linux/pid_namespace.h>
 
 #include "internal.h"
 
@@ -307,6 +308,10 @@ int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
 int proc_readdir(struct file *file, struct dir_context *ctx)
 {
 	struct inode *inode = file_inode(file);
+	struct pid_namespace *ns = inode->i_sb->s_fs_info;
+
+	if (ns->pidfs && inode == d_inode(ns->pidfs))
+		return 1;
 
 	return proc_readdir_de(PDE(inode), file, ctx);
 }
fs/proc/inode.c +2 -0
index 2cc7a80..0c9be65 100644
@@ -109,6 +109,8 @@ static int proc_show_options(struct seq_file *seq, struct dentry *root)
 		seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
 	if (pid->hide_pid != HIDEPID_OFF)
 		seq_printf(seq, ",hidepid=%u", pid->hide_pid);
+	if (root == pid->pidfs)
+		seq_printf(seq, ",pidonly");
 
 	return 0;
 }
fs/proc/internal.h +7 -0
index c5ae09b..a5a4bf1 100644
@@ -260,7 +260,14 @@ static inline void proc_tty_init(void) {}
 /*
  * root.c
  */
+struct proc_options {
+	kgid_t pid_gid;
+	int hide_pid;
+	int pid_only;
+};
+
 extern struct proc_dir_entry proc_root;
+extern struct proc_dir_entry pidfs_root;
 extern int proc_parse_options(char *options, struct pid_namespace *pid);
 
 extern void proc_self_init(void);
fs/proc/root.c +108 -5

169 lines not on this page · raw message

include/linux/pid_namespace.h +1 -0
index c2a989d..e7b4d64 100644
@@ -41,6 +41,7 @@ struct pid_namespace {
 	struct vfsmount *proc_mnt;
 	struct dentry *proc_self;
 	struct dentry *proc_thread_self;
+	struct dentry *pidfs;
 #endif
 #ifdef CONFIG_BSD_PROCESS_ACCT
 	struct fs_pin *bacct;
Signature · 1 line
2.10.2
#29 lore raw

Re: [RFC] Add option to mount only a pids subset

Again, I can't really review this, I know nothing about vfs, but since nobody else replied...

fs/proc/root.c static struct dentry *proc_mount(struct file_system_type *fs_type, · line 176

Quoted 11 lines · Alexey Gladkov
fs/proc/root.c +5 -1
@@ -97,7 +169,23 @@ static struct dentry *proc_mount(struct file_system_type *fs_type,
 		ns = task_active_pid_ns(current);
 	}
 
-	return mount_ns(fs_type, flags, data, ns, ns->user_ns, proc_fill_super);
+	root = mount_ns(fs_type, flags, data, ns, ns->user_ns, proc_fill_super);
+
+	if (!IS_ERR(root)) {
+		if (!proc_fill_options(data, &opts))
+			return ERR_PTR(-EINVAL);

So we have to call proc_fill_options() twice, not good... Yes, I understand why, but perhaps we factor it out somehow, we can pack options + pid_ns into sb->s_fs_info. Nevermind, this is minor.

fs/proc/root.c static struct dentry *proc_mount(struct file_system_type *fs_type, · line 184

Quoted 7 lines
fs/proc/root.c +7 -0
+		if (opts.pid_only) {
+			int ret;
+
+			if (!ns->pidfs && (ret = fill_pidfs_root(root->d_sb)))
+				return ERR_PTR(ret);
+
+			root = ns->pidfs;

Afaics this lacks dget(ns->pidfs) which should pair with dput(mnt.mnt_root) in cleanup_mnt(). IIUC otherwise ns->pidfs can go away after umount, OTOH, if we return ns->pidfs then dget(sb->s_root) in mount_ns() is not balanced. But this all is fixeable.

So with this change "mount -opidonly" creates another IS_ROOT() dentry which is not equal to sb->s_root. I simply do not know if this is technically correct or not... but, say, the "Only bind mounts can have disconnected paths" comment in path_connected() makes me worry ;)

And this obviously means that /path-to-pidonly-mnt/ won't share dentries with the normal /proc mount. Not really good imo even if not really wrong... Lets look at proc_flush_task(). The exiting task will flush its $pid dentries in /proc/ but not in /path-to-pidonly-mnt/ iiuc. Again, not really a bug, but still...

Oleg.

#30 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 2 lines · Oleg Nesterov

Again, I can't really review this, I know nothing about vfs, but since nobody else replied...

Thanks anyway :)

Quoted 6 lines · Alexey Gladkov
Quoted 5 lines
fs/proc/root.c +0 -0
@@ -97,7 +169,23 @@ static struct dentry *proc_mount(struct file_system_type *fs_type,
 		ns = task_active_pid_ns(current);
 	}
 
Quoted 10 lines

fs/proc/root.c static struct dentry *proc_mount(struct file_system_type *fs_type, · line 176

Quoted 6 lines
fs/proc/root.c +5 -1
-	return mount_ns(fs_type, flags, data, ns, ns->user_ns, proc_fill_super);
+	root = mount_ns(fs_type, flags, data, ns, ns->user_ns, proc_fill_super);
+
+	if (!IS_ERR(root)) {
+		if (!proc_fill_options(data, &opts))
+			return ERR_PTR(-EINVAL);

So we have to call proc_fill_options() twice, not good... Yes, I understand why, but perhaps we factor it out somehow, we can pack options + pid_ns into sb->s_fs_info. Nevermind, this is minor.

It happens only once, when we don't have the s_root yet.

Quoted 18 lines

fs/proc/root.c static struct dentry *proc_mount(struct file_system_type *fs_type, · line 184

Quoted 7 lines
fs/proc/root.c +7 -0
+		if (opts.pid_only) {
+			int ret;
+
+			if (!ns->pidfs && (ret = fill_pidfs_root(root->d_sb)))
+				return ERR_PTR(ret);
+
+			root = ns->pidfs;

Afaics this lacks dget(ns->pidfs) which should pair with dput(mnt.mnt_root) in cleanup_mnt(). IIUC otherwise ns->pidfs can go away after umount, OTOH, if we return ns->pidfs then dget(sb->s_root) in mount_ns() is not balanced. But this all is fixeable.

So with this change "mount -opidonly" creates another IS_ROOT() dentry which is not equal to sb->s_root. I simply do not know if this is technically correct or not... but, say, the "Only bind mounts can have disconnected paths" comment in path_connected() makes me worry ;)

Quoted 5 lines

And this obviously means that /path-to-pidonly-mnt/ won't share dentries with the normal /proc mount. Not really good imo even if not really wrong... Lets look at proc_flush_task(). The exiting task will flush its $pid dentries in /proc/ but not in /path-to-pidonly-mnt/ iiuc. Again, not really a bug, but still...

I know that I'm cheater, but I did not start first :)

Signature · 1 line
Rgrds, legion
#31 lore raw

Re: [RFC] Add option to mount only a pids subset

In reply to Alexey Gladkov

Hi Alexey,

Quoted 24 lines · Alexey Gladkov

Al Viro, this patch looks better ?

== Overview ==

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/sys, /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Quoted 6 lines

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an option for /proc, flags applied to /proc have an effect on this subset of filesystem.

I just sent a patch that also has to deal with proc hidepid here:
https://lkml.org/lkml/2017/3/23/505

I'm not sure if that's the right approach, it is still buggy, however seems that your patch also stores the mount option inside the pid_namespace which may get propagated to all mounts inside same pidns ?

I didn't have enough time but maybe if they are related we can work it out together ?

Thank you!

Signature · 1 line
tixxdz
#32 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 31 lines · Djalal Harouni

Hi Alexey,

Quoted 27 lines · Alexey Gladkov

Al Viro, this patch looks better ?

== Overview ==

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/sys, /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

Quoted 6 lines
Quoted 3 lines

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an option for /proc, flags applied to /proc have an effect on this subset of filesystem.

I just sent a patch that also has to deal with proc hidepid here:
https://lkml.org/lkml/2017/3/23/505

I completely agree with you that it looks wrong when options of one mountpoint affect the others mountpoints.

Quoted 4 lines

I'm not sure if that's the right approach, it is still buggy, however seems that your patch also stores the mount option inside the pid_namespace which may get propagated to all mounts inside same pidns ?

I don't store 'pidonly' option in my current patch. I mean in:
https://lkml.org/lkml/2017/3/20/363

I parse options twice at first mount of procfs. It happens before the mounting /proc in userspace.

I know it's bad, but I couldn't find place to store this option.

Quoted 2 lines

I didn't have enough time but maybe if they are related we can work it out together ?

I don't have enough experience in kenrel hacking, but I would happily do my best :)

I also tring to mention it in every patch, as my changes almost completely useless without the ability to use the overlayfs.

Now if you remove the restriction:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/inode.c#n497

and mount procfs as lowerdir in overlayfs then you get NULL pointer dereference at:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/namei.c#n891

I got it when I tried to do ls -la /overlay/proc/self/.

Signature · 1 line
Rgrds, legion
#33 lore raw

Re: [RFC] Add option to mount only a pids subset

(Cc'ed Kees and Jann for the procfs stacking issue)

Quoted 47 lines · Alexey Gladkov
Quoted 31 lines · Djalal Harouni

Hi Alexey,

Quoted 27 lines · Alexey Gladkov

Al Viro, this patch looks better ?

== Overview ==

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/sys, /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

Quoted 6 lines
Quoted 3 lines

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an option for /proc, flags applied to /proc have an effect on this subset of filesystem.

I just sent a patch that also has to deal with proc hidepid here:
https://lkml.org/lkml/2017/3/23/505

I completely agree with you that it looks wrong when options of one mountpoint affect the others mountpoints.

Quoted 4 lines

I'm not sure if that's the right approach, it is still buggy, however seems that your patch also stores the mount option inside the pid_namespace which may get propagated to all mounts inside same pidns ?

Quoted 7 lines

I don't store 'pidonly' option in my current patch. I mean in:
https://lkml.org/lkml/2017/3/20/363

I parse options twice at first mount of procfs. It happens before the mounting /proc in userspace.

I know it's bad, but I couldn't find place to store this option.

Ok, then maybe that approach of having a procfs struct holder instead of using pid namespace may help!

Quoted 5 lines
Quoted 2 lines

I didn't have enough time but maybe if they are related we can work it out together ?

I don't have enough experience in kenrel hacking, but I would happily do my best :)

OK, let's sync on this then.

Quoted 6 lines

I also tring to mention it in every patch, as my changes almost completely useless without the ability to use the overlayfs.

Now if you remove the restriction:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/inode.c#n497

This protection was introduced in e54ad7f1ee263

That's a security fix, ecryptfs seems to schedule some code through kthreads which always run in the initial namespaces with full capabilities, bypassing all security checks.

I don't know if overlayfs has something similar, if not then maybe if it's possible in ecryptfs to check the lower mount and the fs type and move this procfs there... Cc'ed Jann in case he may comment.

Quoted 9 lines

and mount procfs as lowerdir in overlayfs then you get NULL pointer dereference at:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/namei.c#n891

I got it when I tried to do ls -la /overlay/proc/self/.

--
Rgrds, legion

#34 lore raw

Re: [RFC] Add option to mount only a pids subset

Quoted 13 lines · Djalal Harouni

(Cc'ed Kees and Jann for the procfs stacking issue)

Quoted 11 lines

I completely agree with you that it looks wrong when options of one mountpoint affect the others mountpoints.

Quoted 4 lines

I'm not sure if that's the right approach, it is still buggy, however seems that your patch also stores the mount option inside the pid_namespace which may get propagated to all mounts inside same pidns ?

I don't store 'pidonly' option in my current patch. I mean in:
https://lkml.org/lkml/2017/3/20/363

Quoted 7 lines
Quoted 4 lines

I parse options twice at first mount of procfs. It happens before the mounting /proc in userspace.

I know it's bad, but I couldn't find place to store this option.

Ok, then maybe that approach of having a procfs struct holder instead of using pid namespace may help!

I deside to stop doing my patch. I talked with a few people and found out that the overlayfs doesn't feel very well if on the lower level filesystem appear/disappear files.

In addition with the pidfs isn't so simple. Separate the root will lead to a doubling of memory consumption and restrictions on the filesystem operations level can easily be skipped.

It means that even I can do this pidfs (or pid subset in /proc), it will be pointless.

Signature · 1 line
Rgrds, legion
#35 lore raw
In reply to Alexey Gladkov

[CC += [email protected]]

Hi Alexey

This is a change to the kernel-user-space API. Please CC [email protected] on any future iterations of this patch.

Thanks,

Michael

Quoted 345 lines · Alexey Gladkov

The pidfs filesystem contains a subset of the /proc file system which contains only information about the processes.

Some of the container virtualization systems are mounted /proc inside the container. This is done in most cases to operate with information about the processes. Knowing that /proc filesystem is not fully virtualized they are mounted on top of dangerous places empty files or directories (for exmaple /proc/kcore, /sys/firmware, etc.).

The structure of this filesystem is dynamic and any module can create a new object which will not necessarily be virtualized. There are proprietary modules that aren't in the mainline whose work we can not verify.

This opens up a potential threat to the system. The developers of the virtualization system can't predict all dangerous places in /proc by definition.

A more effective solution would be to mount into the container only what is necessary and ignore the rest.

Right now there is the opportunity to pass in the container any port of the /proc filesystem using mount --bind expect the pids.

This patch allows to mount only the part of /proc related to pids without rest objects. Since this is an addon to /proc, flags applied to /proc have an effect on this pidfs filesystem.

Why not implement it as another flag to /proc ?

The /proc flags is stored in the pid_namespace and are global for namespace. It means that if you add a flag to hide all except the pids, then it will act on all mounted instances of /proc.

Originally the idea was that the container will be mounted only pidfs and additional required files will be mounted on top using the overlayfs. But I found out that /proc does not support overlayfs and does not allow to mount anything on top or under it.

My question is whether it's possible to add overlayfs support for /proc?

Cc Kirill A. Shutemov <[email protected]>
---
 Documentation/filesystems/pidfs.txt | 16 ++++++++
 fs/proc/Kconfig                     |  8 ++++
 fs/proc/inode.c                     |  8 +++-
 fs/proc/internal.h                  |  2 +
 fs/proc/root.c                      | 76 ++++++++++++++++++++++++++++++++++---
 fs/proc/self.c                      |  6 +++
 fs/proc/thread_self.c               |  6 +++
 include/linux/pid_namespace.h       |  5 +++
 8 files changed, 119 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/pidfs.txt
Documentation/filesystems/pidfs.txt new file +16 -0
new file mode 100644
index 0000000..ce958a5
@@ -0,0 +1,16 @@
+The PIDFS Filesystem
+====================
+
+The pidfs filesystem contains a subset of the /proc file system which contains
+only information about the processes. The link self points to the process
+reading the file system. All other special files and directories in /proc are
+not available in this filesystem.
+
+The pidfs is not an independent filesystem, its implementation shares code
+with /proc.
+
+All mount options applicable to /proc filesystem are also applicable
+to pidfs filesystem. For example, access to the information in /proc/[pid]
+directories can be restricted using hidepid option.
+
+To get more information about the processes read the proc.txt
fs/proc/Kconfig +8 -0
index 1ade120..fa568f6 100644
@@ -43,6 +43,14 @@ config PROC_VMCORE
         help
         Exports the dump image of crashed kernel in ELF format.
 
+config PROC_PIDFS
+       bool "pidfs file system support"
+       depends on PROC_FS
+       default n
+       help
+         The pidfs filesystem contains a subset of the /proc file system
+         which contains only information only about the processes.
+
 config PROC_SYSCTL
        bool "Sysctl support (/proc/sys)" if EXPERT
        depends on PROC_FS
fs/proc/inode.c +6 -2
index 783bc19..1be65b4 100644
@@ -474,12 +474,16 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
 int proc_fill_super(struct super_block *s, void *data, int silent)
 {
        struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
+       struct proc_dir_entry *fs_root = &proc_root;
        struct inode *root_inode;
        int ret;
 
        if (!proc_parse_options(data, ns))
                return -EINVAL;
 
+       if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type)
+               fs_root = &pidfs_root;
+
        /* User space would break if executables or devices appear on proc */
        s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
        s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
@@ -496,8 +500,8 @@ int proc_fill_super(struct super_block *s, void *data, int silent)
         */
        s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
 
-       pde_get(&proc_root);
-       root_inode = proc_get_inode(s, &proc_root);
+       pde_get(fs_root);
+       root_inode = proc_get_inode(s, fs_root);
        if (!root_inode) {
                pr_err("proc_fill_super: get root inode failed\n");
                return -ENOMEM;
fs/proc/internal.h +2 -0
index 2de5194..a7c068c 100644
@@ -267,6 +267,8 @@ static inline void proc_tty_init(void) {}
 /*
  * root.c
  */
+extern struct file_system_type pidfs_fs_type;
+extern struct proc_dir_entry pidfs_root;
 extern struct proc_dir_entry proc_root;
 extern int proc_parse_options(char *options, struct pid_namespace *pid);
 
fs/proc/root.c +70 -6
index 4bd0373..de16ac1 100644
@@ -102,10 +102,21 @@ static void proc_kill_sb(struct super_block *sb)
        struct pid_namespace *ns;
 
        ns = (struct pid_namespace *)sb->s_fs_info;
-       if (ns->proc_self)
-               dput(ns->proc_self);
-       if (ns->proc_thread_self)
-               dput(ns->proc_thread_self);
+
+       if (IS_ENABLED(CONFIG_PROC_PIDFS) && sb->s_type == &pidfs_fs_type) {
+               if (ns->pidfs_self)
+                       dput(ns->pidfs_self);
+
+               if (ns->pidfs_thread_self)
+                       dput(ns->pidfs_thread_self);
+       } else {
+               if (ns->proc_self)
+                       dput(ns->proc_self);
+
+               if (ns->proc_thread_self)
+                       dput(ns->proc_thread_self);
+       }
+
        kill_anon_super(sb);
        put_pid_ns(ns);
 }
@@ -117,6 +128,13 @@ static struct file_system_type proc_fs_type = {
        .fs_flags       = FS_USERNS_MOUNT,
 };
 
+struct file_system_type pidfs_fs_type = {
+       .name           = "pidfs",
+       .mount          = proc_mount,
+       .kill_sb        = proc_kill_sb,
+       .fs_flags       = FS_USERNS_MOUNT,
+};
+
 void __init proc_root_init(void)
 {
        int err;
@@ -127,6 +145,10 @@ void __init proc_root_init(void)
        if (err)
                return;
 
+       err = register_filesystem(&pidfs_fs_type);
+       if (err)
+               return;
+
        proc_self_init();
        proc_thread_self_init();
        proc_symlink("mounts", NULL, "self/mounts");
@@ -148,8 +170,7 @@ void __init proc_root_init(void)
        proc_sys_init();
 }
 
-static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
-)
+static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
 {
        generic_fillattr(d_inode(dentry), stat);
        stat->nlink = proc_root.nlink + nr_processes();
@@ -176,6 +197,14 @@ static int proc_root_readdir(struct file *file, struct dir_context *ctx)
        return proc_pid_readdir(file, ctx);
 }
 
+static int pidfs_root_readdir(struct file *file, struct dir_context *ctx)
+{
+       if (ctx->pos < FIRST_PROCESS_ENTRY)
+               ctx->pos = FIRST_PROCESS_ENTRY;
+
+       return proc_pid_readdir(file, ctx);
+}
+
 /*
  * The root /proc directory is special, as it has the
  * <pid> directories. Thus we don't use the generic
@@ -187,6 +216,12 @@ static const struct file_operations proc_root_operations = {
        .llseek         = generic_file_llseek,
 };
 
+static const struct file_operations pidfs_root_operations = {
+       .read            = generic_read_dir,
+       .iterate_shared  = pidfs_root_readdir,
+       .llseek         = generic_file_llseek,
+};
+
 /*
  * proc root can do almost nothing..
  */
@@ -195,6 +230,11 @@ static const struct inode_operations proc_root_inode_operations = {
        .getattr        = proc_root_getattr,
 };
 
+static const struct inode_operations pidfs_root_inode_operations = {
+       .lookup         = proc_pid_lookup,
+       .getattr        = proc_root_getattr,
+};
+
 /*
  * This is the root "inode" in the /proc tree..
  */
@@ -211,6 +251,19 @@ struct proc_dir_entry proc_root = {
        .name           = "/proc",
 };
 
+struct proc_dir_entry pidfs_root = {
+       .low_ino        = PROC_ROOT_INO,
+       .namelen        = 6,
+       .mode           = S_IFDIR | S_IRUGO | S_IXUGO,
+       .nlink          = 2,
+       .count          = ATOMIC_INIT(1),
+       .proc_iops      = &pidfs_root_inode_operations,
+       .proc_fops      = &pidfs_root_operations,
+       .parent         = &pidfs_root,
+       .subdir         = RB_ROOT,
+       .name           = "/pidfs",
+};
+
 int pid_ns_prepare_proc(struct pid_namespace *ns)
 {
        struct vfsmount *mnt;
@@ -220,10 +273,21 @@ int pid_ns_prepare_proc(struct pid_namespace *ns)
                return PTR_ERR(mnt);
 
        ns->proc_mnt = mnt;
+
+       if (IS_ENABLED(CONFIG_PROC_PIDFS)) {
+               mnt = kern_mount_data(&pidfs_fs_type, ns);
+               if (IS_ERR(mnt))
+                       return PTR_ERR(mnt);
+
+               ns->pidfs_mnt = mnt;
+       }
        return 0;
 }
 
 void pid_ns_release_proc(struct pid_namespace *ns)
 {
        kern_unmount(ns->proc_mnt);
+
+       if (IS_ENABLED(CONFIG_PROC_PIDFS))
+               kern_unmount(ns->pidfs_mnt);
 }
fs/proc/self.c +6 -0
index 4024595..dea7e17 100644
@@ -74,6 +74,12 @@ int proc_setup_self(struct super_block *s)
                pr_err("proc_fill_super: can't allocate /proc/self\n");
                return PTR_ERR(self);
        }
+
+       if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type) {
+               ns->pidfs_self = self;
+               return 0;
+       }
+
        ns->proc_self = self;
        return 0;
 }
fs/proc/thread_self.c +6 -0
index 595b90a97..274c618 100644
@@ -76,6 +76,12 @@ int proc_setup_thread_self(struct super_block *s)
                pr_err("proc_fill_super: can't allocate /proc/thread_self\n");
                return PTR_ERR(thread_self);
        }
+
+       if (IS_ENABLED(CONFIG_PROC_PIDFS) && s->s_type == &pidfs_fs_type) {
+               ns->pidfs_thread_self = thread_self;
+               return 0;
+       }
+
        ns->proc_thread_self = thread_self;
        return 0;
 }
include/linux/pid_namespace.h +5 -1
index 34cce96..fca3a76 100644
@@ -46,6 +46,11 @@ struct pid_namespace {
        int hide_pid;
        int reboot;     /* group exit code if this pidns was rebooted */
        struct ns_common ns;
+#ifdef CONFIG_PROC_PIDFS
+       struct vfsmount *pidfs_mnt;
+       struct dentry *pidfs_self;
+       struct dentry *pidfs_thread_self;
+#endif
 };
 
 extern struct pid_namespace init_pid_ns;
--

2.10.2

--
Rgrds, legion

Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer;
http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface", http://blog.man7.org/