binderfs.c 19 KB

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