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