miscdev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. u32 counter_nbo;
  236. char packet_length[3];
  237. size_t i;
  238. size_t total_length;
  239. int rc;
  240. mutex_lock(&ecryptfs_daemon_hash_mux);
  241. /* TODO: Just use file->private_data? */
  242. rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid,
  243. current->nsproxy->user_ns);
  244. BUG_ON(rc || !daemon);
  245. mutex_lock(&daemon->mux);
  246. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  247. rc = 0;
  248. mutex_unlock(&ecryptfs_daemon_hash_mux);
  249. printk(KERN_WARNING "%s: Attempt to read from zombified "
  250. "daemon\n", __func__);
  251. goto out_unlock_daemon;
  252. }
  253. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
  254. rc = 0;
  255. mutex_unlock(&ecryptfs_daemon_hash_mux);
  256. goto out_unlock_daemon;
  257. }
  258. /* This daemon will not go away so long as this flag is set */
  259. daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
  260. mutex_unlock(&ecryptfs_daemon_hash_mux);
  261. check_list:
  262. if (list_empty(&daemon->msg_ctx_out_queue)) {
  263. mutex_unlock(&daemon->mux);
  264. rc = wait_event_interruptible(
  265. daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
  266. mutex_lock(&daemon->mux);
  267. if (rc < 0) {
  268. rc = 0;
  269. goto out_unlock_daemon;
  270. }
  271. }
  272. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  273. rc = 0;
  274. goto out_unlock_daemon;
  275. }
  276. if (list_empty(&daemon->msg_ctx_out_queue)) {
  277. /* Something else jumped in since the
  278. * wait_event_interruptable() and removed the
  279. * message from the queue; try again */
  280. goto check_list;
  281. }
  282. BUG_ON(current->euid != daemon->euid);
  283. BUG_ON(current->nsproxy->user_ns != daemon->user_ns);
  284. BUG_ON(task_pid(current) != daemon->pid);
  285. msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
  286. struct ecryptfs_msg_ctx, daemon_out_list);
  287. BUG_ON(!msg_ctx);
  288. mutex_lock(&msg_ctx->mux);
  289. if (msg_ctx->msg) {
  290. rc = ecryptfs_write_packet_length(packet_length,
  291. msg_ctx->msg_size,
  292. &packet_length_size);
  293. if (rc) {
  294. rc = 0;
  295. printk(KERN_WARNING "%s: Error writing packet length; "
  296. "rc = [%d]\n", __func__, rc);
  297. goto out_unlock_msg_ctx;
  298. }
  299. } else {
  300. packet_length_size = 0;
  301. msg_ctx->msg_size = 0;
  302. }
  303. /* miscdevfs packet format:
  304. * Octet 0: Type
  305. * Octets 1-4: network byte order msg_ctx->counter
  306. * Octets 5-N0: Size of struct ecryptfs_message to follow
  307. * Octets N0-N1: struct ecryptfs_message (including data)
  308. *
  309. * Octets 5-N1 not written if the packet type does not
  310. * include a message */
  311. total_length = (1 + 4 + packet_length_size + msg_ctx->msg_size);
  312. if (count < total_length) {
  313. rc = 0;
  314. printk(KERN_WARNING "%s: Only given user buffer of "
  315. "size [%Zd], but we need [%Zd] to read the "
  316. "pending message\n", __func__, count, total_length);
  317. goto out_unlock_msg_ctx;
  318. }
  319. i = 0;
  320. buf[i++] = msg_ctx->type;
  321. counter_nbo = cpu_to_be32(msg_ctx->counter);
  322. memcpy(&buf[i], (char *)&counter_nbo, 4);
  323. i += 4;
  324. if (msg_ctx->msg) {
  325. memcpy(&buf[i], packet_length, packet_length_size);
  326. i += packet_length_size;
  327. rc = copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size);
  328. if (rc) {
  329. printk(KERN_ERR "%s: copy_to_user returned error "
  330. "[%d]\n", __func__, rc);
  331. goto out_unlock_msg_ctx;
  332. }
  333. i += msg_ctx->msg_size;
  334. }
  335. rc = i;
  336. list_del(&msg_ctx->daemon_out_list);
  337. kfree(msg_ctx->msg);
  338. msg_ctx->msg = NULL;
  339. /* We do not expect a reply from the userspace daemon for any
  340. * message type other than ECRYPTFS_MSG_REQUEST */
  341. if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
  342. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  343. out_unlock_msg_ctx:
  344. mutex_unlock(&msg_ctx->mux);
  345. out_unlock_daemon:
  346. daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
  347. mutex_unlock(&daemon->mux);
  348. return rc;
  349. }
  350. /**
  351. * ecryptfs_miscdev_helo
  352. * @euid: effective user id of miscdevess sending helo packet
  353. * @user_ns: The namespace in which @euid applies
  354. * @pid: miscdevess id of miscdevess sending helo packet
  355. *
  356. * Returns zero on success; non-zero otherwise
  357. */
  358. static int ecryptfs_miscdev_helo(uid_t euid, struct user_namespace *user_ns,
  359. struct pid *pid)
  360. {
  361. int rc;
  362. rc = ecryptfs_process_helo(ECRYPTFS_TRANSPORT_MISCDEV, euid, user_ns,
  363. pid);
  364. if (rc)
  365. printk(KERN_WARNING "Error processing HELO; rc = [%d]\n", rc);
  366. return rc;
  367. }
  368. /**
  369. * ecryptfs_miscdev_quit
  370. * @euid: effective user id of miscdevess sending quit packet
  371. * @user_ns: The namespace in which @euid applies
  372. * @pid: miscdevess id of miscdevess sending quit packet
  373. *
  374. * Returns zero on success; non-zero otherwise
  375. */
  376. static int ecryptfs_miscdev_quit(uid_t euid, struct user_namespace *user_ns,
  377. struct pid *pid)
  378. {
  379. int rc;
  380. rc = ecryptfs_process_quit(euid, user_ns, pid);
  381. if (rc)
  382. printk(KERN_WARNING
  383. "Error processing QUIT message; rc = [%d]\n", rc);
  384. return rc;
  385. }
  386. /**
  387. * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
  388. * @data: Bytes comprising struct ecryptfs_message
  389. * @data_size: sizeof(struct ecryptfs_message) + data len
  390. * @euid: Effective user id of miscdevess sending the miscdev response
  391. * @user_ns: The namespace in which @euid applies
  392. * @pid: Miscdevess id of miscdevess sending the miscdev response
  393. * @seq: Sequence number for miscdev response packet
  394. *
  395. * Returns zero on success; non-zero otherwise
  396. */
  397. static int ecryptfs_miscdev_response(char *data, size_t data_size,
  398. uid_t euid, struct user_namespace *user_ns,
  399. struct pid *pid, u32 seq)
  400. {
  401. struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
  402. int rc;
  403. if ((sizeof(*msg) + msg->data_len) != data_size) {
  404. printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
  405. "[%Zd]; data_size = [%Zd]. Invalid packet.\n", __func__,
  406. (sizeof(*msg) + msg->data_len), data_size);
  407. rc = -EINVAL;
  408. goto out;
  409. }
  410. rc = ecryptfs_process_response(msg, euid, user_ns, pid, seq);
  411. if (rc)
  412. printk(KERN_ERR
  413. "Error processing response message; rc = [%d]\n", rc);
  414. out:
  415. return rc;
  416. }
  417. /**
  418. * ecryptfs_miscdev_write - handle write to daemon miscdev handle
  419. * @file: File for misc dev handle (ignored)
  420. * @buf: Buffer containing user data
  421. * @count: Amount of data in @buf
  422. * @ppos: Pointer to offset in file (ignored)
  423. *
  424. * miscdevfs packet format:
  425. * Octet 0: Type
  426. * Octets 1-4: network byte order msg_ctx->counter (0's for non-response)
  427. * Octets 5-N0: Size of struct ecryptfs_message to follow
  428. * Octets N0-N1: struct ecryptfs_message (including data)
  429. *
  430. * Returns the number of bytes read from @buf
  431. */
  432. static ssize_t
  433. ecryptfs_miscdev_write(struct file *file, const char __user *buf,
  434. size_t count, loff_t *ppos)
  435. {
  436. u32 counter_nbo, seq;
  437. size_t packet_size, packet_size_length, i;
  438. ssize_t sz = 0;
  439. char *data;
  440. int rc;
  441. if (count == 0)
  442. goto out;
  443. data = kmalloc(count, GFP_KERNEL);
  444. if (!data) {
  445. printk(KERN_ERR "%s: Out of memory whilst attempting to "
  446. "kmalloc([%Zd], GFP_KERNEL)\n", __func__, count);
  447. goto out;
  448. }
  449. rc = copy_from_user(data, buf, count);
  450. if (rc) {
  451. printk(KERN_ERR "%s: copy_from_user returned error [%d]\n",
  452. __func__, rc);
  453. goto out_free;
  454. }
  455. sz = count;
  456. i = 0;
  457. switch (data[i++]) {
  458. case ECRYPTFS_MSG_RESPONSE:
  459. if (count < (1 + 4 + 1 + sizeof(struct ecryptfs_message))) {
  460. printk(KERN_WARNING "%s: Minimum acceptable packet "
  461. "size is [%Zd], but amount of data written is "
  462. "only [%Zd]. Discarding response packet.\n",
  463. __func__,
  464. (1 + 4 + 1 + sizeof(struct ecryptfs_message)),
  465. count);
  466. goto out_free;
  467. }
  468. memcpy((char *)&counter_nbo, &data[i], 4);
  469. seq = be32_to_cpu(counter_nbo);
  470. i += 4;
  471. rc = ecryptfs_parse_packet_length(&data[i], &packet_size,
  472. &packet_size_length);
  473. if (rc) {
  474. printk(KERN_WARNING "%s: Error parsing packet length; "
  475. "rc = [%d]\n", __func__, rc);
  476. goto out_free;
  477. }
  478. i += packet_size_length;
  479. if ((1 + 4 + packet_size_length + packet_size) != count) {
  480. printk(KERN_WARNING "%s: (1 + packet_size_length([%Zd])"
  481. " + packet_size([%Zd]))([%Zd]) != "
  482. "count([%Zd]). Invalid packet format.\n",
  483. __func__, packet_size_length, packet_size,
  484. (1 + packet_size_length + packet_size), count);
  485. goto out_free;
  486. }
  487. rc = ecryptfs_miscdev_response(&data[i], packet_size,
  488. current->euid,
  489. current->nsproxy->user_ns,
  490. task_pid(current), seq);
  491. if (rc)
  492. printk(KERN_WARNING "%s: Failed to deliver miscdev "
  493. "response to requesting operation; rc = [%d]\n",
  494. __func__, rc);
  495. break;
  496. case ECRYPTFS_MSG_HELO:
  497. rc = ecryptfs_miscdev_helo(current->euid,
  498. current->nsproxy->user_ns,
  499. task_pid(current));
  500. if (rc) {
  501. printk(KERN_ERR "%s: Error attempting to process "
  502. "helo from pid [0x%p]; rc = [%d]\n", __func__,
  503. task_pid(current), rc);
  504. goto out_free;
  505. }
  506. break;
  507. case ECRYPTFS_MSG_QUIT:
  508. rc = ecryptfs_miscdev_quit(current->euid,
  509. current->nsproxy->user_ns,
  510. task_pid(current));
  511. if (rc) {
  512. printk(KERN_ERR "%s: Error attempting to process "
  513. "quit from pid [0x%p]; rc = [%d]\n", __func__,
  514. task_pid(current), rc);
  515. goto out_free;
  516. }
  517. break;
  518. default:
  519. ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
  520. "message of unrecognized type [%d]\n",
  521. data[0]);
  522. break;
  523. }
  524. out_free:
  525. kfree(data);
  526. out:
  527. return sz;
  528. }
  529. static const struct file_operations ecryptfs_miscdev_fops = {
  530. .open = ecryptfs_miscdev_open,
  531. .poll = ecryptfs_miscdev_poll,
  532. .read = ecryptfs_miscdev_read,
  533. .write = ecryptfs_miscdev_write,
  534. .release = ecryptfs_miscdev_release,
  535. };
  536. static struct miscdevice ecryptfs_miscdev = {
  537. .minor = MISC_DYNAMIC_MINOR,
  538. .name = "ecryptfs",
  539. .fops = &ecryptfs_miscdev_fops
  540. };
  541. /**
  542. * ecryptfs_init_ecryptfs_miscdev
  543. *
  544. * Messages sent to the userspace daemon from the kernel are placed on
  545. * a queue associated with the daemon. The next read against the
  546. * miscdev handle by that daemon will return the oldest message placed
  547. * on the message queue for the daemon.
  548. *
  549. * Returns zero on success; non-zero otherwise
  550. */
  551. int ecryptfs_init_ecryptfs_miscdev(void)
  552. {
  553. int rc;
  554. atomic_set(&ecryptfs_num_miscdev_opens, 0);
  555. mutex_lock(&ecryptfs_daemon_hash_mux);
  556. rc = misc_register(&ecryptfs_miscdev);
  557. if (rc)
  558. printk(KERN_ERR "%s: Failed to register miscellaneous device "
  559. "for communications with userspace daemons; rc = [%d]\n",
  560. __func__, rc);
  561. mutex_unlock(&ecryptfs_daemon_hash_mux);
  562. return rc;
  563. }
  564. /**
  565. * ecryptfs_destroy_ecryptfs_miscdev
  566. *
  567. * All of the daemons must be exorcised prior to calling this
  568. * function.
  569. */
  570. void ecryptfs_destroy_ecryptfs_miscdev(void)
  571. {
  572. BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
  573. misc_deregister(&ecryptfs_miscdev);
  574. }