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