binderfs.c 20 KB

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