miscdev.c 16 KB

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