binderfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/compiler.h>
  3. #include <linux/errno.h>
  4. #include <linux/fs.h>
  5. #include <linux/fsnotify.h>
  6. #include <linux/gfp.h>
  7. #include <linux/idr.h>
  8. #include <linux/init.h>
  9. #include <linux/ipc_namespace.h>
  10. #include <linux/kdev_t.h>
  11. #include <linux/kernel.h>
  12. #include <linux/list.h>
  13. #include <linux/namei.h>
  14. #include <linux/magic.h>
  15. #include <linux/major.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/mount.h>
  20. #include <linux/parser.h>
  21. #include <linux/radix-tree.h>
  22. #include <linux/sched.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock_types.h>
  26. #include <linux/stddef.h>
  27. #include <linux/string.h>
  28. #include <linux/time.h>
  29. #include <linux/types.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/user_namespace.h>
  32. #include <asm-generic/errno-base.h>
  33. #include <uapi/linux/android/binder.h>
  34. #include <uapi/linux/android/binderfs.h>
  35. #include "binder_internal.h"
  36. #define FIRST_INODE 1
  37. #define SECOND_INODE 2
  38. #define INODE_OFFSET 3
  39. #define INTSTRLEN 21
  40. #define BINDERFS_MAX_MINOR (1U << MINORBITS)
  41. /* Ensure that the initial ipc namespace always has devices available. */
  42. #define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)
  43. /* Import some functions to 3.4 kernel */
  44. static inline void inode_lock(struct inode *inode)
  45. {
  46. mutex_lock(&inode->i_mutex);
  47. }
  48. static inline void inode_unlock(struct inode *inode)
  49. {
  50. mutex_unlock(&inode->i_mutex);
  51. }
  52. static inline bool d_really_is_positive(const struct dentry *dentry)
  53. {
  54. return dentry->d_inode != NULL;
  55. }
  56. static inline int simple_positive(struct dentry *dentry)
  57. {
  58. return d_really_is_positive(dentry) && !d_unhashed(dentry);
  59. }
  60. static inline void clear_inode(struct inode *inode)
  61. {
  62. end_writeback(inode);
  63. }
  64. struct timespec current_time(struct inode *inode)
  65. {
  66. struct timespec now = current_kernel_time();
  67. if (unlikely(!inode->i_sb)) {
  68. WARN(1, "current_time() called with uninitialized super_block in the inode");
  69. return now;
  70. }
  71. return timespec_trunc(now, inode->i_sb->s_time_gran);
  72. }
  73. static dev_t binderfs_dev;
  74. static DEFINE_MUTEX(binderfs_minors_mutex);
  75. static DEFINE_IDA(binderfs_minors);
  76. enum {
  77. Opt_max,
  78. Opt_stats_mode,
  79. Opt_err
  80. };
  81. enum binderfs_stats_mode {
  82. STATS_NONE,
  83. STATS_GLOBAL,
  84. };
  85. static const match_table_t tokens = {
  86. { Opt_max, "max=%d" },
  87. { Opt_stats_mode, "stats=%s" },
  88. { Opt_err, NULL }
  89. };
  90. static inline struct binderfs_info *BINDERFS_I(const struct inode *inode)
  91. {
  92. return inode->i_sb->s_fs_info;
  93. }
  94. bool is_binderfs_device(const struct inode *inode)
  95. {
  96. if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
  97. return true;
  98. return false;
  99. }
  100. /**
  101. * binderfs_binder_device_create - allocate inode from super block of a
  102. * binderfs mount
  103. * @ref_inode: inode from wich the super block will be taken
  104. * @userp: buffer to copy information about new device for userspace to
  105. * @req: struct binderfs_device as copied from userspace
  106. *
  107. * This function allocates a new binder_device and reserves a new minor
  108. * number for it.
  109. * Minor numbers are limited and tracked globally in binderfs_minors. The
  110. * function will stash a struct binder_device for the specific binder
  111. * device in i_private of the inode.
  112. * It will go on to allocate a new inode from the super block of the
  113. * filesystem mount, stash a struct binder_device in its i_private field
  114. * and attach a dentry to that inode.
  115. *
  116. * Return: 0 on success, negative errno on failure
  117. */
  118. static int binderfs_binder_device_create(struct inode *ref_inode,
  119. struct binderfs_device __user *userp,
  120. struct binderfs_device *req)
  121. {
  122. int minor, ret;
  123. struct dentry *dentry, *root;
  124. struct binder_device *device;
  125. char *name = NULL;
  126. size_t name_len;
  127. struct inode *inode = NULL;
  128. struct super_block *sb = ref_inode->i_sb;
  129. struct binderfs_info *info = sb->s_fs_info;
  130. #if defined(CONFIG_IPC_NS)
  131. bool use_reserve = (info->ipc_ns == &init_ipc_ns);
  132. #else
  133. bool use_reserve = true;
  134. #endif
  135. /* Reserve new minor number for the new device. */
  136. mutex_lock(&binderfs_minors_mutex);
  137. if (++info->device_count <= info->mount_opts.max)
  138. minor = ida_simple_get(&binderfs_minors, 0,
  139. use_reserve ? BINDERFS_MAX_MINOR :
  140. BINDERFS_MAX_MINOR_CAPPED,
  141. GFP_KERNEL);
  142. else
  143. minor = -ENOSPC;
  144. if (minor < 0) {
  145. --info->device_count;
  146. mutex_unlock(&binderfs_minors_mutex);
  147. return minor;
  148. }
  149. mutex_unlock(&binderfs_minors_mutex);
  150. ret = -ENOMEM;
  151. device = kzalloc(sizeof(*device), GFP_KERNEL);
  152. if (!device)
  153. goto err;
  154. inode = new_inode(sb);
  155. if (!inode)
  156. goto err;
  157. inode->i_ino = minor + INODE_OFFSET;
  158. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  159. init_special_inode(inode, S_IFCHR | 0600,
  160. MKDEV(MAJOR(binderfs_dev), minor));
  161. inode->i_fop = &binder_fops;
  162. inode->i_uid = info->root_uid;
  163. inode->i_gid = info->root_gid;
  164. req->name[BINDERFS_MAX_NAME] = '\0'; /* NUL-terminate */
  165. name_len = strlen(req->name);
  166. /* Make sure to include terminating NUL byte */
  167. name = kmemdup(req->name, name_len + 1, GFP_KERNEL);
  168. if (!name)
  169. goto err;
  170. refcount_set(&device->ref, 1);
  171. device->binderfs_inode = inode;
  172. device->context.binder_context_mgr_uid = INVALID_UID;
  173. device->context.name = name;
  174. device->miscdev.name = name;
  175. device->miscdev.minor = minor;
  176. mutex_init(&device->context.context_mgr_node_lock);
  177. req->major = MAJOR(binderfs_dev);
  178. req->minor = minor;
  179. if (userp && copy_to_user(userp, req, sizeof(*req))) {
  180. ret = -EFAULT;
  181. goto err;
  182. }
  183. root = sb->s_root;
  184. inode_lock(d_inode(root));
  185. /* look it up */
  186. dentry = lookup_one_len(name, root, name_len);
  187. if (IS_ERR(dentry)) {
  188. inode_unlock(d_inode(root));
  189. ret = PTR_ERR(dentry);
  190. goto err;
  191. }
  192. if (d_really_is_positive(dentry)) {
  193. /* already exists */
  194. dput(dentry);
  195. inode_unlock(d_inode(root));
  196. ret = -EEXIST;
  197. goto err;
  198. }
  199. inode->i_private = device;
  200. d_instantiate(dentry, inode);
  201. fsnotify_create(root->d_inode, dentry);
  202. inode_unlock(d_inode(root));
  203. return 0;
  204. err:
  205. kfree(name);
  206. kfree(device);
  207. mutex_lock(&binderfs_minors_mutex);
  208. --info->device_count;
  209. ida_remove(&binderfs_minors, minor);
  210. mutex_unlock(&binderfs_minors_mutex);
  211. iput(inode);
  212. return ret;
  213. }
  214. /**
  215. * binderfs_ctl_ioctl - handle binder device node allocation requests
  216. *
  217. * The request handler for the binder-control device. All requests operate on
  218. * the binderfs mount the binder-control device resides in:
  219. * - BINDER_CTL_ADD
  220. * Allocate a new binder device.
  221. *
  222. * Return: 0 on success, negative errno on failure
  223. */
  224. static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
  225. unsigned long arg)
  226. {
  227. int ret = -EINVAL;
  228. struct inode *inode = file_inode(file);
  229. struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
  230. struct binderfs_device device_req;
  231. switch (cmd) {
  232. case BINDER_CTL_ADD:
  233. ret = copy_from_user(&device_req, device, sizeof(device_req));
  234. if (ret) {
  235. ret = -EFAULT;
  236. break;
  237. }
  238. ret = binderfs_binder_device_create(inode, device, &device_req);
  239. break;
  240. default:
  241. break;
  242. }
  243. return ret;
  244. }
  245. static void binderfs_evict_inode(struct inode *inode)
  246. {
  247. struct binder_device *device = inode->i_private;
  248. struct binderfs_info *info = BINDERFS_I(inode);
  249. clear_inode(inode);
  250. if (!S_ISCHR(inode->i_mode) || !device)
  251. return;
  252. mutex_lock(&binderfs_minors_mutex);
  253. --info->device_count;
  254. ida_remove(&binderfs_minors, device->miscdev.minor);
  255. mutex_unlock(&binderfs_minors_mutex);
  256. if (refcount_dec_and_test(&device->ref)) {
  257. kfree(device->context.name);
  258. kfree(device);
  259. }
  260. }
  261. /**
  262. * binderfs_parse_mount_opts - parse binderfs mount options
  263. * @data: options to set (can be NULL in which case defaults are used)
  264. */
  265. static int binderfs_parse_mount_opts(char *data,
  266. struct binderfs_mount_opts *opts)
  267. {
  268. char *p, *stats;
  269. opts->max = BINDERFS_MAX_MINOR;
  270. opts->stats_mode = STATS_NONE;
  271. while ((p = strsep(&data, ",")) != NULL) {
  272. substring_t args[MAX_OPT_ARGS];
  273. int token;
  274. int max_devices;
  275. if (!*p)
  276. continue;
  277. token = match_token(p, tokens, args);
  278. switch (token) {
  279. case Opt_max:
  280. if (match_int(&args[0], &max_devices) ||
  281. (max_devices < 0 ||
  282. (max_devices > BINDERFS_MAX_MINOR)))
  283. return -EINVAL;
  284. opts->max = max_devices;
  285. break;
  286. case Opt_stats_mode:
  287. if (!capable(CAP_SYS_ADMIN))
  288. return -EINVAL;
  289. stats = match_strdup(&args[0]);
  290. if (!stats)
  291. return -ENOMEM;
  292. if (strcmp(stats, "global") != 0) {
  293. kfree(stats);
  294. return -EINVAL;
  295. }
  296. opts->stats_mode = STATS_GLOBAL;
  297. kfree(stats);
  298. break;
  299. default:
  300. pr_err("Invalid mount options\n");
  301. return -EINVAL;
  302. }
  303. }
  304. return 0;
  305. }
  306. static int binderfs_remount(struct super_block *sb, int *flags, char *data)
  307. {
  308. int prev_stats_mode, ret;
  309. struct binderfs_info *info = sb->s_fs_info;
  310. prev_stats_mode = info->mount_opts.stats_mode;
  311. ret = binderfs_parse_mount_opts(data, &info->mount_opts);
  312. if (ret)
  313. return ret;
  314. if (prev_stats_mode != info->mount_opts.stats_mode) {
  315. pr_err("Binderfs stats mode cannot be changed during a remount\n");
  316. info->mount_opts.stats_mode = prev_stats_mode;
  317. return -EINVAL;
  318. }
  319. return 0;
  320. }
  321. static int binderfs_show_mount_opts(struct seq_file *seq, struct dentry *root)
  322. {
  323. struct binderfs_info *info;
  324. info = root->d_sb->s_fs_info;
  325. if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
  326. seq_printf(seq, ",max=%d", info->mount_opts.max);
  327. if (info->mount_opts.stats_mode == STATS_GLOBAL)
  328. seq_printf(seq, ",stats=global");
  329. return 0;
  330. }
  331. static const struct super_operations binderfs_super_ops = {
  332. .evict_inode = binderfs_evict_inode,
  333. .remount_fs = binderfs_remount,
  334. .show_options = binderfs_show_mount_opts,
  335. .statfs = simple_statfs,
  336. };
  337. static inline bool is_binderfs_control_device(const struct dentry *dentry)
  338. {
  339. struct binderfs_info *info = dentry->d_sb->s_fs_info;
  340. return info->control_dentry == dentry;
  341. }
  342. static int binderfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  343. struct inode *new_dir, struct dentry *new_dentry)
  344. {
  345. if (is_binderfs_control_device(old_dentry) ||
  346. is_binderfs_control_device(new_dentry))
  347. return -EPERM;
  348. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  349. }
  350. static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
  351. {
  352. if (is_binderfs_control_device(dentry))
  353. return -EPERM;
  354. return simple_unlink(dir, dentry);
  355. }
  356. static const struct file_operations binder_ctl_fops = {
  357. .owner = THIS_MODULE,
  358. .open = nonseekable_open,
  359. .unlocked_ioctl = binder_ctl_ioctl,
  360. .compat_ioctl = binder_ctl_ioctl,
  361. .llseek = noop_llseek,
  362. };
  363. /**
  364. * binderfs_binder_ctl_create - create a new binder-control device
  365. * @sb: super block of the binderfs mount
  366. *
  367. * This function creates a new binder-control device node in the binderfs mount
  368. * referred to by @sb.
  369. *
  370. * Return: 0 on success, negative errno on failure
  371. */
  372. static int binderfs_binder_ctl_create(struct super_block *sb)
  373. {
  374. int minor, ret;
  375. struct dentry *dentry;
  376. struct binder_device *device;
  377. struct inode *inode = NULL;
  378. struct dentry *root = sb->s_root;
  379. struct binderfs_info *info = sb->s_fs_info;
  380. #if defined(CONFIG_IPC_NS)
  381. bool use_reserve = (info->ipc_ns == &init_ipc_ns);
  382. #else
  383. bool use_reserve = true;
  384. #endif
  385. device = kzalloc(sizeof(*device), GFP_KERNEL);
  386. if (!device)
  387. return -ENOMEM;
  388. /* If we have already created a binder-control node, return. */
  389. if (info->control_dentry) {
  390. ret = 0;
  391. goto out;
  392. }
  393. ret = -ENOMEM;
  394. inode = new_inode(sb);
  395. if (!inode)
  396. goto out;
  397. /* Reserve a new minor number for the new device. */
  398. mutex_lock(&binderfs_minors_mutex);
  399. minor = ida_simple_get(&binderfs_minors, 0,
  400. use_reserve ? BINDERFS_MAX_MINOR :
  401. BINDERFS_MAX_MINOR_CAPPED,
  402. GFP_KERNEL);
  403. mutex_unlock(&binderfs_minors_mutex);
  404. if (minor < 0) {
  405. ret = minor;
  406. goto out;
  407. }
  408. inode->i_ino = SECOND_INODE;
  409. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  410. init_special_inode(inode, S_IFCHR | 0600,
  411. MKDEV(MAJOR(binderfs_dev), minor));
  412. inode->i_fop = &binder_ctl_fops;
  413. inode->i_uid = info->root_uid;
  414. inode->i_gid = info->root_gid;
  415. refcount_set(&device->ref, 1);
  416. device->binderfs_inode = inode;
  417. device->miscdev.minor = minor;
  418. dentry = d_alloc_name(root, "binder-control");
  419. if (!dentry)
  420. goto out;
  421. inode->i_private = device;
  422. info->control_dentry = dentry;
  423. d_add(dentry, inode);
  424. return 0;
  425. out:
  426. kfree(device);
  427. iput(inode);
  428. return ret;
  429. }
  430. static const struct inode_operations binderfs_dir_inode_operations = {
  431. .lookup = simple_lookup,
  432. .rename = binderfs_rename,
  433. .unlink = binderfs_unlink,
  434. };
  435. static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
  436. {
  437. struct inode *ret;
  438. ret = new_inode(sb);
  439. if (ret) {
  440. ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
  441. ret->i_mode = mode;
  442. ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
  443. }
  444. return ret;
  445. }
  446. static struct dentry *binderfs_create_dentry(struct dentry *parent,
  447. const char *name)
  448. {
  449. struct dentry *dentry;
  450. dentry = lookup_one_len(name, parent, strlen(name));
  451. if (IS_ERR(dentry))
  452. return dentry;
  453. /* Return error if the file/dir already exists. */
  454. if (d_really_is_positive(dentry)) {
  455. dput(dentry);
  456. return ERR_PTR(-EEXIST);
  457. }
  458. return dentry;
  459. }
  460. void binderfs_remove_file(struct dentry *dentry)
  461. {
  462. struct inode *parent_inode;
  463. parent_inode = d_inode(dentry->d_parent);
  464. inode_lock(parent_inode);
  465. if (simple_positive(dentry)) {
  466. dget(dentry);
  467. simple_unlink(parent_inode, dentry);
  468. d_delete(dentry);
  469. dput(dentry);
  470. }
  471. inode_unlock(parent_inode);
  472. }
  473. struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
  474. const struct file_operations *fops,
  475. void *data)
  476. {
  477. struct dentry *dentry;
  478. struct inode *new_inode, *parent_inode;
  479. struct super_block *sb;
  480. parent_inode = d_inode(parent);
  481. inode_lock(parent_inode);
  482. dentry = binderfs_create_dentry(parent, name);
  483. if (IS_ERR(dentry))
  484. goto out;
  485. sb = parent_inode->i_sb;
  486. new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
  487. if (!new_inode) {
  488. dput(dentry);
  489. dentry = ERR_PTR(-ENOMEM);
  490. goto out;
  491. }
  492. new_inode->i_fop = fops;
  493. new_inode->i_private = data;
  494. d_instantiate(dentry, new_inode);
  495. fsnotify_create(parent_inode, dentry);
  496. out:
  497. inode_unlock(parent_inode);
  498. return dentry;
  499. }
  500. static struct dentry *binderfs_create_dir(struct dentry *parent,
  501. const char *name)
  502. {
  503. struct dentry *dentry;
  504. struct inode *new_inode, *parent_inode;
  505. struct super_block *sb;
  506. parent_inode = d_inode(parent);
  507. inode_lock(parent_inode);
  508. dentry = binderfs_create_dentry(parent, name);
  509. if (IS_ERR(dentry))
  510. goto out;
  511. sb = parent_inode->i_sb;
  512. new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
  513. if (!new_inode) {
  514. dput(dentry);
  515. dentry = ERR_PTR(-ENOMEM);
  516. goto out;
  517. }
  518. new_inode->i_fop = &simple_dir_operations;
  519. new_inode->i_op = &simple_dir_inode_operations;
  520. set_nlink(new_inode, 2);
  521. d_instantiate(dentry, new_inode);
  522. inc_nlink(parent_inode);
  523. fsnotify_mkdir(parent_inode, dentry);
  524. out:
  525. inode_unlock(parent_inode);
  526. return dentry;
  527. }
  528. static int init_binder_logs(struct super_block *sb)
  529. {
  530. struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
  531. struct binderfs_info *info;
  532. int ret = 0;
  533. binder_logs_root_dir = binderfs_create_dir(sb->s_root,
  534. "binder_logs");
  535. if (IS_ERR(binder_logs_root_dir)) {
  536. ret = PTR_ERR(binder_logs_root_dir);
  537. goto out;
  538. }
  539. dentry = binderfs_create_file(binder_logs_root_dir, "stats",
  540. &binder_stats_fops, NULL);
  541. if (IS_ERR(dentry)) {
  542. ret = PTR_ERR(dentry);
  543. goto out;
  544. }
  545. dentry = binderfs_create_file(binder_logs_root_dir, "state",
  546. &binder_state_fops, NULL);
  547. if (IS_ERR(dentry)) {
  548. ret = PTR_ERR(dentry);
  549. goto out;
  550. }
  551. dentry = binderfs_create_file(binder_logs_root_dir, "transactions",
  552. &binder_transactions_fops, NULL);
  553. if (IS_ERR(dentry)) {
  554. ret = PTR_ERR(dentry);
  555. goto out;
  556. }
  557. dentry = binderfs_create_file(binder_logs_root_dir,
  558. "transaction_log",
  559. &binder_transaction_log_fops,
  560. &binder_transaction_log);
  561. if (IS_ERR(dentry)) {
  562. ret = PTR_ERR(dentry);
  563. goto out;
  564. }
  565. dentry = binderfs_create_file(binder_logs_root_dir,
  566. "failed_transaction_log",
  567. &binder_transaction_log_fops,
  568. &binder_transaction_log_failed);
  569. if (IS_ERR(dentry)) {
  570. ret = PTR_ERR(dentry);
  571. goto out;
  572. }
  573. proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");
  574. if (IS_ERR(proc_log_dir)) {
  575. ret = PTR_ERR(proc_log_dir);
  576. goto out;
  577. }
  578. info = sb->s_fs_info;
  579. info->proc_log_dir = proc_log_dir;
  580. out:
  581. return ret;
  582. }
  583. static int binderfs_fill_super(struct super_block *sb, void *data, int silent)
  584. {
  585. int ret;
  586. struct binderfs_info *info;
  587. struct inode *inode = NULL;
  588. struct binderfs_device device_info = { { 0 } };
  589. const char *name;
  590. size_t len;
  591. sb->s_blocksize = PAGE_SIZE;
  592. sb->s_blocksize_bits = PAGE_SHIFT;
  593. /*
  594. * The binderfs filesystem can be mounted by userns root in a
  595. * non-initial userns. By default such mounts have the SB_I_NODEV flag
  596. * set in s_iflags to prevent security issues where userns root can
  597. * just create random device nodes via mknod() since it owns the
  598. * filesystem mount. But binderfs does not allow to create any files
  599. * including devices nodes. The only way to create binder devices nodes
  600. * is through the binder-control device which userns root is explicitly
  601. * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
  602. * necessary and safe.
  603. */
  604. sb->s_magic = BINDERFS_SUPER_MAGIC;
  605. sb->s_op = &binderfs_super_ops;
  606. sb->s_time_gran = 1;
  607. sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
  608. if (!sb->s_fs_info)
  609. return -ENOMEM;
  610. info = sb->s_fs_info;
  611. info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
  612. ret = binderfs_parse_mount_opts(data, &info->mount_opts);
  613. if (ret)
  614. return ret;
  615. info->root_gid = GLOBAL_ROOT_GID;
  616. info->root_uid = GLOBAL_ROOT_UID;
  617. inode = new_inode(sb);
  618. if (!inode)
  619. return -ENOMEM;
  620. inode->i_ino = FIRST_INODE;
  621. inode->i_fop = &simple_dir_operations;
  622. inode->i_mode = S_IFDIR | 0755;
  623. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  624. inode->i_op = &binderfs_dir_inode_operations;
  625. set_nlink(inode, 2);
  626. sb->s_root = d_make_root(inode);
  627. if (!sb->s_root)
  628. return -ENOMEM;
  629. ret = binderfs_binder_ctl_create(sb);
  630. if (ret)
  631. return ret;
  632. name = binder_devices_param;
  633. for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
  634. strlcpy(device_info.name, name, len + 1);
  635. ret = binderfs_binder_device_create(inode, NULL, &device_info);
  636. if (ret)
  637. return ret;
  638. name += len;
  639. if (*name == ',')
  640. name++;
  641. }
  642. if (info->mount_opts.stats_mode == STATS_GLOBAL)
  643. return init_binder_logs(sb);
  644. return 0;
  645. }
  646. static struct dentry *binderfs_mount(struct file_system_type *fs_type,
  647. int flags, const char *dev_name,
  648. void *data)
  649. {
  650. return mount_nodev(fs_type, flags, data, binderfs_fill_super);
  651. }
  652. static void binderfs_kill_super(struct super_block *sb)
  653. {
  654. struct binderfs_info *info = sb->s_fs_info;
  655. kill_litter_super(sb);
  656. if (info && info->ipc_ns)
  657. put_ipc_ns(info->ipc_ns);
  658. kfree(info);
  659. }
  660. static struct file_system_type binder_fs_type = {
  661. .name = "binder",
  662. .mount = binderfs_mount,
  663. .kill_sb = binderfs_kill_super,
  664. .fs_flags = FS_USERNS_MOUNT,
  665. };
  666. int __init init_binderfs(void)
  667. {
  668. int ret;
  669. const char *name;
  670. size_t len;
  671. /* Verify that the default binderfs device names are valid. */
  672. name = binder_devices_param;
  673. for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
  674. if (len > BINDERFS_MAX_NAME)
  675. return -E2BIG;
  676. name += len;
  677. if (*name == ',')
  678. name++;
  679. }
  680. /* Allocate new major number for binderfs. */
  681. ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
  682. "binder");
  683. if (ret)
  684. return ret;
  685. ret = register_filesystem(&binder_fs_type);
  686. if (ret) {
  687. unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
  688. return ret;
  689. }
  690. return ret;
  691. }