user_namespace.h 3.7 KB
Newer Older
1 2 3 4 5
#ifndef _LINUX_USER_NAMESPACE_H
#define _LINUX_USER_NAMESPACE_H

#include <linux/kref.h>
#include <linux/nsproxy.h>
6
#include <linux/ns_common.h>
7
#include <linux/sched.h>
S
Serge E. Hallyn 已提交
8
#include <linux/err.h>
9

10 11 12 13 14 15 16 17 18 19 20
#define UID_GID_MAP_MAX_EXTENTS 5

struct uid_gid_map {	/* 64 bytes -- 1 cache line */
	u32 nr_extents;
	struct uid_gid_extent {
		u32 first;
		u32 lower_first;
		u32 count;
	} extent[UID_GID_MAP_MAX_EXTENTS];
};

21 22 23 24
#define USERNS_SETGROUPS_ALLOWED 1UL

#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED

25
struct ucounts;
26 27 28

enum ucount_type {
	UCOUNT_USER_NAMESPACES,
29
	UCOUNT_PID_NAMESPACES,
30
	UCOUNT_UTS_NAMESPACES,
31
	UCOUNT_IPC_NAMESPACES,
32
	UCOUNT_NET_NAMESPACES,
33
	UCOUNT_MNT_NAMESPACES,
34
	UCOUNT_CGROUP_NAMESPACES,
35 36 37
	UCOUNT_COUNTS,
};

38
struct user_namespace {
39 40
	struct uid_gid_map	uid_map;
	struct uid_gid_map	gid_map;
41
	struct uid_gid_map	projid_map;
42
	atomic_t		count;
43
	struct user_namespace	*parent;
44
	int			level;
45 46
	kuid_t			owner;
	kgid_t			group;
47
	struct ns_common	ns;
48
	unsigned long		flags;
49 50 51 52 53 54

	/* Register of per-UID persistent keyrings for this namespace */
#ifdef CONFIG_PERSISTENT_KEYRINGS
	struct key		*persistent_keyring_register;
	struct rw_semaphore	persistent_keyring_register_sem;
#endif
55
	struct work_struct	work;
56 57 58 59
#ifdef CONFIG_SYSCTL
	struct ctl_table_set	set;
	struct ctl_table_header *sysctls;
#endif
60
	struct ucounts		*ucounts;
61
	int ucount_max[UCOUNT_COUNTS];
62 63 64 65 66 67 68
};

struct ucounts {
	struct hlist_node node;
	struct user_namespace *ns;
	kuid_t uid;
	atomic_t count;
69
	atomic_t ucount[UCOUNT_COUNTS];
70 71 72
};

extern struct user_namespace init_user_ns;
73 74 75

bool setup_userns_sysctls(struct user_namespace *ns);
void retire_userns_sysctls(struct user_namespace *ns);
76 77
struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
78 79 80 81 82 83

#ifdef CONFIG_USER_NS

static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
{
	if (ns)
84
		atomic_inc(&ns->count);
85 86 87
	return ns;
}

88
extern int create_user_ns(struct cred *new);
89
extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
90
extern void __put_user_ns(struct user_namespace *ns);
91 92 93

static inline void put_user_ns(struct user_namespace *ns)
{
94
	if (ns && atomic_dec_and_test(&ns->count))
95
		__put_user_ns(ns);
96 97
}

98
struct seq_operations;
F
Fabian Frederick 已提交
99 100 101
extern const struct seq_operations proc_uid_seq_operations;
extern const struct seq_operations proc_gid_seq_operations;
extern const struct seq_operations proc_projid_seq_operations;
102 103
extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
104
extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
105 106
extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
extern int proc_setgroups_show(struct seq_file *m, void *v);
107
extern bool userns_may_setgroups(const struct user_namespace *ns);
108
extern bool current_in_userns(const struct user_namespace *target_ns);
109 110

struct ns_common *ns_get_owner(struct ns_common *ns);
111 112 113 114 115 116 117
#else

static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
{
	return &init_user_ns;
}

118
static inline int create_user_ns(struct cred *new)
119
{
120
	return -EINVAL;
121 122
}

123 124 125 126 127 128 129 130
static inline int unshare_userns(unsigned long unshare_flags,
				 struct cred **new_cred)
{
	if (unshare_flags & CLONE_NEWUSER)
		return -EINVAL;
	return 0;
}

131 132 133 134
static inline void put_user_ns(struct user_namespace *ns)
{
}

135 136 137 138
static inline bool userns_may_setgroups(const struct user_namespace *ns)
{
	return true;
}
139 140 141 142 143

static inline bool current_in_userns(const struct user_namespace *target_ns)
{
	return true;
}
144 145 146 147 148

static inline struct ns_common *ns_get_owner(struct ns_common *ns)
{
	return ERR_PTR(-EPERM);
}
149 150
#endif

151
#endif /* _LINUX_USER_H */