miscdev.c 18 KB

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