cuse.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CUSE: Character device in Userspace
  4. *
  5. * Copyright (C) 2008-2009 SUSE Linux Products GmbH
  6. * Copyright (C) 2008-2009 Tejun Heo <tj@kernel.org>
  7. *
  8. * CUSE enables character devices to be implemented from userland much
  9. * like FUSE allows filesystems. On initialization /dev/cuse is
  10. * created. By opening the file and replying to the CUSE_INIT request
  11. * userland CUSE server can create a character device. After that the
  12. * operation is very similar to FUSE.
  13. *
  14. * A CUSE instance involves the following objects.
  15. *
  16. * cuse_conn : contains fuse_conn and serves as bonding structure
  17. * channel : file handle connected to the userland CUSE server
  18. * cdev : the implemented character device
  19. * dev : generic device for cdev
  20. *
  21. * Note that 'channel' is what 'dev' is in FUSE. As CUSE deals with
  22. * devices, it's called 'channel' to reduce confusion.
  23. *
  24. * channel determines when the character device dies. When channel is
  25. * closed, everything begins to destruct. The cuse_conn is taken off
  26. * the lookup table preventing further access from cdev, cdev and
  27. * generic device are removed and the base reference of cuse_conn is
  28. * put.
  29. *
  30. * On each open, the matching cuse_conn is looked up and if found an
  31. * additional reference is taken which is released when the file is
  32. * closed.
  33. */
  34. #define pr_fmt(fmt) "CUSE: " fmt
  35. #include <linux/fuse.h>
  36. #include <linux/cdev.h>
  37. #include <linux/device.h>
  38. #include <linux/file.h>
  39. #include <linux/fs.h>
  40. #include <linux/kdev_t.h>
  41. #include <linux/kthread.h>
  42. #include <linux/list.h>
  43. #include <linux/magic.h>
  44. #include <linux/miscdevice.h>
  45. #include <linux/mutex.h>
  46. #include <linux/slab.h>
  47. #include <linux/stat.h>
  48. #include <linux/module.h>
  49. #include <linux/uio.h>
  50. #include <linux/user_namespace.h>
  51. #include "fuse_i.h"
  52. #include "fuse_dev_i.h"
  53. #define CUSE_CONNTBL_LEN 64
  54. struct cuse_conn {
  55. struct list_head list; /* linked on cuse_conntbl */
  56. struct fuse_mount fm; /* Dummy mount referencing fc */
  57. struct fuse_conn fc; /* fuse connection */
  58. struct cdev *cdev; /* associated character device */
  59. struct device *dev; /* device representing @cdev */
  60. /* init parameters, set once during initialization */
  61. bool unrestricted_ioctl;
  62. };
  63. static DEFINE_MUTEX(cuse_lock); /* protects registration */
  64. static struct list_head cuse_conntbl[CUSE_CONNTBL_LEN];
  65. static struct class *cuse_class;
  66. static struct cuse_conn *fc_to_cc(struct fuse_conn *fc)
  67. {
  68. return container_of(fc, struct cuse_conn, fc);
  69. }
  70. static struct list_head *cuse_conntbl_head(dev_t devt)
  71. {
  72. return &cuse_conntbl[(MAJOR(devt) + MINOR(devt)) % CUSE_CONNTBL_LEN];
  73. }
  74. /**************************************************************************
  75. * CUSE frontend operations
  76. *
  77. * These are file operations for the character device.
  78. *
  79. * On open, CUSE opens a file from the FUSE mnt and stores it to
  80. * private_data of the open file. All other ops call FUSE ops on the
  81. * FUSE file.
  82. */
  83. static ssize_t cuse_read_iter(struct kiocb *kiocb, struct iov_iter *to)
  84. {
  85. struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(kiocb);
  86. loff_t pos = 0;
  87. return fuse_direct_io(&io, to, &pos, FUSE_DIO_CUSE);
  88. }
  89. static ssize_t cuse_write_iter(struct kiocb *kiocb, struct iov_iter *from)
  90. {
  91. struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(kiocb);
  92. loff_t pos = 0;
  93. /*
  94. * No locking or generic_write_checks(), the server is
  95. * responsible for locking and sanity checks.
  96. */
  97. return fuse_direct_io(&io, from, &pos,
  98. FUSE_DIO_WRITE | FUSE_DIO_CUSE);
  99. }
  100. static int cuse_open(struct inode *inode, struct file *file)
  101. {
  102. dev_t devt = inode->i_cdev->dev;
  103. struct cuse_conn *cc = NULL, *pos;
  104. int rc;
  105. /* look up and get the connection */
  106. mutex_lock(&cuse_lock);
  107. list_for_each_entry(pos, cuse_conntbl_head(devt), list)
  108. if (pos->dev->devt == devt) {
  109. fuse_conn_get(&pos->fc);
  110. cc = pos;
  111. break;
  112. }
  113. mutex_unlock(&cuse_lock);
  114. /* dead? */
  115. if (!cc)
  116. return -ENODEV;
  117. /*
  118. * Generic permission check is already done against the chrdev
  119. * file, proceed to open.
  120. */
  121. rc = fuse_do_open(&cc->fm, 0, file, 0);
  122. if (rc)
  123. fuse_conn_put(&cc->fc);
  124. return rc;
  125. }
  126. static int cuse_release(struct inode *inode, struct file *file)
  127. {
  128. struct fuse_file *ff = file->private_data;
  129. struct fuse_mount *fm = ff->fm;
  130. fuse_sync_release(NULL, ff, file->f_flags);
  131. fuse_conn_put(fm->fc);
  132. return 0;
  133. }
  134. static long cuse_file_ioctl(struct file *file, unsigned int cmd,
  135. unsigned long arg)
  136. {
  137. struct fuse_file *ff = file->private_data;
  138. struct cuse_conn *cc = fc_to_cc(ff->fm->fc);
  139. unsigned int flags = 0;
  140. if (cc->unrestricted_ioctl)
  141. flags |= FUSE_IOCTL_UNRESTRICTED;
  142. return fuse_do_ioctl(file, cmd, arg, flags);
  143. }
  144. static long cuse_file_compat_ioctl(struct file *file, unsigned int cmd,
  145. unsigned long arg)
  146. {
  147. struct fuse_file *ff = file->private_data;
  148. struct cuse_conn *cc = fc_to_cc(ff->fm->fc);
  149. unsigned int flags = FUSE_IOCTL_COMPAT;
  150. if (cc->unrestricted_ioctl)
  151. flags |= FUSE_IOCTL_UNRESTRICTED;
  152. return fuse_do_ioctl(file, cmd, arg, flags);
  153. }
  154. static const struct file_operations cuse_frontend_fops = {
  155. .owner = THIS_MODULE,
  156. .read_iter = cuse_read_iter,
  157. .write_iter = cuse_write_iter,
  158. .open = cuse_open,
  159. .release = cuse_release,
  160. .unlocked_ioctl = cuse_file_ioctl,
  161. .compat_ioctl = cuse_file_compat_ioctl,
  162. .poll = fuse_file_poll,
  163. .llseek = noop_llseek,
  164. };
  165. /**************************************************************************
  166. * CUSE channel initialization and destruction
  167. */
  168. struct cuse_devinfo {
  169. const char *name;
  170. };
  171. /**
  172. * cuse_parse_one - parse one key=value pair
  173. * @pp: i/o parameter for the current position
  174. * @end: points to one past the end of the packed string
  175. * @keyp: out parameter for key
  176. * @valp: out parameter for value
  177. *
  178. * *@pp points to packed strings - "key0=val0\0key1=val1\0" which ends
  179. * at @end - 1. This function parses one pair and set *@keyp to the
  180. * start of the key and *@valp to the start of the value. Note that
  181. * the original string is modified such that the key string is
  182. * terminated with '\0'. *@pp is updated to point to the next string.
  183. *
  184. * RETURNS:
  185. * 1 on successful parse, 0 on EOF, -errno on failure.
  186. */
  187. static int cuse_parse_one(char **pp, char *end, char **keyp, char **valp)
  188. {
  189. char *p = *pp;
  190. char *key, *val;
  191. while (p < end && *p == '\0')
  192. p++;
  193. if (p == end)
  194. return 0;
  195. if (end[-1] != '\0') {
  196. pr_err("info not properly terminated\n");
  197. return -EINVAL;
  198. }
  199. key = val = p;
  200. p += strlen(p);
  201. if (valp) {
  202. strsep(&val, "=");
  203. if (!val)
  204. val = key + strlen(key);
  205. key = strstrip(key);
  206. val = strstrip(val);
  207. } else
  208. key = strstrip(key);
  209. if (!strlen(key)) {
  210. pr_err("zero length info key specified\n");
  211. return -EINVAL;
  212. }
  213. *pp = p;
  214. *keyp = key;
  215. if (valp)
  216. *valp = val;
  217. return 1;
  218. }
  219. /**
  220. * cuse_parse_devinfo - parse device info
  221. * @p: device info string
  222. * @len: length of device info string
  223. * @devinfo: out parameter for parsed device info
  224. *
  225. * Parse @p to extract device info and store it into @devinfo. String
  226. * pointed to by @p is modified by parsing and @devinfo points into
  227. * them, so @p shouldn't be freed while @devinfo is in use.
  228. *
  229. * RETURNS:
  230. * 0 on success, -errno on failure.
  231. */
  232. static int cuse_parse_devinfo(char *p, size_t len, struct cuse_devinfo *devinfo)
  233. {
  234. char *end = p + len;
  235. char *key, *val;
  236. int rc;
  237. while (true) {
  238. rc = cuse_parse_one(&p, end, &key, &val);
  239. if (rc < 0)
  240. return rc;
  241. if (!rc)
  242. break;
  243. if (strcmp(key, "DEVNAME") == 0)
  244. devinfo->name = val;
  245. else
  246. pr_warn("unknown device info \"%s\"\n", key);
  247. }
  248. if (!devinfo->name || !strlen(devinfo->name)) {
  249. pr_err("DEVNAME unspecified\n");
  250. return -EINVAL;
  251. }
  252. return 0;
  253. }
  254. static void cuse_gendev_release(struct device *dev)
  255. {
  256. kfree(dev);
  257. }
  258. struct cuse_init_args {
  259. struct fuse_args_pages ap;
  260. struct cuse_init_in in;
  261. struct cuse_init_out out;
  262. struct folio *folio;
  263. struct fuse_folio_desc desc;
  264. };
  265. /**
  266. * cuse_process_init_reply - finish initializing CUSE channel
  267. *
  268. * @fm: The fuse mount information containing the CUSE connection.
  269. * @args: The arguments passed to the init reply.
  270. * @error: The error code signifying if any error occurred during the process.
  271. *
  272. * This function creates the character device and sets up all the
  273. * required data structures for it. Please read the comment at the
  274. * top of this file for high level overview.
  275. */
  276. static void cuse_process_init_reply(struct fuse_mount *fm,
  277. struct fuse_args *args, int error)
  278. {
  279. struct fuse_conn *fc = fm->fc;
  280. struct cuse_init_args *ia = container_of(args, typeof(*ia), ap.args);
  281. struct fuse_args_pages *ap = &ia->ap;
  282. struct cuse_conn *cc = fc_to_cc(fc), *pos;
  283. struct cuse_init_out *arg = &ia->out;
  284. struct folio *folio = ap->folios[0];
  285. struct cuse_devinfo devinfo = { };
  286. struct device *dev;
  287. struct cdev *cdev;
  288. dev_t devt;
  289. int rc, i;
  290. if (error || arg->major != FUSE_KERNEL_VERSION || arg->minor < 11)
  291. goto err;
  292. fc->minor = arg->minor;
  293. fc->max_read = max_t(unsigned, arg->max_read, 4096);
  294. fc->max_write = max_t(unsigned, arg->max_write, 4096);
  295. /* parse init reply */
  296. cc->unrestricted_ioctl = arg->flags & CUSE_UNRESTRICTED_IOCTL;
  297. rc = cuse_parse_devinfo(folio_address(folio), ap->args.out_args[1].size,
  298. &devinfo);
  299. if (rc)
  300. goto err;
  301. /* determine and reserve devt */
  302. devt = MKDEV(arg->dev_major, arg->dev_minor);
  303. if (!MAJOR(devt))
  304. rc = alloc_chrdev_region(&devt, MINOR(devt), 1, devinfo.name);
  305. else
  306. rc = register_chrdev_region(devt, 1, devinfo.name);
  307. if (rc) {
  308. pr_err("failed to register chrdev region\n");
  309. goto err;
  310. }
  311. /* devt determined, create device */
  312. rc = -ENOMEM;
  313. dev = kzalloc_obj(*dev);
  314. if (!dev)
  315. goto err_region;
  316. device_initialize(dev);
  317. dev_set_uevent_suppress(dev, 1);
  318. dev->class = cuse_class;
  319. dev->devt = devt;
  320. dev->release = cuse_gendev_release;
  321. dev_set_drvdata(dev, cc);
  322. dev_set_name(dev, "%s", devinfo.name);
  323. mutex_lock(&cuse_lock);
  324. /* make sure the device-name is unique */
  325. for (i = 0; i < CUSE_CONNTBL_LEN; ++i) {
  326. list_for_each_entry(pos, &cuse_conntbl[i], list)
  327. if (!strcmp(dev_name(pos->dev), dev_name(dev)))
  328. goto err_unlock;
  329. }
  330. rc = device_add(dev);
  331. if (rc)
  332. goto err_unlock;
  333. /* register cdev */
  334. rc = -ENOMEM;
  335. cdev = cdev_alloc();
  336. if (!cdev)
  337. goto err_unlock;
  338. cdev->owner = THIS_MODULE;
  339. cdev->ops = &cuse_frontend_fops;
  340. rc = cdev_add(cdev, devt, 1);
  341. if (rc)
  342. goto err_cdev;
  343. cc->dev = dev;
  344. cc->cdev = cdev;
  345. /* make the device available */
  346. list_add(&cc->list, cuse_conntbl_head(devt));
  347. mutex_unlock(&cuse_lock);
  348. /* announce device availability */
  349. dev_set_uevent_suppress(dev, 0);
  350. kobject_uevent(&dev->kobj, KOBJ_ADD);
  351. out:
  352. kfree(ia);
  353. folio_put(folio);
  354. return;
  355. err_cdev:
  356. cdev_del(cdev);
  357. err_unlock:
  358. mutex_unlock(&cuse_lock);
  359. put_device(dev);
  360. err_region:
  361. unregister_chrdev_region(devt, 1);
  362. err:
  363. fuse_abort_conn(fc);
  364. goto out;
  365. }
  366. static int cuse_send_init(struct cuse_conn *cc)
  367. {
  368. int rc;
  369. struct folio *folio;
  370. struct fuse_mount *fm = &cc->fm;
  371. struct cuse_init_args *ia;
  372. struct fuse_args_pages *ap;
  373. BUILD_BUG_ON(CUSE_INIT_INFO_MAX > PAGE_SIZE);
  374. rc = -ENOMEM;
  375. folio = folio_alloc(GFP_KERNEL | __GFP_ZERO, 0);
  376. if (!folio)
  377. goto err;
  378. ia = kzalloc_obj(*ia);
  379. if (!ia)
  380. goto err_free_folio;
  381. ap = &ia->ap;
  382. ia->in.major = FUSE_KERNEL_VERSION;
  383. ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
  384. ia->in.flags |= CUSE_UNRESTRICTED_IOCTL;
  385. ap->args.opcode = CUSE_INIT;
  386. ap->args.in_numargs = 1;
  387. ap->args.in_args[0].size = sizeof(ia->in);
  388. ap->args.in_args[0].value = &ia->in;
  389. ap->args.out_numargs = 2;
  390. ap->args.out_args[0].size = sizeof(ia->out);
  391. ap->args.out_args[0].value = &ia->out;
  392. ap->args.out_args[1].size = CUSE_INIT_INFO_MAX;
  393. ap->args.out_argvar = true;
  394. ap->args.out_pages = true;
  395. ap->num_folios = 1;
  396. ap->folios = &ia->folio;
  397. ap->descs = &ia->desc;
  398. ia->folio = folio;
  399. ia->desc.length = ap->args.out_args[1].size;
  400. ap->args.end = cuse_process_init_reply;
  401. rc = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
  402. if (rc) {
  403. kfree(ia);
  404. err_free_folio:
  405. folio_put(folio);
  406. }
  407. err:
  408. return rc;
  409. }
  410. static void cuse_fc_release(struct fuse_conn *fc)
  411. {
  412. kfree(fc_to_cc(fc));
  413. }
  414. /**
  415. * cuse_channel_open - open method for /dev/cuse
  416. * @inode: inode for /dev/cuse
  417. * @file: file struct being opened
  418. *
  419. * Userland CUSE server can create a CUSE device by opening /dev/cuse
  420. * and replying to the initialization request kernel sends. This
  421. * function is responsible for handling CUSE device initialization.
  422. * Because the fd opened by this function is used during
  423. * initialization, this function only creates cuse_conn and sends
  424. * init. The rest is delegated to a kthread.
  425. *
  426. * RETURNS:
  427. * 0 on success, -errno on failure.
  428. */
  429. static int cuse_channel_open(struct inode *inode, struct file *file)
  430. {
  431. struct fuse_dev *fud;
  432. struct cuse_conn *cc;
  433. int rc;
  434. /* set up cuse_conn */
  435. cc = kzalloc_obj(*cc);
  436. if (!cc)
  437. return -ENOMEM;
  438. /*
  439. * Limit the cuse channel to requests that can
  440. * be represented in file->f_cred->user_ns.
  441. */
  442. fuse_conn_init(&cc->fc, &cc->fm, file->f_cred->user_ns,
  443. &fuse_dev_fiq_ops, NULL);
  444. cc->fc.release = cuse_fc_release;
  445. fud = fuse_dev_alloc_install(&cc->fc);
  446. fuse_conn_put(&cc->fc);
  447. if (!fud)
  448. return -ENOMEM;
  449. INIT_LIST_HEAD(&cc->list);
  450. cc->fc.initialized = 1;
  451. rc = cuse_send_init(cc);
  452. if (rc) {
  453. fuse_dev_free(fud);
  454. return rc;
  455. }
  456. file->private_data = fud;
  457. return 0;
  458. }
  459. /**
  460. * cuse_channel_release - release method for /dev/cuse
  461. * @inode: inode for /dev/cuse
  462. * @file: file struct being closed
  463. *
  464. * Disconnect the channel, deregister CUSE device and initiate
  465. * destruction by putting the default reference.
  466. *
  467. * RETURNS:
  468. * 0 on success, -errno on failure.
  469. */
  470. static int cuse_channel_release(struct inode *inode, struct file *file)
  471. {
  472. struct fuse_dev *fud = __fuse_get_dev(file);
  473. struct cuse_conn *cc = fc_to_cc(fud->fc);
  474. /* remove from the conntbl, no more access from this point on */
  475. mutex_lock(&cuse_lock);
  476. list_del_init(&cc->list);
  477. mutex_unlock(&cuse_lock);
  478. /* remove device */
  479. if (cc->dev)
  480. device_unregister(cc->dev);
  481. if (cc->cdev) {
  482. unregister_chrdev_region(cc->cdev->dev, 1);
  483. cdev_del(cc->cdev);
  484. }
  485. return fuse_dev_release(inode, file);
  486. }
  487. static struct file_operations cuse_channel_fops; /* initialized during init */
  488. /**************************************************************************
  489. * Misc stuff and module initializatiion
  490. *
  491. * CUSE exports the same set of attributes to sysfs as fusectl.
  492. */
  493. static ssize_t cuse_class_waiting_show(struct device *dev,
  494. struct device_attribute *attr, char *buf)
  495. {
  496. struct cuse_conn *cc = dev_get_drvdata(dev);
  497. return sprintf(buf, "%d\n", atomic_read(&cc->fc.num_waiting));
  498. }
  499. static DEVICE_ATTR(waiting, 0400, cuse_class_waiting_show, NULL);
  500. static ssize_t cuse_class_abort_store(struct device *dev,
  501. struct device_attribute *attr,
  502. const char *buf, size_t count)
  503. {
  504. struct cuse_conn *cc = dev_get_drvdata(dev);
  505. fuse_abort_conn(&cc->fc);
  506. return count;
  507. }
  508. static DEVICE_ATTR(abort, 0200, NULL, cuse_class_abort_store);
  509. static struct attribute *cuse_class_dev_attrs[] = {
  510. &dev_attr_waiting.attr,
  511. &dev_attr_abort.attr,
  512. NULL,
  513. };
  514. ATTRIBUTE_GROUPS(cuse_class_dev);
  515. static struct miscdevice cuse_miscdev = {
  516. .minor = CUSE_MINOR,
  517. .name = "cuse",
  518. .fops = &cuse_channel_fops,
  519. };
  520. MODULE_ALIAS_MISCDEV(CUSE_MINOR);
  521. MODULE_ALIAS("devname:cuse");
  522. static int __init cuse_init(void)
  523. {
  524. int i, rc;
  525. /* init conntbl */
  526. for (i = 0; i < CUSE_CONNTBL_LEN; i++)
  527. INIT_LIST_HEAD(&cuse_conntbl[i]);
  528. /* inherit and extend fuse_dev_operations */
  529. cuse_channel_fops = fuse_dev_operations;
  530. cuse_channel_fops.owner = THIS_MODULE;
  531. cuse_channel_fops.open = cuse_channel_open;
  532. cuse_channel_fops.release = cuse_channel_release;
  533. /* CUSE is not prepared for FUSE_DEV_IOC_CLONE */
  534. cuse_channel_fops.unlocked_ioctl = NULL;
  535. cuse_class = class_create("cuse");
  536. if (IS_ERR(cuse_class))
  537. return PTR_ERR(cuse_class);
  538. cuse_class->dev_groups = cuse_class_dev_groups;
  539. rc = misc_register(&cuse_miscdev);
  540. if (rc) {
  541. class_destroy(cuse_class);
  542. return rc;
  543. }
  544. return 0;
  545. }
  546. static void __exit cuse_exit(void)
  547. {
  548. misc_deregister(&cuse_miscdev);
  549. class_destroy(cuse_class);
  550. }
  551. module_init(cuse_init);
  552. module_exit(cuse_exit);
  553. MODULE_AUTHOR("Tejun Heo <tj@kernel.org>");
  554. MODULE_DESCRIPTION("Character device in Userspace");
  555. MODULE_LICENSE("GPL");