miscdev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 2008 International Business Machines Corp.
  5. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/hash.h>
  23. #include <linux/random.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/poll.h>
  26. #include <linux/slab.h>
  27. #include <linux/wait.h>
  28. #include <linux/module.h>
  29. #include "ecryptfs_kernel.h"
  30. static atomic_t ecryptfs_num_miscdev_opens;
  31. /**
  32. * ecryptfs_miscdev_poll
  33. * @file: dev file (ignored)
  34. * @pt: dev poll table (ignored)
  35. *
  36. * Returns the poll mask
  37. */
  38. static unsigned int
  39. ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
  40. {
  41. struct ecryptfs_daemon *daemon;
  42. unsigned int mask = 0;
  43. uid_t euid = current_euid();
  44. int rc;
  45. mutex_lock(&ecryptfs_daemon_hash_mux);
  46. /* TODO: Just use file->private_data? */
  47. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  48. if (rc || !daemon) {
  49. mutex_unlock(&ecryptfs_daemon_hash_mux);
  50. return -EINVAL;
  51. }
  52. mutex_lock(&daemon->mux);
  53. mutex_unlock(&ecryptfs_daemon_hash_mux);
  54. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  55. printk(KERN_WARNING "%s: Attempt to poll on zombified "
  56. "daemon\n", __func__);
  57. goto out_unlock_daemon;
  58. }
  59. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ)
  60. goto out_unlock_daemon;
  61. if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)
  62. goto out_unlock_daemon;
  63. daemon->flags |= ECRYPTFS_DAEMON_IN_POLL;
  64. mutex_unlock(&daemon->mux);
  65. poll_wait(file, &daemon->wait, pt);
  66. mutex_lock(&daemon->mux);
  67. if (!list_empty(&daemon->msg_ctx_out_queue))
  68. mask |= POLLIN | POLLRDNORM;
  69. out_unlock_daemon:
  70. daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
  71. mutex_unlock(&daemon->mux);
  72. return mask;
  73. }
  74. /**
  75. * ecryptfs_miscdev_open
  76. * @inode: inode of miscdev handle (ignored)
  77. * @file: file for miscdev handle (ignored)
  78. *
  79. * Returns zero on success; non-zero otherwise
  80. */
  81. static int
  82. ecryptfs_miscdev_open(struct inode *inode, struct file *file)
  83. {
  84. struct ecryptfs_daemon *daemon = NULL;
  85. uid_t euid = current_euid();
  86. int rc;
  87. mutex_lock(&ecryptfs_daemon_hash_mux);
  88. rc = try_module_get(THIS_MODULE);
  89. if (rc == 0) {
  90. rc = -EIO;
  91. printk(KERN_ERR "%s: Error attempting to increment module use "
  92. "count; rc = [%d]\n", __func__, rc);
  93. goto out_unlock_daemon_list;
  94. }
  95. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  96. if (rc || !daemon) {
  97. rc = ecryptfs_spawn_daemon(&daemon, euid, current_user_ns(),
  98. task_pid(current));
  99. if (rc) {
  100. printk(KERN_ERR "%s: Error attempting to spawn daemon; "
  101. "rc = [%d]\n", __func__, rc);
  102. goto out_module_put_unlock_daemon_list;
  103. }
  104. }
  105. mutex_lock(&daemon->mux);
  106. if (daemon->pid != task_pid(current)) {
  107. rc = -EINVAL;
  108. printk(KERN_ERR "%s: pid [0x%p] has registered with euid [%d], "
  109. "but pid [0x%p] has attempted to open the handle "
  110. "instead\n", __func__, daemon->pid, daemon->euid,
  111. task_pid(current));
  112. goto out_unlock_daemon;
  113. }
  114. if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
  115. rc = -EBUSY;
  116. printk(KERN_ERR "%s: Miscellaneous device handle may only be "
  117. "opened once per daemon; pid [0x%p] already has this "
  118. "handle open\n", __func__, daemon->pid);
  119. goto out_unlock_daemon;
  120. }
  121. daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
  122. file->private_data = daemon;
  123. atomic_inc(&ecryptfs_num_miscdev_opens);
  124. out_unlock_daemon:
  125. mutex_unlock(&daemon->mux);
  126. out_module_put_unlock_daemon_list:
  127. if (rc)
  128. module_put(THIS_MODULE);
  129. out_unlock_daemon_list:
  130. mutex_unlock(&ecryptfs_daemon_hash_mux);
  131. return rc;
  132. }
  133. /**
  134. * ecryptfs_miscdev_release
  135. * @inode: inode of fs/ecryptfs/euid handle (ignored)
  136. * @file: file for fs/ecryptfs/euid handle (ignored)
  137. *
  138. * This keeps the daemon registered until the daemon sends another
  139. * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
  140. *
  141. * Returns zero on success; non-zero otherwise
  142. */
  143. static int
  144. ecryptfs_miscdev_release(struct inode *inode, struct file *file)
  145. {
  146. struct ecryptfs_daemon *daemon = NULL;
  147. uid_t euid = current_euid();
  148. int rc;
  149. mutex_lock(&ecryptfs_daemon_hash_mux);
  150. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  151. if (rc || !daemon)
  152. daemon = file->private_data;
  153. mutex_lock(&daemon->mux);
  154. BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
  155. daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
  156. atomic_dec(&ecryptfs_num_miscdev_opens);
  157. mutex_unlock(&daemon->mux);
  158. rc = ecryptfs_exorcise_daemon(daemon);
  159. if (rc) {
  160. printk(KERN_CRIT "%s: Fatal error whilst attempting to "
  161. "shut down daemon; rc = [%d]. Please report this "
  162. "bug.\n", __func__, rc);
  163. BUG();
  164. }
  165. module_put(THIS_MODULE);
  166. mutex_unlock(&ecryptfs_daemon_hash_mux);
  167. return rc;
  168. }
  169. /**
  170. * ecryptfs_send_miscdev
  171. * @data: Data to send to daemon; may be NULL
  172. * @data_size: Amount of data to send to daemon
  173. * @msg_ctx: Message context, which is used to handle the reply. If
  174. * this is NULL, then we do not expect a reply.
  175. * @msg_type: Type of message
  176. * @msg_flags: Flags for message
  177. * @daemon: eCryptfs daemon object
  178. *
  179. * Add msg_ctx to queue and then, if it exists, notify the blocked
  180. * miscdevess about the data being available. Must be called with
  181. * ecryptfs_daemon_hash_mux held.
  182. *
  183. * Returns zero on success; non-zero otherwise
  184. */
  185. int ecryptfs_send_miscdev(char *data, size_t data_size,
  186. struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
  187. u16 msg_flags, struct ecryptfs_daemon *daemon)
  188. {
  189. struct ecryptfs_message *msg;
  190. msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
  191. if (!msg) {
  192. printk(KERN_ERR "%s: Out of memory whilst attempting "
  193. "to kmalloc(%zd, GFP_KERNEL)\n", __func__,
  194. (sizeof(*msg) + data_size));
  195. return -ENOMEM;
  196. }
  197. mutex_lock(&msg_ctx->mux);
  198. msg_ctx->msg = msg;
  199. msg_ctx->msg->index = msg_ctx->index;
  200. msg_ctx->msg->data_len = data_size;
  201. msg_ctx->type = msg_type;
  202. memcpy(msg_ctx->msg->data, data, data_size);
  203. msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
  204. list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
  205. mutex_unlock(&msg_ctx->mux);
  206. mutex_lock(&daemon->mux);
  207. daemon->num_queued_msg_ctx++;
  208. wake_up_interruptible(&daemon->wait);
  209. mutex_unlock(&daemon->mux);
  210. return 0;
  211. }
  212. /*
  213. * miscdevfs packet format:
  214. * Octet 0: Type
  215. * Octets 1-4: network byte order msg_ctx->counter
  216. * Octets 5-N0: Size of struct ecryptfs_message to follow
  217. * Octets N0-N1: struct ecryptfs_message (including data)
  218. *
  219. * Octets 5-N1 not written if the packet type does not include a message
  220. */
  221. #define PKT_TYPE_SIZE 1
  222. #define PKT_CTR_SIZE 4
  223. #define MIN_NON_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE)
  224. #define MIN_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
  225. + ECRYPTFS_MIN_PKT_LEN_SIZE)
  226. /* 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES comes from tag 65 packet format */
  227. #define MAX_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
  228. + ECRYPTFS_MAX_PKT_LEN_SIZE \
  229. + sizeof(struct ecryptfs_message) \
  230. + 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES)
  231. #define PKT_TYPE_OFFSET 0
  232. #define PKT_CTR_OFFSET PKT_TYPE_SIZE
  233. #define PKT_LEN_OFFSET (PKT_TYPE_SIZE + PKT_CTR_SIZE)
  234. /**
  235. * ecryptfs_miscdev_read - format and send message from queue
  236. * @file: fs/ecryptfs/euid miscdevfs handle (ignored)
  237. * @buf: User buffer into which to copy the next message on the daemon queue
  238. * @count: Amount of space available in @buf
  239. * @ppos: Offset in file (ignored)
  240. *
  241. * Pulls the most recent message from the daemon queue, formats it for
  242. * being sent via a miscdevfs handle, and copies it into @buf
  243. *
  244. * Returns the number of bytes copied into the user buffer
  245. */
  246. static ssize_t
  247. ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
  248. loff_t *ppos)
  249. {
  250. struct ecryptfs_daemon *daemon;
  251. struct ecryptfs_msg_ctx *msg_ctx;
  252. size_t packet_length_size;
  253. char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE];
  254. size_t i;
  255. size_t total_length;
  256. uid_t euid = current_euid();
  257. int rc;
  258. mutex_lock(&ecryptfs_daemon_hash_mux);
  259. /* TODO: Just use file->private_data? */
  260. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  261. if (rc || !daemon) {
  262. mutex_unlock(&ecryptfs_daemon_hash_mux);
  263. return -EINVAL;
  264. }
  265. mutex_lock(&daemon->mux);
  266. if (task_pid(current) != daemon->pid) {
  267. mutex_unlock(&daemon->mux);
  268. mutex_unlock(&ecryptfs_daemon_hash_mux);
  269. return -EPERM;
  270. }
  271. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  272. rc = 0;
  273. mutex_unlock(&ecryptfs_daemon_hash_mux);
  274. printk(KERN_WARNING "%s: Attempt to read from zombified "
  275. "daemon\n", __func__);
  276. goto out_unlock_daemon;
  277. }
  278. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
  279. rc = 0;
  280. mutex_unlock(&ecryptfs_daemon_hash_mux);
  281. goto out_unlock_daemon;
  282. }
  283. /* This daemon will not go away so long as this flag is set */
  284. daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
  285. mutex_unlock(&ecryptfs_daemon_hash_mux);
  286. check_list:
  287. if (list_empty(&daemon->msg_ctx_out_queue)) {
  288. mutex_unlock(&daemon->mux);
  289. rc = wait_event_interruptible(
  290. daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
  291. mutex_lock(&daemon->mux);
  292. if (rc < 0) {
  293. rc = 0;
  294. goto out_unlock_daemon;
  295. }
  296. }
  297. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  298. rc = 0;
  299. goto out_unlock_daemon;
  300. }
  301. if (list_empty(&daemon->msg_ctx_out_queue)) {
  302. /* Something else jumped in since the
  303. * wait_event_interruptable() and removed the
  304. * message from the queue; try again */
  305. goto check_list;
  306. }
  307. msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
  308. struct ecryptfs_msg_ctx, daemon_out_list);
  309. BUG_ON(!msg_ctx);
  310. mutex_lock(&msg_ctx->mux);
  311. if (msg_ctx->msg) {
  312. rc = ecryptfs_write_packet_length(packet_length,
  313. msg_ctx->msg_size,
  314. &packet_length_size);
  315. if (rc) {
  316. rc = 0;
  317. printk(KERN_WARNING "%s: Error writing packet length; "
  318. "rc = [%d]\n", __func__, rc);
  319. goto out_unlock_msg_ctx;
  320. }
  321. } else {
  322. packet_length_size = 0;
  323. msg_ctx->msg_size = 0;
  324. }
  325. total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size
  326. + msg_ctx->msg_size);
  327. if (count < total_length) {
  328. rc = 0;
  329. printk(KERN_WARNING "%s: Only given user buffer of "
  330. "size [%zd], but we need [%zd] to read the "
  331. "pending message\n", __func__, count, total_length);
  332. goto out_unlock_msg_ctx;
  333. }
  334. rc = -EFAULT;
  335. if (put_user(msg_ctx->type, buf))
  336. goto out_unlock_msg_ctx;
  337. if (put_user(cpu_to_be32(msg_ctx->counter),
  338. (__be32 __user *)(&buf[PKT_CTR_OFFSET])))
  339. goto out_unlock_msg_ctx;
  340. i = PKT_TYPE_SIZE + PKT_CTR_SIZE;
  341. if (msg_ctx->msg) {
  342. if (copy_to_user(&buf[i], packet_length, packet_length_size))
  343. goto out_unlock_msg_ctx;
  344. i += packet_length_size;
  345. if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size))
  346. goto out_unlock_msg_ctx;
  347. i += msg_ctx->msg_size;
  348. }
  349. rc = i;
  350. list_del(&msg_ctx->daemon_out_list);
  351. kfree(msg_ctx->msg);
  352. msg_ctx->msg = NULL;
  353. /* We do not expect a reply from the userspace daemon for any
  354. * message type other than ECRYPTFS_MSG_REQUEST */
  355. if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
  356. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  357. out_unlock_msg_ctx:
  358. mutex_unlock(&msg_ctx->mux);
  359. out_unlock_daemon:
  360. daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
  361. mutex_unlock(&daemon->mux);
  362. return rc;
  363. }
  364. /**
  365. * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
  366. * @data: Bytes comprising struct ecryptfs_message
  367. * @data_size: sizeof(struct ecryptfs_message) + data len
  368. * @euid: Effective user id of miscdevess sending the miscdev response
  369. * @user_ns: The namespace in which @euid applies
  370. * @pid: Miscdevess id of miscdevess sending the miscdev response
  371. * @seq: Sequence number for miscdev response packet
  372. *
  373. * Returns zero on success; non-zero otherwise
  374. */
  375. static int ecryptfs_miscdev_response(char *data, size_t data_size,
  376. uid_t euid, struct user_namespace *user_ns,
  377. struct pid *pid, u32 seq)
  378. {
  379. struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
  380. int rc;
  381. if ((sizeof(*msg) + msg->data_len) != data_size) {
  382. printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
  383. "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__,
  384. (sizeof(*msg) + msg->data_len), data_size);
  385. rc = -EINVAL;
  386. goto out;
  387. }
  388. rc = ecryptfs_process_response(msg, euid, user_ns, pid, seq);
  389. if (rc)
  390. printk(KERN_ERR
  391. "Error processing response message; rc = [%d]\n", rc);
  392. out:
  393. return rc;
  394. }
  395. /**
  396. * ecryptfs_miscdev_write - handle write to daemon miscdev handle
  397. * @file: File for misc dev handle (ignored)
  398. * @buf: Buffer containing user data
  399. * @count: Amount of data in @buf
  400. * @ppos: Pointer to offset in file (ignored)
  401. *
  402. * Returns the number of bytes read from @buf
  403. */
  404. static ssize_t
  405. ecryptfs_miscdev_write(struct file *file, const char __user *buf,
  406. size_t count, loff_t *ppos)
  407. {
  408. __be32 counter_nbo;
  409. u32 seq;
  410. size_t packet_size, packet_size_length;
  411. char *data;
  412. uid_t euid = current_euid();
  413. unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE];
  414. ssize_t rc;
  415. if (count == 0) {
  416. return 0;
  417. } else if (count == MIN_NON_MSG_PKT_SIZE) {
  418. /* Likely a harmless MSG_HELO or MSG_QUIT - no packet length */
  419. goto memdup;
  420. } else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) {
  421. printk(KERN_WARNING "%s: Acceptable packet size range is "
  422. "[%d-%zu], but amount of data written is [%zu].",
  423. __func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count);
  424. return -EINVAL;
  425. }
  426. if (copy_from_user(packet_size_peek, &buf[PKT_LEN_OFFSET],
  427. sizeof(packet_size_peek))) {
  428. printk(KERN_WARNING "%s: Error while inspecting packet size\n",
  429. __func__);
  430. return -EFAULT;
  431. }
  432. rc = ecryptfs_parse_packet_length(packet_size_peek, &packet_size,
  433. &packet_size_length);
  434. if (rc) {
  435. printk(KERN_WARNING "%s: Error parsing packet length; "
  436. "rc = [%zd]\n", __func__, rc);
  437. return rc;
  438. }
  439. if ((PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_size_length + packet_size)
  440. != count) {
  441. printk(KERN_WARNING "%s: Invalid packet size [%zu]\n", __func__,
  442. packet_size);
  443. return -EINVAL;
  444. }
  445. memdup:
  446. data = memdup_user(buf, count);
  447. if (IS_ERR(data)) {
  448. printk(KERN_ERR "%s: memdup_user returned error [%ld]\n",
  449. __func__, PTR_ERR(data));
  450. return PTR_ERR(data);
  451. }
  452. switch (data[PKT_TYPE_OFFSET]) {
  453. case ECRYPTFS_MSG_RESPONSE:
  454. if (count < (MIN_MSG_PKT_SIZE
  455. + sizeof(struct ecryptfs_message))) {
  456. printk(KERN_WARNING "%s: Minimum acceptable packet "
  457. "size is [%zd], but amount of data written is "
  458. "only [%zd]. Discarding response packet.\n",
  459. __func__,
  460. (MIN_MSG_PKT_SIZE
  461. + sizeof(struct ecryptfs_message)), count);
  462. rc = -EINVAL;
  463. goto out_free;
  464. }
  465. memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE);
  466. seq = be32_to_cpu(counter_nbo);
  467. rc = ecryptfs_miscdev_response(
  468. &data[PKT_LEN_OFFSET + packet_size_length],
  469. packet_size, euid, current_user_ns(),
  470. task_pid(current), seq);
  471. if (rc) {
  472. printk(KERN_WARNING "%s: Failed to deliver miscdev "
  473. "response to requesting operation; rc = [%zd]\n",
  474. __func__, rc);
  475. goto out_free;
  476. }
  477. break;
  478. case ECRYPTFS_MSG_HELO:
  479. case ECRYPTFS_MSG_QUIT:
  480. break;
  481. default:
  482. ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
  483. "message of unrecognized type [%d]\n",
  484. data[0]);
  485. rc = -EINVAL;
  486. goto out_free;
  487. }
  488. rc = count;
  489. out_free:
  490. kfree(data);
  491. return rc;
  492. }
  493. static const struct file_operations ecryptfs_miscdev_fops = {
  494. .open = ecryptfs_miscdev_open,
  495. .poll = ecryptfs_miscdev_poll,
  496. .read = ecryptfs_miscdev_read,
  497. .write = ecryptfs_miscdev_write,
  498. .release = ecryptfs_miscdev_release,
  499. .llseek = noop_llseek,
  500. };
  501. static struct miscdevice ecryptfs_miscdev = {
  502. .minor = MISC_DYNAMIC_MINOR,
  503. .name = "ecryptfs",
  504. .fops = &ecryptfs_miscdev_fops
  505. };
  506. /**
  507. * ecryptfs_init_ecryptfs_miscdev
  508. *
  509. * Messages sent to the userspace daemon from the kernel are placed on
  510. * a queue associated with the daemon. The next read against the
  511. * miscdev handle by that daemon will return the oldest message placed
  512. * on the message queue for the daemon.
  513. *
  514. * Returns zero on success; non-zero otherwise
  515. */
  516. int __init ecryptfs_init_ecryptfs_miscdev(void)
  517. {
  518. int rc;
  519. atomic_set(&ecryptfs_num_miscdev_opens, 0);
  520. rc = misc_register(&ecryptfs_miscdev);
  521. if (rc)
  522. printk(KERN_ERR "%s: Failed to register miscellaneous device "
  523. "for communications with userspace daemons; rc = [%d]\n",
  524. __func__, rc);
  525. return rc;
  526. }
  527. /**
  528. * ecryptfs_destroy_ecryptfs_miscdev
  529. *
  530. * All of the daemons must be exorcised prior to calling this
  531. * function.
  532. */
  533. void ecryptfs_destroy_ecryptfs_miscdev(void)
  534. {
  535. BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
  536. misc_deregister(&ecryptfs_miscdev);
  537. }