kdbus: porting to 4.14
[platform/kernel/linux-rpi.git] / ipc / kdbus / domain.c
1 /*
2  * Copyright (C) 2013-2015 Kay Sievers
3  * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
5  * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
6  * Copyright (C) 2013-2015 Linux Foundation
7  * Copyright (C) 2014-2015 Djalal Harouni <tixxdz@opendz.org>
8  *
9  * kdbus is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License as published by the
11  * Free Software Foundation; either version 2.1 of the License, or (at
12  * your option) any later version.
13  */
14
15 #include <linux/cred.h>
16 #include <linux/fs.h>
17 #include <linux/idr.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/sizes.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24
25 #include "bus.h"
26 #include "domain.h"
27 #include "handle.h"
28 #include "item.h"
29 #include "limits.h"
30 #include "util.h"
31
32 static void kdbus_domain_control_free(struct kdbus_node *node)
33 {
34         kfree(node);
35 }
36
37 static struct kdbus_node *kdbus_domain_control_new(struct kdbus_domain *domain,
38                                                    unsigned int access)
39 {
40         struct kdbus_node *node;
41         int ret;
42
43         node = kzalloc(sizeof(*node), GFP_KERNEL);
44         if (!node)
45                 return ERR_PTR(-ENOMEM);
46
47         kdbus_node_init(node, KDBUS_NODE_CONTROL);
48
49         node->free_cb = kdbus_domain_control_free;
50         node->mode = domain->node.mode;
51         node->mode = S_IRUSR | S_IWUSR;
52         if (access & (KDBUS_MAKE_ACCESS_GROUP | KDBUS_MAKE_ACCESS_WORLD))
53                 node->mode |= S_IRGRP | S_IWGRP;
54         if (access & KDBUS_MAKE_ACCESS_WORLD)
55                 node->mode |= S_IROTH | S_IWOTH;
56
57         ret = kdbus_node_link(node, &domain->node, "control");
58         if (ret < 0)
59                 goto exit_free;
60
61         return node;
62
63 exit_free:
64         kdbus_node_deactivate(node);
65         kdbus_node_unref(node);
66         return ERR_PTR(ret);
67 }
68
69 static void kdbus_domain_free(struct kdbus_node *node)
70 {
71         struct kdbus_domain *domain =
72                 container_of(node, struct kdbus_domain, node);
73
74         put_user_ns(domain->user_namespace);
75         ida_destroy(&domain->user_ida);
76         idr_destroy(&domain->user_idr);
77         kfree(domain);
78 }
79
80 /**
81  * kdbus_domain_new() - create a new domain
82  * @access:             The access mode for this node (KDBUS_MAKE_ACCESS_*)
83  *
84  * Return: a new kdbus_domain on success, ERR_PTR on failure
85  */
86 struct kdbus_domain *kdbus_domain_new(unsigned int access)
87 {
88         struct kdbus_domain *d;
89         int ret;
90
91         d = kzalloc(sizeof(*d), GFP_KERNEL);
92         if (!d)
93                 return ERR_PTR(-ENOMEM);
94
95         kdbus_node_init(&d->node, KDBUS_NODE_DOMAIN);
96
97         d->node.free_cb = kdbus_domain_free;
98         d->node.mode = S_IRUSR | S_IXUSR;
99         if (access & (KDBUS_MAKE_ACCESS_GROUP | KDBUS_MAKE_ACCESS_WORLD))
100                 d->node.mode |= S_IRGRP | S_IXGRP;
101         if (access & KDBUS_MAKE_ACCESS_WORLD)
102                 d->node.mode |= S_IROTH | S_IXOTH;
103
104         mutex_init(&d->lock);
105         idr_init(&d->user_idr);
106         ida_init(&d->user_ida);
107
108         /* Pin user namespace so we can guarantee domain-unique bus * names. */
109         d->user_namespace = get_user_ns(current_user_ns());
110
111         ret = kdbus_node_link(&d->node, NULL, NULL);
112         if (ret < 0)
113                 goto exit_unref;
114
115         return d;
116
117 exit_unref:
118         kdbus_node_deactivate(&d->node);
119         kdbus_node_unref(&d->node);
120         return ERR_PTR(ret);
121 }
122
123 /**
124  * kdbus_domain_ref() - take a domain reference
125  * @domain:             Domain
126  *
127  * Return: the domain itself
128  */
129 struct kdbus_domain *kdbus_domain_ref(struct kdbus_domain *domain)
130 {
131         if (domain)
132                 kdbus_node_ref(&domain->node);
133         return domain;
134 }
135
136 /**
137  * kdbus_domain_unref() - drop a domain reference
138  * @domain:             Domain
139  *
140  * When the last reference is dropped, the domain internal structure
141  * is freed.
142  *
143  * Return: NULL
144  */
145 struct kdbus_domain *kdbus_domain_unref(struct kdbus_domain *domain)
146 {
147         if (domain)
148                 kdbus_node_unref(&domain->node);
149         return NULL;
150 }
151
152 /**
153  * kdbus_domain_populate() - populate static domain nodes
154  * @domain:     domain to populate
155  * @access:     KDBUS_MAKE_ACCESS_* access restrictions for new nodes
156  *
157  * Allocate and activate static sub-nodes of the given domain. This will fail if
158  * you call it on a non-active node or if the domain was already populated.
159  *
160  * Return: 0 on success, negative error code on failure.
161  */
162 int kdbus_domain_populate(struct kdbus_domain *domain, unsigned int access)
163 {
164         struct kdbus_node *control;
165
166         /*
167          * Create a control-node for this domain. We drop our own reference
168          * immediately, effectively causing the node to be deactivated and
169          * released when the parent domain is.
170          */
171         control = kdbus_domain_control_new(domain, access);
172         if (IS_ERR(control))
173                 return PTR_ERR(control);
174
175         kdbus_node_activate(control);
176         kdbus_node_unref(control);
177         return 0;
178 }
179
180 /**
181  * kdbus_user_lookup() - lookup a kdbus_user object
182  * @domain:             domain of the user
183  * @uid:                uid of the user; INVALID_UID for an anon user
184  *
185  * Lookup the kdbus user accounting object for the given domain. If INVALID_UID
186  * is passed, a new anonymous user is created which is private to the caller.
187  *
188  * Return: The user object is returned, ERR_PTR on failure.
189  */
190 struct kdbus_user *kdbus_user_lookup(struct kdbus_domain *domain, kuid_t uid)
191 {
192         struct kdbus_user *u = NULL, *old = NULL;
193         int ret;
194
195         mutex_lock(&domain->lock);
196
197         if (uid_valid(uid)) {
198                 old = idr_find(&domain->user_idr, __kuid_val(uid));
199                 /*
200                  * If the object is about to be destroyed, ignore it and
201                  * replace the slot in the IDR later on.
202                  */
203                 if (old && kref_get_unless_zero(&old->kref)) {
204                         mutex_unlock(&domain->lock);
205                         return old;
206                 }
207         }
208
209         u = kzalloc(sizeof(*u), GFP_KERNEL);
210         if (!u) {
211                 ret = -ENOMEM;
212                 goto exit;
213         }
214
215         kref_init(&u->kref);
216         u->domain = kdbus_domain_ref(domain);
217         u->uid = uid;
218         atomic_set(&u->buses, 0);
219         atomic_set(&u->connections, 0);
220
221         if (uid_valid(uid)) {
222                 if (old) {
223                         idr_replace(&domain->user_idr, u, __kuid_val(uid));
224                         old->uid = INVALID_UID; /* mark old as removed */
225                 } else {
226                         ret = idr_alloc(&domain->user_idr, u, __kuid_val(uid),
227                                         __kuid_val(uid) + 1, GFP_KERNEL);
228                         if (ret < 0)
229                                 goto exit;
230                 }
231         }
232
233         /*
234          * Allocate the smallest possible index for this user; used
235          * in arrays for accounting user quota in receiver queues.
236          */
237         ret = ida_simple_get(&domain->user_ida, 1, 0, GFP_KERNEL);
238         if (ret < 0)
239                 goto exit;
240
241         u->id = ret;
242         mutex_unlock(&domain->lock);
243         return u;
244
245 exit:
246         if (u) {
247                 if (uid_valid(u->uid))
248                         idr_remove(&domain->user_idr, __kuid_val(u->uid));
249                 kdbus_domain_unref(u->domain);
250                 kfree(u);
251         }
252         mutex_unlock(&domain->lock);
253         return ERR_PTR(ret);
254 }
255
256 static void __kdbus_user_free(struct kref *kref)
257 {
258         struct kdbus_user *user = container_of(kref, struct kdbus_user, kref);
259
260         WARN_ON(atomic_read(&user->buses) > 0);
261         WARN_ON(atomic_read(&user->connections) > 0);
262
263         mutex_lock(&user->domain->lock);
264         ida_simple_remove(&user->domain->user_ida, user->id);
265         if (uid_valid(user->uid))
266                 idr_remove(&user->domain->user_idr, __kuid_val(user->uid));
267         mutex_unlock(&user->domain->lock);
268
269         kdbus_domain_unref(user->domain);
270         kfree(user);
271 }
272
273 /**
274  * kdbus_user_ref() - take a user reference
275  * @u:          User
276  *
277  * Return: @u is returned
278  */
279 struct kdbus_user *kdbus_user_ref(struct kdbus_user *u)
280 {
281         if (u)
282                 kref_get(&u->kref);
283         return u;
284 }
285
286 /**
287  * kdbus_user_unref() - drop a user reference
288  * @u:          User
289  *
290  * Return: NULL
291  */
292 struct kdbus_user *kdbus_user_unref(struct kdbus_user *u)
293 {
294         if (u)
295                 kref_put(&u->kref, __kdbus_user_free);
296         return NULL;
297 }