rust_binderfs.c 19 KB

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