2 * fs/sdcardfs/packagelist.c
4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
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
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.
22 #include <linux/hashtable.h>
23 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
30 #include <linux/configfs.h>
32 struct hashtable_entry {
33 struct hlist_node hlist;
38 static DEFINE_HASHTABLE(package_to_appid, 8);
40 static struct kmem_cache *hashtable_entry_cachep;
42 static unsigned int str_hash(const char *key) {
44 unsigned int h = strlen(key);
45 char *data = (char *)key;
47 for (i = 0; i < strlen(key); i++) {
54 appid_t get_appid(const char *app_name)
56 struct hashtable_entry *hash_cur;
57 unsigned int hash = str_hash(app_name);
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);
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) {
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")) {
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) {
92 /* No extra permissions to enforce */
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) {
101 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
104 /* Probably O_RDRW, but treat as default to be safe */
105 return 1; /* R_OK | W_OK */
109 static struct hashtable_entry *alloc_packagelist_entry(const char *key,
112 struct hashtable_entry *ret = kmem_cache_alloc(hashtable_entry_cachep,
117 ret->key = kstrdup(key, GFP_KERNEL);
119 kmem_cache_free(hashtable_entry_cachep, ret);
123 atomic_set(&ret->value, value);
127 static int insert_packagelist_entry_locked(const char *key, appid_t value)
129 struct hashtable_entry *hash_cur;
130 struct hashtable_entry *new_entry;
131 unsigned int hash = str_hash(key);
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);
139 new_entry = alloc_packagelist_entry(key, value);
142 hash_add_rcu(package_to_appid, &new_entry->hlist, hash);
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));
152 static void fixup_all_perms(const char *key)
154 struct sdcardfs_sb_info *sbinfo;
155 list_for_each_entry(sbinfo, &sdcardfs_super_list, list)
157 fixup_perms(sbinfo->sb, key);
160 static int insert_packagelist_entry(const char *key, appid_t value)
164 mutex_lock(&sdcardfs_super_list_lock);
165 err = insert_packagelist_entry_locked(key, value);
167 fixup_all_perms(key);
168 mutex_unlock(&sdcardfs_super_list_lock);
173 static void free_packagelist_entry(struct hashtable_entry *entry)
176 hash_del_rcu(&entry->hlist);
177 kmem_cache_free(hashtable_entry_cachep, entry);
180 static void remove_packagelist_entry_locked(const char *key)
182 struct hashtable_entry *hash_cur;
183 unsigned int hash = str_hash(key);
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);
189 free_packagelist_entry(hash_cur);
195 static void remove_packagelist_entry(const char *key)
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);
204 static void packagelist_destroy(void)
206 struct hashtable_entry *hash_cur;
207 struct hlist_node *h_t;
208 HLIST_HEAD(free_list);
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);
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");
223 struct package_details {
224 struct config_item item;
228 static inline struct package_details *to_package_details(struct config_item *item)
230 return item ? container_of(item, struct package_details, item) : NULL;
233 static ssize_t package_details_appid_show(struct config_item *item, char *page)
235 return scnprintf(page, PAGE_SIZE, "%u\n", get_appid(to_package_details(item)->name));
238 static ssize_t package_details_appid_store(struct config_item *item,
239 const char *page, size_t count)
244 ret = kstrtouint(page, 10, &tmp);
248 ret = insert_packagelist_entry(to_package_details(item)->name, tmp);
256 static void package_details_release(struct config_item *item)
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);
265 CONFIGFS_ATTR(package_details_, appid);
267 static struct configfs_attribute *package_details_attrs[] = {
268 &package_details_attr_appid,
272 static struct configfs_item_operations package_details_item_ops = {
273 .release = package_details_release,
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,
282 static struct config_item *packages_make_item(struct config_group *group, const char *name)
284 struct package_details *package_details;
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);
295 config_item_init_type_name(&package_details->item, name,
296 &package_appid_type);
298 return &package_details->item;
301 static ssize_t packages_list_show(struct config_item *item, char *page)
303 struct hashtable_entry *hash_cur;
305 int count = 0, written = 0;
306 const char errormsg[] = "<truncated>\n";
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);
323 static struct configfs_attribute packages_attr_packages_gid_list = {
324 .ca_name = "packages_gid.list",
326 .ca_owner = THIS_MODULE,
327 .show = packages_list_show,
330 static struct configfs_attribute *packages_attrs[] = {
331 &packages_attr_packages_gid_list,
336 * Note that, since no extra work is required on ->drop_item(),
337 * no ->drop_item() is provided.
339 static struct configfs_group_operations packages_group_ops = {
340 .make_item = packages_make_item,
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,
349 static struct configfs_subsystem sdcardfs_packages = {
352 .ci_namebuf = "sdcardfs",
353 .ci_type = &packages_type,
358 static int configfs_sdcardfs_init(void)
361 struct configfs_subsystem *subsys = &sdcardfs_packages;
363 config_group_init(&subsys->su_group);
364 mutex_init(&subsys->su_mutex);
365 ret = configfs_register_subsystem(subsys);
367 printk(KERN_ERR "Error %d while registering subsystem %s\n",
369 subsys->su_group.cg_item.ci_namebuf);
374 static void configfs_sdcardfs_exit(void)
376 configfs_unregister_subsystem(&sdcardfs_packages);
379 int packagelist_init(void)
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");
389 configfs_sdcardfs_init();
393 void packagelist_exit(void)
395 configfs_sdcardfs_exit();
396 packagelist_destroy();
397 if (hashtable_entry_cachep)
398 kmem_cache_destroy(hashtable_entry_cachep);