ipcns: Add a limit on the number of ipc namespaces
[cascardo/linux.git] / include / linux / user_namespace.h
1 #ifndef _LINUX_USER_NAMESPACE_H
2 #define _LINUX_USER_NAMESPACE_H
3
4 #include <linux/kref.h>
5 #include <linux/nsproxy.h>
6 #include <linux/ns_common.h>
7 #include <linux/sched.h>
8 #include <linux/err.h>
9
10 #define UID_GID_MAP_MAX_EXTENTS 5
11
12 struct uid_gid_map {    /* 64 bytes -- 1 cache line */
13         u32 nr_extents;
14         struct uid_gid_extent {
15                 u32 first;
16                 u32 lower_first;
17                 u32 count;
18         } extent[UID_GID_MAP_MAX_EXTENTS];
19 };
20
21 #define USERNS_SETGROUPS_ALLOWED 1UL
22
23 #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
24
25 struct ucounts;
26
27 enum ucount_type {
28         UCOUNT_USER_NAMESPACES,
29         UCOUNT_PID_NAMESPACES,
30         UCOUNT_UTS_NAMESPACES,
31         UCOUNT_IPC_NAMESPACES,
32         UCOUNT_COUNTS,
33 };
34
35 struct user_namespace {
36         struct uid_gid_map      uid_map;
37         struct uid_gid_map      gid_map;
38         struct uid_gid_map      projid_map;
39         atomic_t                count;
40         struct user_namespace   *parent;
41         int                     level;
42         kuid_t                  owner;
43         kgid_t                  group;
44         struct ns_common        ns;
45         unsigned long           flags;
46
47         /* Register of per-UID persistent keyrings for this namespace */
48 #ifdef CONFIG_PERSISTENT_KEYRINGS
49         struct key              *persistent_keyring_register;
50         struct rw_semaphore     persistent_keyring_register_sem;
51 #endif
52         struct work_struct      work;
53 #ifdef CONFIG_SYSCTL
54         struct ctl_table_set    set;
55         struct ctl_table_header *sysctls;
56 #endif
57         struct ucounts          *ucounts;
58         int ucount_max[UCOUNT_COUNTS];
59 };
60
61 struct ucounts {
62         struct hlist_node node;
63         struct user_namespace *ns;
64         kuid_t uid;
65         atomic_t count;
66         atomic_t ucount[UCOUNT_COUNTS];
67 };
68
69 extern struct user_namespace init_user_ns;
70
71 bool setup_userns_sysctls(struct user_namespace *ns);
72 void retire_userns_sysctls(struct user_namespace *ns);
73 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
74 void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
75
76 #ifdef CONFIG_USER_NS
77
78 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
79 {
80         if (ns)
81                 atomic_inc(&ns->count);
82         return ns;
83 }
84
85 extern int create_user_ns(struct cred *new);
86 extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
87 extern void __put_user_ns(struct user_namespace *ns);
88
89 static inline void put_user_ns(struct user_namespace *ns)
90 {
91         if (ns && atomic_dec_and_test(&ns->count))
92                 __put_user_ns(ns);
93 }
94
95 struct seq_operations;
96 extern const struct seq_operations proc_uid_seq_operations;
97 extern const struct seq_operations proc_gid_seq_operations;
98 extern const struct seq_operations proc_projid_seq_operations;
99 extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
100 extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
101 extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
102 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
103 extern int proc_setgroups_show(struct seq_file *m, void *v);
104 extern bool userns_may_setgroups(const struct user_namespace *ns);
105 extern bool current_in_userns(const struct user_namespace *target_ns);
106 #else
107
108 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
109 {
110         return &init_user_ns;
111 }
112
113 static inline int create_user_ns(struct cred *new)
114 {
115         return -EINVAL;
116 }
117
118 static inline int unshare_userns(unsigned long unshare_flags,
119                                  struct cred **new_cred)
120 {
121         if (unshare_flags & CLONE_NEWUSER)
122                 return -EINVAL;
123         return 0;
124 }
125
126 static inline void put_user_ns(struct user_namespace *ns)
127 {
128 }
129
130 static inline bool userns_may_setgroups(const struct user_namespace *ns)
131 {
132         return true;
133 }
134
135 static inline bool current_in_userns(const struct user_namespace *target_ns)
136 {
137         return true;
138 }
139 #endif
140
141 #endif /* _LINUX_USER_H */