ANDROID: sdcardfs: Refactor configfs interface
[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_details {
224         struct config_item item;
225         const char *name;
226 };
227
228 static inline struct package_details *to_package_details(struct config_item *item)
229 {
230         return item ? container_of(item, struct package_details, item) : NULL;
231 }
232
233 static ssize_t package_details_appid_show(struct config_item *item, char *page)
234 {
235         return scnprintf(page, PAGE_SIZE, "%u\n", get_appid(to_package_details(item)->name));
236 }
237
238 static ssize_t package_details_appid_store(struct config_item *item,
239                                        const char *page, size_t count)
240 {
241         unsigned int tmp;
242         int ret;
243
244         ret = kstrtouint(page, 10, &tmp);
245         if (ret)
246                 return ret;
247
248         ret = insert_packagelist_entry(to_package_details(item)->name, tmp);
249
250         if (ret)
251                 return ret;
252
253         return count;
254 }
255
256 static void package_details_release(struct config_item *item)
257 {
258         struct package_details *package_details = to_package_details(item);
259         printk(KERN_INFO "sdcardfs: removing %s\n", package_details->name);
260         remove_packagelist_entry(package_details->name);
261         kfree(package_details->name);
262         kfree(package_details);
263 }
264
265 CONFIGFS_ATTR(package_details_, appid);
266
267 static struct configfs_attribute *package_details_attrs[] = {
268         &package_details_attr_appid,
269         NULL,
270 };
271
272 static struct configfs_item_operations package_details_item_ops = {
273       .release = package_details_release,
274 };
275
276 static struct config_item_type package_appid_type = {
277         .ct_item_ops    = &package_details_item_ops,
278         .ct_attrs       = package_details_attrs,
279         .ct_owner       = THIS_MODULE,
280 };
281
282 static struct config_item *packages_make_item(struct config_group *group, const char *name)
283 {
284         struct package_details *package_details;
285
286         package_details = kzalloc(sizeof(struct package_details), GFP_KERNEL);
287         if (!package_details)
288                 return ERR_PTR(-ENOMEM);
289         package_details->name = kstrdup(name, GFP_KERNEL);
290         if (!package_details->name) {
291                 kfree(package_details);
292                 return ERR_PTR(-ENOMEM);
293         }
294
295         config_item_init_type_name(&package_details->item, name,
296                                    &package_appid_type);
297
298         return &package_details->item;
299 }
300
301 static ssize_t packages_list_show(struct config_item *item, char *page)
302 {
303         struct hashtable_entry *hash_cur;
304         int i;
305         int count = 0, written = 0;
306         const char errormsg[] = "<truncated>\n";
307
308         rcu_read_lock();
309         hash_for_each_rcu(package_to_appid, i, hash_cur, hlist) {
310                 written = scnprintf(page + count, PAGE_SIZE - sizeof(errormsg) - count, "%s %d\n",
311                                         (const char *)hash_cur->key, atomic_read(&hash_cur->value));
312                 if (count + written == PAGE_SIZE - sizeof(errormsg)) {
313                         count += scnprintf(page + count, PAGE_SIZE - count, errormsg);
314                         break;
315                 }
316                 count += written;
317         }
318         rcu_read_unlock();
319
320         return count;
321 }
322
323 static struct configfs_attribute packages_attr_packages_gid_list = {
324         .ca_name        = "packages_gid.list",
325         .ca_mode        = S_IRUGO,
326         .ca_owner       = THIS_MODULE,
327         .show           = packages_list_show,
328 };
329
330 static struct configfs_attribute *packages_attrs[] = {
331         &packages_attr_packages_gid_list,
332         NULL,
333 };
334
335 /*
336  * Note that, since no extra work is required on ->drop_item(),
337  * no ->drop_item() is provided.
338  */
339 static struct configfs_group_operations packages_group_ops = {
340         .make_item      = packages_make_item,
341 };
342
343 static struct config_item_type packages_type = {
344         .ct_group_ops   = &packages_group_ops,
345         .ct_attrs       = packages_attrs,
346         .ct_owner       = THIS_MODULE,
347 };
348
349 static struct configfs_subsystem sdcardfs_packages = {
350         .su_group = {
351                 .cg_item = {
352                         .ci_namebuf = "sdcardfs",
353                         .ci_type = &packages_type,
354                 },
355         },
356 };
357
358 static int configfs_sdcardfs_init(void)
359 {
360         int ret;
361         struct configfs_subsystem *subsys = &sdcardfs_packages;
362
363         config_group_init(&subsys->su_group);
364         mutex_init(&subsys->su_mutex);
365         ret = configfs_register_subsystem(subsys);
366         if (ret) {
367                 printk(KERN_ERR "Error %d while registering subsystem %s\n",
368                        ret,
369                        subsys->su_group.cg_item.ci_namebuf);
370         }
371         return ret;
372 }
373
374 static void configfs_sdcardfs_exit(void)
375 {
376         configfs_unregister_subsystem(&sdcardfs_packages);
377 }
378
379 int packagelist_init(void)
380 {
381         hashtable_entry_cachep =
382                 kmem_cache_create("packagelist_hashtable_entry",
383                                         sizeof(struct hashtable_entry), 0, 0, NULL);
384         if (!hashtable_entry_cachep) {
385                 printk(KERN_ERR "sdcardfs: failed creating pkgl_hashtable entry slab cache\n");
386                 return -ENOMEM;
387         }
388
389         configfs_sdcardfs_init();
390         return 0;
391 }
392
393 void packagelist_exit(void)
394 {
395         configfs_sdcardfs_exit();
396         packagelist_destroy();
397         if (hashtable_entry_cachep)
398                 kmem_cache_destroy(hashtable_entry_cachep);
399 }