Skip to content

Commit adb37c4

Browse files
committed
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 <[email protected]> Cc: Eric Dumazet <[email protected]> Cc: KAMEZAWA Hiroyuki <[email protected]> Cc: Alexey Dobriyan <[email protected]> Acked-by: David S. Miller <[email protected]> Acked-by: Serge Hallyn <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]>
1 parent fc5795c commit adb37c4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

fs/seq_file.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/export.h>
1010
#include <linux/seq_file.h>
1111
#include <linux/slab.h>
12+
#include <linux/cred.h>
1213

1314
#include <asm/uaccess.h>
1415
#include <asm/page.h>
@@ -56,6 +57,9 @@ int seq_open(struct file *file, const struct seq_operations *op)
5657
memset(p, 0, sizeof(*p));
5758
mutex_init(&p->lock);
5859
p->op = op;
60+
#ifdef CONFIG_USER_NS
61+
p->user_ns = file->f_cred->user_ns;
62+
#endif
5963

6064
/*
6165
* Wrappers around seq_open(e.g. swaps_open) need to be

include/linux/seq_file.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct file;
1313
struct path;
1414
struct inode;
1515
struct dentry;
16+
struct user_namespace;
1617

1718
struct seq_file {
1819
char *buf;
@@ -25,6 +26,9 @@ struct seq_file {
2526
struct mutex lock;
2627
const struct seq_operations *op;
2728
int poll_event;
29+
#ifdef CONFIG_USER_NS
30+
struct user_namespace *user_ns;
31+
#endif
2832
void *private;
2933
};
3034

@@ -128,6 +132,16 @@ int seq_put_decimal_ull(struct seq_file *m, char delimiter,
128132
int seq_put_decimal_ll(struct seq_file *m, char delimiter,
129133
long long num);
130134

135+
static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
136+
{
137+
#ifdef CONFIG_USER_NS
138+
return seq->user_ns;
139+
#else
140+
extern struct user_namespace init_user_ns;
141+
return &init_user_ns;
142+
#endif
143+
}
144+
131145
#define SEQ_START_TOKEN ((void *)1)
132146
/*
133147
* Helpers for iteration over list_head-s in seq_files

0 commit comments

Comments
 (0)