Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

userns: Make seq_file's user namespace accessible

struct file already has a user namespace associated with it
in file->f_cred->user_ns, unfortunately because struct
seq_file has no struct file backpointer associated with
it, it is difficult to get at the user namespace in seq_file
context. Therefore add a helper function seq_user_ns to return
the associated user namespace and a user_ns field to struct
seq_file to be used in implementing seq_user_ns.

Cc: Al Viro <viro@ZenIV.linux.org.uk>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Acked-by: David S. Miller <davem@davemloft.net>
Acked-by: Serge Hallyn <serge.hallyn@canonical.com>
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>

+18
+4
fs/seq_file.c
··· 9 9 #include <linux/export.h> 10 10 #include <linux/seq_file.h> 11 11 #include <linux/slab.h> 12 + #include <linux/cred.h> 12 13 13 14 #include <asm/uaccess.h> 14 15 #include <asm/page.h> ··· 57 56 memset(p, 0, sizeof(*p)); 58 57 mutex_init(&p->lock); 59 58 p->op = op; 59 + #ifdef CONFIG_USER_NS 60 + p->user_ns = file->f_cred->user_ns; 61 + #endif 60 62 61 63 /* 62 64 * Wrappers around seq_open(e.g. swaps_open) need to be
+14
include/linux/seq_file.h
··· 13 13 struct path; 14 14 struct inode; 15 15 struct dentry; 16 + struct user_namespace; 16 17 17 18 struct seq_file { 18 19 char *buf; ··· 26 25 struct mutex lock; 27 26 const struct seq_operations *op; 28 27 int poll_event; 28 + #ifdef CONFIG_USER_NS 29 + struct user_namespace *user_ns; 30 + #endif 29 31 void *private; 30 32 }; 31 33 ··· 131 127 unsigned long long num); 132 128 int seq_put_decimal_ll(struct seq_file *m, char delimiter, 133 129 long long num); 130 + 131 + static inline struct user_namespace *seq_user_ns(struct seq_file *seq) 132 + { 133 + #ifdef CONFIG_USER_NS 134 + return seq->user_ns; 135 + #else 136 + extern struct user_namespace init_user_ns; 137 + return &init_user_ns; 138 + #endif 139 + } 134 140 135 141 #define SEQ_START_TOKEN ((void *)1) 136 142 /*