derived_perm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * fs/sdcardfs/derived_perm.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. #include "sdcardfs.h"
  21. /* copy derived state from parent inode */
  22. static void inherit_derived_state(struct inode *parent, struct inode *child)
  23. {
  24. struct sdcardfs_inode_info *pi = SDCARDFS_I(parent);
  25. struct sdcardfs_inode_info *ci = SDCARDFS_I(child);
  26. ci->data->perm = PERM_INHERIT;
  27. ci->data->userid = pi->data->userid;
  28. ci->data->d_uid = pi->data->d_uid;
  29. ci->data->under_android = pi->data->under_android;
  30. ci->data->under_cache = pi->data->under_cache;
  31. ci->data->under_obb = pi->data->under_obb;
  32. }
  33. /* helper function for derived state */
  34. void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
  35. uid_t uid)
  36. {
  37. struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
  38. info->data->perm = perm;
  39. info->data->userid = userid;
  40. info->data->d_uid = uid;
  41. info->data->under_android = false;
  42. info->data->under_cache = false;
  43. info->data->under_obb = false;
  44. }
  45. /* While renaming, there is a point where we want the path from dentry,
  46. * but the name from newdentry
  47. */
  48. void get_derived_permission_new(struct dentry *parent, struct dentry *dentry,
  49. const struct qstr *name)
  50. {
  51. struct sdcardfs_inode_info *info = SDCARDFS_I(dentry->d_inode);
  52. struct sdcardfs_inode_info *parent_info = SDCARDFS_I(parent->d_inode);
  53. struct sdcardfs_inode_data *parent_data = parent_info->data;
  54. appid_t appid;
  55. unsigned long user_num;
  56. int err;
  57. struct qstr q_Android = QSTR_LITERAL("Android");
  58. struct qstr q_data = QSTR_LITERAL("data");
  59. struct qstr q_sandbox = QSTR_LITERAL("sandbox");
  60. struct qstr q_obb = QSTR_LITERAL("obb");
  61. struct qstr q_media = QSTR_LITERAL("media");
  62. struct qstr q_cache = QSTR_LITERAL("cache");
  63. /* By default, each inode inherits from its parent.
  64. * the properties are maintained on its private fields
  65. * because the inode attributes will be modified with that of
  66. * its lower inode.
  67. * These values are used by our custom permission call instead
  68. * of using the inode permissions.
  69. */
  70. inherit_derived_state(parent->d_inode, dentry->d_inode);
  71. /* Files don't get special labels */
  72. if (!S_ISDIR(dentry->d_inode->i_mode)) {
  73. set_top(info, parent_info);
  74. return;
  75. }
  76. /* Derive custom permissions based on parent and current node */
  77. switch (parent_data->perm) {
  78. case PERM_INHERIT:
  79. case PERM_ANDROID_PACKAGE_CACHE:
  80. set_top(info, parent_info);
  81. break;
  82. case PERM_PRE_ROOT:
  83. /* Legacy internal layout places users at top level */
  84. info->data->perm = PERM_ROOT;
  85. err = kstrtoul(name->name, 10, &user_num);
  86. if (err)
  87. info->data->userid = 0;
  88. else
  89. info->data->userid = user_num;
  90. break;
  91. case PERM_ROOT:
  92. /* Assume masked off by default. */
  93. if (qstr_case_eq(name, &q_Android)) {
  94. /* App-specific directories inside; let anyone traverse */
  95. info->data->perm = PERM_ANDROID;
  96. info->data->under_android = true;
  97. } else {
  98. set_top(info, parent_info);
  99. }
  100. break;
  101. case PERM_ANDROID:
  102. if (qstr_case_eq(name, &q_data)) {
  103. /* App-specific directories inside; let anyone traverse */
  104. info->data->perm = PERM_ANDROID_DATA;
  105. } else if (qstr_case_eq(name, &q_sandbox)) {
  106. /* App-specific directories inside; let anyone traverse */
  107. info->data->perm = PERM_ANDROID_DATA;
  108. } else if (qstr_case_eq(name, &q_obb)) {
  109. /* App-specific directories inside; let anyone traverse */
  110. info->data->perm = PERM_ANDROID_OBB;
  111. info->data->under_obb = true;
  112. /* Single OBB directory is always shared */
  113. } else if (qstr_case_eq(name, &q_media)) {
  114. /* App-specific directories inside; let anyone traverse */
  115. info->data->perm = PERM_ANDROID_MEDIA;
  116. } else {
  117. set_top(info, parent_info);
  118. }
  119. break;
  120. case PERM_ANDROID_OBB:
  121. case PERM_ANDROID_DATA:
  122. case PERM_ANDROID_MEDIA:
  123. info->data->perm = PERM_ANDROID_PACKAGE;
  124. appid = get_appid(name->name);
  125. if (appid != 0 && !is_excluded(name->name, parent_data->userid))
  126. info->data->d_uid =
  127. multiuser_get_uid(parent_data->userid, appid);
  128. break;
  129. case PERM_ANDROID_PACKAGE:
  130. if (qstr_case_eq(name, &q_cache)) {
  131. info->data->perm = PERM_ANDROID_PACKAGE_CACHE;
  132. info->data->under_cache = true;
  133. }
  134. set_top(info, parent_info);
  135. break;
  136. }
  137. }
  138. void get_derived_permission(struct dentry *parent, struct dentry *dentry)
  139. {
  140. get_derived_permission_new(parent, dentry, &dentry->d_name);
  141. }
  142. static appid_t get_type(const char *name)
  143. {
  144. const char *ext = strrchr(name, '.');
  145. appid_t id;
  146. if (ext && ext[0]) {
  147. ext = &ext[1];
  148. id = get_ext_gid(ext);
  149. return id?:AID_MEDIA_RW;
  150. }
  151. return AID_MEDIA_RW;
  152. }
  153. void fixup_lower_ownership(struct dentry *dentry, const char *name)
  154. {
  155. struct path path;
  156. struct inode *inode;
  157. int error;
  158. struct sdcardfs_inode_info *info;
  159. struct sdcardfs_inode_data *info_d;
  160. struct sdcardfs_inode_data *info_top;
  161. perm_t perm;
  162. struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
  163. uid_t uid = sbi->options.fs_low_uid;
  164. gid_t gid = sbi->options.fs_low_gid;
  165. struct iattr newattrs;
  166. if (!sbi->options.gid_derivation)
  167. return;
  168. info = SDCARDFS_I(dentry->d_inode);
  169. info_d = info->data;
  170. perm = info_d->perm;
  171. if (info_d->under_obb) {
  172. perm = PERM_ANDROID_OBB;
  173. } else if (info_d->under_cache) {
  174. perm = PERM_ANDROID_PACKAGE_CACHE;
  175. } else if (perm == PERM_INHERIT) {
  176. info_top = top_data_get(info);
  177. perm = info_top->perm;
  178. data_put(info_top);
  179. }
  180. switch (perm) {
  181. case PERM_ROOT:
  182. case PERM_ANDROID:
  183. case PERM_ANDROID_DATA:
  184. case PERM_ANDROID_MEDIA:
  185. case PERM_ANDROID_PACKAGE:
  186. case PERM_ANDROID_PACKAGE_CACHE:
  187. uid = multiuser_get_uid(info_d->userid, uid);
  188. break;
  189. case PERM_ANDROID_OBB:
  190. uid = AID_MEDIA_OBB;
  191. break;
  192. case PERM_PRE_ROOT:
  193. default:
  194. break;
  195. }
  196. switch (perm) {
  197. case PERM_ROOT:
  198. case PERM_ANDROID:
  199. case PERM_ANDROID_DATA:
  200. case PERM_ANDROID_MEDIA:
  201. if (S_ISDIR(dentry->d_inode->i_mode))
  202. gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
  203. else
  204. gid = multiuser_get_uid(info_d->userid, get_type(name));
  205. break;
  206. case PERM_ANDROID_OBB:
  207. gid = AID_MEDIA_OBB;
  208. break;
  209. case PERM_ANDROID_PACKAGE:
  210. if (uid_is_app(info_d->d_uid))
  211. gid = multiuser_get_ext_gid(info_d->d_uid);
  212. else
  213. gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
  214. break;
  215. case PERM_ANDROID_PACKAGE_CACHE:
  216. if (uid_is_app(info_d->d_uid))
  217. gid = multiuser_get_ext_cache_gid(info_d->d_uid);
  218. else
  219. gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
  220. break;
  221. case PERM_PRE_ROOT:
  222. default:
  223. break;
  224. }
  225. sdcardfs_get_lower_path(dentry, &path);
  226. inode = path.dentry->d_inode;
  227. if (path.dentry->d_inode->i_gid != gid || path.dentry->d_inode->i_uid != uid) {
  228. newattrs.ia_valid = ATTR_GID | ATTR_UID | ATTR_FORCE;
  229. newattrs.ia_uid = uid;
  230. newattrs.ia_gid = gid;
  231. if (!S_ISDIR(inode->i_mode))
  232. newattrs.ia_valid |=
  233. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  234. mutex_lock(&inode->i_mutex);
  235. error = security_path_chown(&path, newattrs.ia_uid, newattrs.ia_gid);
  236. if (!error)
  237. error = notify_change2(path.mnt, path.dentry, &newattrs);
  238. mutex_unlock(&inode->i_mutex);
  239. if (error)
  240. pr_debug("sdcardfs: Failed to touch up lower fs gid/uid for %s\n", name);
  241. }
  242. sdcardfs_put_lower_path(dentry, &path);
  243. }
  244. static int descendant_may_need_fixup(struct sdcardfs_inode_data *data,
  245. struct limit_search *limit)
  246. {
  247. if (data->perm == PERM_ROOT)
  248. return (limit->flags & BY_USERID) ?
  249. data->userid == limit->userid : 1;
  250. if (data->perm == PERM_PRE_ROOT || data->perm == PERM_ANDROID)
  251. return 1;
  252. return 0;
  253. }
  254. static int needs_fixup(perm_t perm)
  255. {
  256. if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
  257. || perm == PERM_ANDROID_MEDIA)
  258. return 1;
  259. return 0;
  260. }
  261. static void __fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit, int depth)
  262. {
  263. struct dentry *child;
  264. struct sdcardfs_inode_info *info;
  265. /*
  266. * All paths will terminate their recursion on hitting PERM_ANDROID_OBB,
  267. * PERM_ANDROID_MEDIA, or PERM_ANDROID_DATA. This happens at a depth of
  268. * at most 3.
  269. */
  270. WARN(depth > 3, "%s: Max expected depth exceeded!\n", __func__);
  271. spin_lock_nested(&dentry->d_lock, depth);
  272. if (!dentry->d_inode) {
  273. spin_unlock(&dentry->d_lock);
  274. return;
  275. }
  276. info = SDCARDFS_I(dentry->d_inode);
  277. if (needs_fixup(info->data->perm)) {
  278. list_for_each_entry(child, &dentry->d_subdirs, d_child) {
  279. spin_lock_nested(&child->d_lock, depth + 1);
  280. if (!(limit->flags & BY_NAME) || qstr_case_eq(&child->d_name, &limit->name)) {
  281. if (child->d_inode) {
  282. get_derived_permission(dentry, child);
  283. fixup_tmp_permissions(child->d_inode);
  284. spin_unlock(&child->d_lock);
  285. break;
  286. }
  287. }
  288. spin_unlock(&child->d_lock);
  289. }
  290. } else if (descendant_may_need_fixup(info->data, limit)) {
  291. list_for_each_entry(child, &dentry->d_subdirs, d_child) {
  292. __fixup_perms_recursive(child, limit, depth + 1);
  293. }
  294. }
  295. spin_unlock(&dentry->d_lock);
  296. }
  297. void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit)
  298. {
  299. __fixup_perms_recursive(dentry, limit, 0);
  300. }
  301. /* main function for updating derived permission */
  302. inline void update_derived_permission_lock(struct dentry *dentry)
  303. {
  304. struct dentry *parent;
  305. if (!dentry || !dentry->d_inode) {
  306. pr_err("sdcardfs: %s: invalid dentry\n", __func__);
  307. return;
  308. }
  309. /* FIXME:
  310. * 1. need to check whether the dentry is updated or not
  311. * 2. remove the root dentry update
  312. */
  313. if (!IS_ROOT(dentry)) {
  314. parent = dget_parent(dentry);
  315. if (parent) {
  316. get_derived_permission(parent, dentry);
  317. dput(parent);
  318. }
  319. }
  320. fixup_tmp_permissions(dentry->d_inode);
  321. }
  322. int need_graft_path(struct dentry *dentry)
  323. {
  324. int ret = 0;
  325. struct dentry *parent = dget_parent(dentry);
  326. struct sdcardfs_inode_info *parent_info = SDCARDFS_I(parent->d_inode);
  327. struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
  328. struct qstr obb = QSTR_LITERAL("obb");
  329. if (!sbi->options.unshared_obb &&
  330. parent_info->data->perm == PERM_ANDROID &&
  331. qstr_case_eq(&dentry->d_name, &obb)) {
  332. /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
  333. if (!(sbi->options.multiuser == false
  334. && parent_info->data->userid == 0)) {
  335. ret = 1;
  336. }
  337. }
  338. dput(parent);
  339. return ret;
  340. }
  341. int is_obbpath_invalid(struct dentry *dent)
  342. {
  343. int ret = 0;
  344. struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
  345. struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
  346. char *path_buf, *obbpath_s;
  347. int need_put = 0;
  348. struct path lower_path;
  349. /* check the base obbpath has been changed.
  350. * this routine can check an uninitialized obb dentry as well.
  351. * regarding the uninitialized obb, refer to the sdcardfs_mkdir()
  352. */
  353. spin_lock(&di->lock);
  354. if (di->orig_path.dentry) {
  355. if (!di->lower_path.dentry) {
  356. ret = 1;
  357. } else {
  358. path_get(&di->lower_path);
  359. path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
  360. if (!path_buf) {
  361. ret = 1;
  362. pr_err("sdcardfs: fail to allocate path_buf in %s.\n", __func__);
  363. } else {
  364. obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
  365. if (d_unhashed(di->lower_path.dentry) ||
  366. !str_case_eq(sbi->obbpath_s, obbpath_s)) {
  367. ret = 1;
  368. }
  369. kfree(path_buf);
  370. }
  371. pathcpy(&lower_path, &di->lower_path);
  372. need_put = 1;
  373. }
  374. }
  375. spin_unlock(&di->lock);
  376. if (need_put)
  377. path_put(&lower_path);
  378. return ret;
  379. }
  380. int is_base_obbpath(struct dentry *dentry)
  381. {
  382. int ret = 0;
  383. struct dentry *parent = dget_parent(dentry);
  384. struct sdcardfs_inode_info *parent_info = SDCARDFS_I(parent->d_inode);
  385. struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
  386. struct qstr q_obb = QSTR_LITERAL("obb");
  387. spin_lock(&SDCARDFS_D(dentry)->lock);
  388. if (sbi->options.multiuser) {
  389. if (parent_info->data->perm == PERM_PRE_ROOT &&
  390. qstr_case_eq(&dentry->d_name, &q_obb)) {
  391. ret = 1;
  392. }
  393. } else if (parent_info->data->perm == PERM_ANDROID &&
  394. qstr_case_eq(&dentry->d_name, &q_obb)) {
  395. ret = 1;
  396. }
  397. spin_unlock(&SDCARDFS_D(dentry)->lock);
  398. return ret;
  399. }
  400. /* The lower_path will be stored to the dentry's orig_path
  401. * and the base obbpath will be copyed to the lower_path variable.
  402. * if an error returned, there's no change in the lower_path
  403. * returns: -ERRNO if error (0: no error)
  404. */
  405. int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
  406. {
  407. int err = 0;
  408. struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
  409. struct path obbpath;
  410. /* A local obb dentry must have its own orig_path to support rmdir
  411. * and mkdir of itself. Usually, we expect that the sbi->obbpath
  412. * is avaiable on this stage.
  413. */
  414. sdcardfs_set_orig_path(dentry, lower_path);
  415. err = kern_path(sbi->obbpath_s,
  416. LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
  417. if (!err) {
  418. /* the obbpath base has been found */
  419. pathcpy(lower_path, &obbpath);
  420. } else {
  421. /* if the sbi->obbpath is not available, we can optionally
  422. * setup the lower_path with its orig_path.
  423. * but, the current implementation just returns an error
  424. * because the sdcard daemon also regards this case as
  425. * a lookup fail.
  426. */
  427. pr_info("sdcardfs: the sbi->obbpath is not available\n");
  428. }
  429. return err;
  430. }