03776fa5f26c748c58941f469f3ad68f947a7204
[firefly-linux-kernel-4.4.55.git] / fs / sdcardfs / packagelist.c
1 /*
2  * fs/sdcardfs/packagelist.c
3  *
4  * Copyright (c) 2013 Samsung Electronics Co. Ltd
5  *   Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6  *               Sunghwan Yun, Sungjong Seo
7  *
8  * This program has been developed as a stackable file system based on
9  * the WrapFS which written by
10  *
11  * Copyright (c) 1998-2011 Erez Zadok
12  * Copyright (c) 2009     Shrikar Archak
13  * Copyright (c) 2003-2011 Stony Brook University
14  * Copyright (c) 2003-2011 The Research Foundation of SUNY
15  *
16  * This file is dual licensed.  It may be redistributed and/or modified
17  * under the terms of the Apache 2.0 License OR version 2 of the GNU
18  * General Public License.
19  */
20
21 #include "sdcardfs.h"
22 #include <linux/hashtable.h>
23 #include <linux/delay.h>
24
25
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29
30 #include <linux/configfs.h>
31
32 struct hashtable_entry {
33         struct hlist_node hlist;
34         const char *key;
35         atomic_t value;
36 };
37
38 static DEFINE_HASHTABLE(package_to_appid, 8);
39
40 static struct kmem_cache *hashtable_entry_cachep;
41
42 static unsigned int str_hash(const char *key) {
43         int i;
44         unsigned int h = strlen(key);
45         char *data = (char *)key;
46
47         for (i = 0; i < strlen(key); i++) {
48                 h = h * 31 + *data;
49                 data++;
50         }
51         return h;
52 }
53
54 appid_t get_appid(const char *app_name)
55 {
56         struct hashtable_entry *hash_cur;
57         unsigned int hash = str_hash(app_name);
58         appid_t ret_id;
59
60         rcu_read_lock();
61         hash_for_each_possible_rcu(package_to_appid, hash_cur, hlist, hash) {
62                 if (!strcasecmp(app_name, hash_cur->key)) {
63                         ret_id = atomic_read(&hash_cur->value);
64                         rcu_read_unlock();
65                         return ret_id;
66                 }
67         }
68         rcu_read_unlock();
69         return 0;
70 }
71
72 /* Kernel has already enforced everything we returned through
73  * derive_permissions_locked(), so this is used to lock down access
74  * even further, such as enforcing that apps hold sdcard_rw. */
75 int check_caller_access_to_name(struct inode *parent_node, const char* name) {
76
77         /* Always block security-sensitive files at root */
78         if (parent_node && SDCARDFS_I(parent_node)->perm == PERM_ROOT) {
79                 if (!strcasecmp(name, "autorun.inf")
80                         || !strcasecmp(name, ".android_secure")
81                         || !strcasecmp(name, "android_secure")) {
82                         return 0;
83                 }
84         }
85
86         /* Root always has access; access for any other UIDs should always
87          * be controlled through packages.list. */
88         if (from_kuid(&init_user_ns, current_fsuid()) == 0) {
89                 return 1;
90         }
91
92         /* No extra permissions to enforce */
93         return 1;
94 }
95
96 /* This function is used when file opening. The open flags must be
97  * checked before calling check_caller_access_to_name() */
98 int open_flags_to_access_mode(int open_flags) {
99         if((open_flags & O_ACCMODE) == O_RDONLY) {
100                 return 0; /* R_OK */
101         } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
102                 return 1; /* W_OK */
103         } else {
104                 /* Probably O_RDRW, but treat as default to be safe */
105                 return 1; /* R_OK | W_OK */
106         }
107 }
108
109 static struct hashtable_entry *alloc_packagelist_entry(const char *key,
110                 appid_t value)
111 {
112         struct hashtable_entry *ret = kmem_cache_alloc(hashtable_entry_cachep,
113                         GFP_KERNEL);
114         if (!ret)
115                 return NULL;
116
117         ret->key = kstrdup(key, GFP_KERNEL);
118         if (!ret->key) {
119                 kmem_cache_free(hashtable_entry_cachep, ret);
120                 return NULL;
121         }
122
123         atomic_set(&ret->value, value);
124         return ret;
125 }
126
127 static int insert_packagelist_entry_locked(const char *key, appid_t value)
128 {
129         struct hashtable_entry *hash_cur;
130         struct hashtable_entry *new_entry;
131         unsigned int hash = str_hash(key);
132
133         hash_for_each_possible_rcu(package_to_appid, hash_cur, hlist, hash) {
134                 if (!strcasecmp(key, hash_cur->key)) {
135                         atomic_set(&hash_cur->value, value);
136                         return 0;
137                 }
138         }
139         new_entry = alloc_packagelist_entry(key, value);
140         if (!new_entry)
141                 return -ENOMEM;
142         hash_add_rcu(package_to_appid, &new_entry->hlist, hash);
143         return 0;
144 }
145
146 static void fixup_perms(struct super_block *sb, const char *key) {
147         if (sb && sb->s_magic == SDCARDFS_SUPER_MAGIC) {
148                 fixup_perms_recursive(sb->s_root, key, strlen(key));
149         }
150 }
151
152 static void fixup_all_perms(const char *key)
153 {
154         struct sdcardfs_sb_info *sbinfo;
155         list_for_each_entry(sbinfo, &sdcardfs_super_list, list)
156                 if (sbinfo)
157                         fixup_perms(sbinfo->sb, key);
158 }
159
160 static int insert_packagelist_entry(const char *key, appid_t value)
161 {
162         int err;
163
164         mutex_lock(&sdcardfs_super_list_lock);
165         err = insert_packagelist_entry_locked(key, value);
166         if (!err)
167                 fixup_all_perms(key);
168         mutex_unlock(&sdcardfs_super_list_lock);
169
170         return err;
171 }
172
173 static void free_packagelist_entry(struct hashtable_entry *entry)
174 {
175         kfree(entry->key);
176         hash_del_rcu(&entry->hlist);
177         kmem_cache_free(hashtable_entry_cachep, entry);
178 }
179
180 static void remove_packagelist_entry_locked(const char *key)
181 {
182         struct hashtable_entry *hash_cur;
183         unsigned int hash = str_hash(key);
184
185         hash_for_each_possible_rcu(package_to_appid, hash_cur, hlist, hash) {
186                 if (!strcasecmp(key, hash_cur->key)) {
187                         hash_del_rcu(&hash_cur->hlist);
188                         synchronize_rcu();
189                         free_packagelist_entry(hash_cur);
190                         return;
191                 }
192         }
193 }
194
195 static void remove_packagelist_entry(const char *key)
196 {
197         mutex_lock(&sdcardfs_super_list_lock);
198         remove_packagelist_entry_locked(key);
199         fixup_all_perms(key);
200         mutex_unlock(&sdcardfs_super_list_lock);
201         return;
202 }
203
204 static void packagelist_destroy(void)
205 {
206         struct hashtable_entry *hash_cur;
207         struct hlist_node *h_t;
208         HLIST_HEAD(free_list);
209         int i;
210         mutex_lock(&sdcardfs_super_list_lock);
211         hash_for_each_rcu(package_to_appid, i, hash_cur, hlist) {
212                 hash_del_rcu(&hash_cur->hlist);
213                 hlist_add_head(&hash_cur->hlist, &free_list);
214
215         }
216         synchronize_rcu();
217         hlist_for_each_entry_safe(hash_cur, h_t, &free_list, hlist)
218                 free_packagelist_entry(hash_cur);
219         mutex_unlock(&sdcardfs_super_list_lock);
220         printk(KERN_INFO "sdcardfs: destroyed packagelist pkgld\n");
221 }
222
223 struct package_appid {
224         struct config_item item;
225         int add_pid;
226 };
227
228 static inline struct package_appid *to_package_appid(struct config_item *item)
229 {
230         return item ? container_of(item, struct package_appid, item) : NULL;
231 }
232
233 static ssize_t package_appid_attr_show(struct config_item *item,
234                                       char *page)
235 {
236         return scnprintf(page, PAGE_SIZE, "%u\n", get_appid(item->ci_name));
237 }
238
239 static ssize_t package_appid_attr_store(struct config_item *item,
240                                        const char *page, size_t count)
241 {
242         struct package_appid *package_appid = to_package_appid(item);
243         unsigned int tmp;
244         int ret;
245
246         ret = kstrtouint(page, 10, &tmp);
247         if (ret)
248                 return ret;
249
250         ret = insert_packagelist_entry(item->ci_name, tmp);
251         package_appid->add_pid = tmp;
252         if (ret)
253                 return ret;
254
255         return count;
256 }
257
258 static struct configfs_attribute package_appid_attr_add_pid = {
259         .ca_owner = THIS_MODULE,
260         .ca_name = "appid",
261         .ca_mode = S_IRUGO | S_IWUGO,
262         .show = package_appid_attr_show,
263         .store = package_appid_attr_store,
264 };
265
266 static struct configfs_attribute *package_appid_attrs[] = {
267         &package_appid_attr_add_pid,
268         NULL,
269 };
270
271 static void package_appid_release(struct config_item *item)
272 {
273         printk(KERN_INFO "sdcardfs: removing %s\n", item->ci_dentry->d_name.name);
274         /* item->ci_name is freed already, so we rely on the dentry */
275         remove_packagelist_entry(item->ci_dentry->d_name.name);
276         kfree(to_package_appid(item));
277 }
278
279 static struct configfs_item_operations package_appid_item_ops = {
280         .release                = package_appid_release,
281 };
282
283 static struct config_item_type package_appid_type = {
284         .ct_item_ops    = &package_appid_item_ops,
285         .ct_attrs       = package_appid_attrs,
286         .ct_owner       = THIS_MODULE,
287 };
288
289
290 struct sdcardfs_packages {
291         struct config_group group;
292 };
293
294 static inline struct sdcardfs_packages *to_sdcardfs_packages(struct config_item *item)
295 {
296         return item ? container_of(to_config_group(item), struct sdcardfs_packages, group) : NULL;
297 }
298
299 static struct config_item *sdcardfs_packages_make_item(struct config_group *group, const char *name)
300 {
301         struct package_appid *package_appid;
302
303         package_appid = kzalloc(sizeof(struct package_appid), GFP_KERNEL);
304         if (!package_appid)
305                 return ERR_PTR(-ENOMEM);
306
307         config_item_init_type_name(&package_appid->item, name,
308                                    &package_appid_type);
309
310         package_appid->add_pid = 0;
311
312         return &package_appid->item;
313 }
314
315 static ssize_t packages_attr_show(struct config_item *item,
316                                          char *page)
317 {
318         struct hashtable_entry *hash_cur;
319         int i;
320         int count = 0, written = 0;
321         const char errormsg[] = "<truncated>\n";
322
323         rcu_read_lock();
324         hash_for_each_rcu(package_to_appid, i, hash_cur, hlist) {
325                 written = scnprintf(page + count, PAGE_SIZE - sizeof(errormsg) - count, "%s %d\n",
326                                         (const char *)hash_cur->key, atomic_read(&hash_cur->value));
327                 if (count + written == PAGE_SIZE - sizeof(errormsg)) {
328                         count += scnprintf(page + count, PAGE_SIZE - count, errormsg);
329                         break;
330                 }
331                 count += written;
332         }
333         rcu_read_unlock();
334
335         return count;
336 }
337
338 static struct configfs_attribute sdcardfs_packages_attr_description = {
339         .ca_owner = THIS_MODULE,
340         .ca_name = "packages_gid.list",
341         .ca_mode = S_IRUGO,
342         .show = packages_attr_show,
343 };
344
345 static struct configfs_attribute *sdcardfs_packages_attrs[] = {
346         &sdcardfs_packages_attr_description,
347         NULL,
348 };
349
350 static void sdcardfs_packages_release(struct config_item *item)
351 {
352
353         printk(KERN_INFO "sdcardfs: destroyed something?\n");
354         kfree(to_sdcardfs_packages(item));
355 }
356
357 static struct configfs_item_operations sdcardfs_packages_item_ops = {
358         .release        = sdcardfs_packages_release,
359 };
360
361 /*
362  * Note that, since no extra work is required on ->drop_item(),
363  * no ->drop_item() is provided.
364  */
365 static struct configfs_group_operations sdcardfs_packages_group_ops = {
366         .make_item      = sdcardfs_packages_make_item,
367 };
368
369 static struct config_item_type sdcardfs_packages_type = {
370         .ct_item_ops    = &sdcardfs_packages_item_ops,
371         .ct_group_ops   = &sdcardfs_packages_group_ops,
372         .ct_attrs       = sdcardfs_packages_attrs,
373         .ct_owner       = THIS_MODULE,
374 };
375
376 static struct configfs_subsystem sdcardfs_packages_subsys = {
377         .su_group = {
378                 .cg_item = {
379                         .ci_namebuf = "sdcardfs",
380                         .ci_type = &sdcardfs_packages_type,
381                 },
382         },
383 };
384
385 static int configfs_sdcardfs_init(void)
386 {
387         int ret;
388         struct configfs_subsystem *subsys = &sdcardfs_packages_subsys;
389
390         config_group_init(&subsys->su_group);
391         mutex_init(&subsys->su_mutex);
392         ret = configfs_register_subsystem(subsys);
393         if (ret) {
394                 printk(KERN_ERR "Error %d while registering subsystem %s\n",
395                        ret,
396                        subsys->su_group.cg_item.ci_namebuf);
397         }
398         return ret;
399 }
400
401 static void configfs_sdcardfs_exit(void)
402 {
403         configfs_unregister_subsystem(&sdcardfs_packages_subsys);
404 }
405
406 int packagelist_init(void)
407 {
408         hashtable_entry_cachep =
409                 kmem_cache_create("packagelist_hashtable_entry",
410                                         sizeof(struct hashtable_entry), 0, 0, NULL);
411         if (!hashtable_entry_cachep) {
412                 printk(KERN_ERR "sdcardfs: failed creating pkgl_hashtable entry slab cache\n");
413                 return -ENOMEM;
414         }
415
416         configfs_sdcardfs_init();
417         return 0;
418 }
419
420 void packagelist_exit(void)
421 {
422         configfs_sdcardfs_exit();
423         packagelist_destroy();
424         if (hashtable_entry_cachep)
425                 kmem_cache_destroy(hashtable_entry_cachep);
426 }